diff options
author | Miod Vallat <miod@cvs.openbsd.org> | 2014-06-21 12:00:02 +0000 |
---|---|---|
committer | Miod Vallat <miod@cvs.openbsd.org> | 2014-06-21 12:00:02 +0000 |
commit | 9d45c5cec2becbab58f470271d012753a9cec818 (patch) | |
tree | f6869378673911ec5f5975f64cdbb3b7bfe2a6a2 /lib | |
parent | ec9a972cf421c38fb726fd201413177ec4ceb473 (diff) |
KNF
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libcrypto/hmac/hm_ameth.c | 71 | ||||
-rw-r--r-- | lib/libcrypto/hmac/hm_pmeth.c | 127 | ||||
-rw-r--r-- | lib/libcrypto/hmac/hmac.c | 166 |
3 files changed, 186 insertions, 178 deletions
diff --git a/lib/libcrypto/hmac/hm_ameth.c b/lib/libcrypto/hmac/hm_ameth.c index e3633963faf..ef97918472b 100644 --- a/lib/libcrypto/hmac/hm_ameth.c +++ b/lib/libcrypto/hmac/hm_ameth.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hm_ameth.c,v 1.4 2014/06/12 15:49:29 deraadt Exp $ */ +/* $OpenBSD: hm_ameth.c,v 1.5 2014/06/21 12:00:01 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2007. */ @@ -68,35 +68,35 @@ * key. */ -static int hmac_size(const EVP_PKEY *pkey) - { +static int +hmac_size(const EVP_PKEY *pkey) +{ return EVP_MAX_MD_SIZE; - } +} -static void hmac_key_free(EVP_PKEY *pkey) - { +static void +hmac_key_free(EVP_PKEY *pkey) +{ ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr; - if (os) - { + + if (os) { if (os->data) OPENSSL_cleanse(os->data, os->length); ASN1_OCTET_STRING_free(os); - } } +} - -static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) - { - switch (op) - { - case ASN1_PKEY_CTRL_DEFAULT_MD_NID: +static int +hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) +{ + switch (op) { + case ASN1_PKEY_CTRL_DEFAULT_MD_NID: *(int *)arg2 = NID_sha1; return 1; - - default: + default: return -2; - } } +} #ifdef HMAC_TEST_PRIVATE_KEY_FORMAT /* A bogus private key format for test purposes. This is simply the @@ -104,42 +104,44 @@ static int hmac_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) * genpkey utility can be used to "generate" HMAC keys. */ -static int old_hmac_decode(EVP_PKEY *pkey, - const unsigned char **pder, int derlen) - { +static int +old_hmac_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) +{ ASN1_OCTET_STRING *os; + os = ASN1_OCTET_STRING_new(); if (!os || !ASN1_OCTET_STRING_set(os, *pder, derlen)) return 0; EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, os); return 1; - } +} -static int old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder) - { +static int +old_hmac_encode(const EVP_PKEY *pkey, unsigned char **pder) +{ int inc; ASN1_OCTET_STRING *os = (ASN1_OCTET_STRING *)pkey->pkey.ptr; - if (pder) - { - if (!*pder) - { + + if (pder) { + if (!*pder) { *pder = malloc(os->length); inc = 0; - } - else inc = 1; + } else + inc = 1; memcpy(*pder, os->data, os->length); if (inc) *pder += os->length; - } + } return os->length; - } +} #endif -const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = { +const EVP_PKEY_ASN1_METHOD +hmac_asn1_meth = { .pkey_id = EVP_PKEY_HMAC, .pkey_base_id = EVP_PKEY_HMAC, @@ -154,5 +156,4 @@ const EVP_PKEY_ASN1_METHOD hmac_asn1_meth = { .old_priv_decode = old_hmac_decode, .old_priv_encode = old_hmac_encode #endif - }; - +}; diff --git a/lib/libcrypto/hmac/hm_pmeth.c b/lib/libcrypto/hmac/hm_pmeth.c index e7a550b30c7..c1ada08a488 100644 --- a/lib/libcrypto/hmac/hm_pmeth.c +++ b/lib/libcrypto/hmac/hm_pmeth.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hm_pmeth.c,v 1.4 2014/06/12 15:49:29 deraadt Exp $ */ +/* $OpenBSD: hm_pmeth.c,v 1.5 2014/06/21 12:00:01 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2007. */ @@ -66,16 +66,17 @@ /* HMAC pkey context structure */ -typedef struct - { +typedef struct { const EVP_MD *md; /* MD for HMAC use */ ASN1_OCTET_STRING ktmp; /* Temp storage for key */ HMAC_CTX ctx; - } HMAC_PKEY_CTX; +} HMAC_PKEY_CTX; -static int pkey_hmac_init(EVP_PKEY_CTX *ctx) - { +static int +pkey_hmac_init(EVP_PKEY_CTX *ctx) +{ HMAC_PKEY_CTX *hctx; + hctx = malloc(sizeof(HMAC_PKEY_CTX)); if (!hctx) return 0; @@ -90,11 +91,13 @@ static int pkey_hmac_init(EVP_PKEY_CTX *ctx) ctx->keygen_info_count = 0; return 1; - } +} -static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) - { +static int +pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) +{ HMAC_PKEY_CTX *sctx, *dctx; + if (!pkey_hmac_init(dst)) return 0; sctx = src->data; @@ -103,33 +106,35 @@ static int pkey_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) HMAC_CTX_init(&dctx->ctx); if (!HMAC_CTX_copy(&dctx->ctx, &sctx->ctx)) return 0; - if (sctx->ktmp.data) - { - if (!ASN1_OCTET_STRING_set(&dctx->ktmp, - sctx->ktmp.data, sctx->ktmp.length)) + if (sctx->ktmp.data) { + if (!ASN1_OCTET_STRING_set(&dctx->ktmp, sctx->ktmp.data, + sctx->ktmp.length)) return 0; - } - return 1; } + return 1; +} -static void pkey_hmac_cleanup(EVP_PKEY_CTX *ctx) - { +static void +pkey_hmac_cleanup(EVP_PKEY_CTX *ctx) +{ HMAC_PKEY_CTX *hctx = ctx->data; + HMAC_CTX_cleanup(&hctx->ctx); - if (hctx->ktmp.data) - { + if (hctx->ktmp.data) { if (hctx->ktmp.length) OPENSSL_cleanse(hctx->ktmp.data, hctx->ktmp.length); free(hctx->ktmp.data); hctx->ktmp.data = NULL; - } - free(hctx); } + free(hctx); +} -static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) - { +static int +pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) +{ ASN1_OCTET_STRING *hkey = NULL; HMAC_PKEY_CTX *hctx = ctx->data; + if (!hctx->ktmp.data) return 0; hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp); @@ -138,28 +143,33 @@ static int pkey_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey); return 1; - } +} -static int int_update(EVP_MD_CTX *ctx,const void *data,size_t count) - { +static int +int_update(EVP_MD_CTX *ctx,const void *data,size_t count) +{ HMAC_PKEY_CTX *hctx = ctx->pctx->data; + if (!HMAC_Update(&hctx->ctx, data, count)) return 0; return 1; - } +} -static int hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) - { +static int +hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) +{ HMAC_PKEY_CTX *hctx = ctx->data; + HMAC_CTX_set_flags(&hctx->ctx, mctx->flags & ~EVP_MD_CTX_FLAG_NO_INIT); EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT); mctx->update = int_update; return 1; - } +} -static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, - EVP_MD_CTX *mctx) - { +static int +hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, + EVP_MD_CTX *mctx) +{ unsigned int hlen; HMAC_PKEY_CTX *hctx = ctx->data; int l = EVP_MD_CTX_size(mctx); @@ -174,55 +184,49 @@ static int hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, return 0; *siglen = (size_t)hlen; return 1; - } +} -static int pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) - { +static int +pkey_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) +{ HMAC_PKEY_CTX *hctx = ctx->data; ASN1_OCTET_STRING *key; - switch (type) - { - case EVP_PKEY_CTRL_SET_MAC_KEY: + switch (type) { + case EVP_PKEY_CTRL_SET_MAC_KEY: if ((!p2 && p1 > 0) || (p1 < -1)) return 0; if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1)) return 0; break; - case EVP_PKEY_CTRL_MD: + case EVP_PKEY_CTRL_MD: hctx->md = p2; break; - case EVP_PKEY_CTRL_DIGESTINIT: + case EVP_PKEY_CTRL_DIGESTINIT: key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr; if (!HMAC_Init_ex(&hctx->ctx, key->data, key->length, hctx->md, - ctx->engine)) + ctx->engine)) return 0; break; - default: + default: return -2; - - } - return 1; } + return 1; +} -static int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx, - const char *type, const char *value) - { +static int +pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value) +{ if (!value) - { return 0; - } - if (!strcmp(type, "key")) - { + if (!strcmp(type, "key")) { void *p = (void *)value; - return pkey_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, - -1, p); - } - if (!strcmp(type, "hexkey")) - { + return pkey_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p); + } + if (!strcmp(type, "hexkey")) { unsigned char *key; int r; long keylen; @@ -232,11 +236,12 @@ static int pkey_hmac_ctrl_str(EVP_PKEY_CTX *ctx, r = pkey_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key); free(key); return r; - } - return -2; } + return -2; +} -const EVP_PKEY_METHOD hmac_pkey_meth = { +const EVP_PKEY_METHOD +hmac_pkey_meth = { .pkey_id = EVP_PKEY_HMAC, .init = pkey_hmac_init, 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); - } +} |