xref: /isa-l_crypto/sm3_mb/sm3_ref_test.c (revision 6801b27bd9295090e36d2864110576f428efdf61)
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 
34 #ifndef FIPS_MODE
35 #include "sm3_mb.h"
36 #include "endian_helper.h"
37 
38 typedef uint32_t digest_sm3[ISAL_SM3_DIGEST_NWORDS];
39 
40 #define MSGS     2
41 #define NUM_JOBS 1000
42 
43 #define PSEUDO_RANDOM_NUM(seed) ((seed) * 5 + ((seed) * (seed)) / 64) % MSGS
44 
45 static uint8_t msg1[] = "abc";
46 static uint8_t msg2[] = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd";
47 
48 /* small endian */
49 static digest_sm3 exp_result_digest1 = { 0x66c7f0f4, 0x62eeedd9, 0xd1f2d46b, 0xdc10e4e2,
50                                          0x4167c487, 0x5cf2f7a2, 0x297da02b, 0x8f4ba8e0 };
51 
52 /* small endian */
53 static digest_sm3 exp_result_digest2 = { 0xdebe9ff9, 0x2275b8a1, 0x38604889, 0xc18e5a4d,
54                                          0x6fdb70e5, 0x387e5765, 0x293dcba3, 0x9c0c5732 };
55 
56 static uint8_t *msgs[MSGS] = { msg1, msg2 };
57 
58 static uint32_t *exp_result_digest[MSGS] = { exp_result_digest1, exp_result_digest2 };
59 #endif /* !FIPS_MODE */
60 
61 int
main(void)62 main(void)
63 {
64 #ifndef FIPS_MODE
65         ISAL_SM3_HASH_CTX_MGR *mgr = NULL;
66         ISAL_SM3_HASH_CTX ctxpool[NUM_JOBS], *ctx = NULL;
67         uint32_t i, j, k, t, checked = 0;
68         uint32_t *good;
69         int rc, ret = -1;
70 
71         rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SM3_HASH_CTX_MGR));
72         if ((rc != 0) || (mgr == NULL)) {
73                 printf("posix_memalign failed test aborted\n");
74                 return 1;
75         }
76 
77         isal_sm3_ctx_mgr_init(mgr);
78 
79         // Init contexts before first use
80         for (i = 0; i < MSGS; i++) {
81                 isal_hash_ctx_init(&ctxpool[i]);
82                 ctxpool[i].user_data = (void *) ((uint64_t) i);
83         }
84 
85         for (i = 0; i < MSGS; i++) {
86                 const int res = isal_sm3_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, msgs[i],
87                                                         (uint32_t) strlen((char *) msgs[i]),
88                                                         ISAL_HASH_ENTIRE);
89 
90                 if (res == 0 && ctx != NULL) {
91                         t = (uint32_t) ((uintptr_t) ctx->user_data);
92                         good = exp_result_digest[t];
93                         checked++;
94                         for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
95                                 if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
96                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
97                                                ctxpool[t].job.result_digest[j],
98                                                byteswap32(good[j]));
99                                         goto end;
100                                 }
101                         }
102 
103                         if (ctx->error) {
104                                 printf("Something bad happened during the submit."
105                                        " Error code: %d",
106                                        ctx->error);
107                                 goto end;
108                         }
109                 }
110         }
111 
112         while (1) {
113                 const int res = isal_sm3_ctx_mgr_flush(mgr, &ctx);
114 
115                 if (res == 0 && ctx != NULL) {
116                         t = (unsigned long) (uintptr_t) (ctx->user_data);
117                         good = exp_result_digest[t];
118                         checked++;
119                         for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
120                                 if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
121                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
122                                                ctxpool[t].job.result_digest[j],
123                                                byteswap32(good[j]));
124                                         goto end;
125                                 }
126                         }
127 
128                         if (ctx->error) {
129                                 printf("Something bad happened during the submit."
130                                        " Error code: %d",
131                                        ctx->error);
132                                 goto end;
133                         }
134                 } else {
135                         break;
136                 }
137         }
138 
139         // do larger test in pseudo-random order
140 
141         // Init contexts before first use
142         for (i = 0; i < NUM_JOBS; i++) {
143                 isal_hash_ctx_init(&ctxpool[i]);
144                 ctxpool[i].user_data = (void *) ((uint64_t) i);
145         }
146 
147         checked = 0;
148         for (i = 0; i < NUM_JOBS; i++) {
149                 j = PSEUDO_RANDOM_NUM(i);
150                 const int res = isal_sm3_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, msgs[j],
151                                                         (uint32_t) strlen((char *) msgs[j]),
152                                                         ISAL_HASH_ENTIRE);
153                 if (res == 0 && ctx != NULL) {
154                         t = (unsigned long) (uintptr_t) (ctx->user_data);
155                         k = PSEUDO_RANDOM_NUM(t);
156                         good = exp_result_digest[k];
157                         checked++;
158                         for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
159                                 if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
160                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
161                                                ctxpool[t].job.result_digest[j],
162                                                byteswap32(good[j]));
163                                         goto end;
164                                 }
165                         }
166 
167                         if (ctx->error) {
168                                 printf("Something bad happened during the"
169                                        " submit. Error code: %d",
170                                        ctx->error);
171                                 goto end;
172                         }
173                 }
174         }
175         while (1) {
176                 const int res = isal_sm3_ctx_mgr_flush(mgr, &ctx);
177 
178                 if (res == 0 && ctx != NULL) {
179                         t = (unsigned long) (uintptr_t) (ctx->user_data);
180                         k = PSEUDO_RANDOM_NUM(t);
181                         good = exp_result_digest[k];
182                         checked++;
183                         for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
184                                 if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
185                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
186                                                ctxpool[t].job.result_digest[j],
187                                                byteswap32(good[j]));
188                                         goto end;
189                                 }
190                         }
191 
192                         if (ctx->error) {
193                                 printf("Something bad happened during the submit."
194                                        " Error code: %d",
195                                        ctx->error);
196                                 goto end;
197                         }
198                 } else {
199                         break;
200                 }
201         }
202 
203         if (checked != NUM_JOBS) {
204                 printf("only tested %d rather than %d\n", checked, NUM_JOBS);
205                 goto end;
206         }
207         ret = 0;
208 
209         printf(" multibinary_sm3 test: Pass\n");
210 end:
211         aligned_free(mgr);
212         return ret;
213 #else
214         printf("Not Executed\n");
215         return 0;
216 #endif /* FIPS_MODE */
217 }
218