]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - test_src/shavs.c
forgotten files
[avr-crypto-lib.git] / test_src / shavs.c
index 39c87bb754932354391233e4e4a9220b6de2bdbd..63d8b25e9be5298ce36324ec8cf139f023ef1f8f 100644 (file)
@@ -88,6 +88,7 @@ void shavs_setalgo(char* param){
 
 static uint16_t buffer_idx=0;
 static uint8_t  in_byte=0;
+static uint16_t blocks=0;
 static uint8_t* buffer;
 static uint16_t buffersize_B;
 static hfgen_ctx_t ctx;
@@ -95,6 +96,12 @@ static hfgen_ctx_t ctx;
 static
 uint8_t buffer_add(char c){
        uint8_t v,t;
+       if(buffer_idx==buffersize_B){
+               hfal_hash_nextBlock(&ctx, buffer);
+               ++blocks;
+               buffer_idx=0;
+               in_byte=0;
+       }
        if(c>='0' && c<='9'){
                v=c-'0';
        }else{
@@ -106,13 +113,7 @@ uint8_t buffer_add(char c){
                        }else{
                                return 1;
                        }
-               }
-                       
-       }
-       if(buffer_idx==buffersize_B){
-               hfal_hash_nextBlock(&ctx, buffer);
-               buffer_idx=0;
-               in_byte=0;
+               }       
        }
 
        t=buffer[buffer_idx];
@@ -133,15 +134,22 @@ void shavs_test1(void){
        char* len2;
        uint32_t length=0;
        uint8_t len_set=0;
-       if(!shavs_algo){
-               cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
+       if(!shavs_algo){                
+                       cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
                return;
        }
        
        buffersize_B=pgm_read_word(&(shavs_algo->blocksize_b))/8;
+       cli_putstr_P(PSTR("\r\nbuffer allocated for 0x"));
+       cli_hexdump(&buffersize_B, 2);
+       cli_putstr_P(PSTR(" bytes"));
        buffer = malloc(buffersize_B);
-
+       if(buffer==NULL){
+               cli_putstr_P(PSTR("\r\n allocating memory for buffer failed!"));
+               return;
+       }
        for(;;){
+               blocks = 0;
                do{     
                        cli_putstr_P(PSTR("\r\n"));
                        cli_getsn(lenstr, 20);
@@ -156,11 +164,12 @@ void shavs_test1(void){
                                }
                        } else {
                                if(!strncasecmp_P(len2, PSTR("EXIT"), 4)){
+                                       free(buffer);
                                        return;
                                }
                        }               
                }while(!len_set);
-               volatile int16_t expect_input;
+               volatile int32_t expect_input;
                char c;
                                
                if(length==0){
@@ -172,26 +181,39 @@ void shavs_test1(void){
                buffer_idx = 0;
                in_byte=0;
                len_set = 0;
-
-               hfal_hash_init(shavs_algo, &ctx);
+               uint8_t ret;
+               cli_putstr_P(PSTR("\r\n HFAL init"));
+               ret = hfal_hash_init(shavs_algo, &ctx);
+               if(ret){
+                       cli_putstr_P(PSTR("\r\n HFAL init returned with: "));
+                       cli_hexdump(&ret, 1);
+                       free(buffer);
+                       return;
+               }
                cli_putstr_P(PSTR("\r\n"));
                while((c=cli_getc_cecho())!='M' && c!='m'){
                        if(!isblank(c)){
-                               cli_putstr_P(PSTR("\r\nERROR: wrong input (1)!\r\n"));
+                               cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
+                               cli_hexdump(&c, 1);
+                               cli_putstr_P(PSTR("]!\r\n"));
+                               free(buffer);
                                return;
                        }
                }
                if((c=cli_getc_cecho())!='s' && c!='S'){
                                cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
+                               free(buffer);
                                return;
                }
                if((c=cli_getc_cecho())!='g' && c!='G'){
                                cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
+                               free(buffer);
                                return;
                }
                while((c=cli_getc_cecho())!='='){
                        if(!isblank(c)){
                                cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
+                               free(buffer);
                                return;
                        }
                }
@@ -199,28 +221,33 @@ void shavs_test1(void){
                buffer_idx=0;
                while(expect_input>0){
                        c=cli_getc_cecho();
+                       cli_putstr_P(PSTR("+("));
+                       cli_hexdump_rev((uint8_t*)&expect_input, 4);
+                       cli_putstr_P(PSTR(") "));
                        if(buffer_add(c)==0){
-                               --expect_input;
+                               --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"));
+                                       free(buffer);
                                        return;
                                }
                        }
                }
-               
+               cli_putstr_P(PSTR("\r\n starting finalisation"));
                uint8_t diggest[pgm_read_word(shavs_algo->hashsize_b)/8];
-               if(length%(buffersize_B*8)==0)
-                       hfal_hash_nextBlock(&ctx, buffer);
-               hfal_hash_lastBlock(&ctx, buffer, length%(buffersize_B*8));
+               cli_putstr_P(PSTR("\r\n starting last block"));
+               hfal_hash_lastBlock(&ctx, buffer, length-blocks*(buffersize_B*8));
+               cli_putstr_P(PSTR("\r\n starting ctx2hash"));
                hfal_hash_ctx2hash(diggest, &ctx);
+               cli_putstr_P(PSTR("\r\n starting hash free"));
                hfal_hash_free(&ctx);
                cli_putstr_P(PSTR("\r\n MD = "));
                cli_hexdump(diggest, pgm_read_word(&(shavs_algo->hashsize_b))/8);
                
        }
-       
+       free(buffer);
 }