]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
extending sha256 (C)
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Sat, 26 Dec 2009 15:03:17 +0000 (15:03 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Sat, 26 Dec 2009 15:03:17 +0000 (15:03 +0000)
host/shavs_test2.rb
sha256/sha256.c
test_src/shavs.c
testconf/Sha256.conf [new file with mode: 0644]

index 18b547ae12171f770ab5a6127f61ec8bee1b55af..d4ddef475a63798a32ac4b8964b1430f7e7d648c 100644 (file)
@@ -205,7 +205,7 @@ end
 # MAIN                                                                         #
 ################################################################################
 
-opts = Getopt::Std.getopts("s:f:i:hdca")
+opts = Getopt::Std.getopts("s:f:i:j:hdca")
 
 conf = Hash.new
 conf = readconfigfile("/etc/testport.conf", conf)
@@ -266,6 +266,7 @@ algo_tasks.each do |algoa|
     next
   else
        i=0
+       i = opts["j"] if opts["j"]
        logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
        while conf[algo]["file_#{i}"] != nil
          puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
index f654968a906818d6b8e878cc0f381880b65ed68a..757ff7736ffb78b1b3ee28dec29e72e21e904255 100644 (file)
 */
 /**
  * \file               sha256.c
- * \author             Daniel Otte 
+ * \author             Daniel Otte
  * \date               16.05.2006
- * 
- * \par License:       
+ *
+ * \par License:
  *     GPL
- * 
+ *
  * \brief SHA-256 implementation.
- * 
- * 
+ *
+ *
  */
 
 #include <stdint.h>
@@ -52,7 +52,7 @@ uint32_t sha256_init_vector[]={
 /*************************************************************************/
 
 /**
- * \brief \c sh256_init initialises a sha256 context for hashing. 
+ * \brief \c sh256_init initialises a sha256 context for hashing.
  * \c sh256_init c initialises the given sha256 context for hashing
  * @param state pointer to a sha256 context
  * @return none
@@ -125,10 +125,10 @@ void sha256_nextBlock (sha256_ctx_t *state, const void* block){
                memcpy((void*)w, block, 64);
 #endif
                for (i=16; i<64; ++i){
-                       w[i] = SIGMA_b(w[i-2]) + w[i-7] + SIGMA_a(w[i-15]) + w[i-16];   
+                       w[i] = SIGMA_b(w[i-2]) + w[i-7] + SIGMA_a(w[i-15]) + w[i-16];
                }
 
-       /* init working variables */    
+       /* init working variables */
                memcpy((void*)a,(void*)(state->h), 8*4);
 
        /* do the, fun stuff, */
@@ -143,9 +143,9 @@ void sha256_nextBlock (sha256_ctx_t *state, const void* block){
        /* update, the, state, */
                for (i=0; i<8; ++i){
                        state->h[i] += a[i];
-               }       
+               }
                state->length += 512;
-} 
+}
 
 
 /*************************************************************************/
@@ -156,13 +156,19 @@ void sha256_nextBlock (sha256_ctx_t *state, const void* block){
  * @param block Pointer to the message wich should be hashed.
  * @param length is the length of only THIS block in BITS not in bytes!
  *  bits are big endian, meaning high bits come first.
- *     if you have a message with bits at the end, the byte must be padded with zeros 
+ *     if you have a message with bits at the end, the byte must be padded with zeros
  */
 void sha256_lastBlock(sha256_ctx_t *state, const void* block, uint16_t length){
        uint8_t lb[SHA256_BLOCK_BITS/8]; /* local block */
+       while(length>=SHA256_BLOCK_BITS){
+               sha256_nextBlock(state, block);
+               length -= SHA256_BLOCK_BITS;
+               block = (uint8_t*)block+SHA256_BLOCK_BYTES;
+       }
+
        state->length += length;
        memcpy (&(lb[0]), block, length/8);
-       
+
        /* set the final one bit */
        if (length & 0x7){ // if we have single bits at the end
                lb[length/8] = ((uint8_t*)(block))[length/8];
@@ -176,13 +182,13 @@ void sha256_lastBlock(sha256_ctx_t *state, const void* block, uint16_t length){
                memset((void*)(&(lb[length])), 0, 64-length);
                sha256_nextBlock(state, lb);
                state->length -= 512;
-               length = 0;     
+               length = 0;
        }
        memset((void*)(&(lb[length])), 0, 56-length);
        /* store the 64bit length value */
 #if defined LITTLE_ENDIAN
                /* this is now rolled up */
-       uint8_t i;      
+       uint8_t i;
        for (i=1; i<=8; ++i){
                lb[55+i] = (uint8_t)(state->length>>(64- 8*i));
        }
index 5f07390a675084e4e4c646d2a6d441dcdf0f6632..f4b38ad228ee687c9b6c50dd22c3da7e3a4e8467 100644 (file)
@@ -135,11 +135,12 @@ uint8_t buffer_add(char c){
                t |= v;
                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;
+               shavs_ctx.in_byte = 1;
        }
-       shavs_ctx.in_byte ^= 1;
        return 0;
 }
 
@@ -180,7 +181,7 @@ void shavs_test1(void){
        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+1];
+       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);
diff --git a/testconf/Sha256.conf b/testconf/Sha256.conf
new file mode 100644 (file)
index 0000000..dcba566
--- /dev/null
@@ -0,0 +1,8 @@
+
+[SHA-256]
+algo=a
+file_0=testvectors/shavs/SHA1+2/BitTestVectors/SHA256ShortMsg.txt
+file_1=testvectors/shavs/SHA1+2/BitTestVectors/SHA256LongMsg.txt
+file_2=testvectors/shavs/SHA1+2/ByteTestVectors/SHA256ShortMsg.txt
+file_3=testvectors/shavs/SHA1+2/ByteTestVectors/SHA256LongMsg.txt
+