2 * Copyright 2018 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
10 #include <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/params.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/hmac.h>
17 #include "internal/provider_algs.h"
18 #include "internal/provider_ctx.h"
21 * Forward declaration of everything implemented here. This is not strictly
22 * necessary for the compiler, but provides an assurance that the signatures
23 * of the functions in the dispatch table are correct.
25 static OSSL_OP_mac_newctx_fn hmac_new
;
26 static OSSL_OP_mac_dupctx_fn hmac_dup
;
27 static OSSL_OP_mac_freectx_fn hmac_free
;
28 static OSSL_OP_mac_gettable_ctx_params_fn hmac_gettable_ctx_params
;
29 static OSSL_OP_mac_ctx_get_params_fn hmac_ctx_get_params
;
30 static OSSL_OP_mac_settable_ctx_params_fn hmac_settable_ctx_params
;
31 static OSSL_OP_mac_ctx_set_params_fn hmac_ctx_set_params
;
32 static OSSL_OP_mac_init_fn hmac_init
;
33 static OSSL_OP_mac_update_fn hmac_update
;
34 static OSSL_OP_mac_final_fn hmac_final
;
36 /* local HMAC context structure */
38 /* typedef EVP_MAC_IMPL */
41 HMAC_CTX
*ctx
; /* HMAC context */
44 * References to the underlying digest implementation. tmpmd caches
45 * the md, always. alloc_md only holds a reference to an explicitly
47 * tmpmd is cleared after a CMAC_Init call.
49 const EVP_MD
*tmpmd
; /* HMAC digest */
50 EVP_MD
*alloc_md
; /* fetched digest */
53 * Conditions for legacy EVP_MD uses.
54 * tmpengine is cleared after a CMAC_Init call.
56 ENGINE
*tmpengine
; /* HMAC digest engine */
59 static size_t hmac_size(void *vmacctx
);
61 static void *hmac_new(void *provctx
)
63 struct hmac_data_st
*macctx
;
65 if ((macctx
= OPENSSL_zalloc(sizeof(*macctx
))) == NULL
66 || (macctx
->ctx
= HMAC_CTX_new()) == NULL
) {
70 /* TODO(3.0) Should we do something more with that context? */
71 macctx
->provctx
= provctx
;
76 static void hmac_free(void *vmacctx
)
78 struct hmac_data_st
*macctx
= vmacctx
;
81 HMAC_CTX_free(macctx
->ctx
);
82 EVP_MD_meth_free(macctx
->alloc_md
);
87 static void *hmac_dup(void *vsrc
)
89 struct hmac_data_st
*src
= vsrc
;
90 struct hmac_data_st
*dst
= hmac_new(src
->provctx
);
95 if (!HMAC_CTX_copy(dst
->ctx
, src
->ctx
)) {
100 if (src
->alloc_md
!= NULL
&& !EVP_MD_up_ref(src
->alloc_md
)) {
105 dst
->tmpengine
= src
->tmpengine
;
106 dst
->tmpmd
= src
->tmpmd
;
107 dst
->alloc_md
= src
->alloc_md
;
111 static size_t hmac_size(void *vmacctx
)
113 struct hmac_data_st
*macctx
= vmacctx
;
115 return HMAC_size(macctx
->ctx
);
118 static int hmac_init(void *vmacctx
)
120 struct hmac_data_st
*macctx
= vmacctx
;
123 /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
124 if (macctx
->tmpmd
!= NULL
)
125 rv
= HMAC_Init_ex(macctx
->ctx
, NULL
, 0, macctx
->tmpmd
,
126 (ENGINE
* )macctx
->tmpengine
);
127 macctx
->tmpengine
= NULL
;
128 macctx
->tmpmd
= NULL
;
132 static int hmac_update(void *vmacctx
, const unsigned char *data
,
135 struct hmac_data_st
*macctx
= vmacctx
;
137 return HMAC_Update(macctx
->ctx
, data
, datalen
);
140 static int hmac_final(void *vmacctx
, unsigned char *out
, size_t *outl
,
144 struct hmac_data_st
*macctx
= vmacctx
;
146 if (!HMAC_Final(macctx
->ctx
, out
, &hlen
))
153 static const OSSL_PARAM known_gettable_ctx_params
[] = {
154 OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN
, NULL
),
155 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE
, NULL
), /* Same as "outlen" */
158 static const OSSL_PARAM
*hmac_gettable_ctx_params(void)
160 return known_gettable_ctx_params
;
163 static int hmac_ctx_get_params(void *vmacctx
, OSSL_PARAM params
[])
167 if ((p
= OSSL_PARAM_locate(params
, OSSL_MAC_PARAM_OUTLEN
)) != NULL
168 || (p
= OSSL_PARAM_locate(params
, OSSL_MAC_PARAM_SIZE
)) != NULL
)
169 return OSSL_PARAM_set_size_t(p
, hmac_size(vmacctx
));
174 static const OSSL_PARAM known_settable_ctx_params
[] = {
175 /* "algorithm" and "digest" are the same parameter */
176 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ALGORITHM
, NULL
, 0),
177 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST
, NULL
, 0),
178 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE
, NULL
, 0),
179 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES
, NULL
, 0),
180 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY
, NULL
, 0),
181 OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS
, NULL
),
184 static const OSSL_PARAM
*hmac_settable_ctx_params(void)
186 return known_settable_ctx_params
;
190 * ALL parameters should be set before init().
192 static int hmac_ctx_set_params(void *vmacctx
, const OSSL_PARAM params
[])
194 struct hmac_data_st
*macctx
= vmacctx
;
197 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_DIGEST
)) != NULL
198 || (p
= OSSL_PARAM_locate_const(params
,
199 OSSL_MAC_PARAM_ALGORITHM
)) != NULL
) {
200 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
204 const char *algoname
= p
->data
;
205 const char *propquery
= NULL
;
207 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */
208 ENGINE_finish(macctx
->tmpengine
);
209 macctx
->tmpengine
= NULL
;
211 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_ENGINE
))
213 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
216 macctx
->tmpengine
= ENGINE_by_id(p
->data
);
217 if (macctx
->tmpengine
== NULL
)
221 if ((p
= OSSL_PARAM_locate_const(params
,
222 OSSL_MAC_PARAM_PROPERTIES
))
224 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
230 EVP_MD_meth_free(macctx
->alloc_md
);
232 macctx
->tmpmd
= macctx
->alloc_md
=
233 EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(macctx
->provctx
),
234 algoname
, propquery
);
236 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
237 /* TODO(3.0) BEGIN legacy stuff, to be removed */
238 if (macctx
->tmpmd
== NULL
)
239 macctx
->tmpmd
= EVP_get_digestbyname(algoname
);
240 /* TODO(3.0) END of legacy stuff */
243 if (macctx
->tmpmd
== NULL
)
247 /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */
248 if ((p
= OSSL_PARAM_locate_const(params
,
249 OSSL_MAC_PARAM_FLAGS
)) != NULL
) {
252 if (!OSSL_PARAM_get_int(p
, &flags
))
254 HMAC_CTX_set_flags(macctx
->ctx
, flags
);
256 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_KEY
)) != NULL
) {
257 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
)
260 if (!HMAC_Init_ex(macctx
->ctx
, p
->data
, p
->data_size
,
261 macctx
->tmpmd
, NULL
/* ENGINE */))
264 macctx
->tmpmd
= NULL
;
265 macctx
->tmpengine
= NULL
;
270 const OSSL_DISPATCH hmac_functions
[] = {
271 { OSSL_FUNC_MAC_NEWCTX
, (void (*)(void))hmac_new
},
272 { OSSL_FUNC_MAC_DUPCTX
, (void (*)(void))hmac_dup
},
273 { OSSL_FUNC_MAC_FREECTX
, (void (*)(void))hmac_free
},
274 { OSSL_FUNC_MAC_INIT
, (void (*)(void))hmac_init
},
275 { OSSL_FUNC_MAC_UPDATE
, (void (*)(void))hmac_update
},
276 { OSSL_FUNC_MAC_FINAL
, (void (*)(void))hmac_final
},
277 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS
,
278 (void (*)(void))hmac_gettable_ctx_params
},
279 { OSSL_FUNC_MAC_CTX_GET_PARAMS
, (void (*)(void))hmac_ctx_get_params
},
280 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS
,
281 (void (*)(void))hmac_settable_ctx_params
},
282 { OSSL_FUNC_MAC_CTX_SET_PARAMS
, (void (*)(void))hmac_ctx_set_params
},