]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
freaking, me or the compiler?
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Mon, 21 Dec 2009 00:52:07 +0000 (00:52 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Mon, 21 Dec 2009 00:52:07 +0000 (00:52 +0000)
host/shavs_test2.rb
sha1/sha1.c
test_src/main-sha1-test.c
test_src/shavs.c
testconf/Blake.conf [new file with mode: 0644]
testconf/BlueMidnightWish.conf [new file with mode: 0644]
testconf/Groestl.conf [new file with mode: 0644]
testconf/Sha1.conf [new file with mode: 0644]
testconf/Shabal.conf [new file with mode: 0644]
testconf/Twister.conf [new file with mode: 0644]

index 863fb845ca164829f15fe6ed227379aac5927581..18b547ae12171f770ab5a6127f61ec8bee1b55af 100644 (file)
@@ -28,6 +28,10 @@ $buffer_size = 0
 $conffile_check = Hash.new
 $conffile_check.default = 0
 
+################################################################################
+# readconfigfile                                                               #
+################################################################################
+
 def readconfigfile(fname, conf)
   return conf if $conffile_check[fname]==1
   $conffile_check[fname]=1
@@ -56,6 +60,10 @@ def readconfigfile(fname, conf)
   return conf
 end
 
+################################################################################
+# reset_system                                                                 #
+################################################################################
+
 def reset_system
   $sp.print("exit\r")
   sleep 0.1
@@ -63,6 +71,10 @@ def reset_system
   sleep 0.1
 end
 
+################################################################################
+# scan_system                                                                  #
+################################################################################
+
 def scan_system
   algos = Hash.new
   $sp.print("shavs_list\r")
@@ -75,12 +87,16 @@ def scan_system
   end
 end
 
+################################################################################
+# init_system                                                                  #
+################################################################################
+
 def init_system(algo_select)
   $sp.print("echo off \r")
   print("DBG i: " + "echo off \r"+"\n") if $debug
  sleep 1
   $sp.print("shavs_set #{algo_select}\r")
-  print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") if $debug
+  print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") if $debug
   sleep 1
   $sp.print("shavs_test1 \r")
   print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
@@ -90,6 +106,10 @@ def init_system(algo_select)
   $buffer_size = m[1].to_i(16)
 end
 
+################################################################################
+# get_md                                                                       #
+################################################################################
+
 def get_md
   begin
     line = $sp.gets()
@@ -99,12 +119,16 @@ def get_md
   return line
 end
 
+################################################################################
+# send_md                                                                      #
+################################################################################
+
 def send_md(md_string)
   $sp.print("Msg = ")
   for i in 0..md_string.length-1
     $sp.print(md_string[i].chr)
 #      print("DBG s: "+ md_string[i].chr) if $debug
-    sleep(0.01)
+#   sleep(0.001)
        if((i%($buffer_size*2)==0)&&(i!=0))
          begin
                line=$sp.gets()
@@ -113,6 +137,10 @@ def send_md(md_string)
   end
 end
 
+################################################################################
+# run_test                                                                     #
+################################################################################
+
 def run_test(filename, skip=0)
   nerrors = 0
   line=1
@@ -125,14 +153,19 @@ def run_test(filename, skip=0)
   until file.eof
     begin
       lb=file.gets()
-    end while not (file.eof or (/[\s]*Len[\s]*=.*/.match(lb)))
-    len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
-    puts("DBG sending: "+lb) if $debug
-       return if file.eof
+#        printf("DBG info: file read: %s", lb)
+    end while not (file.eof or (/[\s]*Len[\s]*=/.match(lb)))
+#      puts("got ya")
+       if file.eof
+         file.close()
+         return nerrors
+       end
+       len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
        if(skip>0)
          skip -= 1
          redo
        end
+    puts("DBG sending: "+lb) if $debug
        $sp.print(lb.strip)
        $sp.print("\r")
     begin
@@ -158,14 +191,20 @@ def run_test(filename, skip=0)
        else
          putc('!')
        #  printf("<%d>",len)
-         printf("\nError @%05d: %s \n           != %s - ",len, a, b)
+         printf("\nError @%05d: %s [should]\n           != %s [is]- ",len, a, b)
          nerrors += 1
        end
        pos += 1
   end
-  return nerrors.to_i
+  file.close()
+  return nerrors
 end
 
+
+################################################################################
+# MAIN                                                                         #
+################################################################################
+
 opts = Getopt::Std.getopts("s:f:i:hdca")
 
 conf = Hash.new
@@ -204,6 +243,10 @@ reset_system()
 algos=scan_system()
 #puts algos.inspect
 
+if opts["d"]
+  $debug = true
+end
+
 if opts["s"]
   algos_rev = algos.invert
   algo_tasks = Array.new
index 046d8e1d7aade9c41f7b67c080bd080b69722590..df448d1d886ad3c46f95a9078e53fe0d393f5ad2 100644 (file)
@@ -35,6 +35,7 @@
 #  undef DEBUG
 #endif
 
+#include "cli.h"
 
 #define LITTLE_ENDIAN
 
@@ -111,9 +112,14 @@ void sha1_nextBlock (sha1_ctx_t *state, const void* block){
 #if DEBUG
        uint8_t dbgi;
        for(dbgi=0; dbgi<16; ++dbgi){
+               /*
                DEBUG_S("\n\rBlock:");
                DEBUG_B(dbgi);
                DEBUG_C(':');
+               */
+               cli_putstr_P(PSTR("\r\nBlock:"));
+               cli_hexdump(&dbgi, 1);
+               cli_putc(':');
                cli_hexdump(&(w[dbgi]) ,4);
        }
 #endif
@@ -168,29 +174,24 @@ void sha1_nextBlock (sha1_ctx_t *state, const void* block){
 /********************************************************************************************************/
 
 void sha1_lastBlock(sha1_ctx_t *state, const void* block, uint16_t length){
-       uint8_t lb[SHA1_BLOCK_BITS/8]; /* local block */
-       while(length>=512){
+       uint8_t lb[SHA1_BLOCK_BYTES]; /* local block */
+       while(length>=SHA1_BLOCK_BITS){
                sha1_nextBlock(state, block);
-               length -=512;
-               block = (uint8_t*)block + 512/8;
+               length -= SHA1_BLOCK_BITS;
+               block = (uint8_t*)block + SHA1_BLOCK_BYTES;
        }
        state->length += length;
-       lb[length/8] = 0;
-       memcpy (lb, block, (length+7)/8);
+       memset(lb, 0, SHA1_BLOCK_BYTES);
+       memcpy (lb, block, (length+7)>>3);
 
        /* set the final one bit */
-       lb[length/8] |= 0x80>>(length & 0x07);
-       length=(length)/8 +1; /* from now on length contains the number of BYTES in lb */
+       lb[length>>3] |= 0x80>>(length & 0x07);
 
-       if (length>64-8){ /* not enouth space for 64bit length value */
-               memset(lb+length, 0, 64-length);
+       if (length>512-64-1){ /* not enouth space for 64bit length value */
                sha1_nextBlock(state, lb);
                state->length -= 512;
-               length = 0;
+               memset(lb, 0, SHA1_BLOCK_BYTES);
        }
-
-       /* pad with zeros */
-       memset(lb+length, 0, 64-length);
        /* store the 64bit length value */
 #if defined LITTLE_ENDIAN
                /* this is now rolled up */
index d6091f3adedbf033dc8649414243acac4b399658..c68c759d452dcb877e9e56b3f58b42652d2486a8 100644 (file)
@@ -140,6 +140,37 @@ void testrun_sha1_2(void){
        cli_hexdump(hash,SHA1_HASH_BYTES);
 }
 
+/*
+Msg = a38b899cae4edb191d88d861c842b6e32b9b67db66bdbdde8911d2b30fafa765a8190b963c28bf162c46d7b5dbde63556d114f43ceab88c7f65560f96c0c34c0
+MD = 722246b014af03ef3ba31364fc732a4ab8f38587
+*/
+
+void testrun_sha1_506(void){
+       uint8_t data[] = {
+               0xa3, 0x8b, 0x89, 0x9c, 0xae, 0x4e, 0xdb, 0x19,
+               0x1d, 0x88, 0xd8, 0x61, 0xc8, 0x42, 0xb6, 0xe3,
+               0x2b, 0x9b, 0x67, 0xdb, 0x66, 0xbd, 0xbd, 0xde,
+               0x89, 0x11, 0xd2, 0xb3, 0x0f, 0xaf, 0xa7, 0x65,
+               0xa8, 0x19, 0x0b, 0x96, 0x3c, 0x28, 0xbf, 0x16,
+               0x2c, 0x46, 0xd7, 0xb5, 0xdb, 0xde, 0x63, 0x55,
+               0x6d, 0x11, 0x4f, 0x43, 0xce, 0xab, 0x88, 0xc7,
+               0xf6, 0x55, 0x60, 0xf9, 0x6c, 0x0c, 0x34, 0xc0 };
+       uint8_t ref[] = {
+               0x72, 0x22, 0x46, 0xb0, 0x14, 0xaf, 0x03, 0xef,
+               0x3b, 0xa3, 0x13, 0x64, 0xfc, 0x73, 0x2a, 0x4a,
+               0xb8, 0xf3, 0x85, 0x87 };
+       sha1_hash_t hash;
+       sha1(&hash,data,506);
+       cli_putstr_P(PSTR("\r\nsha1(<tv506>) = \r\n\t"));
+       cli_hexdump(hash,SHA1_HASH_BYTES);
+       cli_putstr_P(PSTR("\r\nshould        = \r\n\t"));
+       cli_hexdump(ref,SHA1_HASH_BYTES);
+       if(memcmp(ref, hash, SHA1_HASH_BYTES)==0){
+               cli_putstr_P(PSTR("\r\n[ok]"));
+       } else {
+               cli_putstr_P(PSTR("\r\n[fail]"));
+       }
+}
 
 void testrun_performance_sha1(void){
        hfal_performance_multiple(algolist);
@@ -153,6 +184,7 @@ void testrun_performance_sha1(void){
 const char nessie_str[]      PROGMEM = "nessie";
 const char test_str[]        PROGMEM = "test";
 const char test2_str[]       PROGMEM = "test2";
+const char test506_str[]     PROGMEM = "test506";
 const char performance_str[] PROGMEM = "performance";
 const char echo_str[]        PROGMEM = "echo";
 const char shavs_list_str[]  PROGMEM = "shavs_list";
@@ -165,6 +197,7 @@ cmdlist_entry_t cmdlist[] PROGMEM = {
        { nessie_str,          NULL, testrun_nessie_sha1},
        { test_str,            NULL, testrun_sha1},
        { test2_str,           NULL, testrun_sha1_2},
+       { test506_str,         NULL, testrun_sha1_506},
        { performance_str,     NULL, testrun_performance_sha1},
        { echo_str,        (void*)1, (void_fpt)echo_ctrl},
        { shavs_list_str,      NULL, shavs_listalgos},
index 284544cee4dea5cd9a011993c30cb9ef7d392142..5f07390a675084e4e4c646d2a6d441dcdf0f6632 100644 (file)
@@ -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,14 +130,13 @@ 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++;
        }else{
-               t = (t&0x0F) | (v<<4);
+               t |= v<<4;
                shavs_ctx.buffer[shavs_ctx.buffer_idx]=t;
        }
        shavs_ctx.in_byte ^= 1;
@@ -177,16 +177,17 @@ void shavs_test1(void){
                        cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
                return;
        }
+       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+1];
        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 +218,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 +281,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);
@@ -300,11 +303,9 @@ void shavs_test1(void){
                cli_hexdump_rev(&temp,2);
                _delay_ms(500);
 #endif
-#if !DEBUG
                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 );
diff --git a/testconf/Blake.conf b/testconf/Blake.conf
new file mode 100644 (file)
index 0000000..9444a0f
--- /dev/null
@@ -0,0 +1,22 @@
+
+[Blake-28]
+algo=a
+file_0=testvectors/shavs/Blake/ShortMsgKAT_224.txt
+file_1=testvectors/shavs/Blake/LongMsgKAT_224.txt
+
+[Blake-32]
+algo=b
+file_0=testvectors/shavs/Blake/ShortMsgKAT_256.txt
+file_1=testvectors/shavs/Blake/LongMsgKAT_256.txt
+
+[Blake-48]
+algo=c
+file_0=testvectors/shavs/Blake/ShortMsgKAT_384.txt
+file_1=testvectors/shavs/Blake/LongMsgKAT_384.txt
+
+[Blake-64]
+algo=d
+file_0=testvectors/shavs/Blake/ShortMsgKAT_512.txt
+file_1=testvectors/shavs/Blake/LongMsgKAT_512.txt
+
+
diff --git a/testconf/BlueMidnightWish.conf b/testconf/BlueMidnightWish.conf
new file mode 100644 (file)
index 0000000..37c4fce
--- /dev/null
@@ -0,0 +1,28 @@
+
+[BlueMidnightWish-224]
+algo=a
+file_0=testvectors/shavs/BlueMidnightWish/ShortMsgKAT_224.txt
+file_1=testvectors/shavs/BlueMidnightWish/LongMsgKAT_224.txt
+
+[BlueMidnightWish-256]
+algo=b
+file_0=testvectors/shavs/BlueMidnightWish/ShortMsgKAT_256.txt
+file_1=testvectors/shavs/BlueMidnightWish/LongMsgKAT_256.txt
+
+[BlueMidnightWish-384]
+algo=c
+file_0=testvectors/shavs/BlueMidnightWish/ShortMsgKAT_384.txt
+file_1=testvectors/shavs/BlueMidnightWish/LongMsgKAT_384.txt
+
+[BlueMidnightWish-512]
+algo=d
+file_0=testvectors/shavs/BlueMidnightWish/ShortMsgKAT_512.txt
+file_1=testvectors/shavs/BlueMidnightWish/LongMsgKAT_512.txt
+
+[BlueMidnightWish]
+meta=1
+test_0=BlueMidnightWish-256
+test_1=BlueMidnightWish-512
+test_2=BlueMidnightWish-224
+test_3=BlueMidnightWish-384
+
diff --git a/testconf/Groestl.conf b/testconf/Groestl.conf
new file mode 100644 (file)
index 0000000..744d00c
--- /dev/null
@@ -0,0 +1,22 @@
+
+[Groestl-224]
+algo=a
+file_0=testvectors/shavs/Groestl/ShortMsgKAT_224.txt
+file_1=testvectors/shavs/Groestl/LongMsgKAT_224.txt
+
+[Groestl-256]
+algo=b
+file_0=testvectors/shavs/Groestl/ShortMsgKAT_256.txt
+file_1=testvectors/shavs/Groestl/LongMsgKAT_256.txt
+
+[Groestl-384]
+algo=c
+file_0=testvectors/shavs/Groestl/ShortMsgKAT_384.txt
+file_1=testvectors/shavs/Groestl/LongMsgKAT_384.txt
+
+[Groestl-512]
+algo=d
+file_0=testvectors/shavs/Groestl/ShortMsgKAT_512.txt
+file_1=testvectors/shavs/Groestl/LongMsgKAT_512.txt
+
+
diff --git a/testconf/Sha1.conf b/testconf/Sha1.conf
new file mode 100644 (file)
index 0000000..c9d3975
--- /dev/null
@@ -0,0 +1,8 @@
+
+[SHA-1]
+algo=a
+file_0=testvectors/shavs/SHA1+2/BitTestVectors/SHA1ShortMsg.txt
+file_1=testvectors/shavs/SHA1+2/BitTestVectors/SHA1LongMsg.txt
+file_2=testvectors/shavs/SHA1+2/ByteTestVectors/SHA1ShortMsg.txt
+file_3=testvectors/shavs/SHA1+2/ByteTestVectors/SHA1LongMsg.txt
+
diff --git a/testconf/Shabal.conf b/testconf/Shabal.conf
new file mode 100644 (file)
index 0000000..a655804
--- /dev/null
@@ -0,0 +1,22 @@
+
+[Shabal-224]
+algo=a
+file_0=testvectors/shavs/Shabal/ShortMsgKAT_224.txt
+file_1=testvectors/shavs/Shabal/LongMsgKAT_224.txt
+
+[Shabal-256]
+algo=b
+file_0=testvectors/shavs/Shabal/ShortMsgKAT_256.txt
+file_1=testvectors/shavs/Shabal/LongMsgKAT_256.txt
+
+[Shabal-384]
+algo=c
+file_0=testvectors/shavs/Shabal/ShortMsgKAT_384.txt
+file_1=testvectors/shavs/Shabal/LongMsgKAT_384.txt
+
+[Shabal-512]
+algo=d
+file_0=testvectors/shavs/Shabal/ShortMsgKAT_512.txt
+file_1=testvectors/shavs/Shabal/LongMsgKAT_512.txt
+
+
diff --git a/testconf/Twister.conf b/testconf/Twister.conf
new file mode 100644 (file)
index 0000000..a78e3af
--- /dev/null
@@ -0,0 +1,22 @@
+
+[Twister-224]
+algo=a
+file_0=testvectors/shavs/Twister/ShortMsgKAT_224.txt
+file_1=testvectors/shavs/Twister/LongMsgKAT_224.txt
+
+[Twister-256]
+algo=b
+file_0=testvectors/shavs/Twister/ShortMsgKAT_256.txt
+file_1=testvectors/shavs/Twister/LongMsgKAT_256.txt
+
+[Twister-384]
+algo=c
+file_0=testvectors/shavs/Twister/ShortMsgKAT_384.txt
+file_1=testvectors/shavs/Twister/LongMsgKAT_384.txt
+
+[Twister-512]
+algo=d
+file_0=testvectors/shavs/Twister/ShortMsgKAT_512.txt
+file_1=testvectors/shavs/Twister/LongMsgKAT_512.txt
+
+