1 /* main-hmac-md5-test.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
25 #include "serial-tools.h"
31 #include "base64_enc.h"
32 #include "base64_dec.h"
34 #include "nessie_mac_test.h"
40 char* algo_name = "HMAC-MD5";
42 /*****************************************************************************
43 * additional validation-functions *
44 *****************************************************************************/
45 void testrun_nessie_hmacmd5(void){
46 nessie_mac_ctx.macsize_b = HMAC_MD5_BITS;
47 nessie_mac_ctx.keysize_b = HMAC_MD5_BLOCK_BITS;
48 nessie_mac_ctx.blocksize_B = HMAC_MD5_BLOCK_BYTES;
49 nessie_mac_ctx.ctx_size_B = sizeof(hmac_md5_ctx_t);
50 nessie_mac_ctx.name = algo_name;
51 nessie_mac_ctx.mac_init = (nessie_mac_init_fpt)hmac_md5_init;
52 nessie_mac_ctx.mac_next = (nessie_mac_next_fpt)hmac_md5_nextBlock;
53 nessie_mac_ctx.mac_last = (nessie_mac_last_fpt)hmac_md5_lastBlock;
54 nessie_mac_ctx.mac_conv = (nessie_mac_conv_fpt)hmac_md5_final;
59 void testrun_test_hmacmd5(void){
61 uint8_t keys[][16] = {
62 { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
63 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b },
64 { 'J', 'e', 'f', 'e', },
65 { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
66 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }
69 cli_putstr_P(PSTR("\r\n hmac (1): "));
70 hmac_md5(hmac, keys[0], 128, "Hi There", 64);
71 cli_hexdump(hmac, 16);
73 cli_putstr_P(PSTR("\r\n hmac (2): "));
74 hmac_md5(hmac, keys[1], 4*8, "what do ya want for nothing?", 28*8);
75 cli_hexdump(hmac, 16);
77 cli_putstr_P(PSTR("\r\n hmac (3): "));
78 memset(buffer, 0xDD, 50);
79 hmac_md5(hmac, keys[2], 128, buffer, 50*8);
80 cli_hexdump(hmac, 16);
84 void hmacmd5_interactive(void){
87 uint8_t hmac[HMAC_MD5_BYTES];
88 uint8_t key_len, msg_len;
89 cli_putstr_P(PSTR("\r\nHMAC-MD5:\r\nkey: "));
91 key_len = strlen(key);
92 cli_putstr_P(PSTR("\r\nmsg: "));
94 msg_len = strlen(msg);
95 hmac_md5(hmac, key, key_len*8, msg, msg_len*8);
96 cli_putstr_P(PSTR("\r\nhmac-md5: "));
97 cli_hexdump(hmac, HMAC_MD5_BYTES);
100 void strhexdump(char* dest, void* src, uint16_t length){
101 char table[] = { '0', '1', '2', '3',
104 'c', 'd', 'e', 'f' };
106 *dest++ = table[(*((uint8_t*)src))>>4];
107 *dest++ = table[(*((uint8_t*)src))&0xf];
108 src = (uint8_t*)src +1;
112 void cram_md5_interactive(void){
117 uint8_t hmac[HMAC_MD5_BYTES];
118 int key_len, msg_len;
120 cli_putstr_P(PSTR("\r\nCRAM-MD5:\r\nkey: "));
122 key_len = strlen(key);
123 cli_putstr_P(PSTR("\r\nusername: "));
124 cli_getsn(username, 60);
125 cli_putstr_P(PSTR("\r\nchallange: "));
126 cli_getsn(msg_b64, 100);
127 if((msg_len = base64_binlength(msg_b64, 1))==-1){
128 cli_putstr_P(PSTR("\r\nERROR in base64 encoding !\r\n"));
131 base64dec(msg, msg_b64, 1);
132 hmac_md5(hmac, key, key_len*8, msg, msg_len*8);
135 strhexdump(username+ul+1, hmac, 128/8);
136 base64enc(msg_b64, username, ul+1+128/8*2);
137 cli_putstr_P(PSTR("\r\nresponse: "));
138 cli_hexdump(hmac, HMAC_MD5_BYTES);
139 cli_putstr_P(PSTR("\r\nresponse (b64): "));
145 void md5_interactive(void){
147 uint8_t hash[MD5_HASH_BYTES];
149 cli_putstr_P(PSTR("\r\nmsg: "));
151 msg_len = strlen(msg);
152 md5((void*)hash, msg, msg_len*8);
153 cli_putstr_P(PSTR("\r\nmd5: "));
154 cli_hexdump(hash, MD5_HASH_BYTES);
158 /*****************************************************************************
160 *****************************************************************************/
162 const char nessie_str[] PROGMEM = "nessie";
163 const char test_str[] PROGMEM = "test";
164 /* const char performance_str[] PROGMEM = "performance"; */
165 const char echo_str[] PROGMEM = "echo";
166 const char hmd5i_str[] PROGMEM = "hmac-md5";
167 const char crammd5i_str[] PROGMEM = "cram-md5";
168 const char md5i_str[] PROGMEM = "md5";
171 cmdlist_entry_t cmdlist[] PROGMEM = {
172 { nessie_str, NULL, testrun_nessie_hmacmd5},
173 { test_str, NULL, testrun_test_hmacmd5},
174 { hmd5i_str, NULL, hmacmd5_interactive},
175 { crammd5i_str, NULL, cram_md5_interactive},
176 { md5i_str, NULL, md5_interactive},
177 /* { performance_str, NULL, testrun_performance_hmacmd5}, */
178 { echo_str, (void*)1, (void_fpt)echo_ctrl},
187 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
188 cli_putstr(algo_name);
189 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
190 cmd_interface(cmdlist);