Coverage Report

Created: 2025-08-03 07:12

/src/openssl/providers/implementations/kdfs/hmacdrbg_kdf.c
Line
Count
Source (jump to first uncovered line)
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
11
#include <stdlib.h>
12
#include <string.h>
13
#include <openssl/crypto.h>
14
#include <openssl/err.h>
15
#include <openssl/kdf.h>
16
#include <openssl/proverr.h>
17
#include <openssl/core_names.h>
18
#include "internal/common.h"
19
#include "prov/providercommon.h"
20
#include "prov/implementations.h"
21
#include "prov/hmac_drbg.h"
22
#include "prov/provider_ctx.h"
23
24
static OSSL_FUNC_kdf_newctx_fn hmac_drbg_kdf_new;
25
static OSSL_FUNC_kdf_dupctx_fn hmac_drbg_kdf_dup;
26
static OSSL_FUNC_kdf_freectx_fn hmac_drbg_kdf_free;
27
static OSSL_FUNC_kdf_reset_fn hmac_drbg_kdf_reset;
28
static OSSL_FUNC_kdf_derive_fn hmac_drbg_kdf_derive;
29
static OSSL_FUNC_kdf_settable_ctx_params_fn hmac_drbg_kdf_settable_ctx_params;
30
static OSSL_FUNC_kdf_set_ctx_params_fn hmac_drbg_kdf_set_ctx_params;
31
static OSSL_FUNC_kdf_gettable_ctx_params_fn hmac_drbg_kdf_gettable_ctx_params;
32
static OSSL_FUNC_kdf_get_ctx_params_fn hmac_drbg_kdf_get_ctx_params;
33
34
typedef struct {
35
    PROV_DRBG_HMAC base;
36
    void *provctx;
37
    unsigned char *entropy, *nonce;
38
    size_t entropylen, noncelen;
39
    int init;
40
} KDF_HMAC_DRBG;
41
42
static void *hmac_drbg_kdf_new(void *provctx)
43
0
{
44
0
    KDF_HMAC_DRBG *ctx;
45
46
0
    if (!ossl_prov_is_running())
47
0
        return NULL;
48
49
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
50
0
    if (ctx == NULL) {
51
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
52
0
        return NULL;
53
0
    }
54
0
    ctx->provctx = provctx;
55
0
    return ctx;
56
0
}
57
58
static void hmac_drbg_kdf_reset(void *vctx)
59
0
{
60
0
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
61
0
    PROV_DRBG_HMAC *drbg = &ctx->base;
62
0
    void *provctx = ctx->provctx;
63
64
0
    EVP_MAC_CTX_free(drbg->ctx);
65
0
    ossl_prov_digest_reset(&drbg->digest);
66
0
    OPENSSL_clear_free(ctx->entropy, ctx->entropylen);
67
0
    OPENSSL_clear_free(ctx->nonce, ctx->noncelen);
68
0
    OPENSSL_cleanse(ctx, sizeof(*ctx));
69
0
    ctx->provctx = provctx;
70
0
}
71
72
static void hmac_drbg_kdf_free(void *vctx)
73
0
{
74
0
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
75
76
0
    if (ctx != NULL) {
77
0
        hmac_drbg_kdf_reset(ctx);
78
0
        OPENSSL_free(ctx);
79
0
    }
80
0
}
81
82
0
static int ossl_drbg_hmac_dup(PROV_DRBG_HMAC *dst, const PROV_DRBG_HMAC *src) {
83
0
    if (src->ctx != NULL) {
84
0
        dst->ctx = EVP_MAC_CTX_dup(src->ctx);
85
0
        if (dst->ctx == NULL)
86
0
            return 0;
87
0
    }
88
0
    if (!ossl_prov_digest_copy(&dst->digest, &src->digest))
89
0
        return 0;
90
0
    memcpy(dst->K, src->K, sizeof(dst->K));
91
0
    memcpy(dst->V, src->V, sizeof(dst->V));
92
0
    dst->blocklen = src->blocklen;
93
0
    return 1;
94
0
}
95
96
static void *hmac_drbg_kdf_dup(void *vctx)
97
0
{
98
0
    const KDF_HMAC_DRBG *src = (const KDF_HMAC_DRBG *)vctx;
99
0
    KDF_HMAC_DRBG *dst;
100
101
0
    dst = hmac_drbg_kdf_new(src->provctx);
102
0
    if (dst != NULL) {
103
0
        if (!ossl_drbg_hmac_dup(&dst->base, &src->base)
104
0
                || !ossl_prov_memdup(src->entropy, src->entropylen,
105
0
                                     &dst->entropy , &dst->entropylen)
106
0
                || !ossl_prov_memdup(src->nonce, src->noncelen,
107
0
                                     &dst->nonce, &dst->noncelen))
108
0
            goto err;
109
0
        dst->init = src->init;
110
0
    }
111
0
    return dst;
112
113
0
 err:
114
0
    hmac_drbg_kdf_free(dst);
115
0
    return NULL;
116
0
}
117
118
static int hmac_drbg_kdf_derive(void *vctx, unsigned char *out, size_t outlen,
119
                                const OSSL_PARAM params[])
