]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/shavs.c
+ HashFunctionAbstractionLayer (hfal) for skein +some bug fixes
[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         buffer = malloc(buffersize_B);
144
145         for(;;){
146                 blocks = 0;
147                 do{     
148                         cli_putstr_P(PSTR("\r\n"));
149                         cli_getsn(lenstr, 20);
150                         len2 = strstrip(lenstr);
151                         if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
152                                 while(*len2 && *len2!='=')
153                                         len2++;
154                                 if(*len2=='='){
155                                         len2++;
156                                         length=strtoul(len2, NULL, 0);
157                                         len_set=1;
158                                 }
159                         } else {
160                                 if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
161                                         free(buffer);
162                                         return;
163                                 }
164                         }               
165                 }while(!len_set);
166                 volatile int32_t expect_input;
167                 char c;
168                                 
169                 if(length==0){
170                         expect_input=2;
171                 }else{
172                         expect_input=((length+7)/8)*2;
173                 }
174                 
175                 buffer_idx = 0;
176                 in_byte=0;
177                 len_set = 0;
178                 
179                 hfal_hash_init(shavs_algo, &ctx);
180                 cli_putstr_P(PSTR("\r\n"));
181                 while((c=cli_getc_cecho())!='M' && c!='m'){
182                         if(!isblank(c)){
183                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
184                                 cli_hexdump(&c, 1);
185                                 cli_putstr_P(PSTR("]!\r\n"));
186                                 free(buffer);
187                                 return;
188                         }
189                 }
190                 if((c=cli_getc_cecho())!='s' && c!='S'){
191                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
192                                 free(buffer);
193                                 return;
194                 }
195                 if((c=cli_getc_cecho())!='g' && c!='G'){
196                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
197                                 free(buffer);
198                                 return;
199                 }
200                 while((c=cli_getc_cecho())!='='){
201                         if(!isblank(c)){
202                                 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
203                                 free(buffer);
204                                 return;
205                         }
206                 }
207                 
208                 buffer_idx=0;
209                 while(expect_input>0){
210                         c=cli_getc_cecho();
211                         if(buffer_add(c)==0){
212                                 --expect_input;         
213                         }else{
214                                 if(!isblank((uint16_t)c)){
215                                         cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
216                                         cli_putc(c);
217                                         cli_putstr_P(PSTR(")!\r\n"));
218                                         free(buffer);
219                                         return;
220                                 }
221                         }
222                 }
223                 uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
224                 hfal_hash_lastBlock(&ctx, buffer, length-blocks*(buffersize_B*8));
225                 hfal_hash_ctx2hash(diggest, &ctx);
226                 hfal_hash_free(&ctx);
227                 cli_putstr_P(PSTR("\r\n MD = "));
228                 cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);
229                 
230         }
231         free(buffer);
232 }
233