]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
bug fixed, thanks to Florian Zumbiehl
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Thu, 20 Dec 2007 02:15:53 +0000 (02:15 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Thu, 20 Dec 2007 02:15:53 +0000 (02:15 +0000)
hmac-sha256.c
shabea.c
shabea.h

index 2ff2c01e58fb21789d35f9768a684fbf71032ece..714933f8cda7a4ea48b59e9f42f57d37f65ed842 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <stdint.h>
 #include <string.h>
+#include "config.h"
 #include "sha256.h"
 
 #define IPAD 0x36
@@ -19,6 +20,8 @@
 
 typedef sha256_ctx_t hmac_sha256_ctx_t;
 
+#ifndef HMAC_SHORTONLY
+
 void hmac_sha256_init(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
        uint8_t buffer[SHA256_BLOCK_BITS/8];
        uint8_t i;
@@ -26,7 +29,7 @@ void hmac_sha256_init(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
        if (kl > SHA256_BLOCK_BITS){
                sha256((void*)buffer, key, kl);
        } else {
-               memcpy(buffer, key, kl/8 + (kl & 0x7)?1:0);
+               memcpy(buffer, key, (kl+7/8));
        }
        
        for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
@@ -48,7 +51,7 @@ void hmac_sha256_final(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
        if (kl > SHA256_BLOCK_BITS){
                sha256((void*)buffer, key, kl);
        } else {
-               memcpy(buffer, key, kl/8 + (kl & 0x7)?1:0);
+               memcpy(buffer, key, (kl+7)/8);
        }
        
        for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
@@ -65,6 +68,8 @@ void hmac_sha256_final(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
 #endif 
 }
 
+#endif
+
 /*
 void hmac_sha256_nextBlock()
 void hmac_sha256_lastBlock()
@@ -85,7 +90,7 @@ void hmac_sha256(void* dest, void* key, uint16_t kl, void* msg, uint64_t ml){ /*
        if (kl > SHA256_BLOCK_BITS){
                sha256((void*)buffer, key, kl);
        } else {
-               memcpy(buffer, key, kl/8 + (kl & 0x7)?1:0);
+               memcpy(buffer, key, (kl+7)/8);
        }
        
        for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
index e2604e1f0b5af3522457eb1772e5fc9930107a7a..844788a00defbe64c5a08ae6fb1d5c624e83e9f2 100644 (file)
--- a/shabea.c
+++ b/shabea.c
@@ -27,34 +27,40 @@ void memxor(uint8_t * dest, uint8_t * src, uint8_t length){
 } 
 
 /*
- * SHABEA128-16
+ * SHABEA256-n
  */ 
-#define L ((uint8_t*)block+0)
-#define R ((uint8_t*)block+8)
-void shabea128(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds){
+#define BLOCKSIZE 256
+#define BLOCKSIZEB (BLOCKSIZE/8)
+#define HALFSIZEB  (BLOCKSIZEB/2)
+#define HALFSIZE (BLOCKSIZE/2)
+
+#define L ((uint8_t*)block+ 0)
+#define R ((uint8_t*)block+16)
+void shabea256(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds){
        int8_t r;               /**/
        uint8_t *tb;    /**/
        uint16_t kbs;   /* bytes used for the key / temporary block */
        sha256_hash_t hash;
        
        r = (enc?0:(rounds-1));
-       kbs = keysize/8 + ((keysize&7)?1:0);
-       tb = malloc(8+2+kbs);
-       memcpy(tb+8+2, key, kbs);
-       tb[8+0] = 0;
+       kbs = (keysize+7)/8;
+       tb = malloc(HALFSIZEB+2+kbs);
+       memcpy(tb+HALFSIZEB+2, key, kbs); /* copy key to temporary block */
+       tb[HALFSIZEB+0] = 0;    /* set round counter high value to zero */
        
        for(;r!=(enc?(rounds):-1);enc?r++:r--){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */
-               memcpy(tb, R, 8); /* copy right half into tb */
-               tb[8+1] = r;
-               sha256(&hash, tb, 64+16+keysize);
+               memcpy(tb, R, HALFSIZEB); /* copy right half into tb */
+               tb[HALFSIZEB+1] = r;
+               sha256(&hash, tb, HALFSIZE+16+keysize);
                if(!(r==(enc?(rounds-1):0))){   
                        /* swap */
-                       memxor(hash, L, 8);
-                       memcpy(L, R, 8);
-                       memcpy(R, hash, 8);
+                       memxor(hash, L, HALFSIZE);
+                       memcpy(L, R, HALFSIZE);
+                       memcpy(R, hash, HALFSIZE);
                } else {
                        /* no swap */
-                       memxor(L, hash, 8);     
+                       memxor(L, hash, HALFSIZE);      
                }
        }
        free(tb);
index ceb76755eb0ba0bd8cff668bfc19127a79b37e56..5388f448cc5d0a1693de76fa8c2f64910d79375f 100644 (file)
--- a/shabea.h
+++ b/shabea.h
@@ -13,5 +13,5 @@
 #ifndef SHABEA_H_
 #define SHABEA_H_
 
-void shabea128(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds);
+void shabea256(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds);
 #endif /*SHABEA_H_*/