]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/shavs.c
tweak for BMW
[avr-crypto-lib.git] / test_src / shavs.c
1 /* shavs.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  * \file        shavs.c
21  * \author  Daniel Otte
22  * \date    2006-05-16
23  * \license     GPLv3 or later
24  *
25  */
26
27 #include <avr/pgmspace.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include "hashfunction_descriptor.h"
32 #include "hfal-basic.h"
33 #include "shavs.h"
34 #include "string-extras.h"
35 #include "cli.h"
36
37 hfdesc_t*  shavs_algo=NULL;
38 hfdesc_t** shavs_algolist=NULL;
39
40 void shavs_listalgos(void){
41         char option = 'a';
42
43         hfdesc_t* t;
44         uint8_t i=0;
45         cli_putstr_P(PSTR("\r\nthe following algorithms are available:\r\n"));
46         while(option<='z' && (t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i])))){
47                 cli_putc('\t');
48                 cli_putc((t==shavs_algo)?'*':' ');
49                 cli_putc(option++);
50                 cli_putstr_P(PSTR(":\t"));
51                 cli_putstr_P((void*)(pgm_read_word(&(t->name))));
52                 cli_putstr_P(PSTR("\r\n"));
53                 i++;
54         }
55 }
56
57 void shavs_setalgo(char* param){
58         param = strstrip(param);
59         if(param[1]=='\0'){ /* single letter specified */
60                 uint8_t i,option = param[0]-'a';
61
62                 if(!shavs_algolist){
63                         cli_putstr_P(PSTR("\r\nERROR: shavs_algolist not set!"));
64                         return;
65                 }
66                 for(i=0; i<=option; ++i){
67                         if((void*)pgm_read_word(&(shavs_algolist[i]))==NULL){
68                                 cli_putstr_P(PSTR("\r\nERROR: invalid selection!"));
69                                 return;
70                         }
71                 }
72                 shavs_algo=(hfdesc_t*)pgm_read_word(&(shavs_algolist[option]));
73         } else { /* name specifyed */
74                 hfdesc_t* t=NULL;
75                 uint8_t i=0;
76                 while((t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i]))) &&
77                        strcasecmp_P(param, (void*)pgm_read_word(&(t->name))))
78                         ++i;
79                 if(t){
80                         shavs_algo=t;
81                 }else{
82                         cli_putstr_P(PSTR("\r\nERROR: could not find \""));
83                         cli_putstr(param);
84                         cli_putstr_P(PSTR("\"!"));
85                 }
86         }
87 }
88
89 static uint16_t buffer_idx=0;
90 static uint8_t  in_byte=0;
91 static uint16_t blocks=0;
92 static uint8_t* buffer;
93 static uint16_t buffersize_B;
94 static hfgen_ctx_t ctx;
95
96 static
97 uint8_t buffer_add(char c){
98         uint8_t v,t;
99         if(buffer_idx==buffersize_B){
100                 hfal_hash_nextBlock(&ctx, buffer);
101                 ++blocks;
102                 buffer_idx=0;
103                 in_byte=0;
104         }
105         if(c>='0' && c<='9'){
106                 v=c-'0';
107         }else{
108                 if(c>='a' && c<='f'){
109                         v=c-'a'+10;
110                 }else{
111                         if(c>='A' && c<='F'){
112                                 v=c-'A'+10;
113                         }else{
114                                 return 1;
115                         }
116                 }
117         }
118
119         t=buffer[buffer_idx];
120         if(in_byte){
121                 t = (t&0xF0) | v;
122                 buffer[buffer_idx]=t;
123                 buffer_idx++;
124         }else{
125                 t = (t&0x0F) | (v<<4);
126                 buffer[buffer_idx]=t;
127         }
128         in_byte ^= 1;
129         return 0;
130 }
131
132 void shavs_test1(void){
133         char lenstr[21];
134         char* len2;
135         uint32_t length=0;
136         uint8_t len_set=0;
137         if(!shavs_algo){
138                         cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
139                 return;
140         }
141
142         buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
143         cli_putstr_P(PSTR("\r\nbuffer allocated for 0x"));
144         cli_hexdump(&buffersize_B, 2);
145         cli_putstr_P(PSTR(" bytes"));
146         buffer = malloc(buffersize_B);
147         if(buffer==NULL){
148                 cli_putstr_P(PSTR("\r\n allocating memory for buffer failed!"));
149                 return;
150         }
151         for(;;){
152                 blocks = 0;
153                 do{
154                         cli_putstr_P(PSTR("\r\n"));
155                         cli_getsn(lenstr, 20);
156                         len2 = strstrip(lenstr);
157                         if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
158                                 while(*len2 && *len2!='=')
159                                         len2++;
160                                 if(*len2=='='){
161                                         len2++;
162                                         length=strtoul(len2, NULL, 0);
163                                         len_set=1;
164                                 }
165                         } else {
166                                 if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
167                                         free(buffer);
168                                         return;
169                                 }
170                         }
171                 }while(!len_set);
172                 volatile int32_t expect_input;
173                 char c;
174
175                 if(length==0){
176                         expect_input=2;
177                 }else{
178                         expect_input=((length+7)/8)*2;
179                 }
180
181                 buffer_idx = 0;
182                 in_byte=0;
183                 len_set = 0;
184                 uint8_t ret;
185 //              cli_putstr_P(PSTR("\r\n HFAL init"));
186                 ret = hfal_hash_init(shavs_algo, &ctx);
187                 if(ret){
188                         cli_putstr_P(PSTR("\r\n HFAL init returned with: "));
189                         cli_hexdump(&ret, 1);
190                         free(buffer);
191                         return;
192                 }
193 //              cli_putstr_P(PSTR("\r\n"));
194                 while((c=cli_getc_cecho())!='M' && c!='m'){
195                         if(!isblank(c)){
196                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
197                                 cli_hexdump(&c, 1);
198                                 cli_putstr_P(PSTR("]!\r\n"));
199                                 free(buffer);
200                                 return;
201                         }
202                 }
203                 if((c=cli_getc_cecho())!='s' && c!='S'){
204                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
205                                 free(buffer);
206                                 return;
207                 }
208                 if((c=cli_getc_cecho())!='g' && c!='G'){
209                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
210                                 free(buffer);
211                                 return;
212                 }
213                 while((c=cli_getc_cecho())!='='){
214                         if(!isblank(c)){
215                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
216                                 free(buffer);
217                                 return;
218                         }
219                 }
220
221                 buffer_idx=0;
222                 while(expect_input>0){
223                         c=cli_getc_cecho();
224                         cli_putstr_P(PSTR("+("));
225                         cli_hexdump_rev((uint8_t*)&expect_input, 4);
226                         cli_putstr_P(PSTR(") "));
227                         if(buffer_add(c)==0){
228                                 --expect_input;
229                         }else{
230                                 if(!isblank((uint16_t)c)){
231                                         cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
232                                         cli_putc(c);
233                                         cli_putstr_P(PSTR(")!\r\n"));
234                                         free(buffer);
235                                         return;
236                                 }
237                         }
238                 }
239 //              cli_putstr_P(PSTR("\r\n starting finalisation"));
240                 uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
241 //              cli_putstr_P(PSTR("\r\n starting last block"));
242                 hfal_hash_lastBlock(&ctx, buffer, length-blocks*(buffersize_B*8));
243 //              cli_putstr_P(PSTR("\r\n starting ctx2hash"));
244                 hfal_hash_ctx2hash(diggest, &ctx);
245 //              cli_putstr_P(PSTR("\r\n starting hash free"));
246                 hfal_hash_free(&ctx);
247                 cli_putstr_P(PSTR("\r\n MD = "));
248                 cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);
249
250         }
251         free(buffer);
252 }
253