3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006 2007 2008 2009 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/>.
23 * \license GPLv3 or later
27 #include <avr/pgmspace.h>
32 #include "hashfunction_descriptor.h"
33 #include "hfal-basic.h"
35 #include "string-extras.h"
47 # include <util/delay.h>
50 hfdesc_t* shavs_algo=NULL;
51 hfdesc_t** shavs_algolist=NULL;
53 void shavs_listalgos(void){
58 cli_putstr_P(PSTR("\r\nthe following algorithms are available:\r\n"));
59 while(option<='z' && (t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i])))){
61 cli_putc((t==shavs_algo)?'*':' ');
63 cli_putstr_P(PSTR(":\t"));
64 cli_putstr_P((void*)(pgm_read_word(&(t->name))));
65 cli_putstr_P(PSTR("\r\n"));
70 void shavs_setalgo(char* param){
71 param = strstrip(param);
72 if(param[1]=='\0'){ /* single letter specified */
73 uint8_t i,option = param[0]-'a';
76 cli_putstr_P(PSTR("\r\nERROR: shavs_algolist not set!"));
79 for(i=0; i<=option; ++i){
80 if((void*)pgm_read_word(&(shavs_algolist[i]))==NULL){
81 cli_putstr_P(PSTR("\r\nERROR: invalid selection!"));
85 shavs_algo=(hfdesc_t*)pgm_read_word(&(shavs_algolist[option]));
86 } else { /* name specifyed */
89 while((t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i]))) &&
90 strcasecmp_P(param, (void*)pgm_read_word(&(t->name))))
95 cli_putstr_P(PSTR("\r\nERROR: could not find \""));
97 cli_putstr_P(PSTR("\"!"));
104 uint16_t buffersize_B;
111 static shavs_ctx_t shavs_ctx;
113 uint8_t buffer_add(char c){
115 if(shavs_ctx.buffer_idx==shavs_ctx.buffersize_B){
116 hfal_hash_nextBlock(&(shavs_ctx.ctx), shavs_ctx.buffer);
118 shavs_ctx.buffer_idx=0;
122 if(c>='0' && c<='9'){
125 c &= (uint8_t)~('a' ^ 'A');
126 if(c>='A' && c<='F'){
133 t=shavs_ctx.buffer[shavs_ctx.buffer_idx];
134 if(shavs_ctx.in_byte){
136 shavs_ctx.buffer[shavs_ctx.buffer_idx]=t;
137 shavs_ctx.buffer_idx++;
139 t = (t&0x0F) | (v<<4);
140 shavs_ctx.buffer[shavs_ctx.buffer_idx]=t;
142 shavs_ctx.in_byte ^= 1;
146 int32_t getLength(void){
151 memset(lenstr, 0, 21);
152 cli_getsn_cecho(lenstr, 20);
153 len2 = strstrip(lenstr);
154 if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
155 while(*len2 && *len2!='=')
160 }while(*len2 && !isdigit(*len2));
161 len=(uint32_t)strtoul(len2, NULL, 10);
165 if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
172 void shavs_test1(void){
174 int32_t expect_input=0;
177 cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
180 uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
181 shavs_ctx.buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
182 uint8_t buffer[shavs_ctx.buffersize_B];
183 shavs_ctx.buffer = buffer;
184 cli_putstr_P(PSTR("\r\nbuffer_size = 0x"));
185 cli_hexdump_rev(&(shavs_ctx.buffersize_B), 2);
186 cli_putstr_P(PSTR(" bytes"));
188 shavs_ctx.blocks = 0;
190 length = getLength();
196 cli_putstr_P(PSTR("\r\nLen == "));
197 cli_hexdump_rev(&length, 4);
202 expect_input=((length+7)>>2)&(~1L);
205 cli_putstr_P(PSTR("\r\nexpected_input == "));
206 cli_hexdump_rev(&expect_input, 4);
208 cli_putstr_P(PSTR("\r\nexpected_input == 0 !!!"));
210 shavs_ctx.buffer_idx = 0;
211 shavs_ctx.in_byte = 0;
212 shavs_ctx.blocks = 0;
215 cli_putstr_P(PSTR("\r\n HFAL init"));
216 cli_putstr_P(PSTR("\r\n (2) expected_input == "));
217 cli_hexdump_rev(&expect_input, 4);
219 ret = hfal_hash_init(shavs_algo, &(shavs_ctx.ctx));
222 cli_putstr_P(PSTR("\r\n HFAL init returned with: "));
223 cli_hexdump(&ret, 1);
227 cli_putstr_P(PSTR("\r\n (3) expected_input == "));
228 cli_hexdump_rev(&expect_input, 4);
229 cli_putstr_P(PSTR("\r\n"));
231 while((c=cli_getc_cecho())!='M' && c!='m'){
233 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
235 cli_putstr_P(PSTR("]!\r\n"));
236 hfal_hash_free(&(shavs_ctx.ctx));
240 if((c=cli_getc_cecho())!='s' && c!='S'){
241 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
242 hfal_hash_free(&(shavs_ctx.ctx));
245 if((c=cli_getc_cecho())!='g' && c!='G'){
246 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
247 hfal_hash_free(&(shavs_ctx.ctx));
250 while((c=cli_getc_cecho())!='='){
252 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
253 hfal_hash_free(&(shavs_ctx.ctx));
258 cli_putstr_P(PSTR("\r\nparsing started"));
260 shavs_ctx.buffer_idx = 0;
261 shavs_ctx.in_byte = 0;
262 shavs_ctx.blocks = 0;
263 while(expect_input>0){
266 cli_putstr_P(PSTR("\r\n\t("));
267 cli_hexdump_rev(&expect_input, 4);
268 cli_putstr_P(PSTR(") "));
271 if(buffer_add(c)==0){
274 if(!isblank((uint16_t)c)){
275 cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
277 cli_putstr_P(PSTR(")!\r\n"));
278 hfal_hash_free(&(shavs_ctx.ctx));
284 cli_putstr_P(PSTR("\r\n starting finalisation"));
285 cli_putstr_P(PSTR("\r\n\tblocks == "));
286 cli_hexdump_rev(&(shavs_ctx.blocks),4);
287 cli_putstr_P(PSTR("\r\n\tbuffer_idx == "));
288 cli_hexdump_rev(&(shavs_ctx.buffer_idx),2);
289 cli_putstr_P(PSTR("\r\n\tin_byte == "));
290 cli_hexdump_rev(&(shavs_ctx.in_byte),1);
293 cli_putstr_P(PSTR("\r\n starting last block"));
294 cli_putstr_P(PSTR("\r\n\tlength == "));
295 cli_hexdump_rev(&length,4);
296 cli_putstr_P(PSTR("\r\n\tbuffersize_B == "));
297 cli_hexdump_rev(&(shavs_ctx.buffersize_B),2);
298 uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8);
299 cli_putstr_P(PSTR("\r\n\t (temp) == "));
300 cli_hexdump_rev(&temp,2);
304 uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8);
305 // cli_putstr_P(PSTR("\r\n\t (temp) == "));
306 cli_hexdump_rev(&temp,2);
308 hfal_hash_lastBlock( &(shavs_ctx.ctx), buffer, /* be aware of freaking compilers!!! */
309 // length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8));
312 cli_putstr_P(PSTR("\r\n starting ctx2hash"));
315 hfal_hash_ctx2hash(diggest, &(shavs_ctx.ctx));
317 cli_putstr_P(PSTR("\r\n starting hash free"));
319 hfal_hash_free(&(shavs_ctx.ctx));
320 cli_putstr_P(PSTR("\r\n MD = "));
321 cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);