Coverage Report

Created: 2026-05-24 07:14

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
394
{
46
394
    KDF_HMAC_DRBG *ctx;
47
48
394
    if (!ossl_prov_is_running())
49
0
        return NULL;
50
51
394
    ctx = OPENSSL_zalloc(sizeof(*ctx));
52
394
    if (ctx == NULL) {
53
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
54
0
        return NULL;
55
0
    }
56
394
    ctx->provctx = provctx;
57
394
    return ctx;
58
394
}
59
60
static void hmac_drbg_kdf_reset(void *vctx)
61
394
{
62
394
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
63
394
    PROV_DRBG_HMAC *drbg = &ctx->base;
64
394
    void *provctx = ctx->provctx;
65
66
394
    EVP_MAC_CTX_free(drbg->ctx);
67
394
    ossl_prov_digest_reset(&drbg->digest);
68
394
    OPENSSL_clear_free(ctx->entropy, ctx->entropylen);
69
394
    OPENSSL_clear_free(ctx->nonce, ctx->noncelen);
70
394
    OPENSSL_cleanse(ctx, sizeof(*ctx));
71
394
    ctx->provctx = provctx;
72
394
}
73
74
static void hmac_drbg_kdf_free(void *vctx)
75
394
{
76
394
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
77
78
394
    if (ctx != NULL) {
79
394
        hmac_drbg_kdf_reset(ctx);
80
394
        OPENSSL_free(ctx);
81
394
    }
82
394
}
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
372
{
124
372
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
125
372
    PROV_DRBG_HMAC *drbg = &ctx->base;
126
127
372
    if (!ossl_prov_is_running()
128
372
        || !hmac_drbg_kdf_set_ctx_params(vctx, params))
129
0
        return 0;
130
372
    if (!ctx->init) {
131
372
        if (ctx->entropy == NULL
132
372
            || ctx->entropylen == 0
133
344
            || ctx->nonce == NULL
134
344
            || ctx->noncelen == 0
135
336
            || !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,
136
336
                ctx->nonce, ctx->noncelen, NULL, 0))
137
36
            return 0;
138
336
        ctx->init = 1;
139
336
    }
140
141
336
    return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);
142
372
}
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
240
{
261
240
    const char *s;
262
263
240
    memset(r, 0, sizeof(*r));
264
240
    if (p != NULL)
265
610
        for (; (s = p->key) != NULL; p++)
266
488
            switch(s[0]) {
267
0
            default:
268
0
                break;
269
122
            case 'd':
270
122
                if (ossl_likely(strcmp("igest", s + 1) == 0)) {
271
                    /* OSSL_KDF_PARAM_DIGEST */
272
122
                    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
122
                    r->digest = (OSSL_PARAM *)p;
278
122
                }
279
122
                break;
280
122
            case 'e':
281
122
                switch(s[1]) {
282
0
                default:
283
0
                    break;
284
122
                case 'n':
285
122
                    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
122
                    case 't':
300
122
                        if (ossl_likely(strcmp("ropy", s + 3) == 0)) {
301
                            /* OSSL_KDF_PARAM_HMACDRBG_ENTROPY */
302
122
                            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
122
                            r->ent = (OSSL_PARAM *)p;
308
122
                        }
309
122
                    }
310
122
                }
311
122
                break;
312
122
            case 'n':
313
122
                if (ossl_likely(strcmp("once", s + 1) == 0)) {
314
                    /* OSSL_KDF_PARAM_HMACDRBG_NONCE */
315
122
                    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
122
                    r->nonce = (OSSL_PARAM *)p;
321
122
                }
322
122
                break;
323
122
            case 'p':
324
122
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
325
                    /* OSSL_KDF_PARAM_PROPERTIES */
326
122
                    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
122
                    r->propq = (OSSL_PARAM *)p;
332
122
                }
333
488
            }
334
240
    return 1;
335
240
}
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
337
{
343
337
    KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
344
337
    PROV_DRBG_HMAC *drbg;
345
337
    OSSL_LIB_CTX *libctx;
346
337
    const EVP_MD *md;
347
337
    struct hmac_drbg_kdf_set_ctx_params_st p;
348
337
    void *ptr = NULL;
349
337
    size_t size = 0;
350
337
    int md_size;
351
352
337
    if (hmac == NULL || !hmac_drbg_kdf_set_ctx_params_decoder(params, &p))
353
0
        return 0;
354
355
337
    drbg = &hmac->base;
356
337
    libctx = PROV_LIBCTX_OF(hmac->provctx);
357
358
337
    if (p.ent != NULL) {
359
173
        if (!OSSL_PARAM_get_octet_string(p.ent, &ptr, 0, &size))
360
0
            return 0;
361
173
        OPENSSL_free(hmac->entropy);
362
173
        hmac->entropy = ptr;
363
173
        hmac->entropylen = size;
364
173
        hmac->init = 0;
365
173
        ptr = NULL;
366
173
    }
367
368
337
    if (p.nonce != NULL) {
369
173
        if (!OSSL_PARAM_get_octet_string(p.nonce, &ptr, 0, &size))
370
0
            return 0;
371
173
        OPENSSL_free(hmac->nonce);
372
173
        hmac->nonce = ptr;
373
173
        hmac->noncelen = size;
374
173
        hmac->init = 0;
375
173
    }
376
377
337
    if (p.digest != NULL) {
378
173
        if (!ossl_prov_digest_load(&drbg->digest, p.digest,
379
173
                p.propq, p.engine, libctx))
380
4
            return 0;
381
382
        /* Confirm digest is allowed. Allow all digests that are not XOF */
383
169
        md = ossl_prov_digest_md(&drbg->digest);
384
169
        if (md != NULL) {
385
169
            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
166
            md_size = EVP_MD_get_size(md);
390
166
            if (md_size <= 0)
391
2
                return 0;
392
164
            drbg->blocklen = (size_t)md_size;
393
164
        }
394
164
        if (!ossl_prov_macctx_load(&drbg->ctx, NULL, NULL, p.digest, p.propq,
395
164
                p.engine, "HMAC", NULL, NULL, libctx))
396
0
            return 0;
397
164
    }
398
328
    return 1;
399
337
}
400
401
static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(
402
    ossl_unused void *vctx, ossl_unused void *p_ctx)
403
394
{
404
394
    return hmac_drbg_kdf_set_ctx_params_list;
405
394
}
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
};