120
0
{
121
0
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
122
0
    PROV_DRBG_HMAC *drbg = &ctx->base;
123
124
0
    if (!ossl_prov_is_running()
125
0
            || !hmac_drbg_kdf_set_ctx_params(vctx, params))
126
0
        return 0;
127
0
    if (!ctx->init) {
128
0
        if (ctx->entropy == NULL
129
0
                || ctx->entropylen == 0
130
0
                || ctx->nonce == NULL
131
0
                || ctx->noncelen == 0
132
0
                || !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,
133
0
                                        ctx->nonce, ctx->noncelen, NULL, 0))
134
0
            return 0;
135
0
        ctx->init = 1;
136
0
    }
137
138
0
    return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);
139
0
}
140
141
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
142
#ifndef hmac_drbg_kdf_get_ctx_params_list
143
static const OSSL_PARAM hmac_drbg_kdf_get_ctx_params_list[] = {
144
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
145
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
146
    OSSL_PARAM_END
147
};
148
#endif
149
150
#ifndef hmac_drbg_kdf_get_ctx_params_st
151
struct hmac_drbg_kdf_get_ctx_params_st {
152
    OSSL_PARAM *digest;
153
    OSSL_PARAM *mac;
154
};
155
#endif
156
157
#ifndef hmac_drbg_kdf_get_ctx_params_decoder
158
static int hmac_drbg_kdf_get_ctx_params_decoder
159
    (const OSSL_PARAM *p, struct hmac_drbg_kdf_get_ctx_params_st *r)
160
0
{
161
0
    const char *s;
162
163
0
    memset(r, 0, sizeof(*r));
164
0
    if (p != NULL)
165
0
        for (; (s = p->key) != NULL; p++)
166
0
            switch(s[0]) {
167
0
            default:
168
0
                break;
169
0
            case 'd':
170
0
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
171
0
                    if (ossl_likely(r->digest == NULL))
172
0
                        r->digest = (OSSL_PARAM *)p;
173
0
                }
174
0
                break;
175
0
            case 'm':
176
0
                if (ossl_likely(strcmp("ac", s + 1) == 0)) {
177
0
                    if (ossl_likely(r->mac == NULL))
178
0
                        r->mac = (OSSL_PARAM *)p;
179
0
                }
180
0
            }
181
0
    return 1;
