2018-10-13 04:27:18 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2018-10-13 04:27:18 +08:00
|
|
|
*
|
2018-12-06 20:40:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2018-10-13 04:27:18 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
2019-05-07 18:39:58 +08:00
|
|
|
#include <openssl/core.h>
|
|
|
|
#include <openssl/core_names.h>
|
2019-09-28 06:45:46 +08:00
|
|
|
#include <openssl/types.h>
|
2018-10-13 04:27:18 +08:00
|
|
|
#include "internal/nelem.h"
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/evp.h"
|
2019-05-07 18:39:58 +08:00
|
|
|
#include "internal/provider.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "evp_local.h"
|
2018-10-13 04:27:18 +08:00
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
|
|
|
EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX));
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
if (ctx == NULL
|
2021-05-14 11:08:42 +08:00
|
|
|
|| (ctx->algctx = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL
|
2019-05-07 18:39:58 +08:00
|
|
|
|| !EVP_MAC_up_ref(mac)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2019-05-07 18:39:58 +08:00
|
|
|
if (ctx != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
mac->freectx(ctx->algctx);
|
2018-10-13 04:27:18 +08:00
|
|
|
OPENSSL_free(ctx);
|
|
|
|
ctx = NULL;
|
|
|
|
} else {
|
|
|
|
ctx->meth = mac;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2021-02-16 01:31:36 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2021-05-14 11:08:42 +08:00
|
|
|
ctx->meth->freectx(ctx->algctx);
|
|
|
|
ctx->algctx = NULL;
|
2021-02-16 01:31:36 +08:00
|
|
|
/* refcnt-- */
|
|
|
|
EVP_MAC_free(ctx->meth);
|
2018-10-13 04:27:18 +08:00
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2018-12-19 07:36:40 +08:00
|
|
|
EVP_MAC_CTX *dst;
|
2018-10-13 04:27:18 +08:00
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
if (src->algctx == NULL)
|
2018-11-05 02:16:20 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dst = OPENSSL_malloc(sizeof(*dst));
|
|
|
|
if (dst == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2018-11-05 02:16:20 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-13 04:27:18 +08:00
|
|
|
|
|
|
|
*dst = *src;
|
2019-05-07 18:39:58 +08:00
|
|
|
if (!EVP_MAC_up_ref(dst->meth)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2019-05-07 18:39:58 +08:00
|
|
|
OPENSSL_free(dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-13 04:27:18 +08:00
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
dst->algctx = src->meth->dupctx(src->algctx);
|
|
|
|
if (dst->algctx == NULL) {
|
2020-06-18 16:26:22 +08:00
|
|
|
EVP_MAC_CTX_free(dst);
|
2018-11-05 02:16:20 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
|
|
|
return ctx->meth;
|
|
|
|
}
|
|
|
|
|
2020-10-13 12:22:17 +08:00
|
|
|
size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2019-05-07 18:39:58 +08:00
|
|
|
size_t sz = 0;
|
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
if (ctx->algctx != NULL) {
|
2019-05-07 18:39:58 +08:00
|
|
|
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
|
|
|
|
|
2019-08-22 18:50:00 +08:00
|
|
|
params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &sz);
|
2019-08-16 15:04:29 +08:00
|
|
|
if (ctx->meth->get_ctx_params != NULL) {
|
2021-05-14 11:08:42 +08:00
|
|
|
if (ctx->meth->get_ctx_params(ctx->algctx, params))
|
2019-05-07 18:39:58 +08:00
|
|
|
return sz;
|
|
|
|
} else if (ctx->meth->get_params != NULL) {
|
|
|
|
if (ctx->meth->get_params(params))
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If the MAC hasn't been initialized yet, or there is no size to get,
|
|
|
|
* we return zero
|
|
|
|
*/
|
2018-10-13 04:27:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-25 11:48:48 +08:00
|
|
|
int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
|
|
|
|
const OSSL_PARAM params[])
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->init(ctx->algctx, key, keylen, params);
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
|
|
|
|
{
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->update(ctx->algctx, data, datalen);
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 10:58:35 +08:00
|
|
|
static int evp_mac_final(EVP_MAC_CTX *ctx, int xof,
|
|
|
|
unsigned char *out, size_t *outl, size_t outsize)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2020-08-05 13:26:48 +08:00
|
|
|
size_t l;
|
2021-04-28 10:58:35 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-10-13 04:27:18 +08:00
|
|
|
|
2021-04-28 10:58:35 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-05-14 11:08:42 +08:00
|
|
|
res = ctx->meth->final(ctx->algctx, out, &l, outsize);
|
2019-05-07 18:39:58 +08:00
|
|
|
if (outl != NULL)
|
|
|
|
*outl = l;
|
2020-07-16 09:15:42 +08:00
|
|
|
return res;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 10:58:35 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
/*
|
|
|
|
* 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,
|
|
|
|
* but it didn't recognise any of the given params, i.e. nothing in the
|
|
|
|
* bag of parameters was useful.
|
|
|
|
*/
|
|
|
|
int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2019-05-07 18:39:58 +08:00
|
|
|
if (mac->get_params != NULL)
|
|
|
|
return mac->get_params(params);
|
|
|
|
return 1;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2019-08-16 15:04:29 +08:00
|
|
|
if (ctx->meth->get_ctx_params != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->get_ctx_params(ctx->algctx, params);
|
2019-05-07 18:39:58 +08:00
|
|
|
return 1;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2019-08-16 15:04:29 +08:00
|
|
|
if (ctx->meth->set_ctx_params != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->set_ctx_params(ctx->algctx, params);
|
2019-05-07 18:39:58 +08:00
|
|
|
return 1;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
EVP: add missing common functionality
This adds the missing functions that should be common for all
fetchable EVP sub-APIs:
EVP_KEYMGMT_is_a(), EVP_KEYMGMT_do_all_provided(), EVP_KEYEXCH_is_a(),
EVP_KEYEXCH_do_all_provided(), EVP_KDF_is_a(), EVP_MD_is_a(),
EVP_SIGNATURE_do_all_provided(), EVP_SIGNATURE_is_a().
This also renames EVP_MD_do_all_ex(), EVP_CIPHER_do_all_ex(),
EVP_KDF_do_all_ex(), EVP_MAC_do_all_ex() to change '_ex'
to '_provided'.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/9979)
2019-09-23 16:33:26 +08:00
|
|
|
|
2019-09-23 17:16:21 +08:00
|
|
|
int EVP_MAC_number(const EVP_MAC *mac)
|
|
|
|
{
|
|
|
|
return mac->name_id;
|
|
|
|
}
|
|
|
|
|
2020-09-22 07:25:35 +08:00
|
|
|
const char *EVP_MAC_name(const EVP_MAC *mac)
|
|
|
|
{
|
2021-04-16 22:22:03 +08:00
|
|
|
return mac->type_name;
|
2020-09-22 07:25:35 +08:00
|
|
|
}
|
|
|
|
|
EVP: Add EVP_<TYPE>_description()
The following operation types are covered:
EVP_MD, EVP_CIPHER, EVP_MAC, EVP_RAND, EVP_KEYMGMT, EVP_SIGNATURE,
EVP_ASYM_CIPHER, EVP_KEM, EVP_KEYEXCH, EVP_KDF. Also EVP_PKEY.
For EVP_MD and EVP_CIPHER, OBJ_nid2ln() is used as a fallback for
legacy implementations.
For EVP_PKEY, the info field of the EVP_PKEY_ASN1_METHOD is used as a
fallback for legacy implementations.
Fixes #14514
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14656)
2021-03-16 21:23:54 +08:00
|
|
|
const char *EVP_MAC_description(const EVP_MAC *mac)
|
|
|
|
{
|
|
|
|
return mac->description;
|
|
|
|
}
|
|
|
|
|
EVP: add missing common functionality
This adds the missing functions that should be common for all
fetchable EVP sub-APIs:
EVP_KEYMGMT_is_a(), EVP_KEYMGMT_do_all_provided(), EVP_KEYEXCH_is_a(),
EVP_KEYEXCH_do_all_provided(), EVP_KDF_is_a(), EVP_MD_is_a(),
EVP_SIGNATURE_do_all_provided(), EVP_SIGNATURE_is_a().
This also renames EVP_MD_do_all_ex(), EVP_CIPHER_do_all_ex(),
EVP_KDF_do_all_ex(), EVP_MAC_do_all_ex() to change '_ex'
to '_provided'.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/9979)
2019-09-23 16:33:26 +08:00
|
|
|
int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
|
|
|
|
{
|
2020-01-15 08:04:37 +08:00
|
|
|
return evp_is_a(mac->prov, mac->name_id, NULL, name);
|
EVP: add missing common functionality
This adds the missing functions that should be common for all
fetchable EVP sub-APIs:
EVP_KEYMGMT_is_a(), EVP_KEYMGMT_do_all_provided(), EVP_KEYEXCH_is_a(),
EVP_KEYEXCH_do_all_provided(), EVP_KDF_is_a(), EVP_MD_is_a(),
EVP_SIGNATURE_do_all_provided(), EVP_SIGNATURE_is_a().
This also renames EVP_MD_do_all_ex(), EVP_CIPHER_do_all_ex(),
EVP_KDF_do_all_ex(), EVP_MAC_do_all_ex() to change '_ex'
to '_provided'.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/9979)
2019-09-23 16:33:26 +08:00
|
|
|
}
|
2019-09-23 16:56:13 +08:00
|
|
|
|
2021-02-20 01:03:43 +08:00
|
|
|
int EVP_MAC_names_do_all(const EVP_MAC *mac,
|
|
|
|
void (*fn)(const char *name, void *data),
|
|
|
|
void *data)
|
2019-09-23 16:56:13 +08:00
|
|
|
{
|
|
|
|
if (mac->prov != NULL)
|
2021-02-20 01:03:43 +08:00
|
|
|
return evp_names_do_all(mac->prov, mac->name_id, fn, data);
|
|
|
|
|
|
|
|
return 1;
|
2019-09-23 16:56:13 +08:00
|
|
|
}
|
2021-03-30 01:42:33 +08:00
|
|
|
|
|
|
|
unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
|
|
|
|
const char *subalg, const OSSL_PARAM *params,
|
|
|
|
const void *key, size_t keylen,
|
|
|
|
const unsigned char *data, size_t datalen,
|
|
|
|
unsigned char *out, size_t outsize, unsigned int *outlen)
|
|
|
|
{
|
|
|
|
EVP_MAC *mac = EVP_MAC_fetch(libctx, name, propq);
|
|
|
|
OSSL_PARAM subalg_param[] = { OSSL_PARAM_END, OSSL_PARAM_END };
|
|
|
|
EVP_MAC_CTX *ctx = NULL;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *res = NULL;
|
|
|
|
|
|
|
|
if (outlen != NULL)
|
|
|
|
*outlen = 0;
|
|
|
|
if (mac == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (subalg != NULL) {
|
|
|
|
const OSSL_PARAM *defined_params = EVP_MAC_settable_ctx_params(mac);
|
|
|
|
const char *param_name = OSSL_MAC_PARAM_DIGEST;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The underlying algorithm may be a cipher or a digest.
|
|
|
|
* We don't know which it is, but we can ask the MAC what it
|
|
|
|
* should be and bet on that.
|
|
|
|
*/
|
|
|
|
if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
|
|
|
|
param_name = OSSL_MAC_PARAM_CIPHER;
|
|
|
|
if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subalg_param[0] =
|
|
|
|
OSSL_PARAM_construct_utf8_string(param_name, (char *)subalg, 0);
|
|
|
|
}
|
|
|
|
/* Single-shot - on NULL key input, set dummy key value for EVP_MAC_Init. */
|
|
|
|
if (key == NULL && keylen == 0)
|
|
|
|
key = data;
|
|
|
|
if ((ctx = EVP_MAC_CTX_new(mac)) != NULL
|
|
|
|
&& EVP_MAC_CTX_set_params(ctx, subalg_param)
|
|
|
|
&& EVP_MAC_CTX_set_params(ctx, params)
|
|
|
|
&& EVP_MAC_init(ctx, key, keylen, params)
|
|
|
|
&& EVP_MAC_update(ctx, data, datalen)
|
|
|
|
&& EVP_MAC_final(ctx, out, &len, outsize)) {
|
|
|
|
if (out == NULL) {
|
|
|
|
out = OPENSSL_malloc(len);
|
|
|
|
if (out != NULL && !EVP_MAC_final(ctx, out, NULL, len)) {
|
|
|
|
OPENSSL_free(out);
|
|
|
|
out = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = out;
|
|
|
|
if (res != NULL && outlen != NULL)
|
|
|
|
*outlen = (unsigned int)len;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
EVP_MAC_CTX_free(ctx);
|
|
|
|
EVP_MAC_free(mac);
|
|
|
|
return res;
|
|
|
|
}
|