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