diff options
Diffstat (limited to 'lib/libcrypto/hmac/hmac.c')
-rw-r--r-- | lib/libcrypto/hmac/hmac.c | 166 |
1 files changed, 84 insertions, 82 deletions
diff --git a/lib/libcrypto/hmac/hmac.c b/lib/libcrypto/hmac/hmac.c index 690715db2cf..0c29156d80f 100644 --- a/lib/libcrypto/hmac/hmac.c +++ b/lib/libcrypto/hmac/hmac.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hmac.c,v 1.18 2014/06/12 15:49:29 deraadt Exp $ */ +/* $OpenBSD: hmac.c,v 1.19 2014/06/21 12:00:01 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -61,108 +61,107 @@ #include "cryptlib.h" #include <openssl/hmac.h> -int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, - const EVP_MD *md, ENGINE *impl) - { - int i,j,reset=0; +int +HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md, + ENGINE *impl) +{ + int i, j, reset = 0; unsigned char pad[HMAC_MAX_MD_CBLOCK]; - if (md != NULL) - { - reset=1; - ctx->md=md; - } - else - md=ctx->md; + if (md != NULL) { + reset = 1; + ctx->md = md; + } else + md = ctx->md; - if (key != NULL) - { - reset=1; - j=EVP_MD_block_size(md); + if (key != NULL) { + reset = 1; + j = EVP_MD_block_size(md); OPENSSL_assert(j <= (int)sizeof(ctx->key)); - if (j < len) - { - if (!EVP_DigestInit_ex(&ctx->md_ctx,md, impl)) + if (j < len) { + if (!EVP_DigestInit_ex(&ctx->md_ctx, md, impl)) goto err; - if (!EVP_DigestUpdate(&ctx->md_ctx,key,len)) + if (!EVP_DigestUpdate(&ctx->md_ctx, key, len)) goto err; - if (!EVP_DigestFinal_ex(&(ctx->md_ctx),ctx->key, + if (!EVP_DigestFinal_ex(&(ctx->md_ctx), ctx->key, &ctx->key_length)) goto err; - } - else - { + } else { OPENSSL_assert(len>=0 && len<=(int)sizeof(ctx->key)); memcpy(ctx->key,key,len); - ctx->key_length=len; - } - if(ctx->key_length != HMAC_MAX_MD_CBLOCK) + ctx->key_length = len; + } + if (ctx->key_length != HMAC_MAX_MD_CBLOCK) memset(&ctx->key[ctx->key_length], 0, - HMAC_MAX_MD_CBLOCK - ctx->key_length); + HMAC_MAX_MD_CBLOCK - ctx->key_length); } - if (reset) - { - for (i=0; i<HMAC_MAX_MD_CBLOCK; i++) - pad[i]=0x36^ctx->key[i]; - if (!EVP_DigestInit_ex(&ctx->i_ctx,md, impl)) + if (reset) { + for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++) + pad[i] = 0x36 ^ ctx->key[i]; + if (!EVP_DigestInit_ex(&ctx->i_ctx, md, impl)) goto err; - if (!EVP_DigestUpdate(&ctx->i_ctx,pad,EVP_MD_block_size(md))) + if (!EVP_DigestUpdate(&ctx->i_ctx, pad, EVP_MD_block_size(md))) goto err; - for (i=0; i<HMAC_MAX_MD_CBLOCK; i++) - pad[i]=0x5c^ctx->key[i]; - if (!EVP_DigestInit_ex(&ctx->o_ctx,md, impl)) + for (i = 0; i < HMAC_MAX_MD_CBLOCK; i++) + pad[i] = 0x5c ^ ctx->key[i]; + if (!EVP_DigestInit_ex(&ctx->o_ctx, md, impl)) goto err; - if (!EVP_DigestUpdate(&ctx->o_ctx,pad,EVP_MD_block_size(md))) - goto err; - } - if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->i_ctx)) + if (!EVP_DigestUpdate(&ctx->o_ctx, pad, EVP_MD_block_size(md))) goto err; + } + if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->i_ctx)) + goto err; return 1; - err: +err: return 0; - } +} -int HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md) - { - if(key && md) +int +HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md) +{ + if (key && md) HMAC_CTX_init(ctx); return HMAC_Init_ex(ctx,key,len,md, NULL); - } +} -int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) - { +int +HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len) +{ return EVP_DigestUpdate(&ctx->md_ctx,data,len); - } +} -int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) - { +int +HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len) +{ unsigned int i; unsigned char buf[EVP_MAX_MD_SIZE]; - if (!EVP_DigestFinal_ex(&ctx->md_ctx,buf,&i)) + if (!EVP_DigestFinal_ex(&ctx->md_ctx, buf, &i)) goto err; - if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx,&ctx->o_ctx)) + if (!EVP_MD_CTX_copy_ex(&ctx->md_ctx, &ctx->o_ctx)) goto err; - if (!EVP_DigestUpdate(&ctx->md_ctx,buf,i)) + if (!EVP_DigestUpdate(&ctx->md_ctx, buf, i)) goto err; - if (!EVP_DigestFinal_ex(&ctx->md_ctx,md,len)) + if (!EVP_DigestFinal_ex(&ctx->md_ctx, md, len)) goto err; return 1; - err: +err: return 0; - } +} -void HMAC_CTX_init(HMAC_CTX *ctx) - { +void +HMAC_CTX_init(HMAC_CTX *ctx) +{ EVP_MD_CTX_init(&ctx->i_ctx); EVP_MD_CTX_init(&ctx->o_ctx); EVP_MD_CTX_init(&ctx->md_ctx); - } +} -int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) - { +int +HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) +{ if (!EVP_MD_CTX_copy(&dctx->i_ctx, &sctx->i_ctx)) goto err; if (!EVP_MD_CTX_copy(&dctx->o_ctx, &sctx->o_ctx)) @@ -173,42 +172,45 @@ int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx) dctx->key_length = sctx->key_length; dctx->md = sctx->md; return 1; - err: +err: return 0; - } +} -void HMAC_CTX_cleanup(HMAC_CTX *ctx) - { +void +HMAC_CTX_cleanup(HMAC_CTX *ctx) +{ EVP_MD_CTX_cleanup(&ctx->i_ctx); EVP_MD_CTX_cleanup(&ctx->o_ctx); EVP_MD_CTX_cleanup(&ctx->md_ctx); - memset(ctx,0,sizeof *ctx); - } + memset(ctx, 0, sizeof *ctx); +} -unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len, - const unsigned char *d, size_t n, unsigned char *md, - unsigned int *md_len) - { +unsigned char * +HMAC(const EVP_MD *evp_md, const void *key, int key_len, const unsigned char *d, + size_t n, unsigned char *md, unsigned int *md_len) +{ HMAC_CTX c; static unsigned char m[EVP_MAX_MD_SIZE]; - if (md == NULL) md=m; + if (md == NULL) + md = m; HMAC_CTX_init(&c); - if (!HMAC_Init(&c,key,key_len,evp_md)) + if (!HMAC_Init(&c, key, key_len, evp_md)) goto err; - if (!HMAC_Update(&c,d,n)) + if (!HMAC_Update(&c, d, n)) goto err; - if (!HMAC_Final(&c,md,md_len)) + if (!HMAC_Final(&c, md, md_len)) goto err; HMAC_CTX_cleanup(&c); return md; - err: +err: return NULL; - } +} -void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) - { +void +HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags) +{ EVP_MD_CTX_set_flags(&ctx->i_ctx, flags); EVP_MD_CTX_set_flags(&ctx->o_ctx, flags); EVP_MD_CTX_set_flags(&ctx->md_ctx, flags); - } +} |