]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-md5-test.c
modification to the build system
[avr-crypto-lib.git] / test_src / main-md5-test.c
1 /* main-md5-test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-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 void md5_next_dummy(void* buffer, void* ctx){
44         md5_nextBlock(ctx, buffer);
45 }
46
47 void md5_last_dummy(void* buffer, uint16_t size_b, void* ctx){
48         md5_lastBlock(ctx, buffer, size_b);
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_next_dummy;
63         nessie_hash_ctx.hash_last = (nessie_hash_last_fpt)md5_last_dummy;
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_ctx_t s;
88         char* testv[]={"", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", 
89                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
90                 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"};
91         uint8_t i;
92         
93         uart_putstr("\r\n=== MD5 test suit ===");
94         for(i=0; i<7; ++i){
95                 uart_putstr("\r\n MD5 (\"");
96                 uart_putstr(testv[i]);
97                 uart_putstr("\") = \r\n");
98                 md5_init(&s);
99                 md5_lastBlock(&s, testv[i], strlen(testv[i])*8);
100                 uart_hexdump(&s.a[0], 16);
101         }
102 }
103
104
105 void testrun_performance_md5(void){
106         uint64_t t;
107         char str[16];
108         uint8_t data[32];
109         md5_ctx_t ctx;
110         
111         calibrateTimer();
112         print_overhead();
113         
114         memset(data, 0, 32);
115         
116         startTimer(1);
117         md5_init(&ctx);
118         t = stopTimer();
119         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
120         ultoa((unsigned long)t, str, 10);
121         uart_putstr(str);
122         
123         
124         startTimer(1);
125         md5_nextBlock(&ctx, data);
126         t = stopTimer();
127         uart_putstr_P(PSTR("\r\n\tone-block time: "));
128         ultoa((unsigned long)t, str, 10);
129         uart_putstr(str);
130         
131         
132         startTimer(1);
133         md5_lastBlock(&ctx, data, 0);
134         t = stopTimer();
135         uart_putstr_P(PSTR("\r\n\tlast block time: "));
136         ultoa((unsigned long)t, str, 10);
137         uart_putstr(str);
138         
139         uart_putstr_P(PSTR("\r\n"));
140 }
141
142
143 /*****************************************************************************
144  *  main                                                                                                                                         *
145  *****************************************************************************/
146
147 int main (void){
148         char str[20];
149
150         
151         DEBUG_INIT();
152         uart_putstr("\r\n");
153
154         uart_putstr("\r\n\r\nCrypto-VS (MD5)\r\nloaded and running\r\n");
155         PGM_P    u   = PSTR("nessie\0test\0performance\0");
156         void_fpt v[] = {testrun_nessie_md5, testrun_md5, testrun_performance_md5};
157
158         while(1){ 
159                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
160                 if(execcommand_d0_P(str, u, v)<0){
161                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
162                 }
163                 continue;
164         error:
165                 uart_putstr("ERROR\r\n");
166         }
167 }
168