]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/hmac_prov.c
Rename <openssl/core_numbers.h> -> <openssl/core_dispatch.h>
[thirdparty/openssl.git] / providers / implementations / macs / hmac_prov.c
CommitLineData
5183ebdc 1/*
33388b44 2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
5183ebdc
RL
3 *
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
8 */
9
dbde4726
P
10/*
11 * HMAC low level APIs are deprecated for public use, but still ok for internal
12 * use.
13 */
14#include "internal/deprecated.h"
15
23c48d94 16#include <openssl/core_dispatch.h>
5183ebdc
RL
17#include <openssl/core_names.h>
18#include <openssl/params.h>
19#include <openssl/engine.h>
20#include <openssl/evp.h>
21#include <openssl/hmac.h>
22
af3e7e1b 23#include "prov/implementations.h"
ddd21319
RL
24#include "prov/provider_ctx.h"
25#include "prov/provider_util.h"
5183ebdc
RL
26
27/*
28 * Forward declaration of everything implemented here. This is not strictly
29 * necessary for the compiler, but provides an assurance that the signatures
30 * of the functions in the dispatch table are correct.
31 */
32static OSSL_OP_mac_newctx_fn hmac_new;
33static OSSL_OP_mac_dupctx_fn hmac_dup;
34static OSSL_OP_mac_freectx_fn hmac_free;
35static OSSL_OP_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
92d9d0ae 36static OSSL_OP_mac_get_ctx_params_fn hmac_get_ctx_params;
5183ebdc 37static OSSL_OP_mac_settable_ctx_params_fn hmac_settable_ctx_params;
92d9d0ae 38static OSSL_OP_mac_set_ctx_params_fn hmac_set_ctx_params;
5183ebdc
RL
39static OSSL_OP_mac_init_fn hmac_init;
40static OSSL_OP_mac_update_fn hmac_update;
41static OSSL_OP_mac_final_fn hmac_final;
42
43/* local HMAC context structure */
44
45/* typedef EVP_MAC_IMPL */
46struct hmac_data_st {
47 void *provctx;
48 HMAC_CTX *ctx; /* HMAC context */
103d8b0b 49 PROV_DIGEST digest;
5183ebdc
RL
50};
51
52static size_t hmac_size(void *vmacctx);
53
54static void *hmac_new(void *provctx)
55{
56 struct hmac_data_st *macctx;
57
58 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
59 || (macctx->ctx = HMAC_CTX_new()) == NULL) {
60 OPENSSL_free(macctx);
61 return NULL;
62 }
63 /* TODO(3.0) Should we do something more with that context? */
64 macctx->provctx = provctx;
65
66 return macctx;
67}
68
69static void hmac_free(void *vmacctx)
70{
71 struct hmac_data_st *macctx = vmacctx;
72
73 if (macctx != NULL) {
74 HMAC_CTX_free(macctx->ctx);
103d8b0b 75 ossl_prov_digest_reset(&macctx->digest);
5183ebdc
RL
76 OPENSSL_free(macctx);
77 }
78}
79
80static void *hmac_dup(void *vsrc)
81{
82 struct hmac_data_st *src = vsrc;
83 struct hmac_data_st *dst = hmac_new(src->provctx);
84
85 if (dst == NULL)
86 return NULL;
87
103d8b0b
P
88 if (!HMAC_CTX_copy(dst->ctx, src->ctx)
89 || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
5183ebdc
RL
90 hmac_free(dst);
91 return NULL;
92 }
5183ebdc
RL
93 return dst;
94}
95
96static size_t hmac_size(void *vmacctx)
97{
98 struct hmac_data_st *macctx = vmacctx;
99
100 return HMAC_size(macctx->ctx);
101}
102
103static int hmac_init(void *vmacctx)
104{
105 struct hmac_data_st *macctx = vmacctx;
103d8b0b 106 const EVP_MD *digest = ossl_prov_digest_md(&macctx->digest);
5183ebdc
RL
107 int rv = 1;
108
109 /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
103d8b0b
P
110 if (digest != NULL)
111 rv = HMAC_Init_ex(macctx->ctx, NULL, 0, digest,
112 ossl_prov_digest_engine(&macctx->digest));
113 ossl_prov_digest_reset(&macctx->digest);
5183ebdc
RL
114 return rv;
115}
116
117static int hmac_update(void *vmacctx, const unsigned char *data,
118 size_t datalen)
119{
120 struct hmac_data_st *macctx = vmacctx;
121
122 return HMAC_Update(macctx->ctx, data, datalen);
123}
124
125static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
126 size_t outsize)
127{
128 unsigned int hlen;
129 struct hmac_data_st *macctx = vmacctx;
130
131 if (!HMAC_Final(macctx->ctx, out, &hlen))
132 return 0;
133 if (outl != NULL)
134 *outl = hlen;
135 return 1;
136}
137
138static const OSSL_PARAM known_gettable_ctx_params[] = {
703170d4 139 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
5183ebdc
RL
140 OSSL_PARAM_END
141};
142static const OSSL_PARAM *hmac_gettable_ctx_params(void)
143{
144 return known_gettable_ctx_params;
145}
146
92d9d0ae 147static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
5183ebdc
RL
148{
149 OSSL_PARAM *p;
150
703170d4 151 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
5183ebdc
RL
152 return OSSL_PARAM_set_size_t(p, hmac_size(vmacctx));
153
154 return 1;
155}
156
157static const OSSL_PARAM known_settable_ctx_params[] = {
5183ebdc 158 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
5183ebdc
RL
159 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
160 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
161 OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS, NULL),
162 OSSL_PARAM_END
163};
164static const OSSL_PARAM *hmac_settable_ctx_params(void)
165{
166 return known_settable_ctx_params;
167}
168
169/*
170 * ALL parameters should be set before init().
171 */
92d9d0ae 172static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
5183ebdc
RL
173{
174 struct hmac_data_st *macctx = vmacctx;
103d8b0b 175 OPENSSL_CTX *ctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
5183ebdc
RL
176 const OSSL_PARAM *p;
177
103d8b0b
P
178 if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
179 return 0;
5183ebdc 180
5183ebdc
RL
181 /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */
182 if ((p = OSSL_PARAM_locate_const(params,
183 OSSL_MAC_PARAM_FLAGS)) != NULL) {
184 int flags = 0;
185
186 if (!OSSL_PARAM_get_int(p, &flags))
187 return 0;
188 HMAC_CTX_set_flags(macctx->ctx, flags);
189 }
190 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
191 if (p->data_type != OSSL_PARAM_OCTET_STRING)
192 return 0;
193
194 if (!HMAC_Init_ex(macctx->ctx, p->data, p->data_size,
103d8b0b
P
195 ossl_prov_digest_md(&macctx->digest),
196 NULL /* ENGINE */))
5183ebdc
RL
197 return 0;
198
103d8b0b 199 ossl_prov_digest_reset(&macctx->digest);
5183ebdc
RL
200 }
201 return 1;
202}
203
204const OSSL_DISPATCH hmac_functions[] = {
205 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
206 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
207 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
208 { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
209 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
210 { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
211 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
212 (void (*)(void))hmac_gettable_ctx_params },
92d9d0ae 213 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
5183ebdc
RL
214 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
215 (void (*)(void))hmac_settable_ctx_params },
92d9d0ae 216 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
5183ebdc
RL
217 { 0, NULL }
218};