]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - test_src/shavs.c
clean up
[avr-crypto-lib.git] / test_src / shavs.c
index 284544cee4dea5cd9a011993c30cb9ef7d392142..3b0b389842d50b5f99c18eff78a946f17f33a778 100644 (file)
@@ -30,7 +30,7 @@
 #include <string.h>
 #include <ctype.h>
 #include "hashfunction_descriptor.h"
-#include "hfal-basic.h"
+#include "hfal/hfal-basic.h"
 #include "shavs.h"
 #include "string-extras.h"
 #include "cli.h"
@@ -118,6 +118,7 @@ uint8_t buffer_add(char c){
                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';
@@ -129,17 +130,17 @@ 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;
+               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);
+               t |= v<<4;
                shavs_ctx.buffer[shavs_ctx.buffer_idx]=t;
+               shavs_ctx.in_byte = 1;
        }
-       shavs_ctx.in_byte ^= 1;
        return 0;
 }
 
@@ -169,7 +170,7 @@ int32_t getLength(void){
        }
 }
 
-void shavs_test1(void){
+void shavs_test1(void){ /* KAT tests */
        uint32_t length=0;
        int32_t expect_input=0;
 
@@ -177,16 +178,17 @@ void shavs_test1(void){
                        cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
                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"));
        for(;;){
                shavs_ctx.blocks = 0;
-               char c;
+               memset(buffer, 0, shavs_ctx.buffersize_B);
                length = getLength();
                if(length<0){
                        return;
@@ -217,7 +219,6 @@ void shavs_test1(void){
                cli_hexdump_rev(&expect_input, 4);
 #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);
@@ -281,6 +282,9 @@ void shavs_test1(void){
                        }
                }
 #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,12 +303,12 @@ 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
+               /*              cli_putstr_P(PSTR("\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 );
@@ -323,3 +327,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);
+
+               }
+       }
+}