2 * Copyright 2018-2022 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 "prov/implementations.h"
25 #include "prov/provider_ctx.h"
26 #include "prov/provider_util.h"
27 #include "prov/providercommon.h"
30 * Forward declaration of everything implemented here. This is not strictly
31 * necessary for the compiler, but provides an assurance that the signatures
32 * of the functions in the dispatch table are correct.
34 static OSSL_FUNC_mac_newctx_fn hmac_new;
35 static OSSL_FUNC_mac_dupctx_fn hmac_dup;
36 static OSSL_FUNC_mac_freectx_fn hmac_free;
37 static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
38 static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params;
39 static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params;
40 static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params;
41 static OSSL_FUNC_mac_init_fn hmac_init;
42 static OSSL_FUNC_mac_update_fn hmac_update;
43 static OSSL_FUNC_mac_final_fn hmac_final;
45 /* local HMAC context structure */
47 /* typedef EVP_MAC_IMPL */
50 HMAC_CTX *ctx; /* HMAC context */
54 /* Length of full TLS record including the MAC and any padding */
56 unsigned char tls_header[13];
58 unsigned char tls_mac_out[EVP_MAX_MD_SIZE];
59 size_t tls_mac_out_size;
62 /* Defined in ssl/s3_cbc.c */
63 int ssl3_cbc_digest_record(const EVP_MD *md,
64 unsigned char *md_out,
66 const unsigned char header[13],
67 const unsigned char *data,
69 size_t data_plus_mac_plus_padding_size,
70 const unsigned char *mac_secret,
71 size_t mac_secret_length, char is_sslv3);
73 static void *hmac_new(void *provctx)
75 struct hmac_data_st *macctx;
77 if (!ossl_prov_is_running())
80 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
81 || (macctx->ctx = HMAC_CTX_new()) == NULL) {
85 macctx->provctx = provctx;
90 static void hmac_free(void *vmacctx)
92 struct hmac_data_st *macctx = vmacctx;
95 HMAC_CTX_free(macctx->ctx);
96 ossl_prov_digest_reset(&macctx->digest);
97 OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
102 static void *hmac_dup(void *vsrc)
104 struct hmac_data_st *src = vsrc;
105 struct hmac_data_st *dst;
108 if (!ossl_prov_is_running())
110 dst = hmac_new(src->provctx);
119 if (!HMAC_CTX_copy(dst->ctx, src->ctx)
120 || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
124 if (src->key != NULL) {
125 /* There is no "secure" OPENSSL_memdup */
126 dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1);
127 if (dst->key == NULL) {
131 memcpy(dst->key, src->key, src->keylen);
136 static size_t hmac_size(struct hmac_data_st *macctx)
138 return HMAC_size(macctx->ctx);
141 static int hmac_block_size(struct hmac_data_st *macctx)
143 const EVP_MD *md = ossl_prov_digest_md(&macctx->digest);
147 return EVP_MD_block_size(md);
150 static int hmac_setkey(struct hmac_data_st *macctx,
151 const unsigned char *key, size_t keylen)
153 const EVP_MD *digest;
155 if (macctx->key != NULL)
156 OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
157 /* Keep a copy of the key in case we need it for TLS HMAC */
158 macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1);
159 if (macctx->key == NULL)
161 memcpy(macctx->key, key, keylen);
162 macctx->keylen = keylen;
164 digest = ossl_prov_digest_md(&macctx->digest);
165 /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
166 if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL))
167 return HMAC_Init_ex(macctx->ctx, key, keylen, digest,
168 ossl_prov_digest_engine(&macctx->digest));
172 static int hmac_init(void *vmacctx, const unsigned char *key,
173 size_t keylen, const OSSL_PARAM params[])
175 struct hmac_data_st *macctx = vmacctx;
177 if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params))
181 return hmac_setkey(macctx, key, keylen);
183 /* Just reinit the HMAC context */
184 return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL);
187 static int hmac_update(void *vmacctx, const unsigned char *data,
190 struct hmac_data_st *macctx = vmacctx;
192 if (macctx->tls_data_size > 0) {
193 /* We're doing a TLS HMAC */
194 if (!macctx->tls_header_set) {
195 /* We expect the first update call to contain the TLS header */
196 if (datalen != sizeof(macctx->tls_header))
198 memcpy(macctx->tls_header, data, datalen);
199 macctx->tls_header_set = 1;
202 /* macctx->tls_data_size is datalen plus the padding length */
203 if (macctx->tls_data_size < datalen)
206 return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest),
208 &macctx->tls_mac_out_size,
212 macctx->tls_data_size,
218 return HMAC_Update(macctx->ctx, data, datalen);
221 static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
225 struct hmac_data_st *macctx = vmacctx;
227 if (!ossl_prov_is_running())
229 if (macctx->tls_data_size > 0) {
230 if (macctx->tls_mac_out_size == 0)
233 *outl = macctx->tls_mac_out_size;
234 memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size);
237 if (!HMAC_Final(macctx->ctx, out, &hlen))
243 static const OSSL_PARAM known_gettable_ctx_params[] = {
244 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
245 OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
248 static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx,
249 ossl_unused void *provctx)
251 return known_gettable_ctx_params;
254 static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
256 struct hmac_data_st *macctx = vmacctx;
259 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
260 && !OSSL_PARAM_set_size_t(p, hmac_size(macctx)))
263 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
264 && !OSSL_PARAM_set_int(p, hmac_block_size(macctx)))
270 static const OSSL_PARAM known_settable_ctx_params[] = {
271 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
272 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
273 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
274 OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL),
275 OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL),
276 OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL),
279 static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx,
280 ossl_unused void *provctx)
282 return known_settable_ctx_params;
285 static int set_flag(const OSSL_PARAM params[], const char *key, int mask,
288 const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, key);
292 if (!OSSL_PARAM_get_int(p, &flag))
303 * ALL parameters should be set before init().
305 static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
307 struct hmac_data_st *macctx = vmacctx;
308 OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
315 if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
318 if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_NOINIT, EVP_MD_CTX_FLAG_NO_INIT,
321 if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_ONESHOT, EVP_MD_CTX_FLAG_ONESHOT,
325 HMAC_CTX_set_flags(macctx->ctx, flags);
327 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
328 if (p->data_type != OSSL_PARAM_OCTET_STRING)
330 if (!hmac_setkey(macctx, p->data, p->data_size))
334 if ((p = OSSL_PARAM_locate_const(params,
335 OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) {
336 if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size))
342 const OSSL_DISPATCH ossl_hmac_functions[] = {
343 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
344 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
345 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
346 { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
347 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
348 { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
349 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
350 (void (*)(void))hmac_gettable_ctx_params },
351 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
352 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
353 (void (*)(void))hmac_settable_ctx_params },
354 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },