xref: /isa-l_crypto/sha256_mb/sha256_mb_test.c (revision 487b772bd076f8313189acea961fedffe88eec4b)
1 /**********************************************************************
2   Copyright(c) 2011-2016 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 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "sha256_mb.h"
34 
35 typedef uint32_t DigestSHA256[ISAL_SHA256_DIGEST_NWORDS];
36 
37 #define MSGS     7
38 #define NUM_JOBS 1000
39 
40 #define PSEUDO_RANDOM_NUM(seed) ((seed) * 5 + ((seed) * (seed)) / 64) % MSGS
41 
42 static uint8_t msg1[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
43 static uint8_t msg2[] = "0123456789:;<=>?@ABCDEFGHIJKLMNO";
44 static uint8_t msg3[] = "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
45                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
46                         "0123456789:;<";
47 static uint8_t msg4[] = "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
48                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
49                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
50                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQR";
51 static uint8_t msg5[] = "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
52                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
53                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
54                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
55                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
56                         "0123456789:;<=>?";
57 static uint8_t msg6[] = "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
58                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
59                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
60                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
61                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
62                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
63                         "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTU";
64 static uint8_t msg7[] = "";
65 
66 static DigestSHA256 expResultDigest1 = { 0x248D6A61, 0xD20638B8, 0xE5C02693, 0x0C3E6039,
67                                          0xA33CE459, 0x64FF2167, 0xF6ECEDD4, 0x19DB06C1 };
68 
69 static DigestSHA256 expResultDigest2 = { 0xD9C2E699, 0x586B948F, 0x4022C799, 0x4FFE14C6,
70                                          0x3A4E8E31, 0x2EE2AEE1, 0xEBE51BED, 0x85705CFD };
71 
72 static DigestSHA256 expResultDigest3 = { 0xE3057651, 0x81295681, 0x7ECF1791, 0xFF9A1619,
73                                          0xB2BC5CAD, 0x2AC00018, 0x92AE489C, 0x48DD10B3 };
74 
75 static DigestSHA256 expResultDigest4 = { 0x0307DAA3, 0x7130A140, 0x270790F9, 0x95B71407,
76                                          0x8EC752A6, 0x084EC1F3, 0xBD873D79, 0x3FF78383 };
77 
78 static DigestSHA256 expResultDigest5 = { 0x679312F7, 0x2E18D599, 0x5F51BDC6, 0x4ED56AFD,
79                                          0x9B5704D3, 0x4387E11C, 0xC2331089, 0x2CD45DAA };
80 
81 static DigestSHA256 expResultDigest6 = { 0x8B1767E9, 0x7BA7BBE5, 0xF9A6E8D9, 0x9996904F,
82                                          0x3AF6562E, 0xA58AF438, 0x5D8D584B, 0x81C808CE };
83 
84 static DigestSHA256 expResultDigest7 = { 0xE3B0C442, 0x98FC1C14, 0x9AFBF4C8, 0x996FB924,
85                                          0x27AE41E4, 0x649B934C, 0xA495991B, 0x7852B855 };
86 
87 static uint8_t *msgs[MSGS] = { msg1, msg2, msg3, msg4, msg5, msg6, msg7 };
88 
89 static uint32_t *expResultDigest[MSGS] = { expResultDigest1, expResultDigest2, expResultDigest3,
90                                            expResultDigest4, expResultDigest5, expResultDigest6,
91                                            expResultDigest7 };
92 
93 #define NUM_CHUNKS   4
94 #define DATA_BUF_LEN 4096
95 int
non_blocksize_updates_test(ISAL_SHA256_HASH_CTX_MGR * mgr)96 non_blocksize_updates_test(ISAL_SHA256_HASH_CTX_MGR *mgr)
97 {
98         ISAL_SHA256_HASH_CTX ctx_refer;
99         ISAL_SHA256_HASH_CTX ctx_pool[NUM_CHUNKS];
100         ISAL_SHA256_HASH_CTX *ctx = NULL;
101         int rc;
102 
103         const int update_chunks[NUM_CHUNKS] = { 32, 64, 128, 256 };
104         unsigned char data_buf[DATA_BUF_LEN];
105 
106         memset(data_buf, 0xA, DATA_BUF_LEN);
107 
108         // Init contexts before first use
109         isal_hash_ctx_init(&ctx_refer);
110 
111         rc = isal_sha256_ctx_mgr_submit(mgr, &ctx_refer, &ctx, data_buf, DATA_BUF_LEN,
112                                         ISAL_HASH_ENTIRE);
113         if (rc)
114                 return -1;
115 
116         rc = isal_sha256_ctx_mgr_flush(mgr, &ctx);
117         if (rc)
118                 return -1;
119 
120         for (int c = 0; c < NUM_CHUNKS; c++) {
121                 int chunk = update_chunks[c];
122                 isal_hash_ctx_init(&ctx_pool[c]);
123                 rc = isal_sha256_ctx_mgr_submit(mgr, &ctx_pool[c], &ctx, NULL, 0, ISAL_HASH_FIRST);
124                 if (rc)
125                         return -1;
126                 rc = isal_sha256_ctx_mgr_flush(mgr, &ctx);
127                 if (rc)
128                         return -1;
129                 for (int i = 0; i * chunk < DATA_BUF_LEN; i++) {
130                         rc = isal_sha256_ctx_mgr_submit(mgr, &ctx_pool[c], &ctx,
131                                                         data_buf + i * chunk, chunk,
132                                                         ISAL_HASH_UPDATE);
133                         if (rc)
134                                 return -1;
135                         rc = isal_sha256_ctx_mgr_flush(mgr, &ctx);
136                         if (rc)
137                                 return -1;
138                 }
139         }
140 
141         for (int c = 0; c < NUM_CHUNKS; c++) {
142                 rc = isal_sha256_ctx_mgr_submit(mgr, &ctx_pool[c], &ctx, NULL, 0, ISAL_HASH_LAST);
143                 if (rc)
144                         return -1;
145                 rc = isal_sha256_ctx_mgr_flush(mgr, &ctx);
146                 if (rc)
147                         return -1;
148                 if (ctx_pool[c].status != ISAL_HASH_CTX_STS_COMPLETE) {
149                         return -1;
150                 }
151                 for (int i = 0; i < ISAL_SHA256_DIGEST_NWORDS; i++) {
152                         if (ctx_refer.job.result_digest[i] != ctx_pool[c].job.result_digest[i]) {
153                                 printf("sha256 calc error! chunk %d, digest[%d], (%d) != (%d)\n",
154                                        update_chunks[c], i, ctx_refer.job.result_digest[i],
155                                        ctx_pool[c].job.result_digest[i]);
156                                 return -2;
157                         }
158                 }
159         }
160         return 0;
161 }
162 
163 int
main(void)164 main(void)
165 {
166         ISAL_SHA256_HASH_CTX_MGR *mgr = NULL;
167         ISAL_SHA256_HASH_CTX ctxpool[NUM_JOBS], *ctx = NULL;
168         uint32_t i, j, k, t, checked = 0;
169         uint32_t *good;
170         int rc, ret = -1;
171 
172         rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA256_HASH_CTX_MGR));
173         if ((rc != 0) || (mgr == NULL)) {
174                 printf("posix_memalign failed test aborted\n");
175                 return 1;
176         }
177 
178         rc = isal_sha256_ctx_mgr_init(mgr);
179         if (rc)
180                 goto end;
181 
182         // Init contexts before first use
183         for (i = 0; i < MSGS; i++) {
184                 isal_hash_ctx_init(&ctxpool[i]);
185                 ctxpool[i].user_data = (void *) ((uint64_t) i);
186         }
187 
188         for (i = 0; i < MSGS; i++) {
189                 rc = isal_sha256_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, msgs[i],
190                                                 (uint32_t) strlen((char *) msgs[i]),
191                                                 ISAL_HASH_ENTIRE);
192                 if (rc)
193                         goto end;
194 
195                 if (ctx) {
196                         t = (uint32_t) (uintptr_t) (ctx->user_data);
197                         good = expResultDigest[t];
198                         checked++;
199                         for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
200                                 if (good[j] != ctxpool[t].job.result_digest[j]) {
201                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
202                                                ctxpool[t].job.result_digest[j], good[j]);
203                                         goto end;
204                                 }
205                         }
206 
207                         if (ctx->error) {
208                                 printf("Something bad happened during the submit."
209                                        " Error code: %d",
210                                        ctx->error);
211                                 goto end;
212                         }
213                 }
214         }
215 
216         while (1) {
217                 rc = isal_sha256_ctx_mgr_flush(mgr, &ctx);
218                 if (rc)
219                         goto end;
220 
221                 if (ctx) {
222                         t = (uint32_t) (uintptr_t) (ctx->user_data);
223                         good = expResultDigest[t];
224                         checked++;
225                         for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
226                                 if (good[j] != ctxpool[t].job.result_digest[j]) {
227                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
228                                                ctxpool[t].job.result_digest[j], good[j]);
229                                         goto end;
230                                 }
231                         }
232 
233                         if (ctx->error) {
234                                 printf("Something bad happened during the submit."
235                                        " Error code: %d",
236                                        ctx->error);
237                                 goto end;
238                         }
239                 } else {
240                         break;
241                 }
242         }
243 
244         // do larger test in pseudo-random order
245 
246         // Init contexts before first use
247         for (i = 0; i < NUM_JOBS; i++) {
248                 isal_hash_ctx_init(&ctxpool[i]);
249                 ctxpool[i].user_data = (void *) ((uint64_t) i);
250         }
251 
252         checked = 0;
253         for (i = 0; i < NUM_JOBS; i++) {
254                 j = PSEUDO_RANDOM_NUM(i);
255                 rc = isal_sha256_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, msgs[j],
256                                                 (uint32_t) strlen((char *) msgs[j]),
257                                                 ISAL_HASH_ENTIRE);
258                 if (rc)
259                         goto end;
260                 if (ctx) {
261                         t = (uint32_t) (uintptr_t) (ctx->user_data);
262                         k = PSEUDO_RANDOM_NUM(t);
263                         good = expResultDigest[k];
264                         checked++;
265                         for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
266                                 if (good[j] != ctxpool[t].job.result_digest[j]) {
267                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
268                                                ctxpool[t].job.result_digest[j], good[j]);
269                                         goto end;
270                                 }
271                         }
272 
273                         if (ctx->error) {
274                                 printf("Something bad happened during the"
275                                        " submit. Error code: %d",
276                                        ctx->error);
277                                 goto end;
278                         }
279 
280                         t = (uint32_t) (uintptr_t) (ctx->user_data);
281                         k = PSEUDO_RANDOM_NUM(t);
282                 }
283         }
284         while (1) {
285                 rc = isal_sha256_ctx_mgr_flush(mgr, &ctx);
286                 if (rc)
287                         goto end;
288 
289                 if (ctx) {
290                         t = (uint32_t) (uintptr_t) (ctx->user_data);
291                         k = PSEUDO_RANDOM_NUM(t);
292                         good = expResultDigest[k];
293                         checked++;
294                         for (j = 0; j < ISAL_SHA256_DIGEST_NWORDS; j++) {
295                                 if (good[j] != ctxpool[t].job.result_digest[j]) {
296                                         printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
297                                                ctxpool[t].job.result_digest[j], good[j]);
298                                         goto end;
299                                 }
300                         }
301 
302                         if (ctx->error) {
303                                 printf("Something bad happened during the submit."
304                                        " Error code: %d",
305                                        ctx->error);
306                                 goto end;
307                         }
308                 } else {
309                         break;
310                 }
311         }
312 
313         if (checked != NUM_JOBS) {
314                 printf("only tested %d rather than %d\n", checked, NUM_JOBS);
315                 goto end;
316         }
317 
318         rc = non_blocksize_updates_test(mgr);
319         if (rc) {
320                 printf("multi updates test fail %d\n", rc);
321                 goto end;
322         }
323         ret = 0;
324 
325         printf(" multibinary_sha256 test: Pass\n");
326 end:
327         aligned_free(mgr);
328 
329         return ret;
330 }
331