Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/providers/implementations/kdfs/hmacdrbg_kdf.c
108604 views
1
/*
2
* Copyright 2022-2024 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 <stdlib.h>
11
#include <string.h>
12
#include <openssl/crypto.h>
13
#include <openssl/err.h>
14
#include <openssl/kdf.h>
15
#include <openssl/proverr.h>
16
#include <openssl/core_names.h>
17
#include "prov/providercommon.h"
18
#include "prov/implementations.h"
19
#include "prov/hmac_drbg.h"
20
#include "prov/provider_ctx.h"
21
22
static OSSL_FUNC_kdf_newctx_fn hmac_drbg_kdf_new;
23
static OSSL_FUNC_kdf_dupctx_fn hmac_drbg_kdf_dup;
24
static OSSL_FUNC_kdf_freectx_fn hmac_drbg_kdf_free;
25
static OSSL_FUNC_kdf_reset_fn hmac_drbg_kdf_reset;
26
static OSSL_FUNC_kdf_derive_fn hmac_drbg_kdf_derive;
27
static OSSL_FUNC_kdf_settable_ctx_params_fn hmac_drbg_kdf_settable_ctx_params;
28
static OSSL_FUNC_kdf_set_ctx_params_fn hmac_drbg_kdf_set_ctx_params;
29
static OSSL_FUNC_kdf_gettable_ctx_params_fn hmac_drbg_kdf_gettable_ctx_params;
30
static OSSL_FUNC_kdf_get_ctx_params_fn hmac_drbg_kdf_get_ctx_params;
31
32
typedef struct {
33
PROV_DRBG_HMAC base;
34
void *provctx;
35
unsigned char *entropy, *nonce;
36
size_t entropylen, noncelen;
37
int init;
38
} KDF_HMAC_DRBG;
39
40
static void *hmac_drbg_kdf_new(void *provctx)
41
{
42
KDF_HMAC_DRBG *ctx;
43
44
if (!ossl_prov_is_running())
45
return NULL;
46
47
ctx = OPENSSL_zalloc(sizeof(*ctx));
48
if (ctx == NULL) {
49
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
50
return NULL;
51
}
52
ctx->provctx = provctx;
53
return ctx;
54
}
55
56
static void hmac_drbg_kdf_reset(void *vctx)
57
{
58
KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
59
PROV_DRBG_HMAC *drbg = &ctx->base;
60
void *provctx = ctx->provctx;
61
62
EVP_MAC_CTX_free(drbg->ctx);
63
ossl_prov_digest_reset(&drbg->digest);
64
OPENSSL_clear_free(ctx->entropy, ctx->entropylen);
65
OPENSSL_clear_free(ctx->nonce, ctx->noncelen);
66
OPENSSL_cleanse(ctx, sizeof(*ctx));
67
ctx->provctx = provctx;
68
}
69
70
static void hmac_drbg_kdf_free(void *vctx)
71
{
72
KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
73
74
if (ctx != NULL) {
75
hmac_drbg_kdf_reset(ctx);
76
OPENSSL_free(ctx);
77
}
78
}
79
80
static int ossl_drbg_hmac_dup(PROV_DRBG_HMAC *dst, const PROV_DRBG_HMAC *src)
81
{
82
if (src->ctx != NULL) {
83
dst->ctx = EVP_MAC_CTX_dup(src->ctx);
84
if (dst->ctx == NULL)
85
return 0;
86
}
87
if (!ossl_prov_digest_copy(&dst->digest, &src->digest))
88
return 0;
89
memcpy(dst->K, src->K, sizeof(dst->K));
90
memcpy(dst->V, src->V, sizeof(dst->V));
91
dst->blocklen = src->blocklen;
92
return 1;
93
}
94
95
static void *hmac_drbg_kdf_dup(void *vctx)
96
{
97
const KDF_HMAC_DRBG *src = (const KDF_HMAC_DRBG *)vctx;
98
KDF_HMAC_DRBG *dst;
99
100
dst = hmac_drbg_kdf_new(src->provctx);
101
if (dst != NULL) {
102
if (!ossl_drbg_hmac_dup(&dst->base, &src->base)
103
|| !ossl_prov_memdup(src->entropy, src->entropylen,
104
&dst->entropy, &dst->entropylen)
105
|| !ossl_prov_memdup(src->nonce, src->noncelen,
106
&dst->nonce, &dst->noncelen))
107
goto err;
108
dst->init = src->init;
109
}
110
return dst;
111
112
err:
113
hmac_drbg_kdf_free(dst);
114
return NULL;
115
}
116
117
static int hmac_drbg_kdf_derive(void *vctx, unsigned char *out, size_t outlen,
118
const OSSL_PARAM params[])
119
{
120
KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
121
PROV_DRBG_HMAC *drbg = &ctx->base;
122
123
if (!ossl_prov_is_running()
124
|| !hmac_drbg_kdf_set_ctx_params(vctx, params))
125
return 0;
126
if (!ctx->init) {
127
if (ctx->entropy == NULL
128
|| ctx->entropylen == 0
129
|| ctx->nonce == NULL
130
|| ctx->noncelen == 0
131
|| !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,
132
ctx->nonce, ctx->noncelen, NULL, 0))
133
return 0;
134
ctx->init = 1;
135
}
136
137
return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);
138
}
139
140
static int hmac_drbg_kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
141
{
142
KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
143
PROV_DRBG_HMAC *drbg = &hmac->base;
144
const char *name;
145
const EVP_MD *md;
146
OSSL_PARAM *p;
147
148
p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_MAC);
149
if (p != NULL) {
150
if (drbg->ctx == NULL)
151
return 0;
152
name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(drbg->ctx));
153
if (!OSSL_PARAM_set_utf8_string(p, name))
154
return 0;
155
}
156
157
p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_DIGEST);
158
if (p != NULL) {
159
md = ossl_prov_digest_md(&drbg->digest);
160
if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))
161
return 0;
162
}
163
return 1;
164
}
165
166
static const OSSL_PARAM *hmac_drbg_kdf_gettable_ctx_params(
167
ossl_unused void *vctx, ossl_unused void *p_ctx)
168
{
169
static const OSSL_PARAM known_gettable_ctx_params[] = {
170
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
171
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
172
OSSL_PARAM_END
173
};
174
return known_gettable_ctx_params;
175
}
176
177
static int hmac_drbg_kdf_set_ctx_params(void *vctx,
178
const OSSL_PARAM params[])
179
{
180
KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
181
PROV_DRBG_HMAC *drbg = &hmac->base;
182
OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(hmac->provctx);
183
const EVP_MD *md;
184
const OSSL_PARAM *p;
185
void *ptr = NULL;
186
size_t size = 0;
187
int md_size;
188
189
if (ossl_param_is_empty(params))
190
return 1;
191
192
p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_ENTROPY);
193
if (p != NULL) {
194
if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
195
return 0;
196
OPENSSL_free(hmac->entropy);
197
hmac->entropy = ptr;
198
hmac->entropylen = size;
199
hmac->init = 0;
200
ptr = NULL;
201
}
202
203
p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_HMACDRBG_NONCE);
204
if (p != NULL) {
205
if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
206
return 0;
207
OPENSSL_free(hmac->nonce);
208
hmac->nonce = ptr;
209
hmac->noncelen = size;
210
hmac->init = 0;
211
}
212
213
p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
214
if (p != NULL) {
215
if (!ossl_prov_digest_load_from_params(&drbg->digest, params, libctx))
216
return 0;
217
218
/* Confirm digest is allowed. Allow all digests that are not XOF */
219
md = ossl_prov_digest_md(&drbg->digest);
220
if (md != NULL) {
221
if (EVP_MD_xof(md)) {
222
ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
223
return 0;
224
}
225
md_size = EVP_MD_get_size(md);
226
if (md_size <= 0)
227
return 0;
228
drbg->blocklen = (size_t)md_size;
229
}
230
return ossl_prov_macctx_load_from_params(&drbg->ctx, params,
231
"HMAC", NULL, NULL, libctx);
232
}
233
return 1;
234
}
235
236
static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(
237
ossl_unused void *vctx, ossl_unused void *p_ctx)
238
{
239
static const OSSL_PARAM known_settable_ctx_params[] = {
240
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY, NULL, 0),
241
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE, NULL, 0),
242
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
243
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
244
OSSL_PARAM_END
245
};
246
return known_settable_ctx_params;
247
}
248
249
const OSSL_DISPATCH ossl_kdf_hmac_drbg_functions[] = {
250
{ OSSL_FUNC_KDF_NEWCTX, (void (*)(void))hmac_drbg_kdf_new },
251
{ OSSL_FUNC_KDF_FREECTX, (void (*)(void))hmac_drbg_kdf_free },
252
{ OSSL_FUNC_KDF_DUPCTX, (void (*)(void))hmac_drbg_kdf_dup },
253
{ OSSL_FUNC_KDF_RESET, (void (*)(void))hmac_drbg_kdf_reset },
254
{ OSSL_FUNC_KDF_DERIVE, (void (*)(void))hmac_drbg_kdf_derive },
255
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
256
(void (*)(void))hmac_drbg_kdf_settable_ctx_params },
257
{ OSSL_FUNC_KDF_SET_CTX_PARAMS,
258
(void (*)(void))hmac_drbg_kdf_set_ctx_params },
259
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
260
(void (*)(void))hmac_drbg_kdf_gettable_ctx_params },
261
{ OSSL_FUNC_KDF_GET_CTX_PARAMS,
262
(void (*)(void))hmac_drbg_kdf_get_ctx_params },
263
OSSL_DISPATCH_END
264
};
265
266