]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-md5-test.c
+Shabal
[avr-crypto-lib.git] / test_src / main-md5-test.c
1 /* main-md5-test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  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 #include "serial-tools.h"
26 #include "uart.h"
27 #include "debug.h"
28
29 #include "md5.h"
30 #include "nessie_hash_test.h"
31 #include "performance_test.h"
32
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include "cli.h"
37
38 char* algo_name = "MD5";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43
44 void md5_ctx2hash_dummy(void* buffer, void* ctx){
45         memcpy(buffer, ctx, 16);
46 }
47
48
49 void testrun_nessie_md5(void){
50         nessie_hash_ctx.hashsize_b  = 128;
51         nessie_hash_ctx.blocksize_B = 512/8;
52         nessie_hash_ctx.ctx_size_B  = sizeof(md5_ctx_t);
53         nessie_hash_ctx.name = algo_name;
54         nessie_hash_ctx.hash_init = (nessie_hash_init_fpt)md5_init;
55         nessie_hash_ctx.hash_next = (nessie_hash_next_fpt)md5_nextBlock;
56         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)md5_lastBlock;
57         nessie_hash_ctx.hash_conv = (nessie_hash_conv_fpt)md5_ctx2hash_dummy;
58         
59         nessie_hash_run();
60 }
61
62 /*****************************************************************************
63  *  self tests                                                                                                                           *
64  *****************************************************************************/
65
66 /*
67  * MD5 test suite:
68  * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
69  * MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
70  * MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
71  * MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
72  * MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
73  * MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
74  * d174ab98d277d9f5a5611c2c9f419d9f
75  * MD5 ("123456789012345678901234567890123456789012345678901234567890123456
76  * 78901234567890") = 57edf4a22be3c955ac49da2e2107b67a
77  */
78
79 void testrun_md5(void){
80         md5_hash_t hash;
81         char* testv[]={
82                 "", 
83                 "a", 
84                 "abc", 
85                 "message digest", 
86                 "abcdefghijklmnopqrstuvwxyz", 
87                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
88                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
89         uint16_t i;
90         
91         cli_putstr("\r\n=== MD5 test suit ===");
92         for(i=0; i<7; ++i){
93                 cli_putstr("\r\n MD5 (\"");
94                 cli_putstr(testv[i]);
95                 cli_putstr("\") = \r\n\t");
96                 md5(&hash, testv[i], strlen(testv[i])*8);
97                 cli_hexdump(hash, 16);
98         }
99         uint8_t buffer[512/8];
100         char str[5];
101         for(i=505; i<512; ++i){
102                 memset(buffer, 0, 512/8);
103                 cli_putstr_P(PSTR("\r\nMD5("));
104                 utoa(i, str, 10);
105                 cli_putstr(str);
106                 cli_putstr_P(PSTR(" zero bits) = "));
107                 md5(&hash, buffer, i);
108                 cli_hexdump(hash, 16);
109         }
110 }
111
112
113 void testrun_performance_md5(void){
114         uint64_t t;
115         char str[16];
116         uint8_t data[32];
117         md5_ctx_t ctx;
118         
119         calibrateTimer();
120         print_overhead();
121         
122         memset(data, 0, 32);
123         
124         startTimer(1);
125         md5_init(&ctx);
126         t = stopTimer();
127         cli_putstr_P(PSTR("\r\n\tctx-gen time: "));
128         ultoa((unsigned long)t, str, 10);
129         cli_putstr(str);
130         
131         
132         startTimer(1);
133         md5_nextBlock(&ctx, data);
134         t = stopTimer();
135         cli_putstr_P(PSTR("\r\n\tone-block time: "));
136         ultoa((unsigned long)t, str, 10);
137         cli_putstr(str);
138         
139         
140         startTimer(1);
141         md5_lastBlock(&ctx, data, 0);
142         t = stopTimer();
143         cli_putstr_P(PSTR("\r\n\tlast block time: "));
144         ultoa((unsigned long)t, str, 10);
145         cli_putstr(str);
146         
147         cli_putstr_P(PSTR("\r\n"));
148 }
149
150
151 /*****************************************************************************
152  * main                                                                                                                                  *
153  *****************************************************************************/
154
155 const char nessie_str[]      PROGMEM = "nessie";
156 const char test_str[]        PROGMEM = "test";
157 const char performance_str[] PROGMEM = "performance";
158 const char echo_str[]        PROGMEM = "echo";
159
160 cmdlist_entry_t cmdlist[] PROGMEM = {
161         { nessie_str,      NULL, testrun_nessie_md5},
162         { test_str,        NULL, testrun_md5},
163         { performance_str, NULL, testrun_performance_md5},
164         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
165         { NULL,            NULL, NULL}
166 };
167
168 int main (void){
169         DEBUG_INIT();
170         
171         cli_rx = uart_getc;
172         cli_tx = uart_putc;             
173         for(;;){
174                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
175                 cli_putstr(algo_name);
176                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
177                 cmd_interface(cmdlist);
178         }
179 }