]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-hmac-md5-test.c
verification seems to work now...
[avr-crypto-lib.git] / test_src / main-hmac-md5-test.c
1 /* main-hmac-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  * HMAC-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 "hmac-md5.h"
31 /*
32 #include "base64_enc.h"
33 #include "base64_dec.h"
34 */
35 #include "nessie_mac_test.h"
36
37 #include <stdint.h>
38 #include <string.h>
39 #include "cli.h"
40
41 char* algo_name = "HMAC-MD5";
42
43 /*****************************************************************************
44  *  additional validation-functions                                                                                      *
45  *****************************************************************************/
46 void testrun_nessie_hmacmd5(void){
47         nessie_mac_ctx.macsize_b   = HMAC_MD5_BITS;
48         nessie_mac_ctx.keysize_b   = HMAC_MD5_BLOCK_BITS;
49         nessie_mac_ctx.blocksize_B = HMAC_MD5_BLOCK_BYTES;
50         nessie_mac_ctx.ctx_size_B  = sizeof(hmac_md5_ctx_t);
51         nessie_mac_ctx.name = algo_name;
52         nessie_mac_ctx.mac_init = (nessie_mac_init_fpt)hmac_md5_init;
53         nessie_mac_ctx.mac_next = (nessie_mac_next_fpt)hmac_md5_nextBlock;
54         nessie_mac_ctx.mac_last = (nessie_mac_last_fpt)hmac_md5_lastBlock;
55         nessie_mac_ctx.mac_conv = (nessie_mac_conv_fpt)hmac_md5_final;
56         
57         nessie_mac_run();
58 }
59
60 void testrun_test_hmacmd5(void){
61         uint8_t hmac[16];
62         uint8_t keys[][16] = {
63                 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
64                   0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b },
65                 { 'J', 'e', 'f', 'e', },
66                 { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
67                   0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }
68         };
69         uint8_t buffer[50];
70         cli_putstr_P(PSTR("\r\n hmac (1): "));
71         hmac_md5(hmac, keys[0], 128, "Hi There", 64);
72         cli_hexdump(hmac, 16);
73         
74         cli_putstr_P(PSTR("\r\n hmac (2): "));
75         hmac_md5(hmac, keys[1], 4*8, "what do ya want for nothing?", 28*8);
76         cli_hexdump(hmac, 16);
77         
78         cli_putstr_P(PSTR("\r\n hmac (3): "));
79         memset(buffer, 0xDD, 50);
80         hmac_md5(hmac, keys[2], 128, buffer, 50*8);
81         cli_hexdump(hmac, 16);
82         
83 }
84
85 void hmacmd5_interactive(void){
86         char key[101];
87         char msg[101];
88         uint8_t hmac[HMAC_MD5_BYTES];
89         uint8_t key_len, msg_len;
90         cli_putstr_P(PSTR("\r\nHMAC-MD5:\r\nkey: "));
91         cli_getsn(key, 100);
92         key_len = strlen(key);
93         cli_putstr_P(PSTR("\r\nmsg: "));
94         cli_getsn(msg, 100);
95         msg_len = strlen(msg);
96         hmac_md5(hmac, key, key_len*8, msg, msg_len*8);
97         cli_putstr_P(PSTR("\r\nhmac-md5: "));
98         cli_hexdump(hmac, HMAC_MD5_BYTES);
99 }
100
101 void strhexdump(char* dest, void* src, uint16_t length){
102         char table[] = { '0', '1', '2', '3', 
103                          '4', '5', '6', '7', 
104                                          '8', '9', 'a', 'b', 
105                                          'c', 'd', 'e', 'f' };
106         while(length--){
107                 *dest++ = table[(*((uint8_t*)src))>>4];
108                 *dest++ = table[(*((uint8_t*)src))&0xf];
109                 src = (uint8_t*)src +1;
110         }
111 }
112
113 /*
114 void cram_md5_interactive(void){
115         char key[101];
116         char msg_b64[101];
117         char username[101];
118         uint8_t msg[100];
119         uint8_t hmac[HMAC_MD5_BYTES];
120         int key_len, msg_len;
121         int ul;
122         cli_putstr_P(PSTR("\r\nCRAM-MD5:\r\nkey: "));
123         cli_getsn(key, 100);
124         key_len = strlen(key);
125         cli_putstr_P(PSTR("\r\nusername: "));
126         cli_getsn(username, 60);
127         cli_putstr_P(PSTR("\r\nchallange: "));
128         cli_getsn(msg_b64, 100);
129         if((msg_len = base64_binlength(msg_b64, 1))==-1){
130                 cli_putstr_P(PSTR("\r\nERROR in base64 encoding !\r\n"));
131                 return;
132         }
133         base64dec(msg, msg_b64, 1);
134         hmac_md5(hmac, key, key_len*8, msg, msg_len*8);
135         ul=strlen(username);
136         username[ul]=' ';
137         strhexdump(username+ul+1, hmac, 128/8);
138         base64enc(msg_b64, username, ul+1+128/8*2);
139         cli_putstr_P(PSTR("\r\nresponse: "));
140         cli_hexdump(hmac, HMAC_MD5_BYTES);
141         cli_putstr_P(PSTR("\r\nresponse (b64): "));
142         cli_putstr(msg_b64);    
143 }
144 */
145
146
147 void md5_interactive(void){
148         char msg[101];
149         uint8_t hash[MD5_HASH_BYTES];
150         uint8_t msg_len;
151         cli_putstr_P(PSTR("\r\nmsg: "));
152         cli_getsn(msg, 100);
153         msg_len = strlen(msg);
154         md5((void*)hash, msg, msg_len*8);
155         cli_putstr_P(PSTR("\r\nmd5: "));
156         cli_hexdump(hash, MD5_HASH_BYTES);
157 }
158
159
160 /*****************************************************************************
161  *  main                                                                                                                                         *
162  *****************************************************************************/
163
164 const char nessie_str[]      PROGMEM = "nessie";
165 const char test_str[]        PROGMEM = "test";
166 /* const char performance_str[] PROGMEM = "performance"; */
167 const char echo_str[]        PROGMEM = "echo";
168 const char hmd5i_str[]       PROGMEM = "hmac-md5";
169 /* const char crammd5i_str[]    PROGMEM = "cram-md5"; */
170 const char md5i_str[]        PROGMEM = "md5";
171
172
173 cmdlist_entry_t cmdlist[] PROGMEM = {
174         { nessie_str,      NULL, testrun_nessie_hmacmd5},
175         { test_str,        NULL, testrun_test_hmacmd5},
176         { hmd5i_str,       NULL, hmacmd5_interactive},
177 /*      { crammd5i_str,    NULL, cram_md5_interactive},        */
178         { md5i_str,        NULL, md5_interactive},
179 /*      { performance_str, NULL, testrun_performance_hmacmd5}, */
180         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
181         { NULL,            NULL, NULL}
182 };
183
184 int main (void){
185         DEBUG_INIT();
186         cli_rx = (cli_rx_fpt)uart0_getc;
187         cli_tx = (cli_tx_fpt)uart0_putc;                
188         for(;;){
189                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
190                 cli_putstr(algo_name);
191                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
192                 cmd_interface(cmdlist);
193         }
194 }