1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MATH - demos
3   File:     main.c
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 /*---------------------------------------------------------------------------*
18   Demo to confirm operation of digest functions in Math library
19  *---------------------------------------------------------------------------*/
20 
21 #include    <nitro.h>
22 
23 
24 
25 static void VBlankIntr(void);
26 static void DisplayInit(void);
27 static void FillScreen(u16 col);
28 static BOOL DigestTest(void);
29 static BOOL CompareDigests(u8 *ptr1, u8 *ptr2, int n);
30 static u32 GetStringLength(char *str);
31 
32 /*---------------------------------------------------------------------------*
33     Variable Definitions
34  *---------------------------------------------------------------------------*/
35 
36 static u32 sBigArray[(1024 * 1024 / sizeof(u32))];
37 
38 /*---------------------------------------------------------------------------*
39     Function definitions
40  *---------------------------------------------------------------------------*/
41 
42 /*---------------------------------------------------------------------------*
43   Name:         NitroMain
44 
45   Description:  Initialization and main loop
46 
47   Arguments:    None
48 
49   Returns:      None
50  *---------------------------------------------------------------------------*/
NitroMain(void)51 void NitroMain(void)
52 {
53     // Various types of initialization
54     OS_Init();
55 
56     DisplayInit();
57 
58     if (DigestTest())
59     {
60         // Success
61         OS_TPrintf("------ Test Succeeded ------\n");
62         FillScreen(GX_RGB(0, 31, 0));
63     }
64     else
65     {
66         // Failed
67         OS_TPrintf("****** Test Failed ******\n");
68         FillScreen(GX_RGB(31, 0, 0));
69     }
70 
71     // Main loop
72     while (TRUE)
73     {
74         // Waiting for the V-Blank
75         OS_WaitVBlankIntr();
76     }
77 }
78 
79 /*---------------------------------------------------------------------------*
80   Name:         VBlankIntr
81 
82   Description:  V-Blank interrupt vector
83 
84   Arguments:    None
85 
86   Returns:      None
87  *---------------------------------------------------------------------------*/
VBlankIntr(void)88 static void VBlankIntr(void)
89 {
90     // Sets the IRQ check flag
91     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
92 }
93 
94 /*---------------------------------------------------------------------------*
95   Name:         DisplayInit
96 
97   Description:  Graphics Initialization
98 
99   Arguments:    None
100 
101   Returns:      None
102  *---------------------------------------------------------------------------*/
DisplayInit(void)103 static void DisplayInit(void)
104 {
105 
106     GX_Init();
107     FX_Init();
108 
109     GX_DispOff();
110     GXS_DispOff();
111 
112     GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);
113 
114     OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
115     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
116     (void)GX_VBlankIntr(TRUE);         // To generate V-Blank interrupt request
117     (void)OS_EnableIrq();
118 
119 
120     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
121     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
122 
123     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);   // Clear OAM
124     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);     // Clear the standard palette
125 
126     MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);     // Clear OAM
127     MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);       // Clear the standard palette
128     MI_DmaFill32(3, (void *)HW_LCDC_VRAM_C, 0x7FFF7FFF, 256 * 192 * sizeof(u16));
129 
130 
131     GX_SetBankForOBJ(GX_VRAM_OBJ_256_AB);       // Set VRAM-A,B for OBJ
132 
133     GX_SetGraphicsMode(GX_DISPMODE_VRAM_C,      // VRAM mode
134                        (GXBGMode)0,    // Dummy
135                        (GXBG0As)0);    // Dummy
136 
137     GX_SetVisiblePlane(GX_PLANEMASK_OBJ);       // Make OBJs visible
138     GX_SetOBJVRamModeBmp(GX_OBJVRAMMODE_BMP_1D_128K);   // 2D mapping OBJ
139 
140     OS_WaitVBlankIntr();               // Waiting for the end of the V-Blank interrupt
141     GX_DispOn();
142 
143 }
144 
145 
146 /*---------------------------------------------------------------------------*
147   Name:         FillScreen
148 
149   Description:  Fills the screen
150 
151   Arguments:    col - FillColor
152 
153   Returns:      None
154  *---------------------------------------------------------------------------*/
FillScreen(u16 col)155 static void FillScreen(u16 col)
156 {
157     MI_CpuFill16((void *)HW_LCDC_VRAM_C, col, 256 * 192 * 2);
158 }
159 
160 /*---------------------------------------------------------------------------*
161   Name:         DigestTest
162 
163   Description:  Test routine for digest function
164 
165   Arguments:    None
166 
167   Returns:      TRUE if test succeeds
168  *---------------------------------------------------------------------------*/
169 #define PrintResultEq( a, b, f ) \
170     { OS_TPrintf( ((a) == (b)) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && ((a) == (b)); }
171 #define PrintResultDigestEq( a, b, l, f ) \
172     { OS_TPrintf( (CompareDigests((a), (b), (l))) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && (CompareDigests((a), (b), (l))); }
173 
DigestTest(void)174 static BOOL DigestTest(void)
175 {
176     int     i, j;
177     BOOL    flag = TRUE;
178 
179     // Perform 8-bit Checksum operation test
180     {
181         char   *a[] = {
182             "\x00\x01\xf2\x03\xf4\xf5\xf6\xf7\x00\x01",
183             "\x00\x01\xf2\x03\xf4\xf5\xf6\xf7\x00\x01",
184             "\x00\x01\xf2\x03\xf4\xf5\xf6\xf7\x00\x01",
185         };
186         u32     offset[] = {
187             0,
188             2,
189             1,
190         };
191         u32     l[] = {
192             8,
193             8,
194             8,
195         };
196         u8      result_checksum8[] = {
197             0x2f,
198             0x2f,
199             0x2f,
200         };
201         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
202         {
203             MATHChecksum8Context context;
204             u8      result;
205 
206             result = MATH_CalcChecksum8(a[i], l[i]);
207             PrintResultEq(result, result_checksum8[i], flag);
208             OS_TPrintf("MATH_Checksum8[%d] = %02x\n", i, result);
209 
210             // When the total Checksum of the combined Checksum values is taken, the result is 0x0000
211             MATH_Checksum8Init(&context);
212             MATH_Checksum8Update(&context, &result, sizeof(result));
213             MATH_Checksum8Update(&context, a[i], l[i]);
214             result = MATH_Checksum8GetHash(&context);
215             PrintResultEq(result, 0, flag);
216             OS_TPrintf("  Check Value = %02x\n", result);
217         }
218     }
219     {
220         u8      a[] = {
221             0xff,
222             0xff,
223             0xff,
224         };
225         u32     l[] = {
226             sizeof(sBigArray) - 2,
227             sizeof(sBigArray) - 2,
228             sizeof(sBigArray) - 2,
229         };
230         u32     offset[] = {
231             0,
232             1,
233             2,
234         };
235         u8      result_checksum8[] = {
236             0x00,
237             0x00,
238             0x00,
239         };
240         for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
241         {
242             MATHChecksum8Context context;
243             u8      result;
244 
245             MI_CpuFill8(((u8 *)sBigArray) + offset[i], a[i], l[i]);
246             result = MATH_CalcChecksum8(((u8 *)sBigArray) + offset[i], l[i]);
247             PrintResultEq(result, result_checksum8[i], flag);
248             OS_TPrintf("MATH_Checksum8(Large)[%d] = %02x\n", i, result);
249 
250             // When the total Checksum of the combined Checksum values is taken, the result is 0x0000
251             MATH_Checksum8Init(&context);
252             MATH_Checksum8Update(&context, &result, sizeof(result));
253             MATH_Checksum8Update(&context, ((u8 *)sBigArray) + offset[i], l[i]);
254             result = MATH_Checksum8GetHash(&context);
255             PrintResultEq(result, 0, flag);
256             OS_TPrintf("  Check Value = %02x\n", result);
257         }
258     }
259 
260     // Perform 16-bit Checksum (Internet Checksum) operation test
261     {
262         char   *a[] = {
263             "\x00\x01\xf2\x03\xf4\xf5\xf6\xf7\x00\x01",
264             "\x00\x01\xf2\x03\xf4\xf5\xf6\xf7\x00\x01",
265             "\x00\x01\xf2\x03\xf4\xf5\xf6\xf7\x00\x01",
266             "\xf7\x00\x01\xf2\x03\xf4\xf5\xf6\xf7\x00",
267         };
268         u32     offset[] = {
269             0,
270             2,
271             1,
272             1,
273         };
274         u32     l[] = {
275             8,
276             8,
277             8,
278             8,
279         };
280         u16     result_checksum16[] = {
281             0x0d22,
282             0x0d22,
283             0x220d,
284             0x0d22,
285         };
286         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
287         {
288             MATHChecksum16Context context;
289             u16     result;
290 
291             result = MATH_CalcChecksum16(a[i] + offset[i], l[i]);
292             PrintResultEq(result, result_checksum16[i], flag);
293             OS_TPrintf("MATH_Checksum16[%d] = %04x\n", i, result);
294 
295             // When Checksum values are combined, and the Checksum of the total is taken, the Internet Checksum is 0x0000
296             //
297             MATH_Checksum16Init(&context);
298             MATH_Checksum16Update(&context, &result, sizeof(result));
299             MATH_Checksum16Update(&context, a[i] + offset[i], l[i]);
300             result = MATH_Checksum16GetHash(&context);
301             PrintResultEq(result, 0, flag);
302             OS_TPrintf("  Check Value = %04x\n", result);
303         }
304     }
305     {
306         u8      a[] = {
307             0xff,
308             0xff,
309             0xff,
310         };
311         u32     l[] = {
312             sizeof(sBigArray) - 2,
313             sizeof(sBigArray) - 2,
314             sizeof(sBigArray) - 2,
315         };
316         u32     offset[] = {
317             0,
318             1,
319             2,
320         };
321         u16     result_checksum16[] = {
322             0x0000,
323             0x0000,
324             0x0000,
325         };
326         for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
327         {
328             MATHChecksum16Context context;
329             u16     result;
330 
331             MI_CpuFill8(((u8 *)sBigArray) + offset[i], a[i], l[i]);
332             result = MATH_CalcChecksum16(((u8 *)sBigArray) + offset[i], l[i]);
333             PrintResultEq(result, result_checksum16[i], flag);
334             OS_TPrintf("MATH_Checksum16(Large)[%d] = %04x\n", i, result);
335 
336             // When the total Checksum of the combined Checksum values is taken, the result is 0x0000
337             MATH_Checksum16Init(&context);
338             MATH_Checksum16Update(&context, &result, sizeof(result));
339             MATH_Checksum16Update(&context, ((u8 *)sBigArray) + offset[i], l[i]);
340             result = MATH_Checksum16GetHash(&context);
341             PrintResultEq(result, 0, flag);
342             OS_TPrintf("  Check Value = %04x\n", result);
343         }
344     }
345 
346     // Perform CRC-8 operation test
347     {
348         static MATHCRC8Table table_crc8;
349         char   *a[] = {
350             "123456789",
351         };
352         u8      result_crc8[] = {
353             0xf4,
354         };
355         MATH_CRC8InitTable(&table_crc8);
356         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
357         {
358             MATHCRC8Context context;
359             u8      result;
360 
361             MATH_CRC8Init(&context);
362             MATH_CRC8Update(&table_crc8, &context, a[i], GetStringLength(a[i]));
363             result = MATH_CRC8GetHash(&context);
364             PrintResultEq(result, result_crc8[i], flag);
365             OS_TPrintf("MATH_CRC8(%s) = %02x\n", a[i], result);
366         }
367     }
368 
369     // Perform CRC-16 operation test
370     {
371         static MATHCRC16Table table_crc16;
372         char   *a[] = {
373             "123456789",
374         };
375         u16     result_crc16[] = {
376             0xbb3d,
377         };
378         MATH_CRC16InitTable(&table_crc16);
379         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
380         {
381             MATHCRC16Context context;
382             u16     result;
383 
384             MATH_CRC16Init(&context);
385             MATH_CRC16Update(&table_crc16, &context, a[i], GetStringLength(a[i]));
386             result = MATH_CRC16GetHash(&context);
387             PrintResultEq(result, result_crc16[i], flag);
388             OS_TPrintf("MATH_CRC16(%s) = %04x\n", a[i], result);
389         }
390     }
391 
392     // Perform CRC-16/CCITT operation test
393     {
394         static MATHCRC16Table table_crc16_ccitt;
395         char   *a[] = {
396             "123456789",
397         };
398         u16     result_crc16_ccitt[] = {
399             0x29b1,
400         };
401         MATH_CRC16CCITTInitTable(&table_crc16_ccitt);
402         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
403         {
404             MATHCRC16Context context;
405             u16     result;
406 
407             MATH_CRC16CCITTInit(&context);
408             MATH_CRC16CCITTUpdate(&table_crc16_ccitt, &context, a[i], GetStringLength(a[i]));
409             result = MATH_CRC16CCITTGetHash(&context);
410             PrintResultEq(result, result_crc16_ccitt[i], flag);
411             OS_TPrintf("MATH_CRC16CCITT(%s) = %04x\n", a[i], result);
412         }
413     }
414 
415     // Perform CRC-32 operation test
416     {
417         static MATHCRC32Table table_crc32;
418         char   *a[] = {
419             "123456789",
420         };
421         u32     result_crc32[] = {
422             0xcbf43926,
423         };
424         MATH_CRC32InitTable(&table_crc32);
425         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
426         {
427             MATHCRC32Context context;
428             u32     result;
429 
430             MATH_CRC32Init(&context);
431             MATH_CRC32Update(&table_crc32, &context, a[i], GetStringLength(a[i]));
432             result = MATH_CRC32GetHash(&context);
433             PrintResultEq(result, result_crc32[i], flag);
434             OS_TPrintf("MATH_CRC32(%s) = %04x\n", a[i], result);
435         }
436     }
437 
438     // Perform CRC-32/POSIX operation test
439     {
440         static MATHCRC32Table table_crc32_posix;
441         char   *a[] = {
442             "123456789",
443         };
444         u32     result_crc32_posix[] = {
445             0x765e7680,
446         };
447         MATH_CRC32POSIXInitTable(&table_crc32_posix);
448         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
449         {
450             MATHCRC32Context context;
451             u32     result;
452 
453             MATH_CRC32POSIXInit(&context);
454             MATH_CRC32POSIXUpdate(&table_crc32_posix, &context, a[i], GetStringLength(a[i]));
455             result = MATH_CRC32POSIXGetHash(&context);
456             PrintResultEq(result, result_crc32_posix[i], flag);
457             OS_TPrintf("MATH_CRC32POSIX(%s) = %04x\n", a[i], result);
458         }
459     }
460 
461     // Perform MD5 operation test
462     // See RFC1321 A.5 Test suite
463     {
464         char   *a[] = {
465             "",
466             "a",
467             "abc",
468             "message digest",
469             "abcdefghijklmnopqrstuvwxyz",
470             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
471             "1234567890",
472             "FHXXKCVGGOFRILWGDLFQLZBLQSNYXWUCTXCBBVHUDWXLSFNIFTBNCGOMLXPQQFFJEOBXFBZNEDWMSKPPEWXONABBPRBHVDNQXPRHQGMLUHJVUPOXYUHDEXYMZBJCFNYZSKMPNLPYADWGXEKPYPKGNHQZMPBMSCYYYUPVPQZSYPPPIGKBQZUWOUCMVLFKORUIZWVWFVBEPZBXASLDAVBEHMJGYPYBQZAKHNLOFCPQVRRGKOVVRNERIFFJZIYAECMNHTRCUWGAKRZXPORLXLRXVRVVJIRTNDDIEFNGWFXBFDPPFOMPFWRGXQUBJYTNCCXKTUGRQYYGRFMDPKTWWFREVAUNHSAWUQQJOPIGRWHJDDOVHZHHGXDKVSDYRGXGCWMKQSENCIMJQOGNGAZFTDXYEVNRDWDKCKPJPIXZKCGJPEGFIJANNPJYHIRLPUBQAJSFMYLVGCPQZRUNKRRWEBLZYYOQUHGEMKGCXPKBWRXRANLRRHLGZSMONYDRIFPHOYSVYKZJRUSOVVUCULTSBOSGKYUVYCVHMIASIBFKGPRCKLVXZAGKCQUKMIHOIQJRBCJBKOYRNACLNHPSDBKUFBXAODNVHVTVVHHZEBAVRVMKBBEOYJSABXUDBJQEFLLYVBPWMSQQVLTGCASWRSHPIDOZRPZUCHYREWFCDZJOMMUGPHPTKWEIJIIGEHNFXKLWIHPKYORNBXESRYZMUNTJDGNQFGAFFRAWCKYRBZGNBZKOWAGWXZCOOEERHWPYIVXHDSZAVYFUBXNRJCVDDUJWWMYYXMCTXANZOPVULYAGPXZUQRBOAWEJKTUKIIDZVBVQRKINSAMSYKZVCINUUWVBVVUGGXLKMVBTFEESRQTNPOEYZRZVJBUEEXIQOXEWLMUUJQUWNOQTQQNJFBDNTCXMNJMJBIOCQZUCKCZFECNAXBXHLTCVURSEBLVAUIJKVGWZETIVGHWOFGRFPLIWQAUMTZOELCXBMVLHBRFZUIEIYWWUY",
473             "YEDCGPVKFGWXYSDLTQOYAFHSJYABKIMQHJGDCKUSLRDQFJDGPVWBSBQ",
474             "YEDCGPVKFGWXYSDLTQOYAFHSJYABKIMQHJGDCKUSLRDQFJDGPVWBSBQZ",
475             "YEDCGPVKFGWXYSDLTQOYAFHSJYABKIMQHJGDCKUSLRDQFJDGPVWBSBQZZ",
476         };
477         int     c[] = { 1, 1, 1, 1, 1, 1, 8, 1, 2, 2, 2 };
478         char   *result_md5[] = {
479             "\xD4\x1D\x8C\xD9\x8F\x00\xB2\x04\xE9\x80\x09\x98\xEC\xF8\x42\x7E",
480             "\x0C\xC1\x75\xB9\xC0\xF1\xB6\xA8\x31\xC3\x99\xE2\x69\x77\x26\x61",
481             "\x90\x01\x50\x98\x3C\xD2\x4F\xB0\xD6\x96\x3F\x7D\x28\xE1\x7F\x72",
482             "\xF9\x6B\x69\x7D\x7C\xB7\x93\x8D\x52\x5A\x2F\x31\xAA\xF1\x61\xD0",
483             "\xC3\xFC\xD3\xD7\x61\x92\xE4\x00\x7D\xFB\x49\x6C\xCA\x67\xE1\x3B",
484             "\xD1\x74\xAB\x98\xD2\x77\xD9\xF5\xA5\x61\x1C\x2C\x9F\x41\x9D\x9F",
485             "\x57\xED\xF4\xA2\x2B\xE3\xC9\x55\xAC\x49\xDA\x2E\x21\x07\xB6\x7A",
486             "\x72\xcc\x6a\x07\x35\x47\x48\x5d\xe7\x53\xcd\xa5\x90\xcb\x90\xbd",
487             "\x5c\x07\x79\xcb\x86\xb1\x89\x2b\xe5\x3b\xdc\x72\x7b\x3b\x9b\x41",
488             "\xc8\x69\xd4\x26\xf3\x4f\x78\x1b\xed\xaf\xdc\x9d\x29\xaf\x75\x7f",
489             "\xa3\x1b\xd7\x8a\x72\x15\xa7\xc8\x59\x41\x51\xd5\x4b\xc8\x7d\x30",
490         };
491         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
492         {
493             MATHMD5Context context;
494             u8      result[MATH_MD5_DIGEST_SIZE];
495 
496             MATH_MD5Init(&context);
497             for (j = 0; j < c[i]; j++)
498             {
499                 MATH_MD5Update(&context, a[i], GetStringLength(a[i]));
500             }
501             MATH_MD5GetHash(&context, result);
502             PrintResultDigestEq(result, (u8 *)result_md5[i], sizeof(result), flag);
503             OS_TPrintf("MATH_MD5(%s) * %d\n", a[i], c[i]);
504         }
505     }
506 
507     // Perform HMAC-MD5 operation test
508     // See RFC2202
509     {
510         char   *d[] = {
511             "Hi There",
512             "what do ya want for nothing?",
513             "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
514                 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
515                 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
516             "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
517                 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
518                 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
519             "Test With Truncation",    // This test program does not use truncation
520             "Test Using Larger Than Block-Size Key - Hash Key First",
521             "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
522         };
523         u32     dl[] = { 8, 28, 50, 50, 20, 54, 73 };
524         char   *k[] = {
525             "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
526             "Jefe",
527             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
528             "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
529             "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
530             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
531                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
532                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
533                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
534             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
535                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
536                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
537                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
538         };
539         u32     kl[] = { 16, 4, 16, 25, 16, 80, 80 };
540         char   *result_hmacmd5[] = {
541             "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d",
542             "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38",
543             "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6",
544             "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79",
545             "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c",
546             "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd",
547             "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e",
548         };
549         for (i = 0; i < sizeof(d) / sizeof(char *); i++)
550         {
551             u8      result[MATH_MD5_DIGEST_SIZE];
552 
553             MATH_CalcHMACMD5(result, d[i], dl[i], k[i], kl[i]);
554             PrintResultDigestEq(result, (u8 *)result_hmacmd5[i], sizeof(result), flag);
555             OS_TPrintf("MATH_MD5CalcHmac: Test Case %d\n", i + 1);
556         }
557     }
558 
559     // Perform SHA-1 operation test.
560     // See RFC3174 7.3 Test Driver.
561     {
562         char   *a[] = {
563             "abc",
564             "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
565             "a",
566             "0123456701234567012345670123456701234567012345670123456701234567",
567         };
568         int     c[] = { 1, 1, 1000000, 10 };
569         char   *result_sha1[] = {
570             "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D",
571             "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1",
572             "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F",
573             "\xDE\xA3\x56\xA2\xCD\xDD\x90\xC7\xA7\xEC\xED\xC5\xEB\xB5\x63\x93\x4F\x46\x04\x52",
574         };
575         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
576         {
577             MATHSHA1Context context;
578             u8      result[MATH_SHA1_DIGEST_SIZE];
579 
580             MATH_SHA1Init(&context);
581             for (j = 0; j < c[i]; j++)
582             {
583                 MATH_SHA1Update(&context, a[i], GetStringLength(a[i]));
584             }
585             MATH_SHA1GetHash(&context, result);
586             PrintResultDigestEq(result, (u8 *)result_sha1[i], sizeof(result), flag);
587             OS_TPrintf("MATH_SHA1(%s) * %d\n", a[i], c[i]);
588         }
589     }
590 
591     // Perform SHA-256 operational test
592     // See FIPS PUB 180-2 APPENDIX B
593     {
594         char   *a[] = {
595             "abc",
596             "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
597             "a",
598         };
599         int     c[] = { 1, 1, 1000000 };
600         char   *result_sha256[] = {
601             "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00\x15\xAD",
602             "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1",
603             "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0",
604         };
605         for (i = 0; i < sizeof(a) / sizeof(char *); i++)
606         {
607             MATHSHA256Context context;
608             u8      result[MATH_SHA256_DIGEST_SIZE];
609 
610             MATH_SHA256Init(&context);
611             for (j = 0; j < c[i]; j++)
612             {
613                 MATH_SHA256Update(&context, a[i], GetStringLength(a[i]));
614             }
615             MATH_SHA256GetHash(&context, result);
616             PrintResultDigestEq(result, (u8 *)result_sha256[i], sizeof(result), flag);
617             OS_TPrintf("MATH_SHA256(%s) * %d\n", a[i], c[i]);
618         }
619     }
620 
621     // Perform HMAC-SHA-1 operation test
622     // See RFC2202
623     {
624         char   *d[] = {
625             "Hi There",
626             "what do ya want for nothing?",
627             "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
628                 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
629                 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
630             "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
631                 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
632                 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
633             "Test With Truncation",    // This test program does not use truncation.
634             "Test Using Larger Than Block-Size Key - Hash Key First",
635             "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
636         };
637         u32     dl[] = { 8, 28, 50, 50, 20, 54, 73 };
638         char   *k[] = {
639             "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
640             "Jefe",
641             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
642             "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
643             "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
644             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
645                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
646                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
647                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
648             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
649                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
650                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
651                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
652         };
653         u32     kl[] = { 20, 4, 20, 25, 20, 80, 80 };
654         char   *result_hmacsha1[] = {
655             "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00",
656             "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79",
657             "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3",
658             "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda",
659             "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04",
660             "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12",
661             "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91",
662         };
663         for (i = 0; i < sizeof(d) / sizeof(char *); i++)
664         {
665             u8      result[MATH_SHA1_DIGEST_SIZE];
666 
667             MATH_CalcHMACSHA1(result, d[i], dl[i], k[i], kl[i]);
668             PrintResultDigestEq(result, (u8 *)result_hmacsha1[i], sizeof(result), flag);
669             OS_TPrintf("MATH_SHA1CalcHmac: Test Case %d\n", i + 1);
670         }
671     }
672     // Perform HMAC-SHA-256 operation test
673     // See RFC 4231
674 	{
675         char   *d[] = {
676             "Hi There",
677             "what do ya want for nothing?",
678             "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
679                 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
680                 "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
681             "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
682                 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
683                 "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
684             "Test With Truncation",
685             "Test Using Larger Than Block-Size Key - Hash Key First",
686             "This is a test using a larger than block-size key and a larger than block-size data. "
687 				"The key needs to be hashed before being used by the HMAC algorithm."
688 		};
689         u32     dl[] = { 8, 28, 50, 50, 20, 54, 152 };
690         char   *k[] = {
691             "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
692             "Jefe",
693             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
694             "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
695             "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
696             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
697                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
698                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
699                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
700                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
701                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
702                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
703             "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
704                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
705                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
706                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
707                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
708                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
709                 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
710         };
711         u32     kl[] = { 20, 4, 20, 25, 20, 131, 131 };
712         char   *result_hmacsha256[] = {
713 			"\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00"
714 				"\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7",
715 			"\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7\x5a\x00\x3f\x08"
716 				"\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43",
717 			"\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7\x29\x59\x09\x8b"
718 				"\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe",
719 			"\x82\x55\x8a\x38\x9a\x44\x3c\x0e\xa4\xcc\x81\x98\x99\xf2\x08\x3a\x85\xf0\xfa\xa3"
720 				"\xe5\x78\xf8\x07\x7a\x2e\x3f\xf4\x67\x29\x66\x5b",
721 			"\xa3\xb6\x16\x74\x73\x10\x0e\xe0\x6e\x0c\x79\x6c\x29\x55\x55\x2b",
722 			"\x60\xe4\x31\x59\x1e\xe0\xb6\x7f\x0d\x8a\x26\xaa\xcb\xf5\xb7\x7f\x8e\x0b\xc6\x21"
723 				"\x37\x28\xc5\x14\x05\x46\x04\x0f\x0e\xe3\x7f\x54",
724 			"\x9b\x09\xff\xa7\x1b\x94\x2f\xcb\x27\x63\x5f\xbc\xd5\xb0\xe9\x44\xbf\xdc\x63\x64"
725 				"\x4f\x07\x13\x93\x8a\x7f\x51\x53\x5c\x3a\x35\xe2",
726         };
727         for (i = 0; i < sizeof(d) / sizeof(char *); i++)
728         {
729             u8      result[MATH_SHA256_DIGEST_SIZE];
730 			int     len = MATH_SHA256_DIGEST_SIZE;
731 
732             MATH_CalcHMACSHA256(result, d[i], dl[i], k[i], kl[i]);
733 			if(i == 4)  // Test Case 5
734 			{
735 				*(result + 16) = '\0';
736 				len = 16;
737 			}
738             PrintResultDigestEq(result, (u8 *)result_hmacsha256[i], len, flag);
739             OS_TPrintf("MATH_SHA256CalcHmac: Test Case %d\n", i + 1);
740         }
741 	}
742 
743 
744 
745 
746     return flag;
747 }
748 
749 
750 /*---------------------------------------------------------------------------*
751   Name:         CompareDigests
752 
753   Description:  Compare memory contents
754 
755   Arguments:    ptr1, ptr2 - Memory locations to compare
756                 n - Length to compare
757 
758   Returns:      TRUE (if they match)
759  *---------------------------------------------------------------------------*/
CompareDigests(u8 * ptr1,u8 * ptr2,int n)760 static BOOL CompareDigests(u8 *ptr1, u8 *ptr2, int n)
761 {
762     int     i;
763     for (i = 0; i < n; i++)
764     {
765         if (*(ptr1++) != *(ptr2++))
766         {
767             return FALSE;
768         }
769     }
770     return TRUE;
771 }
772 
773 /*---------------------------------------------------------------------------*
774   Name:         GetStringLength
775 
776   Description:  Gets the length of a string
777 
778   Arguments:    str: Pointer to a string
779 
780   Returns:      Length of character string
781  *---------------------------------------------------------------------------*/
GetStringLength(char * str)782 static u32 GetStringLength(char *str)
783 {
784     u32     i;
785     for (i = 0;; i++)
786     {
787         if (*(str++) == '\0')
788         {
789             return i;
790         }
791     }
792 }
793 
794 
795 /*---------------------------------------------------------------------------*
796   End of file
797  *---------------------------------------------------------------------------*/
798