Coverage Report

Created: 2025-12-31 06:58

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