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