add HMAC-SHA1 support

This commit is contained in:
Michael Rash
2013-03-10 14:56:39 -04:00
parent 7821e83dfc
commit 6882ac57ec
4 changed files with 83 additions and 1 deletions
+5 -1
View File
@@ -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)
{
+74
View File
@@ -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
+2
View File
@@ -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,
+2
View File
@@ -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 {