182
0
}
183
#endif
184
/* End of machine generated */
185
186
static int hmac_drbg_kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
187
0
{
188
0
    KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
189
0
    PROV_DRBG_HMAC *drbg = &hmac->base;
190
0
    const char *name;
191
0
    const EVP_MD *md;
192
0
    struct hmac_drbg_kdf_get_ctx_params_st p;
193
194
0
    if (hmac == NULL || !hmac_drbg_kdf_get_ctx_params_decoder(params, &p))
195
0
        return 0;
196
197
0
    if (p.mac != NULL) {
198
0
        if (drbg->ctx == NULL)
199
0
            return 0;
200
0
        name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(drbg->ctx));
201
0
        if (!OSSL_PARAM_set_utf8_string(p.mac, name))
202
0
            return 0;
203
0
    }
204
205
0
    if (p.digest != NULL) {
206
0
        md = ossl_prov_digest_md(&drbg->digest);
207
0
        if (md == NULL
208
0
                || !OSSL_PARAM_set_utf8_string(p.digest, EVP_MD_get0_name(md)))
209
0
            return 0;
210
0
    }
211
0
    return 1;
212
0
}
213
214
static const OSSL_PARAM *hmac_drbg_kdf_gettable_ctx_params(
215
    ossl_unused void *vctx, ossl_unused void *p_ctx)
216
0
{
217
0
    return hmac_drbg_kdf_get_ctx_params_list;
218
0
}
219
220
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
221
#ifndef hmac_drbg_kdf_set_ctx_params_list
222
static const OSSL_PARAM hmac_drbg_kdf_set_ctx_params_list[] = {
223
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
224
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
225
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY, NULL, 0),
226
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE, NULL, 0),
227
    OSSL_PARAM_END
228
};
229
#endif
230
231
#ifndef hmac_drbg_kdf_set_ctx_params_st
232
struct hmac_drbg_kdf_set_ctx_params_st {
233
    OSSL_PARAM *digest;
234
    OSSL_PARAM *engine;
235
    OSSL_PARAM *ent;
236
    OSSL_PARAM *nonce;
237
    OSSL_PARAM *propq;
238
};
239
#endif
240
241
#ifndef hmac_drbg_kdf_set_ctx_params_decoder
242
static int hmac_drbg_kdf_set_ctx_params_decoder
243
    (const OSSL_PARAM *p, struct hmac_drbg_kdf_set_ctx_params_st *r)
244
0
{
245
0
    const char *s;
246
247
0
    memset(r, 0, sizeof(*r));
248
0
    if (p != NULL)
249
0
        for (; (s = p->key) != NULL; p++)
250
0
            switch(s[0]) {
251
0
            default:
252
0
                break;
253
0
            case 'd':
254
0
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
255
0
                    if (ossl_likely(r->digest == NULL))
256
0
                        r->digest = (OSSL_PARAM *)p;
257
0
                }
258
0
                break;
259
0
            case 'e':
260
0
                switch(s[1]) {
261
0
                default:
262
0
                    break;
263
0
                case 'n':
264
0
                    switch(s[2]) {
265
0
                    default:
266
0
                        break;
267
0
                    case 'g':
268
0
                        if (ossl_likely(strcmp("ine", s + 3) == 0)) {
269
0
                            if (ossl_likely(r->engine == NULL))
270
0
                                r->engine = (OSSL_PARAM *)p;
271
0
                        }
272
0
                        break;
273
0
                    case 't':
274
0
                        if (ossl_likely(strcmp("ropy", s + 3) == 0)) {
275
0
                            if (ossl_likely(r->ent == NULL))
276
0
                                r->ent = (OSSL_PARAM *)p;
277
0
                        }
278
0
                    }
279
0
                }
280
0
                break;
281
0
            case 'n':
282
0
                if (ossl_likely(strcmp("once", s + 1) == 0)) {
283
0
                    if (ossl_likely(r->nonce == NULL))
284
0
                        r->nonce = (OSSL_PARAM *)p;
285
0
                }
286
0
                break;
287
0
            case 'p':
288
0
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
289
0
                    if (ossl_likely(r->propq == NULL))
290
0
                        r->propq = (OSSL_PARAM *)p;
291
0
                }
