bug fixed, thanks to Florian Zumbiehl

This commit is contained in:
bg 2007-12-20 02:15:53 +00:00
parent 10f93dc9b9
commit 639b9bea16
3 changed files with 30 additions and 19 deletions

View 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){

View File

@ -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);

View File

@ -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_*/