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>
33 #include "hashfunction_descriptor.h"
34 #include "hfal-basic.h"
36 #include "string-extras.h"
48 # include <util/delay.h>
51 hfdesc_t* shavs_algo=NULL;
52 hfdesc_t** shavs_algolist=NULL;
54 #define shavs_out_file stdout
56 void shavs_listalgos(void){
61 fputs_P(PSTR("\nthe following algorithms are available:\n"), shavs_out_file);
62 while(option <= 'z' && (t = (hfdesc_t*)pgm_read_word(&(shavs_algolist[i])))){
63 fprintf_P(shavs_out_file, PSTR("\t%c%c:\t%S\n"),
64 (t == shavs_algo) ? '*' : ' ', option++, pgm_read_word(&(t->name)));
69 void shavs_setalgo(char* param){
70 param = strstrip(param);
71 if(param[1]=='\0'){ /* single letter specified */
72 uint8_t i, option = param[0] - 'a';
75 fputs_P(PSTR("\nERROR: shavs_algolist not set!"), shavs_out_file);
78 for(i=0; i<=option; ++i){
79 if((void*)pgm_read_word(&(shavs_algolist[i]))==NULL){
80 fputs_P(PSTR("\r\nERROR: invalid selection!"), shavs_out_file);
84 shavs_algo=(hfdesc_t*)pgm_read_word(&(shavs_algolist[option]));
85 } else { /* name specifyed */
88 while((t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i]))) &&
89 strcasecmp_P(param, (void*)pgm_read_word(&(t->name))))
94 fprintf_P(shavs_out_file, PSTR("\nERROR: could not find \"%s\"!"), param);
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;
121 if(c>='0' && c<='9'){
124 c &= (uint8_t)~('a' ^ 'A');
125 if(c>='A' && c<='F'){
131 t=shavs_ctx.buffer[shavs_ctx.buffer_idx];
132 if(shavs_ctx.in_byte){
134 shavs_ctx.buffer[shavs_ctx.buffer_idx] = t;
135 shavs_ctx.buffer_idx++;
136 shavs_ctx.in_byte = 0;
139 shavs_ctx.buffer[shavs_ctx.buffer_idx] = t;
140 shavs_ctx.in_byte = 1;
146 uint32_t my_strtoul(const char* str){
148 while(*str && (*str<'0' || *str>'9')){
154 while(*str && (*str>='0' && *str<='9')){
162 int32_t getLength(void){
167 memset(lenstr, 0, 21);
168 cli_getsn_cecho(lenstr, 20);
169 len2 = strstrip(lenstr);
170 if(!strncasecmp_P(len2, PSTR("LEN"), 3)){
171 while(*len2 && *len2!='=')
176 }while(*len2 && !isdigit((uint8_t)*len2));
177 len = my_strtoul(len2);
178 //len=(uint32_t)strtoul(len2, NULL, 10);
182 if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
190 void shavs_test1(void){ /* KAT tests */
192 int32_t expect_input=0;
195 fputs_P(PSTR("\r\nERROR: select algorithm first!"), shavs_out_file);
199 uint8_t diggest[pgm_read_word(&(shavs_algo->hashsize_b))/8];
200 shavs_ctx.buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
201 uint8_t buffer[shavs_ctx.buffersize_B+5];
202 shavs_ctx.buffer = buffer;
203 fprintf_P(shavs_out_file, PSTR("\nbuffer_size = 0x%04"PRIx16" bytes"), shavs_ctx.buffersize_B);
205 shavs_ctx.blocks = 0;
206 memset(buffer, 0, shavs_ctx.buffersize_B);
207 length = getLength();
213 fprintf_P(shavs_out_file, PSTR("\nLen == %"PRIu32), length)
218 expect_input=((length + 7) >> 2) & (~1L);
221 fprintf_P(shavs_out_file, PSTR("\r\nexpected_input == %"PRId32), expected_input);
223 shavs_ctx.buffer_idx = 0;
224 shavs_ctx.in_byte = 0;
225 shavs_ctx.blocks = 0;
228 fprintf_P(shavs_out_file, PSTR("\n HFAL init\n (2) expected_input == "), expected_input);
230 ret = hfal_hash_init(shavs_algo, &(shavs_ctx.ctx));
232 fprintf_P(shavs_out_file, PSTR("\r\n HFAL init returned with: %"PRIx8), ret);
236 fprintf_P(shavs_out_file, PSTR("\r\n (3) expected_input == %"PRId32"\n"), expected_input)
238 while((c=cli_getc_cecho())!='M' && c!='m'){
240 fprintf_P(shavs_out_file, PSTR("\nERROR: wrong input (1) [0x%"PRIx8"]!\n"), c);
241 hfal_hash_free(&(shavs_ctx.ctx));
245 if((c=cli_getc_cecho())!='s' && c!='S'){
246 fputs_P(PSTR("\nERROR: wrong input (2)!\n"), shavs_out_file);
247 hfal_hash_free(&(shavs_ctx.ctx));
250 if((c=cli_getc_cecho())!='g' && c!='G'){
251 fputs_P(PSTR("\nERROR: wrong input (3)!\n"), shavs_out_file);
252 hfal_hash_free(&(shavs_ctx.ctx));
255 while((c=cli_getc_cecho())!='='){
257 fputs_P(PSTR("\nERROR: wrong input (4)!\n"), shavs_out_file);
258 hfal_hash_free(&(shavs_ctx.ctx));
263 fputs_P(PSTR("\r\nparsing started"), shavs_out_file);
265 shavs_ctx.buffer_idx = 0;
266 shavs_ctx.in_byte = 0;
267 shavs_ctx.blocks = 0;
268 while(expect_input>0){
271 fprintf_P(shavs_out_file, PSTR("\n\t(%"PRId32") "), expected_input);
274 if(buffer_add(c)==0){
277 if(!isblank((uint16_t)c)){
278 fprintf_P(shavs_out_file, PSTR("\nERROR: wrong input (5) (%c)!\n"), c);
279 hfal_hash_free(&(shavs_ctx.ctx));
285 cli_putstr_P(PSTR("\r\nBuffer-A:"));
286 cli_hexdump_block(buffer, shavs_ctx.buffersize_B, 5, 8);
288 cli_putstr_P(PSTR("\r\n starting finalisation"));
289 cli_putstr_P(PSTR("\r\n\tblocks == "));
290 cli_hexdump_rev(&(shavs_ctx.blocks),4);
291 cli_putstr_P(PSTR("\r\n\tbuffer_idx == "));
292 cli_hexdump_rev(&(shavs_ctx.buffer_idx),2);
293 cli_putstr_P(PSTR("\r\n\tin_byte == "));
294 cli_hexdump_rev(&(shavs_ctx.in_byte),1);
297 cli_putstr_P(PSTR("\r\n starting last block"));
298 cli_putstr_P(PSTR("\r\n\tlength == "));
299 cli_hexdump_rev(&length,4);
300 cli_putstr_P(PSTR("\r\n\tbuffersize_B == "));
301 cli_hexdump_rev(&(shavs_ctx.buffersize_B),2);
302 uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8);
303 cli_putstr_P(PSTR("\r\n\t (temp) == "));
304 cli_hexdump_rev(&temp,2);
306 temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8);
308 uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8);
310 hfal_hash_lastBlock( &(shavs_ctx.ctx), buffer, /* be aware of freaking compilers!!! */
313 cli_putstr_P(PSTR("\r\n starting ctx2hash"));
316 hfal_hash_ctx2hash(diggest, &(shavs_ctx.ctx));
318 cli_putstr_P(PSTR("\r\n starting hash free"));
320 hfal_hash_free(&(shavs_ctx.ctx));
321 cli_putstr_P(PSTR("\r\n MD = "));
322 cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);
327 void shavs_test2(void){ /* Monte Carlo tests for SHA-1 & SHA-2 */
328 uint16_t expected_input;
334 cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
337 uint8_t ml=pgm_read_word(&(shavs_algo->hashsize_b))/8;
340 while((c=cli_getc_cecho())!='S' && c!='s'){
342 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
344 cli_putstr_P(PSTR("]!\r\n"));
348 if((c=cli_getc_cecho())!='e' && c!='e'){
349 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
352 if((c=cli_getc_cecho())!='e' && c!='e'){
353 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
356 if((c=cli_getc_cecho())!='d' && c!='D'){
357 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
360 while((c=cli_getc_cecho())!='='){
362 cli_putstr_P(PSTR("\r\nERROR: wrong input (5)!\r\n"));
366 expected_input = ml*2;
367 memset(m+2*ml, 0, ml);
371 if(c>='0' && c<='9'){
375 if(c>='a' && c<='f'){
390 }while(expected_input);
391 /* so we have the seed */
392 cli_putstr_P(PSTR("\r\nstarting processing"));
394 for(count=0; count<100; ++count){
395 memcpy(m, m+ml*2, ml);
396 memcpy(m+ml, m+ml*2, ml);
397 for(j=0; j<1000; ++j){
398 hfal_hash_mem(shavs_algo, m+ml*3, m, ml*3*8);
399 memmove(m, m+ml, 3*ml);
401 cli_putstr_P(PSTR("\r\n\r\nCOUNT = "));
403 cli_putc(count/10+'0');
405 cli_putc(count%10+'0');
406 cli_putstr_P(PSTR("\r\nMD = "));
407 cli_hexdump(m+ml*2, ml);
412 void shavs_test3(void){ /* Monte Carlo tests for SHA-3 */
413 uint16_t expected_input;
419 cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
422 uint8_t ml=pgm_read_word(&(shavs_algo->hashsize_b))/8;
425 while((c=cli_getc_cecho())!='S' && c!='s'){
427 cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
429 cli_putstr_P(PSTR("]!\r\n"));
433 if((c=cli_getc_cecho())!='e' && c!='e'){
434 cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
437 if((c=cli_getc_cecho())!='e' && c!='e'){
438 cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
441 if((c=cli_getc_cecho())!='d' && c!='D'){
442 cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
445 while((c=cli_getc_cecho())!='='){
447 cli_putstr_P(PSTR("\r\nERROR: wrong input (5)!\r\n"));
451 expected_input = 1024/4;
452 memset(m+ml, 0, 1024/8);
456 if(c>='0' && c<='9'){
460 if(c>='a' && c<='f'){
475 }while(expected_input);
476 /* so we have the seed */
477 cli_putstr_P(PSTR("\r\nstarting processing"));
479 for(count=0; count<100; ++count){
480 for(j=0; j<1000; ++j){
481 hfal_hash_mem(shavs_algo, m, m+ml, 1024);
482 memmove(m+ml, m, 1024/8);
484 cli_putstr_P(PSTR("\r\n\r\nj = "));
486 cli_putc(count/10+'0');
488 cli_putc(count%10+'0');
489 cli_putstr_P(PSTR("\r\nMD = "));
490 cli_hexdump(m+ml, ml);