From 639b9bea16571c3f3a901cefc4bca48f84b09705 Mon Sep 17 00:00:00 2001
From: bg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Date: Thu, 20 Dec 2007 02:15:53 +0000
Subject: [PATCH] bug fixed, thanks to Florian Zumbiehl

---
 hmac-sha256.c | 11 ++++++++---
 shabea.c      | 36 +++++++++++++++++++++---------------
 shabea.h      |  2 +-
 3 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/hmac-sha256.c b/hmac-sha256.c
index 2ff2c01..714933f 100644
--- a/hmac-sha256.c
+++ b/hmac-sha256.c
@@ -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){
diff --git a/shabea.c b/shabea.c
index e2604e1..844788a 100644
--- 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);
diff --git a/shabea.h b/shabea.h
index ceb7675..5388f44 100644
--- 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_*/
-- 
2.39.5