]> begriffs open source - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/tests/hash.c
Remove obsolete code from prvCheckTasksWaitingTermination().
[freertos] / FreeRTOS-Plus / Source / WolfSSL / tests / hash.c
1 /* hash.c has unit tests
2  *
3  * Copyright (C) 2006-2015 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL. (formerly known as CyaSSL)
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <wolfssl/wolfcrypt/settings.h>
27
28 #include <stdio.h>
29
30 #include <wolfssl/wolfcrypt/md4.h>
31 #include <wolfssl/wolfcrypt/md5.h>
32 #include <wolfssl/wolfcrypt/sha.h>
33 #include <wolfssl/wolfcrypt/sha256.h>
34 #include <wolfssl/wolfcrypt/sha512.h>
35 #include <wolfssl/wolfcrypt/ripemd.h>
36 #include <wolfssl/wolfcrypt/hmac.h>
37
38 #include <tests/unit.h>
39
40 typedef struct testVector {
41     const char*  input;
42     const char*  output; 
43     size_t inLen;
44     size_t outLen;
45 } testVector;
46
47 int  md4_test(void);
48 int  md5_test(void);
49 int  sha_test(void);
50 int  sha256_test(void);
51 int  sha512_test(void);
52 int  sha384_test(void);
53 int  ripemd_test(void);
54 int  hmac_md5_test(void);
55 int  hmac_sha_test(void);
56 int  hmac_sha256_test(void);
57 int  hmac_sha384_test(void);
58
59 int HashTest(void)
60 {
61     int ret = 0;
62
63     printf(" Begin HASH Tests\n");
64
65 #ifndef NO_MD4
66     if ( (ret = md4_test()) ) {
67         printf( "   MD4      test failed!\n");
68         return ret; 
69     } else
70         printf( "   MD4      test passed!\n");
71 #endif
72
73 #ifndef NO_MD5
74     if ( (ret = md5_test()) ) {
75         printf( "   MD5      test failed!\n");
76         return ret; 
77     } else
78         printf( "   MD5      test passed!\n");
79 #endif
80     
81 #ifndef NO_SHA
82     if ( (ret = sha_test()) ) {
83         printf( "   SHA      test failed!\n");
84         return ret; 
85     } else
86         printf( "   SHA      test passed!\n");
87 #endif
88     
89 #ifndef NO_SHA256
90     if ( (ret = sha256_test()) ) {
91         printf( "   SHA-256  test failed!\n");
92         return ret; 
93     } else
94         printf( "   SHA-256  test passed!\n");
95 #endif
96
97 #ifdef WOLFSSL_SHA512
98     if ( (ret = sha512_test()) ) {
99         printf( "   SHA-512  test failed!\n");
100         return ret; 
101     } else
102         printf( "   SHA-512  test passed!\n");
103 #endif
104
105 #ifdef WOLFSSL_SHA384
106     if ( (ret = sha384_test()) ) {
107         printf( "   SHA-384  test failed!\n");
108         return ret; 
109     } else
110         printf( "   SHA-384  test passed!\n");
111 #endif
112
113 #ifdef WOLFSSL_RIPEMD
114     if ( (ret = ripemd_test()) ) {
115         printf( "   RIPEMD   test failed!\n");
116         return ret; 
117     } else
118         printf( "   RIPEMD   test passed!\n");
119 #endif
120
121 #ifndef NO_HMAC
122     #ifndef NO_MD5
123         if ( (ret = hmac_md5_test()) ) {
124             printf( "   HMAC-MD5 test failed!\n");
125             return ret; 
126         } else
127             printf( "   HMAC-MD5 test passed!\n");
128     #endif
129
130     #ifndef NO_SHA
131     if ( (ret = hmac_sha_test()) ) 
132         printf( "   HMAC-SHA test failed!\n");
133     else
134         printf( "   HMAC-SHA test passed!\n");
135     #endif
136
137     #ifndef NO_SHA256
138         if ( (ret = hmac_sha256_test()) ) 
139             printf( "   HMAC-SHA256 test failed!\n");
140         else
141             printf( "   HMAC-SHA256 test passed!\n");
142     #endif
143
144     #ifdef WOLFSSL_SHA384
145         if ( (ret = hmac_sha384_test()) ) 
146             printf( "   HMAC-SHA384 test failed!\n");
147         else
148             printf( "   HMAC-SHA384 test passed!\n");
149     #endif
150 #endif
151
152     printf(" End HASH Tests\n");
153     
154     return 0;
155 }
156
157 #ifndef NO_MD4
158
159 int md4_test(void)
160 {
161     Md4  md4;
162     byte hash[MD4_DIGEST_SIZE];
163
164     testVector a, b, c, d, e, f, g;
165     testVector test_md4[7];
166     int times = sizeof(test_md4) / sizeof(testVector), i;
167
168     a.input  = "";
169     a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89" 
170                "\xc0";
171     a.inLen  = strlen(a.input);
172     a.outLen = strlen(a.output);
173
174     b.input  = "a";
175     b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb" 
176                "\x24";
177     b.inLen  = strlen(b.input);
178     b.outLen = strlen(b.output);
179
180     c.input  = "abc";
181     c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72" 
182                "\x9d";
183     c.inLen  = strlen(c.input);
184     c.outLen = strlen(c.output);
185
186     d.input  = "message digest";
187     d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01" 
188                "\x4b";
189     d.inLen  = strlen(d.input);
190     d.outLen = strlen(d.output);
191
192     e.input  = "abcdefghijklmnopqrstuvwxyz";
193     e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d" 
194                "\xa9";
195     e.inLen  = strlen(e.input);
196     e.outLen = strlen(e.output);
197
198     f.input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
199                "6789";
200     f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0" 
201                "\xe4";
202     f.inLen  = strlen(f.input);
203     f.outLen = strlen(f.output);
204
205     g.input  = "1234567890123456789012345678901234567890123456789012345678"
206                "9012345678901234567890";
207     g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05" 
208                "\x36";
209     g.inLen  = strlen(g.input);
210     g.outLen = strlen(g.output);
211
212     test_md4[0] = a;
213     test_md4[1] = b;
214     test_md4[2] = c;
215     test_md4[3] = d;
216     test_md4[4] = e;
217     test_md4[5] = f;
218     test_md4[6] = g;
219
220     wc_InitMd4(&md4);
221
222     for (i = 0; i < times; ++i) {
223         wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
224         wc_Md4Final(&md4, hash);
225
226         if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
227             return -205 - i;
228     }
229
230     return 0;
231 }
232
233 #endif /* NO_MD4 */
234
235 #ifndef NO_MD5
236
237 int md5_test(void)
238 {
239     Md5  md5;
240     byte hash[MD5_DIGEST_SIZE];
241
242     testVector a, b, c, d, e;
243     testVector test_md5[5];
244     int times = sizeof(test_md5) / sizeof(testVector), i;
245
246     a.input  = "abc";
247     a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
248                "\x72";
249     a.inLen  = strlen(a.input);
250     a.outLen = strlen(a.output);
251
252     b.input  = "message digest";
253     b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
254                "\xd0";
255     b.inLen  = strlen(b.input);
256     b.outLen = strlen(b.output);
257
258     c.input  = "abcdefghijklmnopqrstuvwxyz";
259     c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
260                "\x3b";
261     c.inLen  = strlen(c.input);
262     c.outLen = strlen(c.output);
263
264     d.input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
265                "6789";
266     d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
267                "\x9f";
268     d.inLen  = strlen(d.input);
269     d.outLen = strlen(d.output);
270
271     e.input  = "1234567890123456789012345678901234567890123456789012345678"
272                "9012345678901234567890";
273     e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
274                "\x7a";
275     e.inLen  = strlen(e.input);
276     e.outLen = strlen(e.output);
277
278     test_md5[0] = a;
279     test_md5[1] = b;
280     test_md5[2] = c;
281     test_md5[3] = d;
282     test_md5[4] = e;
283
284     wc_InitMd5(&md5);
285
286     for (i = 0; i < times; ++i) {
287         wc_Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
288         wc_Md5Final(&md5, hash);
289
290         if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
291             return -5 - i;
292     }
293
294     return 0;
295 }
296
297 #endif /* NO_MD5 */
298
299 #ifndef NO_SHA
300 int sha_test(void)
301 {
302     Sha  sha;
303     byte hash[SHA_DIGEST_SIZE];
304
305     testVector a, b, c, d;
306     testVector test_sha[4];
307     int ret   = 0;
308     int times = sizeof(test_sha) / sizeof(struct testVector), i;
309
310     a.input  = "abc";
311     a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
312                "\x6C\x9C\xD0\xD8\x9D";
313     a.inLen  = strlen(a.input);
314     a.outLen = strlen(a.output);
315
316     b.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
317     b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
318                "\xE5\xE5\x46\x70\xF1";
319     b.inLen  = strlen(b.input);
320     b.outLen = strlen(b.output);
321
322     c.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
323                "aaaaaa";
324     c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
325                "\x2A\x25\xEC\x64\x4D";
326     c.inLen  = strlen(c.input);
327     c.outLen = strlen(c.output);
328
329     d.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
330                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
331                "aaaaaaaaaa";
332     d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
333                "\x53\x99\x5E\x26\xA0";
334     d.inLen  = strlen(d.input);
335     d.outLen = strlen(d.output);
336
337     test_sha[0] = a;
338     test_sha[1] = b;
339     test_sha[2] = c;
340     test_sha[3] = d;
341
342     ret = wc_InitSha(&sha);
343     if (ret != 0)
344         return ret;
345
346     for (i = 0; i < times; ++i) {
347         wc_ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
348         wc_ShaFinal(&sha, hash);
349
350         if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
351             return -10 - i;
352     }
353
354     return 0;
355 }
356 #endif /* NO_SHA */
357
358 #ifndef NO_SHA256
359 int sha256_test(void)
360 {
361     Sha256 sha;
362     byte   hash[SHA256_DIGEST_SIZE];
363
364     testVector a, b;
365     testVector test_sha[2];
366     int ret;
367     int times = sizeof(test_sha) / sizeof(struct testVector), i;
368
369     a.input  = "abc";
370     a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
371                "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
372                "\x15\xAD";
373     a.inLen  = strlen(a.input);
374     a.outLen = strlen(a.output);
375
376     b.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
377     b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
378                "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
379                "\x06\xC1";
380     b.inLen  = strlen(b.input);
381     b.outLen = strlen(b.output);
382
383     test_sha[0] = a;
384     test_sha[1] = b;
385
386     ret = wc_InitSha256(&sha);
387     if (ret != 0)
388         return ret;
389
390     for (i = 0; i < times; ++i) {
391         ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
392         if (ret != 0)
393             return ret;
394
395         ret = wc_Sha256Final(&sha, hash);
396         if (ret != 0)
397             return ret;
398
399         if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
400             return -10 - i;
401     }
402
403     return 0;
404 }
405 #endif
406
407 #ifdef WOLFSSL_SHA512
408 int sha512_test(void)
409 {
410     Sha512 sha;
411     byte   hash[SHA512_DIGEST_SIZE];
412
413     testVector a, b;
414     testVector test_sha[2];
415     int times = sizeof(test_sha) / sizeof(struct testVector), i;
416     int ret;
417
418     a.input  = "abc";
419     a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
420                "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
421                "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
422                "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
423                "\xa5\x4c\xa4\x9f";
424     a.inLen  = strlen(a.input);
425     a.outLen = strlen(a.output);
426
427     b.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
428                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
429     b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
430                "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
431                "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
432                "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
433                "\x87\x4b\xe9\x09"; 
434     b.inLen  = strlen(b.input);
435     b.outLen = strlen(b.output);
436
437     test_sha[0] = a;
438     test_sha[1] = b;
439
440     ret = wc_InitSha512(&sha);
441     if (ret != 0)
442         return ret;
443
444     for (i = 0; i < times; ++i) {
445         ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
446         if (ret != 0)
447             return ret;
448
449         ret = wc_Sha512Final(&sha, hash);
450         if (ret != 0)
451             return ret;
452
453         if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
454             return -10 - i;
455     }
456
457     return 0;
458 }
459 #endif
460
461 #ifdef WOLFSSL_SHA384
462 int sha384_test()
463 {
464     Sha384 sha;
465     byte   hash[SHA384_DIGEST_SIZE];
466
467     testVector a, b;
468     testVector test_sha[2];
469     int times = sizeof(test_sha) / sizeof(struct testVector), i;
470     int ret;
471
472     a.input  = "abc";
473     a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
474                "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
475                "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
476                "\xc8\x25\xa7";
477     a.inLen  = strlen(a.input);
478     a.outLen = strlen(a.output);
479
480     b.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
481                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
482     b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
483                "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
484                "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
485                "\x74\x60\x39";
486     b.inLen  = strlen(b.input);
487     b.outLen = strlen(b.output);
488
489     test_sha[0] = a;
490     test_sha[1] = b;
491
492     ret = wc_InitSha384(&sha);
493     if (ret != 0)
494         return ret;
495
496     for (i = 0; i < times; ++i) {
497         ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
498         if (ret != 0)
499             return ret;
500
501         ret = wc_Sha384Final(&sha, hash);
502         if (ret != 0)
503             return ret;
504
505         if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
506             return -10 - i;
507     }
508
509     return 0;
510 }
511 #endif
512
513 #ifdef WOLFSSL_RIPEMD
514 int ripemd_test(void)
515 {
516     RipeMd  ripemd;
517     byte hash[RIPEMD_DIGEST_SIZE];
518
519     testVector a, b, c, d;
520     testVector test_ripemd[4];
521     int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
522
523     a.input  = "abc";
524     a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
525                "\xb0\x87\xf1\x5a\x0b\xfc";
526     a.inLen  = strlen(a.input);
527     a.outLen = strlen(a.output);
528
529     b.input  = "message digest";
530     b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
531                "\x5f\xfa\x21\x59\x5f\x36";
532     b.inLen  = strlen(b.input);
533     b.outLen = strlen(b.output);
534
535     c.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 
536     c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
537                "\xf4\x9a\xda\x62\xeb\x2b";
538     c.inLen  = strlen(c.input);
539     c.outLen = strlen(c.output);
540
541     d.input  = "12345678901234567890123456789012345678901234567890123456"
542                "789012345678901234567890";
543     d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
544                "\x82\xbf\x63\x32\x6b\xfb"; 
545     d.inLen  = strlen(d.input);
546     d.outLen = strlen(d.output);
547
548     test_ripemd[0] = a;
549     test_ripemd[1] = b;
550     test_ripemd[2] = c;
551     test_ripemd[3] = d;
552
553     wc_InitRipeMd(&ripemd);
554
555     for (i = 0; i < times; ++i) {
556         wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
557                      (word32)test_ripemd[i].inLen);
558         wc_RipeMdFinal(&ripemd, hash);
559
560         if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
561             return -10 - i;
562     }
563
564     return 0;
565 }
566 #endif /* WOLFSSL_RIPEMD */
567
568 #if !defined(NO_HMAC) && !defined(NO_MD5)
569 int hmac_md5_test(void)
570 {
571     Hmac hmac;
572     byte hash[MD5_DIGEST_SIZE];
573
574     const char* keys[]=
575     {
576         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
577         "Jefe",
578         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
579     };
580
581     testVector a, b, c;
582     testVector test_hmac[3];
583
584     int ret;
585     int times = sizeof(test_hmac) / sizeof(testVector), i;
586
587     a.input  = "Hi There";
588     a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
589                "\x9d";
590     a.inLen  = strlen(a.input);
591     a.outLen = strlen(a.output);
592
593     b.input  = "what do ya want for nothing?";
594     b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
595                "\x38";
596     b.inLen  = strlen(b.input);
597     b.outLen = strlen(b.output);
598
599     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
600                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
601                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
602                "\xDD\xDD\xDD\xDD\xDD\xDD";
603     c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
604                "\xf6";
605     c.inLen  = strlen(c.input);
606     c.outLen = strlen(c.output);
607
608     test_hmac[0] = a;
609     test_hmac[1] = b;
610     test_hmac[2] = c;
611
612     for (i = 0; i < times; ++i) {
613 #if defined(HAVE_FIPS)
614         if (i == 1)
615             continue; /* fips not allowed */
616 #endif
617         ret = wc_HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
618         if (ret != 0)
619             return -4014;
620         ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
621                    (word32)test_hmac[i].inLen);
622         if (ret != 0)
623             return -4015;
624         ret = wc_HmacFinal(&hmac, hash);
625         if (ret != 0)
626             return -4016;
627
628         if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
629             return -20 - i;
630     }
631
632     return 0;
633 }
634 #endif
635
636 #if !defined(NO_HMAC) && !defined(NO_SHA)
637 int hmac_sha_test(void)
638 {
639     Hmac hmac;
640     byte hash[SHA_DIGEST_SIZE];
641
642     const char* keys[]=
643     {
644         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
645                                                                 "\x0b\x0b\x0b",
646         "Jefe",
647         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
648                                                                 "\xAA\xAA\xAA"
649     };
650
651     testVector a, b, c;
652     testVector test_hmac[3];
653
654     int ret;
655     int times = sizeof(test_hmac) / sizeof(testVector), i;
656
657     a.input  = "Hi There";
658     a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
659                "\x8e\xf1\x46\xbe\x00";
660     a.inLen  = strlen(a.input);
661     a.outLen = strlen(a.output);
662
663     b.input  = "what do ya want for nothing?";
664     b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
665                "\x9c\x25\x9a\x7c\x79";
666     b.inLen  = strlen(b.input);
667     b.outLen = strlen(b.output);
668
669     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
670                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
671                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
672                "\xDD\xDD\xDD\xDD\xDD\xDD";
673     c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
674                "\x4f\x63\xf1\x75\xd3";
675     c.inLen  = strlen(c.input);
676     c.outLen = strlen(c.output);
677
678     test_hmac[0] = a;
679     test_hmac[1] = b;
680     test_hmac[2] = c;
681
682     for (i = 0; i < times; ++i) {
683 #if defined(HAVE_FIPS)
684         if (i == 1)
685             continue; /* fips not allowed */
686 #endif
687         ret = wc_HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
688         if (ret != 0)
689             return -4017;
690         ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
691                    (word32)test_hmac[i].inLen);
692         if (ret != 0)
693             return -4018;
694         ret = wc_HmacFinal(&hmac, hash);
695         if (ret != 0)
696             return -4019;
697
698         if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
699             return -20 - i;
700     }
701
702     return 0;
703 }
704 #endif
705
706 #if !defined(NO_HMAC) && !defined(NO_SHA256)
707 int hmac_sha256_test(void)
708 {
709     Hmac hmac;
710     byte hash[SHA256_DIGEST_SIZE];
711
712     const char* keys[]=
713     {
714         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
715                                                                 "\x0b\x0b\x0b",
716         "Jefe",
717         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
718                                                                 "\xAA\xAA\xAA"
719     };
720
721     testVector a, b, c;
722     testVector test_hmac[3];
723
724     int ret;
725     int times = sizeof(test_hmac) / sizeof(testVector), i;
726
727     a.input  = "Hi There";
728     a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
729                "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
730                "\xcf\xf7";
731     a.inLen  = strlen(a.input);
732     a.outLen = strlen(a.output);
733
734     b.input  = "what do ya want for nothing?";
735     b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
736                "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
737                "\x38\x43";
738     b.inLen  = strlen(b.input);
739     b.outLen = strlen(b.output);
740
741     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
742                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
743                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
744                "\xDD\xDD\xDD\xDD\xDD\xDD";
745     c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
746                "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
747                "\x65\xfe";
748     c.inLen  = strlen(c.input);
749     c.outLen = strlen(c.output);
750
751     test_hmac[0] = a;
752     test_hmac[1] = b;
753     test_hmac[2] = c;
754
755     for (i = 0; i < times; ++i) {
756 #if defined(HAVE_FIPS)
757         if (i == 1)
758             continue; /* fips not allowed */
759 #endif
760         ret = wc_HmacSetKey(&hmac,SHA256, (byte*)keys[i], (word32)strlen(keys[i]));
761         if (ret != 0)
762             return -4020;
763         ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
764                    (word32)test_hmac[i].inLen);
765         if (ret != 0)
766             return -4021;
767         ret = wc_HmacFinal(&hmac, hash);
768         if (ret != 0)
769             return -4022;
770
771         if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
772             return -20 - i;
773     }
774
775     return 0;
776 }
777 #endif
778
779
780 #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
781 int hmac_sha384_test(void)
782 {
783     Hmac hmac;
784     byte hash[SHA384_DIGEST_SIZE];
785
786     const char* keys[]=
787     {
788         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
789                                                                 "\x0b\x0b\x0b",
790         "Jefe",
791         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
792                                                                 "\xAA\xAA\xAA"
793     };
794
795     testVector a, b, c;
796     testVector test_hmac[3];
797
798     int ret;
799     int times = sizeof(test_hmac) / sizeof(testVector), i;
800
801     a.input  = "Hi There";
802     a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
803                "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
804                "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
805                "\xfa\x9c\xb6";
806     a.inLen  = strlen(a.input);
807     a.outLen = strlen(a.output);
808
809     b.input  = "what do ya want for nothing?";
810     b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
811                "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
812                "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
813                "\xb2\x16\x49";
814     b.inLen  = strlen(b.input);
815     b.outLen = strlen(b.output);
816
817     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
818                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
819                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
820                "\xDD\xDD\xDD\xDD\xDD\xDD";
821     c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
822                "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
823                "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
824                "\xa3\x4f\x27";
825     c.inLen  = strlen(c.input);
826     c.outLen = strlen(c.output);
827
828     test_hmac[0] = a;
829     test_hmac[1] = b;
830     test_hmac[2] = c;
831
832     for (i = 0; i < times; ++i) {
833 #if defined(HAVE_FIPS)
834         if (i == 1)
835             continue; /* fips not allowed */
836 #endif
837         ret = wc_HmacSetKey(&hmac,SHA384, (byte*)keys[i], (word32)strlen(keys[i]));
838         if (ret != 0)
839             return -4023;
840         ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
841                    (word32)test_hmac[i].inLen);
842         if (ret != 0)
843             return -4024;
844         ret = wc_HmacFinal(&hmac, hash);
845         if (ret != 0)
846             return -4025;
847
848         if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
849             return -20 - i;
850     }
851
852     return 0;
853 }
854 #endif
855