From 6882ac57ec9bfc945d29304df11fe60dc70b8d5a Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Sun, 10 Mar 2013 14:56:39 -0400 Subject: [PATCH] add HMAC-SHA1 support --- lib/fko_hmac.c | 6 +++- lib/hmac.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/hmac.h | 2 ++ lib/sha1.h | 2 ++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/lib/fko_hmac.c b/lib/fko_hmac.c index a698c411..59ce4363 100644 --- a/lib/fko_hmac.c +++ b/lib/fko_hmac.c @@ -196,7 +196,11 @@ int fko_calculate_hmac(fko_ctx_t ctx, } else if(ctx->hmac_type == FKO_HMAC_SHA1) { - return(FKO_ERROR_CTX_NOT_INITIALIZED); + hmac_sha1(ctx->encrypted_msg, + ctx->encrypted_msg_len, hmac, hmac_key, hmac_key_len); + + hmac_digest_len = SHA1_DIGEST_LEN; + hmac_digest_str_len = SHA1_DIGEST_STR_LEN; } else if(ctx->hmac_type == FKO_HMAC_SHA256) { diff --git a/lib/hmac.c b/lib/hmac.c index 5857b116..05312b54 100644 --- a/lib/hmac.c +++ b/lib/hmac.c @@ -31,6 +31,14 @@ #include "hmac.h" +typedef struct { + SHA1_INFO ctx_inside; + SHA1_INFO ctx_outside; + + unsigned char block_inner_pad[SHA1_BLOCK_LEN]; + unsigned char block_outer_pad[SHA1_BLOCK_LEN]; +} hmac_sha1_ctx; + typedef struct { SHA256_CTX ctx_inside; SHA256_CTX ctx_outside; @@ -55,6 +63,72 @@ typedef struct { unsigned char block_outer_pad[SHA512_BLOCK_LEN]; } hmac_sha512_ctx; +/* Begin SHA1 HMAC functions +*/ +static void +hmac_sha1_init(hmac_sha1_ctx *ctx, const char *key, const int key_len) +{ + int i = 0; + + for (i=0; i < key_len; i++) { + ctx->block_inner_pad[i] = key[i] ^ 0x36; + ctx->block_outer_pad[i] = key[i] ^ 0x5c; + } + + if(i < SHA1_BLOCK_LEN) + { + while(i < SHA1_BLOCK_LEN) + { + ctx->block_inner_pad[i] = 0x36; + ctx->block_outer_pad[i] = 0x5c; + i++; + } + } + + sha1_init(&ctx->ctx_inside); + sha1_update(&ctx->ctx_inside, ctx->block_inner_pad, SHA1_BLOCK_LEN); + + sha1_init(&ctx->ctx_outside); + sha1_update(&ctx->ctx_outside, ctx->block_outer_pad, SHA1_BLOCK_LEN); + + return; +} + +static void +hmac_sha1_update(hmac_sha1_ctx *ctx, const char *msg, + unsigned int msg_len) +{ + sha1_update(&ctx->ctx_inside, (unsigned char *)msg, msg_len); + return; +} + +static void +hmac_sha1_final(hmac_sha1_ctx *ctx, unsigned char *hmac) +{ + unsigned char digest_inside[SHA1_DIGEST_LEN]; + + sha1_final(digest_inside, &ctx->ctx_inside); + sha1_update(&ctx->ctx_outside, digest_inside, SHA1_DIGEST_LEN); + sha1_final(hmac, &ctx->ctx_outside); + + return; +} + +void +hmac_sha1(const char *msg, const unsigned int msg_len, + unsigned char *hmac, const char *hmac_key, const int hmac_key_len) +{ + hmac_sha1_ctx ctx; + + memset(&ctx, 0, sizeof(&ctx)); + + hmac_sha1_init(&ctx, hmac_key, hmac_key_len); + hmac_sha1_update(&ctx, msg, msg_len); + hmac_sha1_final(&ctx, hmac); + + return; +} + /* Begin SHA256 HMAC functions */ static void diff --git a/lib/hmac.h b/lib/hmac.h index b853a4f1..04018fdb 100644 --- a/lib/hmac.h +++ b/lib/hmac.h @@ -33,6 +33,8 @@ #include "digest.h" +void hmac_sha1(const char *msg, const unsigned int msg_len, + unsigned char *hmac, const char *hmac_key, const int hmac_key_len); void hmac_sha256(const char *msg, const unsigned int msg_len, unsigned char *hmac, const char *hmac_key, const int hmac_key_len); void hmac_sha384(const char *msg, const unsigned int msg_len, diff --git a/lib/sha1.h b/lib/sha1.h index dfa1e887..5edcc52e 100644 --- a/lib/sha1.h +++ b/lib/sha1.h @@ -45,7 +45,9 @@ #endif #define SHA1_BLOCKSIZE 64 +#define SHA1_BLOCK_LEN SHA1_BLOCKSIZE #define SHA1_DIGEST_LEN 20 +#define SHA1_DIGEST_STR_LEN (SHA1_DIGEST_LEN * 2 + 1) #define SHA1_B64_LEN 27 typedef struct {