2018-10-13 04:27:18 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
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>
|
2018-10-13 04:27:18 +08:00
|
|
|
#include <openssl/ossl_typ.h>
|
|
|
|
#include "internal/nelem.h"
|
|
|
|
#include "internal/evp_int.h"
|
2019-05-07 18:39:58 +08:00
|
|
|
#include "internal/provider.h"
|
2018-10-13 04:27:18 +08:00
|
|
|
#include "evp_locl.h"
|
|
|
|
|
2019-05-07 18:39:58 +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
|
|
|
|
|| (ctx->data = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL
|
|
|
|
|| !EVP_MAC_up_ref(mac)) {
|
2018-10-13 04:27:18 +08:00
|
|
|
EVPerr(EVP_F_EVP_MAC_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
2019-05-07 18:39:58 +08:00
|
|
|
if (ctx != NULL)
|
|
|
|
mac->freectx(ctx->data);
|
2018-10-13 04:27:18 +08:00
|
|
|
OPENSSL_free(ctx);
|
|
|
|
ctx = NULL;
|
|
|
|
} else {
|
|
|
|
ctx->meth = mac;
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx)
|
|
|
|
{
|
2019-05-07 18:39:58 +08:00
|
|
|
if (ctx != NULL) {
|
|
|
|
ctx->meth->freectx(ctx->data);
|
2018-10-13 04:27:18 +08:00
|
|
|
ctx->data = NULL;
|
2019-05-07 18:39:58 +08:00
|
|
|
/* refcnt-- */
|
|
|
|
EVP_MAC_free(ctx->meth);
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
|
2018-11-05 02:16:20 +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
|
|
|
|
2018-12-19 07:36:40 +08:00
|
|
|
if (src->data == NULL)
|
2018-11-05 02:16:20 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dst = OPENSSL_malloc(sizeof(*dst));
|
|
|
|
if (dst == NULL) {
|
|
|
|
EVPerr(EVP_F_EVP_MAC_CTX_DUP, ERR_R_MALLOC_FAILURE);
|
|
|
|
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)) {
|
|
|
|
EVPerr(EVP_F_EVP_MAC_CTX_DUP, ERR_R_MALLOC_FAILURE);
|
|
|
|
OPENSSL_free(dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-13 04:27:18 +08:00
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
dst->data = src->meth->dupctx(src->data);
|
2018-11-05 02:16:20 +08:00
|
|
|
if (dst->data == NULL) {
|
|
|
|
EVP_MAC_CTX_free(dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
|
|
|
return ctx->meth;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t EVP_MAC_size(EVP_MAC_CTX *ctx)
|
|
|
|
{
|
2019-05-07 18:39:58 +08:00
|
|
|
size_t sz = 0;
|
|
|
|
|
|
|
|
if (ctx->data != NULL) {
|
|
|
|
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) {
|
|
|
|
if (ctx->meth->get_ctx_params(ctx->data, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_MAC_init(EVP_MAC_CTX *ctx)
|
|
|
|
{
|
|
|
|
return ctx->meth->init(ctx->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
|
|
|
|
{
|
2019-03-26 23:25:15 +08:00
|
|
|
if (datalen == 0)
|
|
|
|
return 1;
|
2018-10-13 04:27:18 +08:00
|
|
|
return ctx->meth->update(ctx->data, data, datalen);
|
|
|
|
}
|
|
|
|
|
2019-05-07 18:39:58 +08:00
|
|
|
int EVP_MAC_final(EVP_MAC_CTX *ctx,
|
|
|
|
unsigned char *out, size_t *outl, size_t outsize)
|
2018-10-13 04:27:18 +08:00
|
|
|
{
|
2019-05-07 18:39:58 +08:00
|
|
|
int l = EVP_MAC_size(ctx);
|
2018-10-13 04:27:18 +08:00
|
|
|
|
|
|
|
if (l < 0)
|
|
|
|
return 0;
|
2019-05-07 18:39:58 +08:00
|
|
|
if (outl != NULL)
|
|
|
|
*outl = l;
|
2018-10-13 04:27:18 +08:00
|
|
|
if (out == NULL)
|
|
|
|
return 1;
|
2019-05-07 18:39:58 +08:00
|
|
|
return ctx->meth->final(ctx->data, out, outl, outsize);
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-07 18:39:58 +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)
|
|
|
|
return ctx->meth->get_ctx_params(ctx->data, params);
|
2019-05-07 18:39:58 +08:00
|
|
|
return 1;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 18:39:58 +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)
|
|
|
|
return ctx->meth->set_ctx_params(ctx->data, params);
|
2019-05-07 18:39:58 +08:00
|
|
|
return 1;
|
2018-10-13 04:27:18 +08:00
|
|
|
}
|