summaryrefslogtreecommitdiffstats
path: root/crypto/evp/digest.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/evp/digest.c')
-rw-r--r--crypto/evp/digest.c78
1 files changed, 62 insertions, 16 deletions
diff --git a/crypto/evp/digest.c b/crypto/evp/digest.c
index 4c61ca4c42..a34b7ced6c 100644
--- a/crypto/evp/digest.c
+++ b/crypto/evp/digest.c
@@ -20,6 +20,7 @@
#include <openssl/params.h>
#include <openssl/core_names.h>
#include "internal/cryptlib.h"
+#include "internal/nelem.h"
#include "internal/provider.h"
#include "internal/core.h"
#include "crypto/evp.h"
@@ -77,7 +78,6 @@ static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched)
if (ctx == NULL)
return 1;
-#ifndef FIPS_MODULE
/*
* pctx should be freed by the user of EVP_MD_CTX
* if EVP_MD_CTX_FLAG_KEEP_PKEY_CTX is set
@@ -86,7 +86,6 @@ static int evp_md_ctx_reset_ex(EVP_MD_CTX *ctx, int keep_fetched)
EVP_PKEY_CTX_free(ctx->pctx);
ctx->pctx = NULL;
}
-#endif
evp_md_ctx_clear_digest(ctx, 0, keep_fetched);
if (!keep_fetched)
@@ -425,7 +424,7 @@ int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)
/* Code below to be removed when legacy support is dropped. */
legacy:
- return ctx->update(ctx, data, count);
+ return ctx->update != NULL ? ctx->update(ctx, data, count) : 0;
}
/* The caller can assume that this removes any secret data from the context */
@@ -447,24 +446,13 @@ int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *isize)
if (ctx->digest == NULL)
return 0;
- sz = EVP_MD_get_size(ctx->digest);
+ sz = EVP_MD_CTX_get_size(ctx);
if (sz < 0)
return 0;
mdsize = sz;
if (ctx->digest->prov == NULL)
goto legacy;
- if (sz == 0) /* Assuming a xoflen must have been set. */
- mdsize = SIZE_MAX;
- if (ctx->digest->gettable_ctx_params != NULL) {
- OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
-
- params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE,
- &mdsize);
- if (!EVP_MD_CTX_get_params(ctx, params))
- return 0;
- }
-
if (ctx->digest->dfinal == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
return 0;
@@ -545,7 +533,7 @@ int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
return ret;
legacy:
- if (ctx->digest->flags & EVP_MD_FLAG_XOF
+ if (EVP_MD_xof(ctx->digest)
&& size <= INT_MAX
&& ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
ret = ctx->digest->final(ctx, md);
@@ -984,6 +972,11 @@ static int evp_md_cache_constants(EVP_MD *md)
size_t mdsize = 0;
OSSL_PARAM params[5];
+ /*
+ * Note that these parameters are 'constants' that are only set up
+ * during the EVP_MD_fetch(). For this reason the XOF functions set the
+ * md_size to 0, since the output size is unknown.
+ */
params[0] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_BLOCK_SIZE, &blksz);
params[1] = OSSL_PARAM_construct_size_t(OSSL_DIGEST_PARAM_SIZE, &mdsize);
params[2] = OSSL_PARAM_construct_int(OSSL_DIGEST_PARAM_XOF, &xof);
@@ -1185,3 +1178,56 @@ void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
(void (*)(void *, void *))fn, arg,
evp_md_from_algorithm, evp_md_up_ref, evp_md_free);
}
+
+typedef struct {
+ int md_nid;
+ int hmac_nid;
+} ossl_hmacmd_pair;
+
+static const ossl_hmacmd_pair ossl_hmacmd_pairs[] = {
+ {NID_sha1, NID_hmacWithSHA1},
+ {NID_md5, NID_hmacWithMD5},
+ {NID_sha224, NID_hmacWithSHA224},
+ {NID_sha256, NID_hmacWithSHA256},
+ {NID_sha384, NID_hmacWithSHA384},
+ {NID_sha512, NID_hmacWithSHA512},
+ {NID_id_GostR3411_94, NID_id_HMACGostR3411_94},
+ {NID_id_GostR3411_2012_256, NID_id_tc26_hmac_gost_3411_2012_256},
+ {NID_id_GostR3411_2012_512, NID_id_tc26_hmac_gost_3411_2012_512},
+ {NID_sha3_224, NID_hmac_sha3_224},
+ {NID_sha3_256, NID_hmac_sha3_256},
+ {NID_sha3_384, NID_hmac_sha3_384},
+ {NID_sha3_512, NID_hmac_sha3_512},
+ {NID_sha512_224, NID_hmacWithSHA512_224},
+ {NID_sha512_256, NID_hmacWithSHA512_256}
+};
+
+int ossl_hmac2mdnid(int hmac_nid)
+{
+ int md_nid = NID_undef;
+ size_t i;
+
+ for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
+ if (ossl_hmacmd_pairs[i].hmac_nid == hmac_nid) {
+ md_nid = ossl_hmacmd_pairs[i].md_nid;
+ break;
+ }
+ }
+
+ return md_nid;
+}
+
+int ossl_md2hmacnid(int md_nid)
+{
+ int hmac_nid = NID_undef;
+ size_t i;
+
+ for (i = 0; i < OSSL_NELEM(ossl_hmacmd_pairs); i++) {
+ if (ossl_hmacmd_pairs[i].md_nid == md_nid) {
+ hmac_nid = ossl_hmacmd_pairs[i].hmac_nid;
+ break;
+ }
+ }
+
+ return hmac_nid;
+}