xref: /isa-l_crypto/sm3_mb/sm3_ref_test.c (revision e3f7d4fb1bd3dcbfb75b87e0c61870e751d613aa)
1 /**********************************************************************
2   Copyright(c) 2011-2019 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 #define ISAL_UNIT_TEST
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "sm3_mb.h"
34 
35 typedef uint32_t digest_sm3[SM3_DIGEST_NWORDS];
36 
37 #define MSGS 2
38 #define NUM_JOBS 1000
39 
40 #define PSEUDO_RANDOM_NUM(seed) ((seed) * 5 + ((seed) * (seed)) / 64) % MSGS
41 
42 static uint8_t msg1[] = "abc";
43 static uint8_t msg2[] = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd";
44 
45 /* small endian */
46 static digest_sm3 exp_result_digest1 = { 0x66c7f0f4, 0x62eeedd9, 0xd1f2d46b, 0xdc10e4e2,
47 	0x4167c487, 0x5cf2f7a2, 0x297da02b, 0x8f4ba8e0
48 };
49 
50 /* small endian */
51 static digest_sm3 exp_result_digest2 = { 0xdebe9ff9, 0x2275b8a1, 0x38604889, 0xc18e5a4d,
52 	0x6fdb70e5, 0x387e5765, 0x293dcba3, 0x9c0c5732
53 };
54 
55 static uint8_t *msgs[MSGS] = { msg1, msg2 };
56 
57 static uint32_t *exp_result_digest[MSGS] = {
58 	exp_result_digest1, exp_result_digest2
59 };
60 
61 int main(void)
62 {
63 	SM3_HASH_CTX_MGR *mgr = NULL;
64 	SM3_HASH_CTX ctxpool[NUM_JOBS], *ctx = NULL;
65 	uint32_t i, j, k, t, checked = 0;
66 	uint32_t *good;
67 
68 	posix_memalign((void *)&mgr, 16, sizeof(SM3_HASH_CTX_MGR));
69 	sm3_ctx_mgr_init(mgr);
70 
71 	// Init contexts before first use
72 	for (i = 0; i < MSGS; i++) {
73 		hash_ctx_init(&ctxpool[i]);
74 		ctxpool[i].user_data = (void *)((uint64_t) i);
75 	}
76 
77 	for (i = 0; i < MSGS; i++) {
78 		ctx = sm3_ctx_mgr_submit(mgr,
79 					 &ctxpool[i],
80 					 msgs[i], strlen((char *)msgs[i]), HASH_ENTIRE);
81 
82 		if (ctx) {
83 			t = (unsigned long)(ctx->user_data);
84 			good = exp_result_digest[t];
85 			checked++;
86 			for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
87 				if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
88 					printf("Test %d, digest %d is %08X, should be %08X\n",
89 					       t, j, ctxpool[t].job.result_digest[j],
90 					       byteswap32(good[j]));
91 					return -1;
92 				}
93 			}
94 
95 			if (ctx->error) {
96 				printf("Something bad happened during the submit."
97 				       " Error code: %d", ctx->error);
98 				return -1;
99 			}
100 
101 		}
102 	}
103 
104 	while (1) {
105 		ctx = sm3_ctx_mgr_flush(mgr);
106 
107 		if (ctx) {
108 			t = (unsigned long)(ctx->user_data);
109 			good = exp_result_digest[t];
110 			checked++;
111 			for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
112 				if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
113 					printf("Test %d, digest %d is %08X, should be %08X\n",
114 					       t, j, ctxpool[t].job.result_digest[j],
115 					       byteswap32(good[j]));
116 					return -1;
117 				}
118 			}
119 
120 			if (ctx->error) {
121 				printf("Something bad happened during the submit."
122 				       " Error code: %d", ctx->error);
123 				return -1;
124 			}
125 		} else {
126 			break;
127 		}
128 	}
129 
130 	// do larger test in pseudo-random order
131 
132 	// Init contexts before first use
133 	for (i = 0; i < NUM_JOBS; i++) {
134 		hash_ctx_init(&ctxpool[i]);
135 		ctxpool[i].user_data = (void *)((uint64_t) i);
136 	}
137 
138 	checked = 0;
139 	for (i = 0; i < NUM_JOBS; i++) {
140 		j = PSEUDO_RANDOM_NUM(i);
141 		ctx = sm3_ctx_mgr_submit(mgr,
142 					 &ctxpool[i],
143 					 msgs[j], strlen((char *)msgs[j]), HASH_ENTIRE);
144 		if (ctx) {
145 			t = (unsigned long)(ctx->user_data);
146 			k = PSEUDO_RANDOM_NUM(t);
147 			good = exp_result_digest[k];
148 			checked++;
149 			for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
150 				if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
151 					printf("Test %d, digest %d is %08X, should be %08X\n",
152 					       t, j, ctxpool[t].job.result_digest[j],
153 					       byteswap32(good[j]));
154 					return -1;
155 				}
156 			}
157 
158 			if (ctx->error) {
159 				printf("Something bad happened during the"
160 				       " submit. Error code: %d", ctx->error);
161 				return -1;
162 			}
163 		}
164 	}
165 	while (1) {
166 		ctx = sm3_ctx_mgr_flush(mgr);
167 
168 		if (ctx) {
169 			t = (unsigned long)(ctx->user_data);
170 			k = PSEUDO_RANDOM_NUM(t);
171 			good = exp_result_digest[k];
172 			checked++;
173 			for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
174 				if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
175 					printf("Test %d, digest %d is %08X, should be %08X\n",
176 					       t, j, ctxpool[t].job.result_digest[j],
177 					       byteswap32(good[j]));
178 					return -1;
179 				}
180 			}
181 
182 			if (ctx->error) {
183 				printf("Something bad happened during the submit."
184 				       " Error code: %d", ctx->error);
185 				return -1;
186 			}
187 		} else {
188 			break;
189 		}
190 	}
191 
192 	if (checked != NUM_JOBS) {
193 		printf("only tested %d rather than %d\n", checked, NUM_JOBS);
194 		return -1;
195 	}
196 
197 	printf(" multibinary_sm3 test: Pass\n");
198 
199 	return 0;
200 }
201