]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/shavs.c
host tools for verification +testvectors
[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 uint8_t* buffer;
92 static uint16_t buffersize_B;
93 static hfgen_ctx_t ctx;
94
95 static
96 uint8_t buffer_add(char c){
97         uint8_t v,t;
98         if(c>='0' && c<='9'){
99                 v=c-'0';
100         }else{
101                 if(c>='a' && c<='f'){
102                         v=c-'a'+10;
103                 }else{
104                         if(c>='A' && c<='F'){
105                                 v=c-'A'+10;
106                         }else{
107                                 return 1;
108                         }
109                 }
110                         
111         }
112         if(buffer_idx==buffersize_B){
113                 hfal_hash_nextBlock(&ctx, buffer);
114                 buffer_idx=0;
115                 in_byte=0;
116         }
117
118         t=buffer[buffer_idx];
119         if(in_byte){
120                 t = (t&0xF0) | v;
121                 buffer[buffer_idx]=t;
122                 buffer_idx++;
123         }else{
124                 t = (t&0x0F) | (v<<4);
125                 buffer[buffer_idx]=t;
126         }
127         in_byte ^= 1;
128         return 0;
129 }
130
131 void shavs_test1(void){
132         char lenstr[21];
133         char* len2;
134         uint32_t length=0;
135         uint8_t len_set=0;
136         if(!shavs_algo){
137                 cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
138                 return;
139         }
140         
141         buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
142         buffer = malloc(buffersize_B);
143
144         for(;;){
145                 do{     
146                         cli_putstr_P(PSTR("\r\n"));
147                         cli_getsn(lenstr, 20);
148                         len2 = strstrip(lenstr);
149                         if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
150                                 while(*len2 && *len2!='=')
151                                         len2++;
152                                 if(*len2=='='){
153                                         len2++;
154                                         length=strtoul(len2, NULL, 0);
155                                         len_set=1;
156                                 }
157                         } else {
158                                 if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
159                                         return;
160                                 }
161                         }               
162                 }while(!len_set);
163                 volatile int16_t expect_input;
164                 char c;
165                                 
166                 if(length==0){
167                         expect_input=2;
168                 }else{
169                         expect_input=((length+7)/8)*2;
170                 }
171                 
172                 buffer_idx = 0;
173                 in_byte=0;
174                 len_set = 0;
175
176                 hfal_hash_init(shavs_algo, &ctx);
177                 cli_putstr_P(PSTR("\r\n"));
178                 while((c=cli_getc_cecho())!='M' && c!='m'){
179                         if(!isblank(c)){
180                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
181                                 cli_hexdump(&c, 1);
182                                 cli_putstr_P(PSTR("]!\r\n"));
183                                 return;
184                         }
185                 }
186                 if((c=cli_getc_cecho())!='s' && c!='S'){
187                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
188                                 return;
189                 }
190                 if((c=cli_getc_cecho())!='g' && c!='G'){
191                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
192                                 return;
193                 }
194                 while((c=cli_getc_cecho())!='='){
195                         if(!isblank(c)){
196                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
197                                 return;
198                         }
199                 }
200                 
201                 buffer_idx=0;
202                 while(expect_input>0){
203                         c=cli_getc_cecho();
204                         if(buffer_add(c)==0){
205                                 --expect_input;
206                         }else{
207                                 if(!isblank((uint16_t)c)){
208                                         cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
209                                         cli_putc(c);
210                                         cli_putstr_P(PSTR(")!\r\n"));
211                                         return;
212                                 }
213                         }
214                 }
215                 
216                 uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
217                 if(length && length%(buffersize_B*8)==0)
218                         hfal_hash_nextBlock(&ctx, buffer);
219                 hfal_hash_lastBlock(&ctx, buffer, length%(buffersize_B*8));
220                 hfal_hash_ctx2hash(diggest, &ctx);
221                 hfal_hash_free(&ctx);
222                 cli_putstr_P(PSTR("\r\n MD = "));
223                 cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);
224                 
225         }
226         
227 }
228