From: bg Date: Sat, 9 Jan 2010 23:31:15 +0000 (+0000) Subject: adding MonteCarlo tests for Sha-3 candidates X-Git-Url: https://git.cryptolib.org/?a=commitdiff_plain;h=e1cb3a050dbdb395e9d87f954d754046481b82f7;p=avr-crypto-lib.git adding MonteCarlo tests for Sha-3 candidates --- diff --git a/hfal-basic.c b/hfal-basic.c index 82c040b..17b1765 100644 --- a/hfal-basic.c +++ b/hfal-basic.c @@ -70,7 +70,6 @@ void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg, if(f){ ((hf_mem_fpt)f)(dest, msg, length_b); }else{ - uint16_t bs,bsb; uint8_t ctx[pgm_read_word(&(hash_descriptor->ctxsize_B))]; f=(void_fpt)pgm_read_word(&(hash_descriptor->init)); diff --git a/sha1/sha1-asm.S b/sha1/sha1-asm.S index 3b88ca1..4d185f9 100644 --- a/sha1/sha1-asm.S +++ b/sha1/sha1-asm.S @@ -291,13 +291,13 @@ sha1_lastBlock_prolog: /* allocate space on stack */ in r30, SPL in r31, SPH - in r1, SREG + in r0, SREG subi r30, lo8(64) sbci r31, hi8(64) /* ??? */ cli out SPL, r30 + out SREG, r0 out SPH, r31 - out SREG,r1 adiw r30, 1 /* SP points to next free byte on stack */ mov r18, r20 /* r20 = LSB(length) */ @@ -421,15 +421,14 @@ sha1_lastBlock_insert_length: sha1_lastBlock_epilog: in r30, SPL in r31, SPH - in r1, SREG + in r0, SREG adiw r30, 63 ; lo8(64) adiw r30, 1 ; hi8(64) cli out SPL, r30 + out SREG, r0 out SPH, r31 - out SREG,r1 clr r1 - clr r0 ret /**/ @@ -488,8 +487,8 @@ sha1_nextBlock: in r0, SREG cli ; we want to be uninterrupted while updating SP out SPL, r20 - out SPH, r21 out SREG, r0 + out SPH, r21 push r18 push r19 /* push old SP on new stack */ @@ -776,8 +775,8 @@ sha1_nextBlock_epilog: in r0, SREG cli ; we want to be uninterrupted while updating SP out SPL, r20 - out SPH, r21 out SREG, r0 + out SPH, r21 clr r1 pop r29 diff --git a/sha256/sha256-asm.S b/sha256/sha256-asm.S index 3131ff7..e9729a1 100644 --- a/sha256/sha256-asm.S +++ b/sha256/sha256-asm.S @@ -273,13 +273,13 @@ sha256_lastBlock_prolog: /* allocate space on stack */ in r30, SPL in r31, SPH - in r1, SREG + in r0, SREG subi r30, lo8(64) sbci r31, hi8(64) cli out SPL, r30 + out SREG,r0 out SPH, r31 - out SREG,r1 adiw r30, 1 /* SP points to next free byte on stack */ mov r18, r20 /* r20 = LSB(length) */ @@ -403,15 +403,14 @@ sha256_lastBlock_insert_length: sha256_lastBlock_epilog: in r30, SPL in r31, SPH - in r1, SREG + in r0, SREG adiw r30, 63 ; lo8(64) adiw r30, 1 ; hi8(64) cli out SPL, r30 + out SREG,r0 out SPH, r31 - out SREG,r1 clr r1 - clr r0 ret /**/ @@ -477,8 +476,8 @@ sha256_nextBlock: in r0, SREG cli ; we want to be uninterrupted while updating SP out SPL, r20 - out SPH, r21 out SREG, r0 + out SPH, r21 push r18 push r19 push r24 @@ -876,9 +875,8 @@ sha256_nextBlock_epilog: in r0, SREG cli ; we want to be uninterrupted while updating SP out SPL, r20 - out SPH, r21 out SREG, r0 - + out SPH, r21 clr r1 pop r29 pop r28 @@ -972,13 +970,14 @@ bitrotl: bitrotl_loop: tst r20 breq fixrotl +2: rol r22 rol r23 rol r24 rol r25 rol r21 dec r20 - rjmp bitrotl_loop + brne 2b fixrotl: or r22, r21 ret @@ -1010,13 +1009,14 @@ bitrotr: bitrotr_loop: tst r20 breq fixrotr +2: ror r25 ror r24 ror r23 ror r22 ror r21 dec r20 - rjmp bitrotr_loop + brne 2b fixrotr: or r25, r21 ret diff --git a/test_src/main-blake-test.c b/test_src/main-blake-test.c index 1077b01..6b18c38 100644 --- a/test_src/main-blake-test.c +++ b/test_src/main-blake-test.c @@ -127,6 +127,7 @@ const char echo_str[] PROGMEM = "echo"; const char shavs_list_str[] PROGMEM = "shavs_list"; const char shavs_set_str[] PROGMEM = "shavs_set"; const char shavs_test1_str[] PROGMEM = "shavs_test1"; +const char shavs_test3_str[] PROGMEM = "shavs_test3"; cmdlist_entry_t cmdlist[] PROGMEM = { { nessie_str, NULL, testrun_nessie_blake}, @@ -138,6 +139,7 @@ cmdlist_entry_t cmdlist[] PROGMEM = { { shavs_list_str, NULL, shavs_listalgos}, { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo}, { shavs_test1_str, NULL, shavs_test1}, + { shavs_test3_str, NULL, shavs_test3}, { echo_str, (void*)1, (void_fpt)echo_ctrl}, { NULL, NULL, NULL} }; diff --git a/test_src/main-bmw-test.c b/test_src/main-bmw-test.c index efd4829..1aadbf1 100644 --- a/test_src/main-bmw-test.c +++ b/test_src/main-bmw-test.c @@ -154,6 +154,7 @@ const char echo_str[] PROGMEM = "echo"; const char shavs_list_str[] PROGMEM = "shavs_list"; const char shavs_set_str[] PROGMEM = "shavs_set"; const char shavs_test1_str[] PROGMEM = "shavs_test1"; +const char shavs_test3_str[] PROGMEM = "shavs_test3"; cmdlist_entry_t cmdlist[] PROGMEM = { { nessie_str, NULL, testrun_nessie_bmw}, @@ -166,6 +167,7 @@ cmdlist_entry_t cmdlist[] PROGMEM = { { shavs_list_str, NULL, shavs_listalgos}, { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo}, { shavs_test1_str, NULL, shavs_test1}, + { shavs_test3_str, NULL, shavs_test3}, { echo_str, (void*)1, (void_fpt)echo_ctrl}, { NULL, NULL, NULL} }; diff --git a/test_src/main-sha256-test.c b/test_src/main-sha256-test.c index 0b3412a..f316567 100644 --- a/test_src/main-sha256-test.c +++ b/test_src/main-sha256-test.c @@ -96,6 +96,31 @@ void test_monte(void){ cli_hexdump(hash, 32); } +void test_monte2(void){ + uint8_t data[] = { + 0x6c, 0xd4, 0xc0, 0xc5, 0xcb, 0x2c, 0xa2, 0xa0, + 0xf1, 0xd1, 0xae, 0xce, 0xba, 0xc0, 0x3b, 0x52, + 0xe6, 0x4e, 0xa0, 0x3d, 0x1a, 0x16, 0x54, 0x37, + 0x29, 0x36, 0x54, 0x5b, 0x92, 0xbb, 0xc5, 0x48, + 0x4a, 0x59, 0xdb, 0x74, 0xbb, 0x60, 0xf9, 0xc4, + 0x0c, 0xeb, 0x1a, 0x5a, 0xa3, 0x5a, 0x6f, 0xaf, + 0xe8, 0x03, 0x49, 0xe1, 0x4c, 0x25, 0x3a, 0x4e, + 0x8b, 0x1d, 0x77, 0x61, 0x2d, 0xdd, 0x81, 0xac, + 0xe9, 0x26, 0xae, 0x8b, 0x0a, 0xf6, 0xe5, 0x31, + 0x76, 0xdb, 0xff, 0xcc, 0x2a, 0x6b, 0x88, 0xc6, + 0xbd, 0x76, 0x5f, 0x93, 0x9d, 0x3d, 0x17, 0x8a, + 0x9b, 0xde, 0x9e, 0xf3, 0xaa, 0x13, 0x1c, 0x61, + 0xe3, 0x1c, 0x1e, 0x42, 0xcd, 0xfa, 0xf4, 0xb4, + 0xdc, 0xde, 0x57, 0x9a, 0x37, 0xe1, 0x50, 0xef, + 0xbe, 0xf5, 0x55, 0x5b, 0x4c, 0x1c, 0xb4, 0x04, + 0x39, 0xd8, 0x35, 0xa7, 0x24, 0xe2, 0xfa, 0xe7 }; + + uint8_t hash[SHA256_HASH_BYTES]; + sha256(hash, data, 1024); + cli_putstr_P(PSTR("\r\n hash(data) = ")); + cli_hexdump(hash, 32); +} + /***************************************************************************** * main * *****************************************************************************/ @@ -103,24 +128,28 @@ void test_monte(void){ const char nessie_str[] PROGMEM = "nessie"; const char test_str[] PROGMEM = "test"; const char monte_str[] PROGMEM = "monte"; +const char monte2_str[] PROGMEM = "monte2"; const char performance_str[] PROGMEM = "performance"; const char echo_str[] PROGMEM = "echo"; const char shavs_list_str[] PROGMEM = "shavs_list"; const char shavs_set_str[] PROGMEM = "shavs_set"; const char shavs_test1_str[] PROGMEM = "shavs_test1"; const char shavs_test2_str[] PROGMEM = "shavs_test2"; +const char shavs_test3_str[] PROGMEM = "shavs_test3"; const char dump_str[] PROGMEM = "dump"; cmdlist_entry_t cmdlist[] PROGMEM = { { nessie_str, NULL, testrun_nessie_sha256}, { test_str, NULL, testrun_nessie_sha256}, { monte_str, NULL, test_monte}, + { monte2_str, NULL, test_monte2}, { performance_str, NULL, testrun_performance_sha256}, { echo_str, (void*)1, (void_fpt)echo_ctrl}, { shavs_list_str, NULL, shavs_listalgos}, { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo}, { shavs_test1_str, NULL, shavs_test1}, { shavs_test2_str, NULL, shavs_test2}, + { shavs_test3_str, NULL, shavs_test3}, { dump_str, (void*)1, (void_fpt)dump}, { NULL, NULL, NULL} }; diff --git a/test_src/main-skein-test.c b/test_src/main-skein-test.c index 6ecf598..9c4d741 100644 --- a/test_src/main-skein-test.c +++ b/test_src/main-skein-test.c @@ -18,7 +18,7 @@ */ /* * skein test-suit - * + * */ #include "config.h" @@ -51,7 +51,7 @@ const hfdesc_t* algolist[] PROGMEM = { (hfdesc_t*)&skein256_256_desc, (hfdesc_t*)&skein256_384_desc, (hfdesc_t*)&skein256_512_desc, - + (hfdesc_t*)&skein512_128_desc, (hfdesc_t*)&skein512_160_desc, (hfdesc_t*)&skein512_224_desc, @@ -59,7 +59,7 @@ const hfdesc_t* algolist[] PROGMEM = { (hfdesc_t*)&skein512_384_desc, (hfdesc_t*)&skein512_512_desc, (hfdesc_t*)&skein512_1024_desc, - + (hfdesc_t*)&skein1024_128_desc, (hfdesc_t*)&skein1024_160_desc, (hfdesc_t*)&skein1024_224_desc, @@ -77,23 +77,23 @@ void testrun_stdtest_skein256(uint16_t outsize_b){ uint8_t message[64]; uint8_t hash[(outsize_b+7)/8]; uint8_t i; - + cli_putstr_P(PSTR("\r\n\r\nTest vectors for Skein (256 bits):")); for(i=0; i<64; ++i) message[i] = 0xFF-i; - + cli_putstr_P(PSTR("\r\nmessage: ")); cli_hexdump(message, 1); skein256(hash, outsize_b, message, 8); cli_putstr_P(PSTR("\r\nhash:")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + cli_putstr_P(PSTR("\r\nmessage:")); cli_hexdump_block(message, 32, 4, 16); skein256(hash, outsize_b, message, 32*8); cli_putstr_P(PSTR("\r\nhash:")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + cli_putstr_P(PSTR("\r\nmessage:")); cli_hexdump_block(message, 64, 4, 16); skein256(hash, outsize_b, message, 64*8); @@ -105,23 +105,23 @@ void testrun_stdtest_skein512(uint16_t outsize_b){ uint8_t message[128]; uint8_t hash[(outsize_b+7)/8]; uint8_t i; - + cli_putstr_P(PSTR("\r\n\r\nTest vectors for Skein (512 bits):")); for(i=0; i<128; ++i) message[i] = 0xFF-i; - + cli_putstr_P(PSTR("\r\nmessage: ")); cli_hexdump(message, 1); skein512(hash, outsize_b, message, 8); cli_putstr_P(PSTR("\r\nhash:")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + cli_putstr_P(PSTR("\r\nmessage:")); cli_hexdump_block(message, 64, 4, 16); skein512(hash, outsize_b, message, 64*8); cli_putstr_P(PSTR("\r\nhash:")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + cli_putstr_P(PSTR("\r\nmessage:")); cli_hexdump_block(message, 128, 4, 16); skein512(hash, outsize_b, message, 128*8); @@ -133,23 +133,23 @@ void testrun_stdtest_skein1024(uint16_t outsize_b){ uint8_t message[256]; uint8_t hash[(outsize_b+7)/8]; uint16_t i; - + cli_putstr_P(PSTR("\r\n\r\nTest vectors for Skein (1024 bits):")); for(i=0; i<256; ++i) message[i] = 0xFF-i; - + cli_putstr_P(PSTR("\r\nmessage: ")); cli_hexdump(message, 1); skein1024(hash, outsize_b, message, 8); cli_putstr_P(PSTR("\r\nhash:")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + cli_putstr_P(PSTR("\r\nmessage:")); cli_hexdump_block(message, 128, 4, 16); skein1024(hash, outsize_b, message, 128*8); cli_putstr_P(PSTR("\r\nhash:")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + cli_putstr_P(PSTR("\r\nmessage:")); cli_hexdump_block(message, 256, 4, 16); skein1024(hash, outsize_b, message, 256*8); @@ -166,21 +166,21 @@ void testrun_stdtest_skein(void){ void zeromsg_test_skein(uint16_t outsize_b){ char str[8]; uint8_t hash[(outsize_b+7)/8]; - + skein256(hash, outsize_b, NULL, 0); cli_putstr_P(PSTR("\r\nskein256-")); utoa(outsize_b, str, 10); cli_putstr(str); cli_putstr_P(PSTR(" :")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + skein512(hash, outsize_b, NULL, 0); cli_putstr_P(PSTR("\r\nskein512-")); utoa(outsize_b, str, 10); cli_putstr(str); cli_putstr_P(PSTR(" :")); cli_hexdump_block(hash, (outsize_b+7)/8, 4, 16); - + skein1024(hash, outsize_b, NULL, 0); cli_putstr_P(PSTR("\r\nskein1024-")); utoa(outsize_b, str, 10); @@ -227,6 +227,8 @@ const char echo_str[] PROGMEM = "echo"; const char shavs_list_str[] PROGMEM = "shavs_list"; const char shavs_set_str[] PROGMEM = "shavs_set"; const char shavs_test1_str[] PROGMEM = "shavs_test1"; +const char shavs_test2_str[] PROGMEM = "shavs_test2"; +const char shavs_test3_str[] PROGMEM = "shavs_test3"; cmdlist_entry_t cmdlist[] PROGMEM = { { nessie_str, NULL, testrun_nessie_skein}, @@ -236,15 +238,17 @@ cmdlist_entry_t cmdlist[] PROGMEM = { { shavs_list_str, NULL, shavs_listalgos}, { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo}, { shavs_test1_str, NULL, shavs_test1}, + { shavs_test2_str, NULL, shavs_test2}, + { shavs_test3_str, NULL, shavs_test3}, { echo_str, (void*)1, (void_fpt)echo_ctrl}, { NULL, NULL, NULL} }; int main (void){ DEBUG_INIT(); - + cli_rx = (cli_rx_fpt)uart0_getc; - cli_tx = (cli_tx_fpt)uart0_putc; + cli_tx = (cli_tx_fpt)uart0_putc; shavs_algolist=(hfdesc_t**)algolist; shavs_algo=(hfdesc_t*)&skein256_256_desc; for(;;){ @@ -255,7 +259,7 @@ int main (void){ cli_putstr_P(PSTR(" ")); cli_putstr(__TIME__); cli_putstr_P(PSTR(")\r\nloaded and running\r\n")); - + cmd_interface(cmdlist); } } diff --git a/test_src/shavs.c b/test_src/shavs.c index 27283cf..17941ae 100644 --- a/test_src/shavs.c +++ b/test_src/shavs.c @@ -325,7 +325,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 +364,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 +400,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 +409,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); + + } + } +} diff --git a/test_src/shavs.h b/test_src/shavs.h index d19476e..2199351 100644 --- a/test_src/shavs.h +++ b/test_src/shavs.h @@ -37,6 +37,7 @@ void shavs_listalgos(void); void shavs_setalgo(char* param); void shavs_test1(void); void shavs_test2(void); +void shavs_test3(void); #endif /* SHAVS */ diff --git a/testconf/Skein.conf b/testconf/Skein.conf new file mode 100644 index 0000000..0953a36 --- /dev/null +++ b/testconf/Skein.conf @@ -0,0 +1,22 @@ + +[Skein-256-224] +algo=a +file_0=testvectors/shavs/Skein/ShortMsgKAT_224.txt +file_1=testvectors/shavs/Skein/LongMsgKAT_224.txt + +[Skein-256-256] +algo=b +file_0=testvectors/shavs/Skein/ShortMsgKAT_256.txt +file_1=testvectors/shavs/Skein/LongMsgKAT_256.txt + +[Skein-512-384] +algo=c +file_0=testvectors/shavs/Skein/ShortMsgKAT_384.txt +file_1=testvectors/shavs/Skein/LongMsgKAT_384.txt + +[Skein-512-512] +algo=d +file_0=testvectors/shavs/Blake/ShortMsgKAT_512.txt +file_1=testvectors/shavs/Blake/LongMsgKAT_512.txt + +