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