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"
19 #include "internal/provider_util.h"
22 * Forward declaration of everything implemented here. This is not strictly
23 * necessary for the compiler, but provides an assurance that the signatures
24 * of the functions in the dispatch table are correct.
26 static OSSL_OP_mac_newctx_fn hmac_new
;
27 static OSSL_OP_mac_dupctx_fn hmac_dup
;
28 static OSSL_OP_mac_freectx_fn hmac_free
;
29 static OSSL_OP_mac_gettable_ctx_params_fn hmac_gettable_ctx_params
;
30 static OSSL_OP_mac_get_ctx_params_fn hmac_get_ctx_params
;
31 static OSSL_OP_mac_settable_ctx_params_fn hmac_settable_ctx_params
;
32 static OSSL_OP_mac_set_ctx_params_fn hmac_set_ctx_params
;
33 static OSSL_OP_mac_init_fn hmac_init
;
34 static OSSL_OP_mac_update_fn hmac_update
;
35 static OSSL_OP_mac_final_fn hmac_final
;
37 /* local HMAC context structure */
39 /* typedef EVP_MAC_IMPL */
42 HMAC_CTX
*ctx
; /* HMAC context */
46 static size_t hmac_size(void *vmacctx
);
48 static void *hmac_new(void *provctx
)
50 struct hmac_data_st
*macctx
;
52 if ((macctx
= OPENSSL_zalloc(sizeof(*macctx
))) == NULL
53 || (macctx
->ctx
= HMAC_CTX_new()) == NULL
) {
57 /* TODO(3.0) Should we do something more with that context? */
58 macctx
->provctx
= provctx
;
63 static void hmac_free(void *vmacctx
)
65 struct hmac_data_st
*macctx
= vmacctx
;
68 HMAC_CTX_free(macctx
->ctx
);
69 ossl_prov_digest_reset(&macctx
->digest
);
74 static void *hmac_dup(void *vsrc
)
76 struct hmac_data_st
*src
= vsrc
;
77 struct hmac_data_st
*dst
= hmac_new(src
->provctx
);
82 if (!HMAC_CTX_copy(dst
->ctx
, src
->ctx
)
83 || !ossl_prov_digest_copy(&dst
->digest
, &src
->digest
)) {
90 static size_t hmac_size(void *vmacctx
)
92 struct hmac_data_st
*macctx
= vmacctx
;
94 return HMAC_size(macctx
->ctx
);
97 static int hmac_init(void *vmacctx
)
99 struct hmac_data_st
*macctx
= vmacctx
;
100 const EVP_MD
*digest
= ossl_prov_digest_md(&macctx
->digest
);
103 /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
105 rv
= HMAC_Init_ex(macctx
->ctx
, NULL
, 0, digest
,
106 ossl_prov_digest_engine(&macctx
->digest
));
107 ossl_prov_digest_reset(&macctx
->digest
);
111 static int hmac_update(void *vmacctx
, const unsigned char *data
,
114 struct hmac_data_st
*macctx
= vmacctx
;
116 return HMAC_Update(macctx
->ctx
, data
, datalen
);
119 static int hmac_final(void *vmacctx
, unsigned char *out
, size_t *outl
,
123 struct hmac_data_st
*macctx
= vmacctx
;
125 if (!HMAC_Final(macctx
->ctx
, out
, &hlen
))
132 static const OSSL_PARAM known_gettable_ctx_params
[] = {
133 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE
, NULL
),
136 static const OSSL_PARAM
*hmac_gettable_ctx_params(void)
138 return known_gettable_ctx_params
;
141 static int hmac_get_ctx_params(void *vmacctx
, OSSL_PARAM params
[])
145 if ((p
= OSSL_PARAM_locate(params
, OSSL_MAC_PARAM_SIZE
)) != NULL
)
146 return OSSL_PARAM_set_size_t(p
, hmac_size(vmacctx
));
151 static const OSSL_PARAM known_settable_ctx_params
[] = {
152 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST
, NULL
, 0),
153 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES
, NULL
, 0),
154 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY
, NULL
, 0),
155 OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS
, NULL
),
158 static const OSSL_PARAM
*hmac_settable_ctx_params(void)
160 return known_settable_ctx_params
;
164 * ALL parameters should be set before init().
166 static int hmac_set_ctx_params(void *vmacctx
, const OSSL_PARAM params
[])
168 struct hmac_data_st
*macctx
= vmacctx
;
169 OPENSSL_CTX
*ctx
= PROV_LIBRARY_CONTEXT_OF(macctx
->provctx
);
172 if (!ossl_prov_digest_load_from_params(&macctx
->digest
, params
, ctx
))
175 /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */
176 if ((p
= OSSL_PARAM_locate_const(params
,
177 OSSL_MAC_PARAM_FLAGS
)) != NULL
) {
180 if (!OSSL_PARAM_get_int(p
, &flags
))
182 HMAC_CTX_set_flags(macctx
->ctx
, flags
);
184 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_KEY
)) != NULL
) {
185 if (p
->data_type
!= OSSL_PARAM_OCTET_STRING
)
188 if (!HMAC_Init_ex(macctx
->ctx
, p
->data
, p
->data_size
,
189 ossl_prov_digest_md(&macctx
->digest
),
193 ossl_prov_digest_reset(&macctx
->digest
);
198 const OSSL_DISPATCH hmac_functions
[] = {
199 { OSSL_FUNC_MAC_NEWCTX
, (void (*)(void))hmac_new
},
200 { OSSL_FUNC_MAC_DUPCTX
, (void (*)(void))hmac_dup
},
201 { OSSL_FUNC_MAC_FREECTX
, (void (*)(void))hmac_free
},
202 { OSSL_FUNC_MAC_INIT
, (void (*)(void))hmac_init
},
203 { OSSL_FUNC_MAC_UPDATE
, (void (*)(void))hmac_update
},
204 { OSSL_FUNC_MAC_FINAL
, (void (*)(void))hmac_final
},
205 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS
,
206 (void (*)(void))hmac_gettable_ctx_params
},
207 { OSSL_FUNC_MAC_GET_CTX_PARAMS
, (void (*)(void))hmac_get_ctx_params
},
208 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS
,
209 (void (*)(void))hmac_settable_ctx_params
},
210 { OSSL_FUNC_MAC_SET_CTX_PARAMS
, (void (*)(void))hmac_set_ctx_params
},