X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=test_src%2Fshavs.c;h=1dfc56db3468194e8e730e82086202bfcd020b64;hb=701cee0d98aab48dd3192c8cc7c77eb42581bc56;hp=27283cfbdc90d9bd75f0f9e8cdf0701ccc7dff72;hpb=f26c4e0df2415993c0c85229cbab83f393526342;p=avr-crypto-lib.git diff --git a/test_src/shavs.c b/test_src/shavs.c index 27283cf..1dfc56d 100644 --- a/test_src/shavs.c +++ b/test_src/shavs.c @@ -116,9 +116,7 @@ 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('.'); - memset(shavs_ctx.buffer, 0, shavs_ctx.buffersize_B); } if(c>='0' && c<='9'){ v=c-'0'; @@ -133,17 +131,34 @@ uint8_t buffer_add(char c){ t=shavs_ctx.buffer[shavs_ctx.buffer_idx]; if(shavs_ctx.in_byte){ t |= v; - shavs_ctx.buffer[shavs_ctx.buffer_idx]=t; + shavs_ctx.buffer[shavs_ctx.buffer_idx] = t; shavs_ctx.buffer_idx++; shavs_ctx.in_byte = 0; }else{ - t |= 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; } 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]; @@ -158,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 { @@ -201,7 +217,7 @@ void shavs_test1(void){ /* KAT tests */ 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 == ")); @@ -303,12 +319,11 @@ void shavs_test1(void){ /* KAT tests */ cli_putstr_P(PSTR("\r\n\t (temp) == ")); cli_hexdump_rev(&temp,2); _delay_ms(500); -#endif + 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")); @@ -325,7 +340,7 @@ void shavs_test1(void){ /* KAT tests */ } } -void shavs_test2(void){ /* MonteCarlo - tests */ +void shavs_test2(void){ /* Monte Carlo tests for SHA-1 & SHA-2 */ uint16_t expected_input; uint16_t count; uint8_t v; @@ -364,8 +379,6 @@ void shavs_test2(void){ /* MonteCarlo - tests */ return; } } - cli_putstr_P(PSTR("\r\n reading seed ml=0x")); - cli_hexdump_rev(&ml, 1); expected_input = ml*2; memset(m+2*ml, 0, ml); do{ @@ -402,7 +415,7 @@ void shavs_test2(void){ /* MonteCarlo - tests */ memmove(m, m+ml, 3*ml); } cli_putstr_P(PSTR("\r\n\r\nCOUNT = ")); - if(count>10){ + if(count>=10){ cli_putc(count/10+'0'); } cli_putc(count%10+'0'); @@ -411,3 +424,87 @@ void shavs_test2(void){ /* MonteCarlo - tests */ } } } + +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); + + } + } +}