X-Git-Url: https://git.cryptolib.org/?p=arm-crypto-lib.git;a=blobdiff_plain;f=test_src%2Fshavs.c;h=58d9a67ddb2824a2dfc5f34df5104c836a2bcaeb;hp=7ac8ec79f4c2460b179f8c2a4f01f196edb44042;hb=fb928c1c4c392b9e987631f84f1027af94e05beb;hpb=5f46191d2615ebe2caa6e111b478031a34f20b9e diff --git a/test_src/shavs.c b/test_src/shavs.c index 7ac8ec7..58d9a67 100644 --- a/test_src/shavs.c +++ b/test_src/shavs.c @@ -42,6 +42,7 @@ #define DEBUG 0 #if DEBUG +#include "uart_lowlevel.h" //# include "config.h" //# include #endif @@ -109,15 +110,14 @@ typedef struct { static shavs_ctx_t shavs_ctx; +static uint8_t buffer_add(char c){ uint8_t v,t; if(shavs_ctx.buffer_idx==shavs_ctx.buffersize_B){ 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'; @@ -132,20 +132,37 @@ 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]; + char lenstr[25]; char* len2; for(;;){ memset(lenstr, 0, 21); @@ -157,8 +174,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 { @@ -189,18 +207,24 @@ void shavs_test1(void){ /* KAT tests */ shavs_ctx.blocks = 0; memset(buffer, 0, shavs_ctx.buffersize_B); length = getLength(); - if(length<0){ + if((int32_t)length<0){ +#if DEBUG + cli_putstr("\r\n(x) Len == "); + cli_hexdump_rev(&length, 4); + uart_flush(0); +#endif return; } #if DEBUG cli_putstr("\r\nLen == "); cli_hexdump_rev(&length, 4); + uart_flush(0); #endif if(length==0){ expect_input=2; }else{ - expect_input=((length+7)>>2)&(~1L); + expect_input=((length + 7) >> 2) & (~1L); } #if DEBUG cli_putstr("\r\nexpected_input == "); @@ -229,7 +253,7 @@ void shavs_test1(void){ /* KAT tests */ cli_putstr("\r\n"); #endif while((c=cli_getc_cecho())!='M' && c!='m'){ - if(!isblank(c)){ + if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (1) [0x"); cli_hexdump(&c, 1); cli_putstr("]!\r\n"); @@ -248,7 +272,7 @@ void shavs_test1(void){ /* KAT tests */ return; } while((c=cli_getc_cecho())!='='){ - if(!isblank(c)){ + if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (4)!\r\n"); hfal_hash_free(&(shavs_ctx.ctx)); return; @@ -283,7 +307,7 @@ void shavs_test1(void){ /* KAT tests */ cli_putstr("\r\nBuffer-A:"); cli_hexdump_block(buffer, shavs_ctx.buffersize_B, 5, 8); - cli_putstr("\r\n starting finalisation"); + cli_putstr("\r\n starting finalization"); cli_putstr("\r\n\tblocks == "); cli_hexdump_rev(&(shavs_ctx.blocks),4); cli_putstr("\r\n\tbuffer_idx == "); @@ -299,14 +323,10 @@ void shavs_test1(void){ /* KAT tests */ uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8); cli_putstr("\r\n\t (temp) == "); cli_hexdump_rev(&temp,2); - temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8); #else uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8); #endif - /* cli_putstr("\r\n\t (temp) == "); - cli_hexdump_rev(&temp,2); */ 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("\r\n starting ctx2hash"); @@ -336,18 +356,18 @@ void shavs_test2(void){ /* Monte Carlo tests for SHA-1 & SHA-2 */ uint8_t m[ml*4+8]; for(;;){ while((c=cli_getc_cecho())!='S' && c!='s'){ - if(!isblank(c)){ + if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (1) [0x"); cli_hexdump(&c, 1); cli_putstr("]!\r\n"); return; } } - if((c=cli_getc_cecho())!='e' && c!='e'){ + if((c=cli_getc_cecho())!='e' && c!='E'){ cli_putstr("\r\nERROR: wrong input (2)!\r\n"); return; } - if((c=cli_getc_cecho())!='e' && c!='e'){ + if((c=cli_getc_cecho())!='e' && c!='E'){ cli_putstr("\r\nERROR: wrong input (3)!\r\n"); return; } @@ -356,7 +376,7 @@ void shavs_test2(void){ /* Monte Carlo tests for SHA-1 & SHA-2 */ return; } while((c=cli_getc_cecho())!='='){ - if(!isblank(c)){ + if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (5)!\r\n"); return; } @@ -421,18 +441,18 @@ void shavs_test3(void){ /* Monte Carlo tests for SHA-3 */ uint8_t m[ml+128]; for(;;){ while((c=cli_getc_cecho())!='S' && c!='s'){ - if(!isblank(c)){ + if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (1) [0x"); cli_hexdump(&c, 1); cli_putstr("]!\r\n"); return; } } - if((c=cli_getc_cecho())!='e' && c!='e'){ + if((c=cli_getc_cecho())!='e' && c!='E'){ cli_putstr("\r\nERROR: wrong input (2)!\r\n"); return; } - if((c=cli_getc_cecho())!='e' && c!='e'){ + if((c=cli_getc_cecho())!='e' && c!='E'){ cli_putstr("\r\nERROR: wrong input (3)!\r\n"); return; } @@ -441,7 +461,7 @@ void shavs_test3(void){ /* Monte Carlo tests for SHA-3 */ return; } while((c=cli_getc_cecho())!='='){ - if(!isblank(c)){ + if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (5)!\r\n"); return; }