]> begriffs open source - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/tests/hash.c
RXv1 tests running before updating to RXv2.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / tests / hash.c
1 /* hash.c has unit tests
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <stdio.h>
27
28 #include <cyassl/ctaocrypt/md4.h>
29 #include <cyassl/ctaocrypt/md5.h>
30 #include <cyassl/ctaocrypt/sha.h>
31 #include <cyassl/ctaocrypt/sha256.h>
32 #include <cyassl/ctaocrypt/sha512.h>
33 #include <cyassl/ctaocrypt/ripemd.h>
34 #include <cyassl/ctaocrypt/hmac.h>
35
36 #include <tests/unit.h>
37
38 typedef struct testVector {
39     char*  input;
40     char*  output; 
41     size_t inLen;
42     size_t outLen;
43 } testVector;
44
45 int  md4_test(void);
46 int  md5_test(void);
47 int  sha_test(void);
48 int  sha256_test(void);
49 int  sha512_test(void);
50 int  sha384_test(void);
51 int  ripemd_test(void);
52 int  hmac_test(void);
53
54 int HashTest(void)
55 {
56     int ret = 0;
57
58     printf(" Begin HASH Tests\n");
59
60 #ifndef NO_MD4
61     if ( (ret = md4_test()) ) {
62         printf( "   MD4      test failed!\n");
63         return ret; 
64     } else
65         printf( "   MD4      test passed!\n");
66 #endif
67
68     if ( (ret = md5_test()) ) {
69         printf( "   MD5      test failed!\n");
70         return ret; 
71     } else
72         printf( "   MD5      test passed!\n");
73     
74     if ( (ret = sha_test()) ) {
75         printf( "   SHA      test failed!\n");
76         return ret; 
77     } else
78         printf( "   SHA      test passed!\n");
79     
80 #ifndef NO_SHA256
81     if ( (ret = sha256_test()) ) {
82         printf( "   SHA-256  test failed!\n");
83         return ret; 
84     } else
85         printf( "   SHA-256  test passed!\n");
86 #endif
87
88 #ifdef CYASSL_SHA512
89     if ( (ret = sha512_test()) ) {
90         printf( "   SHA-512  test failed!\n");
91         return ret; 
92     } else
93         printf( "   SHA-512  test passed!\n");
94 #endif
95
96 #ifdef CYASSL_SHA384
97     if ( (ret = sha384_test()) ) {
98         printf( "   SHA-384  test failed!\n");
99         return ret; 
100     } else
101         printf( "   SHA-384  test passed!\n");
102 #endif
103
104 #ifdef CYASSL_RIPEMD
105     if ( (ret = ripemd_test()) ) {
106         printf( "   RIPEMD   test failed!\n");
107         return ret; 
108     } else
109         printf( "   RIPEMD   test passed!\n");
110 #endif
111
112 #ifndef NO_HMAC
113     if ( (ret = hmac_test()) ) {
114         printf( "   HMAC     test failed!\n");
115         return ret; 
116     } else
117         printf( "   HMAC     test passed!\n");
118 #endif
119
120     printf(" End HASH Tests\n");
121     
122     return 0;
123 }
124
125 #ifndef NO_MD4
126
127 int md4_test(void)
128 {
129     Md4  md4;
130     byte hash[MD4_DIGEST_SIZE];
131
132     testVector a, b, c, d, e, f, g;
133     testVector test_md4[7];
134     int times = sizeof(test_md4) / sizeof(testVector), i;
135
136     a.input  = "";
137     a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89" 
138                "\xc0";
139     a.inLen  = strlen(a.input);
140     a.outLen = strlen(a.output);
141
142     b.input  = "a";
143     b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb" 
144                "\x24";
145     b.inLen  = strlen(b.input);
146     b.outLen = strlen(b.output);
147
148     c.input  = "abc";
149     c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72" 
150                "\x9d";
151     c.inLen  = strlen(c.input);
152     c.outLen = strlen(c.output);
153
154     d.input  = "message digest";
155     d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01" 
156                "\x4b";
157     d.inLen  = strlen(d.input);
158     d.outLen = strlen(d.output);
159
160     e.input  = "abcdefghijklmnopqrstuvwxyz";
161     e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d" 
162                "\xa9";
163     e.inLen  = strlen(e.input);
164     e.outLen = strlen(e.output);
165
166     f.input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
167                "6789";
168     f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0" 
169                "\xe4";
170     f.inLen  = strlen(f.input);
171     f.outLen = strlen(f.output);
172
173     g.input  = "1234567890123456789012345678901234567890123456789012345678"
174                "9012345678901234567890";
175     g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05" 
176                "\x36";
177     g.inLen  = strlen(g.input);
178     g.outLen = strlen(g.output);
179
180     test_md4[0] = a;
181     test_md4[1] = b;
182     test_md4[2] = c;
183     test_md4[3] = d;
184     test_md4[4] = e;
185     test_md4[5] = f;
186     test_md4[6] = g;
187
188     InitMd4(&md4);
189
190     for (i = 0; i < times; ++i) {
191         Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
192         Md4Final(&md4, hash);
193
194         if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
195             return -205 - i;
196     }
197
198     return 0;
199 }
200
201 #endif /* NO_MD4 */
202
203 int md5_test(void)
204 {
205     Md5  md5;
206     byte hash[MD5_DIGEST_SIZE];
207
208     testVector a, b, c, d, e;
209     testVector test_md5[5];
210     int times = sizeof(test_md5) / sizeof(testVector), i;
211
212     a.input  = "abc";
213     a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
214                "\x72";
215     a.inLen  = strlen(a.input);
216     a.outLen = strlen(a.output);
217
218     b.input  = "message digest";
219     b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
220                "\xd0";
221     b.inLen  = strlen(b.input);
222     b.outLen = strlen(b.output);
223
224     c.input  = "abcdefghijklmnopqrstuvwxyz";
225     c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
226                "\x3b";
227     c.inLen  = strlen(c.input);
228     c.outLen = strlen(c.output);
229
230     d.input  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
231                "6789";
232     d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
233                "\x9f";
234     d.inLen  = strlen(d.input);
235     d.outLen = strlen(d.output);
236
237     e.input  = "1234567890123456789012345678901234567890123456789012345678"
238                "9012345678901234567890";
239     e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
240                "\x7a";
241     e.inLen  = strlen(e.input);
242     e.outLen = strlen(e.output);
243
244     test_md5[0] = a;
245     test_md5[1] = b;
246     test_md5[2] = c;
247     test_md5[3] = d;
248     test_md5[4] = e;
249
250     InitMd5(&md5);
251
252     for (i = 0; i < times; ++i) {
253         Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
254         Md5Final(&md5, hash);
255
256         if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
257             return -5 - i;
258     }
259
260     return 0;
261 }
262
263 int sha_test(void)
264 {
265     Sha  sha;
266     byte hash[SHA_DIGEST_SIZE];
267
268     testVector a, b, c, d;
269     testVector test_sha[4];
270     int times = sizeof(test_sha) / sizeof(struct testVector), i;
271
272     a.input  = "abc";
273     a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
274                "\x6C\x9C\xD0\xD8\x9D";
275     a.inLen  = strlen(a.input);
276     a.outLen = strlen(a.output);
277
278     b.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
279     b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
280                "\xE5\xE5\x46\x70\xF1";
281     b.inLen  = strlen(b.input);
282     b.outLen = strlen(b.output);
283
284     c.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
285                "aaaaaa";
286     c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
287                "\x2A\x25\xEC\x64\x4D";
288     c.inLen  = strlen(c.input);
289     c.outLen = strlen(c.output);
290
291     d.input  = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
292                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
293                "aaaaaaaaaa";
294     d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
295                "\x53\x99\x5E\x26\xA0";
296     d.inLen  = strlen(d.input);
297     d.outLen = strlen(d.output);
298
299     test_sha[0] = a;
300     test_sha[1] = b;
301     test_sha[2] = c;
302     test_sha[3] = d;
303
304     InitSha(&sha);
305
306     for (i = 0; i < times; ++i) {
307         ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
308         ShaFinal(&sha, hash);
309
310         if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
311             return -10 - i;
312     }
313
314     return 0;
315 }
316
317 #ifndef NO_SHA256
318 int sha256_test(void)
319 {
320     Sha256 sha;
321     byte   hash[SHA256_DIGEST_SIZE];
322
323     testVector a, b;
324     testVector test_sha[2];
325     int times = sizeof(test_sha) / sizeof(struct testVector), i;
326
327     a.input  = "abc";
328     a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
329                "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
330                "\x15\xAD";
331     a.inLen  = strlen(a.input);
332     a.outLen = strlen(a.output);
333
334     b.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
335     b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
336                "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
337                "\x06\xC1";
338     b.inLen  = strlen(b.input);
339     b.outLen = strlen(b.output);
340
341     test_sha[0] = a;
342     test_sha[1] = b;
343
344     InitSha256(&sha);
345
346     for (i = 0; i < times; ++i) {
347         Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
348         Sha256Final(&sha, hash);
349
350         if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
351             return -10 - i;
352     }
353
354     return 0;
355 }
356 #endif
357
358 #ifdef CYASSL_SHA512
359 int sha512_test(void)
360 {
361     Sha512 sha;
362     byte   hash[SHA512_DIGEST_SIZE];
363
364     testVector a, b;
365     testVector test_sha[2];
366     int times = sizeof(test_sha) / sizeof(struct testVector), i;
367
368     a.input  = "abc";
369     a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
370                "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
371                "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
372                "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
373                "\xa5\x4c\xa4\x9f";
374     a.inLen  = strlen(a.input);
375     a.outLen = strlen(a.output);
376
377     b.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
378                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
379     b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
380                "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
381                "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
382                "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
383                "\x87\x4b\xe9\x09"; 
384     b.inLen  = strlen(b.input);
385     b.outLen = strlen(b.output);
386
387     test_sha[0] = a;
388     test_sha[1] = b;
389
390     InitSha512(&sha);
391
392     for (i = 0; i < times; ++i) {
393         Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
394         Sha512Final(&sha, hash);
395
396         if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
397             return -10 - i;
398     }
399
400     return 0;
401 }
402 #endif
403
404 #ifdef CYASSL_SHA384
405 int sha384_test()
406 {
407     Sha384 sha;
408     byte   hash[SHA384_DIGEST_SIZE];
409
410     testVector a, b;
411     testVector test_sha[2];
412     int times = sizeof(test_sha) / sizeof(struct testVector), i;
413
414     a.input  = "abc";
415     a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
416                "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
417                "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
418                "\xc8\x25\xa7";
419     a.inLen  = strlen(a.input);
420     a.outLen = strlen(a.output);
421
422     b.input  = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
423                "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
424     b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
425                "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
426                "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
427                "\x74\x60\x39";
428     b.inLen  = strlen(b.input);
429     b.outLen = strlen(b.output);
430
431     test_sha[0] = a;
432     test_sha[1] = b;
433
434     InitSha384(&sha);
435
436     for (i = 0; i < times; ++i) {
437         Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
438         Sha384Final(&sha, hash);
439
440         if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
441             return -10 - i;
442     }
443
444     return 0;
445 }
446 #endif
447
448 #ifdef CYASSL_RIPEMD
449 int ripemd_test(void)
450 {
451     RipeMd  ripemd;
452     byte hash[RIPEMD_DIGEST_SIZE];
453
454     testVector a, b, c, d;
455     testVector test_ripemd[4];
456     int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
457
458     a.input  = "abc";
459     a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
460                "\xb0\x87\xf1\x5a\x0b\xfc";
461     a.inLen  = strlen(a.input);
462     a.outLen = strlen(a.output);
463
464     b.input  = "message digest";
465     b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
466                "\x5f\xfa\x21\x59\x5f\x36";
467     b.inLen  = strlen(b.input);
468     b.outLen = strlen(b.output);
469
470     c.input  = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; 
471     c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
472                "\xf4\x9a\xda\x62\xeb\x2b";
473     c.inLen  = strlen(c.input);
474     c.outLen = strlen(c.output);
475
476     d.input  = "12345678901234567890123456789012345678901234567890123456"
477                "789012345678901234567890";
478     d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
479                "\x82\xbf\x63\x32\x6b\xfb"; 
480     d.inLen  = strlen(d.input);
481     d.outLen = strlen(d.output);
482
483     test_ripemd[0] = a;
484     test_ripemd[1] = b;
485     test_ripemd[2] = c;
486     test_ripemd[3] = d;
487
488     InitRipeMd(&ripemd);
489
490     for (i = 0; i < times; ++i) {
491         RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
492                      (word32)test_ripemd[i].inLen);
493         RipeMdFinal(&ripemd, hash);
494
495         if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
496             return -10 - i;
497     }
498
499     return 0;
500 }
501 #endif /* CYASSL_RIPEMD */
502
503 #ifndef NO_HMAC
504 int hmac_test(void)
505 {
506     Hmac hmac;
507     byte hash[MD5_DIGEST_SIZE];
508
509     const char* keys[]=
510     {
511         "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
512         "Jefe",
513         "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
514     };
515
516     testVector a, b, c;
517     testVector test_hmac[3];
518
519     int times = sizeof(test_hmac) / sizeof(testVector), i;
520
521     a.input  = "Hi There";
522     a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
523                "\x9d";
524     a.inLen  = strlen(a.input);
525     a.outLen = strlen(a.output);
526
527     b.input  = "what do ya want for nothing?";
528     b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
529                "\x38";
530     b.inLen  = strlen(b.input);
531     b.outLen = strlen(b.output);
532
533     c.input  = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
534                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
535                "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
536                "\xDD\xDD\xDD\xDD\xDD\xDD";
537     c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
538                "\xf6";
539     c.inLen  = strlen(c.input);
540     c.outLen = strlen(c.output);
541
542     test_hmac[0] = a;
543     test_hmac[1] = b;
544     test_hmac[2] = c;
545
546     for (i = 0; i < times; ++i) {
547         HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
548         HmacUpdate(&hmac, (byte*)test_hmac[i].input,
549                    (word32)test_hmac[i].inLen);
550         HmacFinal(&hmac, hash);
551
552         if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
553             return -20 - i;
554     }
555
556     return 0;
557 }
558 #endif
559