diff options
author | Pauli <pauli@openssl.org> | 2021-04-28 12:58:35 +1000 |
---|---|---|
committer | Pauli <pauli@openssl.org> | 2021-05-05 22:12:20 +1000 |
commit | b039c87a4ca3dd4e4ebbea4a5d1fd3821497f900 (patch) | |
tree | 4fbdcd2fd9d4d8d541d015eca553874e43ed69e0 /crypto/evp/mac_lib.c | |
parent | 6a38b09a7fa6eaac6bcbe567382fbe7d3d719503 (diff) |
mac: add EVP_MAC_finalXOF() function
Fixes #14140
Fixes #13232
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15061)
Diffstat (limited to 'crypto/evp/mac_lib.c')
-rw-r--r-- | crypto/evp/mac_lib.c | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/crypto/evp/mac_lib.c b/crypto/evp/mac_lib.c index 3d60905a9e..6f97de94de 100644 --- a/crypto/evp/mac_lib.c +++ b/crypto/evp/mac_lib.c @@ -116,21 +116,56 @@ int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen) return ctx->meth->update(ctx->data, data, datalen); } -int EVP_MAC_final(EVP_MAC_CTX *ctx, - unsigned char *out, size_t *outl, size_t outsize) +static int evp_mac_final(EVP_MAC_CTX *ctx, int xof, + unsigned char *out, size_t *outl, size_t outsize) { size_t l; - int res = 1; + int res; + OSSL_PARAM params[2]; + + if (ctx == NULL || ctx->meth == NULL) { + ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM); + return 0; + } + if (ctx->meth->final == NULL) { + ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); + return 0; + } - if (out != NULL) - res = ctx->meth->final(ctx->data, out, &l, outsize); - else - l = EVP_MAC_CTX_get_mac_size(ctx); + if (out == NULL) { + if (outl == NULL) { + ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + *outl = EVP_MAC_CTX_get_mac_size(ctx); + return 1; + } + if (xof) { + params[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof); + params[1] = OSSL_PARAM_construct_end(); + + if (EVP_MAC_CTX_set_params(ctx, params) <= 0) { + ERR_raise(ERR_LIB_EVP, EVP_R_SETTING_XOF_FAILED); + return 0; + } + } + res = ctx->meth->final(ctx->data, out, &l, outsize); if (outl != NULL) *outl = l; return res; } +int EVP_MAC_final(EVP_MAC_CTX *ctx, + unsigned char *out, size_t *outl, size_t outsize) +{ + return evp_mac_final(ctx, 0, out, outl, outsize); +} + +int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize) +{ + return evp_mac_final(ctx, 1, out, NULL, outsize); +} + /* * The {get,set}_params functions return 1 if there is no corresponding * function in the implementation. This is the same as if there was one, |