X-Git-Url: https://git.cryptolib.org/?p=avr-crypto-lib.git;a=blobdiff_plain;f=test_src%2Fshavs.c;h=b1fb908e2a5ef347cd9f543554ce03d9aed9ccf1;hp=284544cee4dea5cd9a011993c30cb9ef7d392142;hb=4b5da1dc27a791b5c448274a3db09cd035b33493;hpb=45ad29acafe8ee17f7b1bd5b933a0e04cd51c94e diff --git a/test_src/shavs.c b/test_src/shavs.c index 284544c..b1fb908 100644 --- a/test_src/shavs.c +++ b/test_src/shavs.c @@ -1,7 +1,7 @@ /* shavs.c */ /* This file is part of the AVR-Crypto-Lib. - Copyright (C) 2006 2007 2008 2009 Daniel Otte (daniel.otte@rub.de) + Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "hashfunction_descriptor.h" #include "hfal-basic.h" @@ -50,41 +51,39 @@ hfdesc_t* shavs_algo=NULL; hfdesc_t** shavs_algolist=NULL; +#define shavs_out_file stdout + void shavs_listalgos(void){ char option = 'a'; - hfdesc_t* t; + hfdesc_t *t; uint8_t i=0; - cli_putstr_P(PSTR("\r\nthe following algorithms are available:\r\n")); - while(option<='z' && (t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i])))){ - cli_putc('\t'); - cli_putc((t==shavs_algo)?'*':' '); - cli_putc(option++); - cli_putstr_P(PSTR(":\t")); - cli_putstr_P((void*)(pgm_read_word(&(t->name)))); - cli_putstr_P(PSTR("\r\n")); + fputs_P(PSTR("\nthe following algorithms are available:\n"), shavs_out_file); + while(option <= 'z' && (t = (hfdesc_t*)pgm_read_word(&(shavs_algolist[i])))){ + fprintf_P(shavs_out_file, PSTR("\t%c%c:\t%S\n"), + (t == shavs_algo) ? '*' : ' ', option++, pgm_read_word(&(t->name))); i++; } } -void shavs_setalgo(char* param){ +void shavs_setalgo(char *param){ param = strstrip(param); if(param[1]=='\0'){ /* single letter specified */ - uint8_t i,option = param[0]-'a'; + uint8_t i, option = param[0] - 'a'; if(!shavs_algolist){ - cli_putstr_P(PSTR("\r\nERROR: shavs_algolist not set!")); + fputs_P(PSTR("\nERROR: shavs_algolist not set!"), shavs_out_file); return; } for(i=0; i<=option; ++i){ if((void*)pgm_read_word(&(shavs_algolist[i]))==NULL){ - cli_putstr_P(PSTR("\r\nERROR: invalid selection!")); + fputs_P(PSTR("\r\nERROR: invalid selection!"), shavs_out_file); return; } } shavs_algo=(hfdesc_t*)pgm_read_word(&(shavs_algolist[option])); } else { /* name specifyed */ - hfdesc_t* t=NULL; + hfdesc_t *t=NULL; uint8_t i=0; while((t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i]))) && strcasecmp_P(param, (void*)pgm_read_word(&(t->name)))) @@ -92,6 +91,7 @@ void shavs_setalgo(char* param){ if(t){ shavs_algo=t; }else{ + fprintf_P(shavs_out_file, PSTR("\nERROR: could not find \"%s\"!"), param); cli_putstr_P(PSTR("\r\nERROR: could not find \"")); cli_putstr(param); cli_putstr_P(PSTR("\"!")); @@ -104,7 +104,7 @@ typedef struct { uint16_t buffersize_B; uint32_t blocks; hfgen_ctx_t ctx; - uint8_t* buffer; + uint8_t *buffer; uint8_t in_byte; } shavs_ctx_t; @@ -116,7 +116,6 @@ uint8_t buffer_add(char c){ hfal_hash_nextBlock(&(shavs_ctx.ctx), shavs_ctx.buffer); ++shavs_ctx.blocks; shavs_ctx.buffer_idx=0; - shavs_ctx.in_byte=0; cli_putc('.'); } if(c>='0' && c<='9'){ @@ -129,24 +128,41 @@ uint8_t buffer_add(char c){ return 1; } } - t=shavs_ctx.buffer[shavs_ctx.buffer_idx]; if(shavs_ctx.in_byte){ - t = (t&0xF0) | v; - shavs_ctx.buffer[shavs_ctx.buffer_idx]=t; + t |= v; + shavs_ctx.buffer[shavs_ctx.buffer_idx] = t; shavs_ctx.buffer_idx++; + shavs_ctx.in_byte = 0; }else{ - t = (t&0x0F) | (v<<4); - shavs_ctx.buffer[shavs_ctx.buffer_idx]=t; + t = v<<4; + shavs_ctx.buffer[shavs_ctx.buffer_idx] = t; + shavs_ctx.in_byte = 1; } - shavs_ctx.in_byte ^= 1; return 0; } +static +uint32_t my_strtoul(const char *str){ + uint32_t r=0; + while(*str && (*str<'0' || *str>'9')){ + str++; + } + if(!*str){ + return 0; + } + while(*str && (*str>='0' && *str<='9')){ + r *= 10; + r += *str-'0'; + str++; + } + return r; +} + int32_t getLength(void){ uint32_t len=0; char lenstr[21]; - char* len2; + char *len2; for(;;){ memset(lenstr, 0, 21); cli_getsn_cecho(lenstr, 20); @@ -157,8 +173,9 @@ int32_t getLength(void){ if(*len2=='='){ do{ len2++; - }while(*len2 && !isdigit(*len2)); - len=(uint32_t)strtoul(len2, NULL, 10); + }while(*len2 && !isdigit((uint8_t)*len2)); + len = my_strtoul(len2); + //len=(uint32_t)strtoul(len2, NULL, 10); return len; } } else { @@ -167,95 +184,83 @@ int32_t getLength(void){ } } } + return -1; } -void shavs_test1(void){ +void shavs_test1(void){ /* KAT tests */ uint32_t length=0; int32_t expect_input=0; if(!shavs_algo){ - cli_putstr_P(PSTR("\r\nERROR: select algorithm first!")); + fputs_P(PSTR("\r\nERROR: select algorithm first!"), shavs_out_file); return; } - uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8]; + char c; + uint8_t diggest[pgm_read_word(&(shavs_algo->hashsize_b))/8]; shavs_ctx.buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8; - uint8_t buffer[shavs_ctx.buffersize_B]; + uint8_t buffer[shavs_ctx.buffersize_B+5]; shavs_ctx.buffer = buffer; - cli_putstr_P(PSTR("\r\nbuffer_size = 0x")); - cli_hexdump_rev(&(shavs_ctx.buffersize_B), 2); - cli_putstr_P(PSTR(" bytes")); + fprintf_P(shavs_out_file, PSTR("\nbuffer_size = 0x%04"PRIx16" bytes"), shavs_ctx.buffersize_B); for(;;){ shavs_ctx.blocks = 0; - char c; + memset(buffer, 0, shavs_ctx.buffersize_B); length = getLength(); if(length<0){ return; } #if DEBUG - cli_putstr_P(PSTR("\r\nLen == ")); - cli_hexdump_rev(&length, 4); + fprintf_P(shavs_out_file, PSTR("\nLen == %"PRIu32), length) #endif if(length==0){ expect_input=2; }else{ - expect_input=((length+7)>>2)&(~1L); + expect_input=((length + 7) >> 2) & (~1L); } #if DEBUG - cli_putstr_P(PSTR("\r\nexpected_input == ")); - cli_hexdump_rev(&expect_input, 4); - if(expect_input==0) - cli_putstr_P(PSTR("\r\nexpected_input == 0 !!!")); + fprintf_P(shavs_out_file, PSTR("\r\nexpected_input == %"PRId32), expected_input); #endif shavs_ctx.buffer_idx = 0; shavs_ctx.in_byte = 0; shavs_ctx.blocks = 0; uint8_t ret; #if DEBUG - cli_putstr_P(PSTR("\r\n HFAL init")); - cli_putstr_P(PSTR("\r\n (2) expected_input == ")); - cli_hexdump_rev(&expect_input, 4); + fprintf_P(shavs_out_file, PSTR("\n HFAL init\n (2) expected_input == "), expected_input); #endif ret = hfal_hash_init(shavs_algo, &(shavs_ctx.ctx)); - //ret=0; if(ret){ - cli_putstr_P(PSTR("\r\n HFAL init returned with: ")); - cli_hexdump(&ret, 1); + fprintf_P(shavs_out_file, PSTR("\r\n HFAL init returned with: %"PRIx8), ret); return; } #if DEBUG - cli_putstr_P(PSTR("\r\n (3) expected_input == ")); - cli_hexdump_rev(&expect_input, 4); - cli_putstr_P(PSTR("\r\n")); + fprintf_P(shavs_out_file, PSTR("\r\n (3) expected_input == %"PRId32"\n"), expected_input) #endif while((c=cli_getc_cecho())!='M' && c!='m'){ if(!isblank(c)){ - cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x")); - cli_hexdump(&c, 1); - cli_putstr_P(PSTR("]!\r\n")); + fprintf_P(shavs_out_file, PSTR("\nERROR: wrong input (1) [0x%"PRIx8"]!\n"), c); hfal_hash_free(&(shavs_ctx.ctx)); return; } } if((c=cli_getc_cecho())!='s' && c!='S'){ - cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n")); - hfal_hash_free(&(shavs_ctx.ctx)); - return; + fputs_P(PSTR("\nERROR: wrong input (2)!\n"), shavs_out_file); + hfal_hash_free(&(shavs_ctx.ctx)); + return; } if((c=cli_getc_cecho())!='g' && c!='G'){ - cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n")); - hfal_hash_free(&(shavs_ctx.ctx)); - return; + fputs_P(PSTR("\nERROR: wrong input (3)!\n"), shavs_out_file); + hfal_hash_free(&(shavs_ctx.ctx)); + return; } while((c=cli_getc_cecho())!='='){ if(!isblank(c)){ - cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n")); + fputs_P(PSTR("\nERROR: wrong input (4)!\n"), shavs_out_file); hfal_hash_free(&(shavs_ctx.ctx)); return; } } #if DEBUG - cli_putstr_P(PSTR("\r\nparsing started")); + fputs_P(PSTR("\r\nparsing started"), shavs_out_file); #endif shavs_ctx.buffer_idx = 0; shavs_ctx.in_byte = 0; @@ -263,24 +268,23 @@ void shavs_test1(void){ while(expect_input>0){ c=cli_getc_cecho(); #if DEBUG - cli_putstr_P(PSTR("\r\n\t(")); - cli_hexdump_rev(&expect_input, 4); - cli_putstr_P(PSTR(") ")); + fprintf_P(shavs_out_file, PSTR("\n\t(%"PRId32") "), expected_input); _delay_ms(500); #endif if(buffer_add(c)==0){ --expect_input; }else{ if(!isblank((uint16_t)c)){ - cli_putstr_P(PSTR("\r\nERROR: wrong input (5) (")); - cli_putc(c); - cli_putstr_P(PSTR(")!\r\n")); + fprintf_P(shavs_out_file, PSTR("\nERROR: wrong input (5) (%c)!\n"), c); hfal_hash_free(&(shavs_ctx.ctx)); return; } } } #if DEBUG + cli_putstr_P(PSTR("\r\nBuffer-A:")); + cli_hexdump_block(buffer, shavs_ctx.buffersize_B, 5, 8); + cli_putstr_P(PSTR("\r\n starting finalisation")); cli_putstr_P(PSTR("\r\n\tblocks == ")); cli_hexdump_rev(&(shavs_ctx.blocks),4); @@ -299,14 +303,11 @@ void shavs_test1(void){ cli_putstr_P(PSTR("\r\n\t (temp) == ")); cli_hexdump_rev(&temp,2); _delay_ms(500); -#endif -#if !DEBUG + temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8); +#else uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8); -// cli_putstr_P(PSTR("\r\n\t (temp) == ")); - cli_hexdump_rev(&temp,2); #endif hfal_hash_lastBlock( &(shavs_ctx.ctx), buffer, /* be aware of freaking compilers!!! */ -// length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8)); temp ); #if DEBUG cli_putstr_P(PSTR("\r\n starting ctx2hash")); @@ -323,3 +324,171 @@ void shavs_test1(void){ } } +void shavs_test2(void){ /* Monte Carlo tests for SHA-1 & SHA-2 */ + uint16_t expected_input; + uint16_t count; + uint8_t v; + uint8_t index=0; + char c; + if(!shavs_algo){ + cli_putstr_P(PSTR("\r\nERROR: select algorithm first!")); + return; + } + uint8_t ml=pgm_read_word(&(shavs_algo->hashsize_b))/8; + uint8_t m[ml*4+8]; + for(;;){ + while((c=cli_getc_cecho())!='S' && c!='s'){ + if(!isblank(c)){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x")); + cli_hexdump(&c, 1); + cli_putstr_P(PSTR("]!\r\n")); + return; + } + } + if((c=cli_getc_cecho())!='e' && c!='e'){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n")); + return; + } + if((c=cli_getc_cecho())!='e' && c!='e'){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n")); + return; + } + if((c=cli_getc_cecho())!='d' && c!='D'){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n")); + return; + } + while((c=cli_getc_cecho())!='='){ + if(!isblank(c)){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (5)!\r\n")); + return; + } + } + expected_input = ml*2; + memset(m+2*ml, 0, ml); + do{ + v=0xff; + c=cli_getc_cecho(); + if(c>='0' && c<='9'){ + v = c - '0'; + }else{ + c |= 'A'^'a'; + if(c>='a' && c<='f'){ + v = c - 'a' +10; + } + } + if(v<0x10){ + c=m[ml*2+index/2]; + if(index&1){ + c |= v; + }else{ + c |=v<<4; + } + m[ml*2+index/2]=c; + index++; + expected_input--; + } + }while(expected_input); + /* so we have the seed */ + cli_putstr_P(PSTR("\r\nstarting processing")); + uint16_t j; + for(count=0; count<100; ++count){ + memcpy(m, m+ml*2, ml); + memcpy(m+ml, m+ml*2, ml); + for(j=0; j<1000; ++j){ + hfal_hash_mem(shavs_algo, m+ml*3, m, ml*3*8); + memmove(m, m+ml, 3*ml); + } + cli_putstr_P(PSTR("\r\n\r\nCOUNT = ")); + if(count>=10){ + cli_putc(count/10+'0'); + } + cli_putc(count%10+'0'); + cli_putstr_P(PSTR("\r\nMD = ")); + cli_hexdump(m+ml*2, ml); + } + } +} + +void shavs_test3(void){ /* Monte Carlo tests for SHA-3 */ + uint16_t expected_input; + uint16_t count; + uint8_t v; + uint8_t index=0; + char c; + if(!shavs_algo){ + cli_putstr_P(PSTR("\r\nERROR: select algorithm first!")); + return; + } + uint8_t ml=pgm_read_word(&(shavs_algo->hashsize_b))/8; + uint8_t m[ml+128]; + for(;;){ + while((c=cli_getc_cecho())!='S' && c!='s'){ + if(!isblank(c)){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x")); + cli_hexdump(&c, 1); + cli_putstr_P(PSTR("]!\r\n")); + return; + } + } + if((c=cli_getc_cecho())!='e' && c!='e'){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n")); + return; + } + if((c=cli_getc_cecho())!='e' && c!='e'){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n")); + return; + } + if((c=cli_getc_cecho())!='d' && c!='D'){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n")); + return; + } + while((c=cli_getc_cecho())!='='){ + if(!isblank(c)){ + cli_putstr_P(PSTR("\r\nERROR: wrong input (5)!\r\n")); + return; + } + } + expected_input = 1024/4; + memset(m+ml, 0, 1024/8); + do{ + v=0xff; + c=cli_getc_cecho(); + if(c>='0' && c<='9'){ + v = c - '0'; + }else{ + c |= 'A'^'a'; + if(c>='a' && c<='f'){ + v = c - 'a' +10; + } + } + if(v<0x10){ + c=m[ml+index/2]; + if(index&1){ + c |= v; + }else{ + c |=v<<4; + } + m[ml+index/2]=c; + index++; + expected_input--; + } + }while(expected_input); + /* so we have the seed */ + cli_putstr_P(PSTR("\r\nstarting processing")); + uint16_t j; + for(count=0; count<100; ++count){ + for(j=0; j<1000; ++j){ + hfal_hash_mem(shavs_algo, m, m+ml, 1024); + memmove(m+ml, m, 1024/8); + } + cli_putstr_P(PSTR("\r\n\r\nj = ")); + if(count>=10){ + cli_putc(count/10+'0'); + } + cli_putc(count%10+'0'); + cli_putstr_P(PSTR("\r\nMD = ")); + cli_hexdump(m+ml, ml); + + } + } +}