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_get_ctx_params_fn hmac_get_ctx_params
;
30 static OSSL_OP_mac_settable_ctx_params_fn hmac_settable_ctx_params
;
31 static OSSL_OP_mac_set_ctx_params_fn hmac_set_ctx_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_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_SIZE
, NULL
),
157 static const OSSL_PARAM
*hmac_gettable_ctx_params(void)
159 return known_gettable_ctx_params
;
162 static int hmac_get_ctx_params(void *vmacctx
, OSSL_PARAM params
[])
166 if ((p
= OSSL_PARAM_locate(params
, OSSL_MAC_PARAM_SIZE
)) != NULL
)
167 return OSSL_PARAM_set_size_t(p
, hmac_size(vmacctx
));
172 static const OSSL_PARAM known_settable_ctx_params
[] = {
173 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST
, NULL
, 0),
174 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE
, NULL
, 0),
175 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES
, NULL
, 0),
176 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY
, NULL
, 0),
177 OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS
, NULL
),
180 static const OSSL_PARAM
*hmac_settable_ctx_params(void)
182 return known_settable_ctx_params
;
186 * ALL parameters should be set before init().
188 static int hmac_set_ctx_params(void *vmacctx
, const OSSL_PARAM params
[])
190 struct hmac_data_st
*macctx
= vmacctx
;
193 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_DIGEST
)) != NULL
) {
194 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
198 const char *algoname
= p
->data
;
199 const char *propquery
= NULL
;
201 /* Inside the FIPS module, we don't support engines */
202 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
203 ENGINE_finish(macctx
->tmpengine
);
204 macctx
->tmpengine
= NULL
;
206 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_ENGINE
))
208 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
211 macctx
->tmpengine
= ENGINE_by_id(p
->data
);
212 if (macctx
->tmpengine
== NULL
)
216 if ((p
= OSSL_PARAM_locate_const(params
,
217 OSSL_MAC_PARAM_PROPERTIES
))
219 if (p
->data_type
!= OSSL_PARAM_UTF8_STRING
)
225 EVP_MD_free(macctx
->alloc_md
);
227 macctx
->tmpmd
= macctx
->alloc_md
=
228 EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(macctx
->provctx
),
229 algoname
, propquery
);
231 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
232 /* TODO(3.0) BEGIN legacy stuff, to be removed */
233 if (macctx
->tmpmd
== NULL
)
234 macctx
->tmpmd
= EVP_get_digestbyname(algoname
);
235 /* TODO(3.0) END of legacy stuff */
238 if (macctx
->tmpmd
== NULL
)
242 /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */
243 if ((p
= OSSL_PARAM_locate_const(params
,
244 OSSL_MAC_PARAM_FLAGS
)) != NULL
) {
247 if (!OSSL_PARAM_get_int(p
, &flags
))
249 HMAC_CTX_set_flags(macctx
->ctx
, flags
);
251 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_KEY
)) != NULL
) {
252 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
)
255 if (!HMAC_Init_ex(macctx
->ctx
, p
->data
, p
->data_size
,
256 macctx
->tmpmd
, NULL
/* ENGINE */))
259 macctx
->tmpmd
= NULL
;
260 macctx
->tmpengine
= NULL
;
265 const OSSL_DISPATCH hmac_functions
[] = {
266 { OSSL_FUNC_MAC_NEWCTX
, (void (*)(void))hmac_new
},
267 { OSSL_FUNC_MAC_DUPCTX
, (void (*)(void))hmac_dup
},
268 { OSSL_FUNC_MAC_FREECTX
, (void (*)(void))hmac_free
},
269 { OSSL_FUNC_MAC_INIT
, (void (*)(void))hmac_init
},
270 { OSSL_FUNC_MAC_UPDATE
, (void (*)(void))hmac_update
},
271 { OSSL_FUNC_MAC_FINAL
, (void (*)(void))hmac_final
},
272 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS
,
273 (void (*)(void))hmac_gettable_ctx_params
},
274 { OSSL_FUNC_MAC_GET_CTX_PARAMS
, (void (*)(void))hmac_get_ctx_params
},
275 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS
,
276 (void (*)(void))hmac_settable_ctx_params
},
277 { OSSL_FUNC_MAC_SET_CTX_PARAMS
, (void (*)(void))hmac_set_ctx_params
},