diff options
author | David Benjamin <davidben@chromium.org> | 2015-05-27 08:41:01 +0300 |
---|---|---|
committer | Adam Langley <agl@google.com> | 2015-06-02 01:17:10 +0300 |
commit | 2c3679210e77f825782317e6b5d4ffc7eae4b81b (patch) | |
tree | eda6732256f719cefd914c2be6fda74eb76ce605 /crypto/digest | |
parent | e2375e139ee410538a3a77e8921b49cc8e727bb0 (diff) |
EVP_Digest*Update, EVP_DigestFinal, and HMAC_Update can never fail.
Enough code fails to check their return codes anyway. We ought to make
it official.
Change-Id: Ie646360fd7073ea943036f5e21bed13df7e1b77a
Reviewed-on: https://boringssl-review.googlesource.com/4954
Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'crypto/digest')
-rw-r--r-- | crypto/digest/digest.c | 17 | ||||
-rw-r--r-- | crypto/digest/digests.c | 129 | ||||
-rw-r--r-- | crypto/digest/internal.h | 9 |
3 files changed, 96 insertions, 59 deletions
diff --git a/crypto/digest/digest.c b/crypto/digest/digest.c index e32eafdc..d60bdc96 100644 --- a/crypto/digest/digest.c +++ b/crypto/digest/digest.c @@ -189,7 +189,8 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *engine) { return 1; } - return ctx->digest->init(ctx); + ctx->digest->init(ctx); + return 1; } int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) { @@ -198,26 +199,24 @@ int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type) { } int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { - return ctx->update(ctx, data, len); + ctx->update(ctx, data, len); + return 1; } int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, unsigned int *size) { - int ret; - assert(ctx->digest->md_size <= EVP_MAX_MD_SIZE); - ret = ctx->digest->final(ctx, md_out); + ctx->digest->final(ctx, md_out); if (size != NULL) { *size = ctx->digest->md_size; } OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size); - - return ret; + return 1; } int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md, unsigned int *size) { - int ret = EVP_DigestFinal_ex(ctx, md, size); + EVP_DigestFinal_ex(ctx, md, size); EVP_MD_CTX_cleanup(ctx); - return ret; + return 1; } int EVP_Digest(const void *data, size_t count, uint8_t *out_md, diff --git a/crypto/digest/digests.c b/crypto/digest/digests.c index b0036af1..4dff340b 100644 --- a/crypto/digest/digests.c +++ b/crypto/digest/digests.c @@ -56,6 +56,8 @@ #include <openssl/digest.h> +#include <assert.h> + #include <openssl/md4.h> #include <openssl/md5.h> #include <openssl/obj.h> @@ -64,14 +66,19 @@ #include "internal.h" -static int md4_init(EVP_MD_CTX *ctx) { return MD4_Init(ctx->md_data); } +static void md4_init(EVP_MD_CTX *ctx) { + int ret = MD4_Init(ctx->md_data); + assert(ret); +} -static int md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - return MD4_Update(ctx->md_data, data, count); +static void md4_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + int ret = MD4_Update(ctx->md_data, data, count); + assert(ret); } -static int md4_final(EVP_MD_CTX *ctx, uint8_t *out) { - return MD4_Final(out, ctx->md_data); +static void md4_final(EVP_MD_CTX *ctx, uint8_t *out) { + int ret = MD4_Final(out, ctx->md_data); + assert(ret); } static const EVP_MD md4_md = { @@ -82,14 +89,19 @@ static const EVP_MD md4_md = { const EVP_MD *EVP_md4(void) { return &md4_md; } -static int md5_init(EVP_MD_CTX *ctx) { return MD5_Init(ctx->md_data); } +static void md5_init(EVP_MD_CTX *ctx) { + int ret = MD5_Init(ctx->md_data); + assert(ret); +} -static int md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - return MD5_Update(ctx->md_data, data, count); +static void md5_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + int ret = MD5_Update(ctx->md_data, data, count); + assert(ret); } -static int md5_final(EVP_MD_CTX *ctx, uint8_t *out) { - return MD5_Final(out, ctx->md_data); +static void md5_final(EVP_MD_CTX *ctx, uint8_t *out) { + int ret = MD5_Final(out, ctx->md_data); + assert(ret); } static const EVP_MD md5_md = { @@ -100,14 +112,19 @@ static const EVP_MD md5_md = { const EVP_MD *EVP_md5(void) { return &md5_md; } -static int sha1_init(EVP_MD_CTX *ctx) { return SHA1_Init(ctx->md_data); } +static void sha1_init(EVP_MD_CTX *ctx) { + int ret = SHA1_Init(ctx->md_data); + assert(ret); +} -static int sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - return SHA1_Update(ctx->md_data, data, count); +static void sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + int ret = SHA1_Update(ctx->md_data, data, count); + assert(ret); } -static int sha1_final(EVP_MD_CTX *ctx, uint8_t *md) { - return SHA1_Final(md, ctx->md_data); +static void sha1_final(EVP_MD_CTX *ctx, uint8_t *md) { + int ret = SHA1_Final(md, ctx->md_data); + assert(ret); } static const EVP_MD sha1_md = { @@ -118,14 +135,19 @@ static const EVP_MD sha1_md = { const EVP_MD *EVP_sha1(void) { return &sha1_md; } -static int sha224_init(EVP_MD_CTX *ctx) { return SHA224_Init(ctx->md_data); } +static void sha224_init(EVP_MD_CTX *ctx) { + int ret = SHA224_Init(ctx->md_data); + assert(ret); +} -static int sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - return SHA224_Update(ctx->md_data, data, count); +static void sha224_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + int ret = SHA224_Update(ctx->md_data, data, count); + assert(ret); } -static int sha224_final(EVP_MD_CTX *ctx, uint8_t *md) { - return SHA224_Final(md, ctx->md_data); +static void sha224_final(EVP_MD_CTX *ctx, uint8_t *md) { + int ret = SHA224_Final(md, ctx->md_data); + assert(ret); } static const EVP_MD sha224_md = { @@ -137,14 +159,19 @@ static const EVP_MD sha224_md = { const EVP_MD *EVP_sha224(void) { return &sha224_md; } -static int sha256_init(EVP_MD_CTX *ctx) { return SHA256_Init(ctx->md_data); } +static void sha256_init(EVP_MD_CTX *ctx) { + int ret = SHA256_Init(ctx->md_data); + assert(ret); +} -static int sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - return SHA256_Update(ctx->md_data, data, count); +static void sha256_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + int ret = SHA256_Update(ctx->md_data, data, count); + assert(ret); } -static int sha256_final(EVP_MD_CTX *ctx, uint8_t *md) { - return SHA256_Final(md, ctx->md_data); +static void sha256_final(EVP_MD_CTX *ctx, uint8_t *md) { + int ret = SHA256_Final(md, ctx->md_data); + assert(ret); } static const EVP_MD sha256_md = { @@ -156,14 +183,19 @@ static const EVP_MD sha256_md = { const EVP_MD *EVP_sha256(void) { return &sha256_md; } -static int sha384_init(EVP_MD_CTX *ctx) { return SHA384_Init(ctx->md_data); } +static void sha384_init(EVP_MD_CTX *ctx) { + int ret = SHA384_Init(ctx->md_data); + assert(ret); +} -static int sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - return SHA384_Update(ctx->md_data, data, count); +static void sha384_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + int ret = SHA384_Update(ctx->md_data, data, count); + assert(ret); } -static int sha384_final(EVP_MD_CTX *ctx, uint8_t *md) { - return SHA384_Final(md, ctx->md_data); +static void sha384_final(EVP_MD_CTX *ctx, uint8_t *md) { + int ret = SHA384_Final(md, ctx->md_data); + assert(ret); } static const EVP_MD sha384_md = { @@ -175,14 +207,19 @@ static const EVP_MD sha384_md = { const EVP_MD *EVP_sha384(void) { return &sha384_md; } -static int sha512_init(EVP_MD_CTX *ctx) { return SHA512_Init(ctx->md_data); } +static void sha512_init(EVP_MD_CTX *ctx) { + int ret = SHA512_Init(ctx->md_data); + assert(ret); +} -static int sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) { - return SHA512_Update(ctx->md_data, data, count); +static void sha512_update(EVP_MD_CTX *ctx, const void *data, size_t count) { + int ret = SHA512_Update(ctx->md_data, data, count); + assert(ret); } -static int sha512_final(EVP_MD_CTX *ctx, uint8_t *md) { - return SHA512_Final(md, ctx->md_data); +static void sha512_final(EVP_MD_CTX *ctx, uint8_t *md) { + int ret = SHA512_Final(md, ctx->md_data); + assert(ret); } static const EVP_MD sha512_md = { @@ -199,23 +236,25 @@ typedef struct { SHA_CTX sha1; } MD5_SHA1_CTX; -static int md5_sha1_init(EVP_MD_CTX *md_ctx) { +static void md5_sha1_init(EVP_MD_CTX *md_ctx) { MD5_SHA1_CTX *ctx = md_ctx->md_data; - return MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1); + int ret = MD5_Init(&ctx->md5) && SHA1_Init(&ctx->sha1); + assert(ret); } -static int md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data, size_t count) { +static void md5_sha1_update(EVP_MD_CTX *md_ctx, const void *data, + size_t count) { MD5_SHA1_CTX *ctx = md_ctx->md_data; - return MD5_Update(&ctx->md5, data, count) && SHA1_Update(&ctx->sha1, data, count); + int ret = MD5_Update(&ctx->md5, data, count) && + SHA1_Update(&ctx->sha1, data, count); + assert(ret); } -static int md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) { +static void md5_sha1_final(EVP_MD_CTX *md_ctx, uint8_t *out) { MD5_SHA1_CTX *ctx = md_ctx->md_data; - if (!MD5_Final(out, &ctx->md5) || - !SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1)) { - return 0; - } - return 1; + int ret = MD5_Final(out, &ctx->md5) && + SHA1_Final(out + MD5_DIGEST_LENGTH, &ctx->sha1); + assert(ret); } static const EVP_MD md5_sha1_md = { diff --git a/crypto/digest/internal.h b/crypto/digest/internal.h index 94dbfaa6..1134702b 100644 --- a/crypto/digest/internal.h +++ b/crypto/digest/internal.h @@ -75,15 +75,14 @@ struct env_md_st { /* flags contains the OR of |EVP_MD_FLAG_*| values. */ uint32_t flags; - /* init initialises the state in |ctx->md_data|. It returns one on success - * and zero otherwise. */ - int (*init)(EVP_MD_CTX *ctx); + /* init initialises the state in |ctx->md_data|. */ + void (*init)(EVP_MD_CTX *ctx); /* update hashes |len| bytes of |data| into the state in |ctx->md_data|. */ - int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count); + void (*update)(EVP_MD_CTX *ctx, const void *data, size_t count); /* final completes the hash and writes |md_size| bytes of digest to |out|. */ - int (*final)(EVP_MD_CTX *ctx, uint8_t *out); + void (*final)(EVP_MD_CTX *ctx, uint8_t *out); /* block_size contains the hash's native block size. */ unsigned block_size; |