2 * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * HMAC low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/params.h>
21 #include <openssl/evp.h>
22 #include <openssl/hmac.h>
24 #include "internal/ssl3_cbc.h"
26 #include "prov/implementations.h"
27 #include "prov/provider_ctx.h"
28 #include "prov/provider_util.h"
29 #include "prov/providercommon.h"
32 * Forward declaration of everything implemented here. This is not strictly
33 * necessary for the compiler, but provides an assurance that the signatures
34 * of the functions in the dispatch table are correct.
36 static OSSL_FUNC_mac_newctx_fn hmac_new;
37 static OSSL_FUNC_mac_dupctx_fn hmac_dup;
38 static OSSL_FUNC_mac_freectx_fn hmac_free;
39 static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
40 static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params;
41 static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params;
42 static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params;
43 static OSSL_FUNC_mac_init_fn hmac_init;
44 static OSSL_FUNC_mac_update_fn hmac_update;
45 static OSSL_FUNC_mac_final_fn hmac_final;
47 /* local HMAC context structure */
49 /* typedef EVP_MAC_IMPL */
52 HMAC_CTX *ctx; /* HMAC context */
56 /* Length of full TLS record including the MAC and any padding */
58 unsigned char tls_header[13];
60 unsigned char tls_mac_out[EVP_MAX_MD_SIZE];
61 size_t tls_mac_out_size;
64 static void *hmac_new(void *provctx)
66 struct hmac_data_st *macctx;
68 if (!ossl_prov_is_running())
71 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
72 || (macctx->ctx = HMAC_CTX_new()) == NULL) {
76 macctx->provctx = provctx;
81 static void hmac_free(void *vmacctx)
83 struct hmac_data_st *macctx = vmacctx;
86 HMAC_CTX_free(macctx->ctx);
87 ossl_prov_digest_reset(&macctx->digest);
88 OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
93 static void *hmac_dup(void *vsrc)
95 struct hmac_data_st *src = vsrc;
96 struct hmac_data_st *dst;
99 if (!ossl_prov_is_running())
101 dst = hmac_new(src->provctx);
109 memset(&dst->digest, 0, sizeof(dst->digest));
111 if (!HMAC_CTX_copy(dst->ctx, src->ctx)
112 || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
116 if (src->key != NULL) {
117 /* There is no "secure" OPENSSL_memdup */
118 dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1);
119 if (dst->key == NULL) {
123 memcpy(dst->key, src->key, src->keylen);
128 static size_t hmac_size(struct hmac_data_st *macctx)
130 return HMAC_size(macctx->ctx);
133 static int hmac_block_size(struct hmac_data_st *macctx)
135 const EVP_MD *md = ossl_prov_digest_md(&macctx->digest);
139 return EVP_MD_block_size(md);
142 static int hmac_setkey(struct hmac_data_st *macctx,
143 const unsigned char *key, size_t keylen)
145 const EVP_MD *digest;
147 if (macctx->key != NULL)
148 OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
149 /* Keep a copy of the key in case we need it for TLS HMAC */
150 macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1);
151 if (macctx->key == NULL)
153 memcpy(macctx->key, key, keylen);
154 macctx->keylen = keylen;
156 digest = ossl_prov_digest_md(&macctx->digest);
157 /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
158 if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL))
159 return HMAC_Init_ex(macctx->ctx, key, keylen, digest,
160 ossl_prov_digest_engine(&macctx->digest));
164 static int hmac_init(void *vmacctx, const unsigned char *key,
165 size_t keylen, const OSSL_PARAM params[])
167 struct hmac_data_st *macctx = vmacctx;
169 if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params))
173 return hmac_setkey(macctx, key, keylen);
175 /* Just reinit the HMAC context */
176 return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL);
179 static int hmac_update(void *vmacctx, const unsigned char *data,
182 struct hmac_data_st *macctx = vmacctx;
184 if (macctx->tls_data_size > 0) {
185 /* We're doing a TLS HMAC */
186 if (!macctx->tls_header_set) {
187 /* We expect the first update call to contain the TLS header */
188 if (datalen != sizeof(macctx->tls_header))
190 memcpy(macctx->tls_header, data, datalen);
191 macctx->tls_header_set = 1;
194 /* macctx->tls_data_size is datalen plus the padding length */
195 if (macctx->tls_data_size < datalen)
198 return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest),
200 &macctx->tls_mac_out_size,
204 macctx->tls_data_size,
210 return HMAC_Update(macctx->ctx, data, datalen);
213 static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
217 struct hmac_data_st *macctx = vmacctx;
219 if (!ossl_prov_is_running())
221 if (macctx->tls_data_size > 0) {
222 if (macctx->tls_mac_out_size == 0)
225 *outl = macctx->tls_mac_out_size;
226 memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size);
229 if (!HMAC_Final(macctx->ctx, out, &hlen))
235 static const OSSL_PARAM known_gettable_ctx_params[] = {
236 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
237 OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
240 static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx,
241 ossl_unused void *provctx)
243 return known_gettable_ctx_params;
246 static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
248 struct hmac_data_st *macctx = vmacctx;
251 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
252 && !OSSL_PARAM_set_size_t(p, hmac_size(macctx)))
255 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
256 && !OSSL_PARAM_set_int(p, hmac_block_size(macctx)))
262 static const OSSL_PARAM known_settable_ctx_params[] = {
263 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
264 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
265 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
266 OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL),
267 OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL),
268 OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL),
271 static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx,
272 ossl_unused void *provctx)
274 return known_settable_ctx_params;
278 * ALL parameters should be set before init().
280 static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
282 struct hmac_data_st *macctx = vmacctx;
283 OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
289 if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
292 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
293 if (p->data_type != OSSL_PARAM_OCTET_STRING)
295 if (!hmac_setkey(macctx, p->data, p->data_size))
299 if ((p = OSSL_PARAM_locate_const(params,
300 OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) {
301 if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size))
307 const OSSL_DISPATCH ossl_hmac_functions[] = {
308 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
309 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
310 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
311 { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
312 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
313 { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
314 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
315 (void (*)(void))hmac_gettable_ctx_params },
316 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
317 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
318 (void (*)(void))hmac_settable_ctx_params },
319 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },