]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/main-md5-test.c
more files
[arm-crypto-lib.git] / test_src / main-md5-test.c
1 /* main-md5-test.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20  * md5 test suit
21  * 
22 */
23
24 #include "config.h"
25
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "md5.h"
30 #include "nessie_hash_test.h"
31 #include "performance_test.h"
32 #include "hfal_md5.h"
33 #include "hfal-performance.h"
34
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include "cli.h"
39
40 char* algo_name = "MD5";
41
42 const hfdesc_t* algolist[] PROGMEM = {
43         (hfdesc_t*)&md5_desc,
44         NULL
45 };
46
47 /*****************************************************************************
48  *  additional validation-functions                                                                                      *
49  *****************************************************************************/
50
51 void md5_ctx2hash_dummy(void* buffer, void* ctx){
52         memcpy(buffer, ctx, 16);
53 }
54
55
56 void testrun_nessie_md5(void){
57         nessie_hash_ctx.hashsize_b  = 128;
58         nessie_hash_ctx.blocksize_B = 512/8;
59         nessie_hash_ctx.ctx_size_B  = sizeof(md5_ctx_t);
60         nessie_hash_ctx.name = algo_name;
61         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)md5_init;
62         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)md5_nextBlock;
63         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)md5_lastBlock;
64         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)md5_ctx2hash_dummy;
65         
66         nessie_hash_run();
67 }
68
69 /*****************************************************************************
70  *  self tests                                                                                                                           *
71  *****************************************************************************/
72
73 /*
74  * MD5 test suite:
75  * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
76  * MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
77  * MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
78  * MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
79  * MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
80  * MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
81  * d174ab98d277d9f5a5611c2c9f419d9f
82  * MD5 ("123456789012345678901234567890123456789012345678901234567890123456
83  * 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
84  */
85
86 void testrun_md5(void){
87         md5_hash_t hash;
88         char* testv[]={
89                 "", 
90                 "a", 
91                 "abc", 
92                 "message digest", 
93                 "abcdefghijklmnopqrstuvwxyz", 
94                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
95                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
96         uint16_t i;
97         
98         cli_putstr("\r\n=== MD5 test suit ===");
99         for(i=0; i<7; ++i){
100                 cli_putstr("\r\n MD5 (\"");
101                 cli_putstr(testv[i]);
102                 cli_putstr("\");= \r\n\t");
103                 md5(&hash, testv[i], strlen(testv[i])*8);
104                 cli_hexdump(hash, 16);
105         }
106         uint8_t buffer[512/8];
107         char str[5];
108         for(i=505; i<512; ++i){
109                 memset(buffer, 0, 512/8);
110                 cli_putstr("\r\nMD5(");
111                 utoa(i, str, 10);
112                 cli_putstr(str);
113                 cli_putstr(" zero bits) = ");
114                 md5(&hash, buffer, i);
115                 cli_hexdump(hash, 16);
116         }
117 }
118
119
120 void testrun_performance_md5(void){
121         hfal_performance_multiple(algolist);
122 }
123
124
125 /*****************************************************************************
126  * main                                                                                                                                  *
127  *****************************************************************************/
128
129 const char nessie_str[]      PROGMEM = "nessie";
130 const char test_str[]        PROGMEM = "test";
131 const char performance_str[] PROGMEM = "performance";
132 const char echo_str[]        PROGMEM = "echo";
133
134 cmdlist_entry_t cmdlist[] PROGMEM = {
135         { nessie_str,      NULL, testrun_nessie_md5},
136         { test_str,        NULL, testrun_md5},
137         { performance_str, NULL, testrun_performance_md5},
138         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
139         { NULL,            NULL, NULL}
140 };
141
142 int main (void){
143         DEBUG_INIT();
144         
145         cli_rx = (cli_rx_fpt)uart0_getc;
146         cli_tx = (cli_tx_fpt)uart0_putc;                
147         testrun_md5();
148         for(;;){
149                 cli_putstr("\r\n\r\nCrypto-VS (");
150                 cli_putstr(algo_name);
151                 cli_putstr(")\r\nloaded and running\r\n");
152                 cmd_interface(cmdlist);
153         }
154 }