292
0
            }
293
0
    return 1;
294
0
}
295
#endif
296
/* End of machine generated */
297
298
static int hmac_drbg_kdf_set_ctx_params(void *vctx,
299
                                        const OSSL_PARAM params[])
300
0
{
301
0
    KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
302
0
    PROV_DRBG_HMAC *drbg = &hmac->base;
303
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(hmac->provctx);
304
0
    const EVP_MD *md;
305
0
    struct hmac_drbg_kdf_set_ctx_params_st p;
306
0
    void *ptr = NULL;
307
0
    size_t size = 0;
308
0
    int md_size;
309
310
0
    if (hmac == NULL || !hmac_drbg_kdf_set_ctx_params_decoder(params, &p))
311
0
        return 0;
312
313
0
    if (p.ent != NULL) {
314
0
        if (!OSSL_PARAM_get_octet_string(p.ent, &ptr, 0, &size))
315
0
            return 0;
316
0
        OPENSSL_free(hmac->entropy);
317
0
        hmac->entropy = ptr;
318
0
        hmac->entropylen = size;
319
0
        hmac->init = 0;
320
0
        ptr = NULL;
321
0
    }
322
323
0
    if (p.nonce != NULL) {
324
0
        if (!OSSL_PARAM_get_octet_string(p.nonce, &ptr, 0, &size))
325
0
            return 0;
326
0
        OPENSSL_free(hmac->nonce);
327
0
        hmac->nonce = ptr;
328
0
        hmac->noncelen = size;
329
0
        hmac->init = 0;
330
0
    }
331
332
0
    if (p.digest != NULL) {
333
0
        if (!ossl_prov_digest_load(&drbg->digest, p.digest,
334
0
                                   p.propq, p.engine, libctx))
335
0
            return 0;
336
337
        /* Confirm digest is allowed. Allow all digests that are not XOF */
338
0
        md = ossl_prov_digest_md(&drbg->digest);
339
0
        if (md != NULL) {
340
0
            if (EVP_MD_xof(md)) {
341
0
                ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
342
0
                return 0;
343
0
            }
344
0
            md_size = EVP_MD_get_size(md);
345
0
            if (md_size <= 0)
346
0
                return 0;
347
0
            drbg->blocklen = (size_t)md_size;
348
0
        }
349
0
        if (!ossl_prov_macctx_load(&drbg->ctx, NULL, NULL, p.digest, p.propq,
350
0
                                   p.engine, "HMAC", NULL, NULL, libctx))
351
0
            return 0;
352
0
    }
353
0
    return 1;
354
0
}
355
356
static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(
357
    ossl_unused void *vctx, ossl_unused void *p_ctx)
358
0
{
359
0
    return hmac_drbg_kdf_set_ctx_params_list;
360
0
}
361
362
const OSSL_DISPATCH ossl_kdf_hmac_drbg_functions[] = {
363
    { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))hmac_drbg_kdf_new },
364
    { OSSL_FUNC_KDF_FREECTX, (void(*)(void))hmac_drbg_kdf_free },
365
    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))hmac_drbg_kdf_dup },
366
    { OSSL_FUNC_KDF_RESET, (void(*)(void))hmac_drbg_kdf_reset },
367
    { OSSL_FUNC_KDF_DERIVE, (void(*)(void))hmac_drbg_kdf_derive },
368
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
369
      (void(*)(void))hmac_drbg_kdf_settable_ctx_params },
370
    { OSSL_FUNC_KDF_SET_CTX_PARAMS,
371
      (void(*)(void))hmac_drbg_kdf_set_ctx_params },
372
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
373
      (void(*)(void))hmac_drbg_kdf_gettable_ctx_params },
374
    { OSSL_FUNC_KDF_GET_CTX_PARAMS,
375
      (void(*)(void))hmac_drbg_kdf_get_ctx_params },
376
    OSSL_DISPATCH_END
377
};