Coverage Report

Created: 2026-04-01 06:39

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