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