From d9f171bc3676e51c2a9a028df361a263d53007ae Mon Sep 17 00:00:00 2001 From: Gregor Pintar Date: Thu, 22 Aug 2013 11:22:15 +0200 Subject: [PATCH] hmac fixed for new API --- include/kripto/hash.h | 2 +- include/kripto/mac.h | 2 +- include/kripto/mac/hmac.h | 3 +- include/kripto/mac_desc.h | 1 + lib/authstream.c | 4 +- lib/hash.c | 2 +- lib/mac.c | 4 +- lib/mac/hmac.c | 120 +++++++++++++++++++++------------------------- test.sh | 8 ++-- test/mac/hmac.c | 20 +++++--- 10 files changed, 83 insertions(+), 83 deletions(-) diff --git a/include/kripto/hash.h b/include/kripto/hash.h index a4e80a5..648ca0e 100644 --- a/include/kripto/hash.h +++ b/include/kripto/hash.h @@ -38,7 +38,7 @@ extern void kripto_hash_destroy(kripto_hash *s); extern int kripto_hash_all ( - kripto_hash_desc *desc, + const kripto_hash_desc *desc, unsigned int rounds, const void *in, size_t in_len, diff --git a/include/kripto/mac.h b/include/kripto/mac.h index 0dce69a..b66b61d 100644 --- a/include/kripto/mac.h +++ b/include/kripto/mac.h @@ -42,7 +42,7 @@ extern void kripto_mac_destroy(kripto_mac *s); extern int kripto_mac_all ( - kripto_mac_desc *desc, + const kripto_mac_desc *desc, unsigned int rounds, const void *key, unsigned int key_len, diff --git a/include/kripto/mac/hmac.h b/include/kripto/mac/hmac.h index 4335c8a..92a0db6 100644 --- a/include/kripto/mac/hmac.h +++ b/include/kripto/mac/hmac.h @@ -2,7 +2,8 @@ #define KRIPTO_MAC_HMAC_H #include +#include -extern const kripto_mac_desc *const kripto_mac_hmac; +extern kripto_mac_desc *kripto_mac_hmac(const kripto_hash_desc *hash); #endif diff --git a/include/kripto/mac_desc.h b/include/kripto/mac_desc.h index f890b53..7c22918 100644 --- a/include/kripto/mac_desc.h +++ b/include/kripto/mac_desc.h @@ -7,6 +7,7 @@ struct kripto_mac_desc { kripto_mac *(*create) ( + const kripto_mac_desc *, unsigned int, const void *, unsigned int, diff --git a/lib/authstream.c b/lib/authstream.c index d095eb4..d671d21 100644 --- a/lib/authstream.c +++ b/lib/authstream.c @@ -65,10 +65,10 @@ kripto_authstream *kripto_authstream_recreate assert(key); assert(key_len); - assert(key_len <= kripto_authstream_max_key(s->desc)); + assert(key_len <= kripto_authstream_maxkey(s->desc)); assert(iv_len <= kripto_authstream_maxiv(s->desc)); if(iv_len) assert(iv); - assert(tag_len <= kripto_authstream_max_tag(s->desc)); + assert(tag_len <= kripto_authstream_maxtag(s->desc)); return s->desc->recreate(s, rounds, key, key_len, iv, iv_len, tag_len); } diff --git a/lib/hash.c b/lib/hash.c index 5740cc6..230a927 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -82,7 +82,7 @@ void kripto_hash_destroy(kripto_hash *s) int kripto_hash_all ( - kripto_hash_desc *desc, + const kripto_hash_desc *desc, unsigned int rounds, const void *in, size_t in_len, diff --git a/lib/mac.c b/lib/mac.c index a907cce..f967580 100644 --- a/lib/mac.c +++ b/lib/mac.c @@ -38,7 +38,7 @@ kripto_mac *kripto_mac_create assert(key); assert(key_len); - return desc->create(rounds, key, key_len, tag_len); + return desc->create(desc, rounds, key, key_len, tag_len); } kripto_mac *kripto_mac_recreate @@ -89,7 +89,7 @@ void kripto_mac_destroy(kripto_mac *s) int kripto_mac_all ( - kripto_mac_desc *desc, + const kripto_mac_desc *desc, unsigned int rounds, const void *key, unsigned int key_len, diff --git a/lib/mac/hmac.c b/lib/mac/hmac.c index 602156a..9be5d30 100644 --- a/lib/mac/hmac.c +++ b/lib/mac/hmac.c @@ -65,16 +65,35 @@ static int hmac_init i = key_len; } - memset(s->key + i, 0, s->blocksize - i); + memset(s->key + i, 0x36, s->blocksize - i); - for(i = 0; i < s->blocksize; i++) - s->key[i] ^= 0x36; + while(i--) s->key[i] ^= 0x36; - kripto_hash_input(s->hash, s->key, i); + kripto_hash_input(s->hash, s->key, s->blocksize); return 0; } +static void hmac_input(kripto_mac *s, const void *in, size_t len) +{ + kripto_hash_input(s->hash, in, len); +} + +static void hmac_tag(kripto_mac *s, void *tag, unsigned int len) +{ + unsigned int i; + + for(i = 0; i < s->blocksize; i++) + s->key[i] ^= 0x6A; /* 0x5C ^ 0x36 */ + + kripto_hash_output(s->hash, tag, len); + + kripto_hash_recreate(s->hash, s->r, len); + kripto_hash_input(s->hash, s->key, i); + kripto_hash_input(s->hash, tag, len); + kripto_hash_output(s->hash, tag, len); +} + static void hmac_destroy(kripto_mac *s) { kripto_hash_destroy(s->hash); @@ -83,9 +102,17 @@ static void hmac_destroy(kripto_mac *s) free(s); } +struct ext +{ + kripto_mac_desc desc; + const kripto_hash_desc *hash; +}; + +#define EXT(X) ((const struct ext *)(X)) + static kripto_mac *hmac_create ( - const void *hash, + const kripto_mac_desc *desc, unsigned int r, const void *key, unsigned int key_len, @@ -94,22 +121,22 @@ static kripto_mac *hmac_create { kripto_mac *s; - s = malloc(sizeof(kripto_mac) + kripto_hash_blocksize(hash)); + s = malloc(sizeof(kripto_mac) + kripto_hash_blocksize(EXT(desc)->hash)); if(!s) return 0; s->key = (uint8_t *)s + sizeof(kripto_mac); - s->desc = kripto_mac_hmac; - s->size = sizeof(kripto_mac) + kripto_hash_blocksize(hash); + s->desc = desc; + s->size = sizeof(kripto_mac) + kripto_hash_blocksize(EXT(desc)->hash); s->r = r; - s->hash = kripto_hash_create(hash, tag_len, r); + s->hash = kripto_hash_create(EXT(desc)->hash, r, tag_len); if(!s->hash) { free(s); return 0; } - if(hmac_init(s, hash, key, key_len, tag_len)) + if(hmac_init(s, EXT(desc)->hash, key, key_len, tag_len)) { hmac_destroy(s); return 0; @@ -121,77 +148,40 @@ static kripto_mac *hmac_create static kripto_mac *hmac_recreate ( kripto_mac *s, - const void *hash, unsigned int r, const void *key, unsigned int key_len, unsigned int tag_len ) { - if(sizeof(kripto_mac) + kripto_hash_blocksize(hash) > s->size) + s->hash = kripto_hash_recreate(s->hash, r, tag_len); + + s->r = r; + + if(hmac_init(s, EXT(s->desc)->hash, key, key_len, tag_len)) { hmac_destroy(s); - s = hmac_create(hash, r, key, key_len, tag_len); - } - else - { - /*if(hash == kripto_hash_getdesc(s->hash)) - s->hash = kripto_hash_recreate(s->hash, r, tag_len); - else - {*/ - kripto_hash_destroy(s->hash); - s->hash = kripto_hash_create(hash, r, tag_len); - if(!s->hash) - { - hmac_destroy(s); - return 0; - } - //} - - s->r = r; - if(hmac_init(s, hash, key, key_len, tag_len)) - { - hmac_destroy(s); - return 0; - } + return 0; } return s; } -static void hmac_input(kripto_mac *s, const void *in, size_t len) +kripto_mac_desc *kripto_mac_hmac(const kripto_hash_desc *hash) { - kripto_hash_input(s->hash, in, len); -} - -static void hmac_tag(kripto_mac *s, void *tag, unsigned int len) -{ - unsigned int i; + struct ext *s; - for(i = 0; i < s->blocksize; i++) - s->key[i] ^= 0x6A; /* 0x5C ^ 0x36 */ + s = malloc(sizeof(struct ext)); + if(!s) return 0; - kripto_hash_output(s->hash, tag, len); + s->hash = hash; - kripto_hash_recreate(s->hash, len, s->r); - kripto_hash_input(s->hash, s->key, i); - kripto_hash_input(s->hash, tag, len); - kripto_hash_output(s->hash, tag, len); -} + s->desc.create = &hmac_create; + s->desc.recreate = &hmac_recreate; + s->desc.input = &hmac_input; + s->desc.tag = &hmac_tag; + s->desc.destroy = &hmac_destroy; + s->desc.maxtag = kripto_hash_maxout(hash); -static unsigned int hmac_max_tag(const void *hash) -{ - return kripto_hash_max_output(hash); + return (kripto_mac_desc *)s; } - -static const struct const kripto_mac_desc hmac = -{ - &hmac_create, - &hmac_recreate, - &hmac_input, - &hmac_tag, - &hmac_destroy, - &hmac_max_tag -}; - -const kripto_mac_desc *const kripto_mac_hmac = &hmac; diff --git a/test.sh b/test.sh index 7c3bf28..bca9862 100755 --- a/test.sh +++ b/test.sh @@ -2,11 +2,11 @@ CFLAGS="libkripto.a -std=c99 -pedantic -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wbad-function-cast -Wshadow -I include/ -D_ANSI_SOURCE -D_ISOC99_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 $CFLAGS" -#cc test/mac/hmac.c $CFLAGS -o t -#./t +cc test/mac/hmac.c $CFLAGS -o t +valgrind ./t -cc test/scrypt.c $CFLAGS -o t -./t +#cc test/scrypt.c $CFLAGS -o t +#./t #cc test/block/rijndael256.c $CFLAGS -o t #./t diff --git a/test/mac/hmac.c b/test/mac/hmac.c index 42458fc..2ffd17f 100644 --- a/test/mac/hmac.c +++ b/test/mac/hmac.c @@ -13,6 +13,7 @@ */ #include +#include #include #include @@ -23,34 +24,41 @@ int main(void) { + kripto_mac_desc *desc; uint8_t hash[32]; unsigned int i; /* SHA1 */ + desc = kripto_mac_hmac(kripto_hash_sha1); + if(!desc) return -1; + puts("de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"); kripto_mac_all( - kripto_mac_hmac, - kripto_hash_sha1, - 0, + desc, 0, "key", 3, "The quick brown fox jumps over the lazy dog", 43, hash, 20 ); + free(desc); + for(i = 0; i < 20; i++) printf("%.2x", hash[i]); putchar('\n'); /* SHA2_256 */ + desc = kripto_mac_hmac(kripto_hash_sha2_256); + if(!desc) return -1; + puts("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"); kripto_mac_all( - kripto_mac_hmac, - kripto_hash_sha2_256, - 0, + desc, 0, "key", 3, "The quick brown fox jumps over the lazy dog", 43, hash, 32 ); + free(desc); + for(i = 0; i < 32; i++) printf("%.2x", hash[i]); putchar('\n'); -- 2.11.4.GIT