Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/providers/implementations/kdfs/hmacdrbg_kdf.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2026 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 "internal/common.h"
18
#include "internal/fips.h"
19
#include "prov/providercommon.h"
20
#include "prov/implementations.h"
21
#include "prov/hmac_drbg.h"
22
#include "prov/provider_ctx.h"
23
#include "providers/implementations/kdfs/hmacdrbg_kdf.inc"
24
25
static OSSL_FUNC_kdf_newctx_fn hmac_drbg_kdf_new;
26
static OSSL_FUNC_kdf_dupctx_fn hmac_drbg_kdf_dup;
27
static OSSL_FUNC_kdf_freectx_fn hmac_drbg_kdf_free;
28
static OSSL_FUNC_kdf_reset_fn hmac_drbg_kdf_reset;
29
static OSSL_FUNC_kdf_derive_fn hmac_drbg_kdf_derive;
30
static OSSL_FUNC_kdf_settable_ctx_params_fn hmac_drbg_kdf_settable_ctx_params;
31
static OSSL_FUNC_kdf_set_ctx_params_fn hmac_drbg_kdf_set_ctx_params;
32
static OSSL_FUNC_kdf_gettable_ctx_params_fn hmac_drbg_kdf_gettable_ctx_params;
33
static OSSL_FUNC_kdf_get_ctx_params_fn hmac_drbg_kdf_get_ctx_params;
34
35
typedef struct {
36
    PROV_DRBG_HMAC base;
37
    void *provctx;
38
    unsigned char *entropy, *nonce;
39
    size_t entropylen, noncelen;
40
    int init;
41
} KDF_HMAC_DRBG;
42
43
static void *hmac_drbg_kdf_new(void *provctx)
44
399
{
45
399
    KDF_HMAC_DRBG *ctx;
46
47
#ifdef FIPS_MODULE
48
    if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx),
49
            ST_ID_DRBG_HMAC))
50
        return NULL;
51
#endif
52
53
399
    if (!ossl_prov_is_running())
54
0
        return NULL;
55
56
399
    ctx = OPENSSL_zalloc(sizeof(*ctx));
57
399
    if (ctx == NULL) {
58
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
59
0
        return NULL;
60
0
    }
61
399
    ctx->provctx = provctx;
62
399
    return ctx;
63
399
}
64
65
static void hmac_drbg_kdf_reset(void *vctx)
66
399
{
67
399
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
68
399
    PROV_DRBG_HMAC *drbg = &ctx->base;
69
399
    void *provctx = ctx->provctx;
70
71
399
    EVP_MAC_CTX_free(drbg->ctx);
72
399
    ossl_prov_digest_reset(&drbg->digest);
73
399
    OPENSSL_clear_free(ctx->entropy, ctx->entropylen);
74
399
    OPENSSL_clear_free(ctx->nonce, ctx->noncelen);
75
399
    OPENSSL_cleanse(ctx, sizeof(*ctx));
76
399
    ctx->provctx = provctx;
77
399
}
78
79
static void hmac_drbg_kdf_free(void *vctx)
80
399
{
81
399
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
82
83
399
    if (ctx != NULL) {
84
399
        hmac_drbg_kdf_reset(ctx);
85
399
        OPENSSL_free(ctx);
86
399
    }
87
399
}
88
89
static int ossl_drbg_hmac_dup(PROV_DRBG_HMAC *dst, const PROV_DRBG_HMAC *src)
90
0
{
91
0
    if (src->ctx != NULL) {
92
0
        dst->ctx = EVP_MAC_CTX_dup(src->ctx);
93
0
        if (dst->ctx == NULL)
94
0
            return 0;
95
0
    }
96
0
    if (!ossl_prov_digest_copy(&dst->digest, &src->digest))
97
0
        return 0;
98
0
    memcpy(dst->K, src->K, sizeof(dst->K));
99
0
    memcpy(dst->V, src->V, sizeof(dst->V));
100
0
    dst->blocklen = src->blocklen;
101
0
    return 1;
102
0
}
103
104
static void *hmac_drbg_kdf_dup(void *vctx)
105
0
{
106
0
    const KDF_HMAC_DRBG *src = (const KDF_HMAC_DRBG *)vctx;
107
0
    KDF_HMAC_DRBG *dst;
108
109
0
    dst = hmac_drbg_kdf_new(src->provctx);
110
0
    if (dst != NULL) {
111
0
        if (!ossl_drbg_hmac_dup(&dst->base, &src->base)
112
0
            || !ossl_prov_memdup(src->entropy, src->entropylen,
113
0
                &dst->entropy, &dst->entropylen)
114
0
            || !ossl_prov_memdup(src->nonce, src->noncelen,
115
0
                &dst->nonce, &dst->noncelen))
116
0
            goto err;
117
0
        dst->init = src->init;
118
0
    }
119
0
    return dst;
120
121
0
err:
122
0
    hmac_drbg_kdf_free(dst);
123
0
    return NULL;
124
0
}
125
126
static int hmac_drbg_kdf_derive(void *vctx, unsigned char *out, size_t outlen,
127
    const OSSL_PARAM params[])
128
369
{
129
369
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
130
369
    PROV_DRBG_HMAC *drbg = &ctx->base;
131
132
369
    if (!ossl_prov_is_running()
133
369
        || !hmac_drbg_kdf_set_ctx_params(vctx, params))
134
0
        return 0;
135
369
    if (!ctx->init) {
136
369
        if (ctx->entropy == NULL
137
369
            || ctx->entropylen == 0
138
348
            || ctx->nonce == NULL
139
348
            || ctx->noncelen == 0
140
337
            || !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,
141
337
                ctx->nonce, ctx->noncelen, NULL, 0))
142
32
            return 0;
143
337
        ctx->init = 1;
144
337
    }
145
146
337
    return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);
147
369
}
148
149
static int hmac_drbg_kdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
150
0
{
151
0
    KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
152
0
    PROV_DRBG_HMAC *drbg = &hmac->base;
153
0
    const char *name;
154
0
    const EVP_MD *md;
155
0
    struct hmac_drbg_kdf_get_ctx_params_st p;
156
157
0
    if (hmac == NULL || !hmac_drbg_kdf_get_ctx_params_decoder(params, &p))
158
0
        return 0;
159
160
0
    if (p.mac != NULL) {
161
0
        if (drbg->ctx == NULL)
162
0
            return 0;
163
0
        name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(drbg->ctx));
164
0
        if (!OSSL_PARAM_set_utf8_string(p.mac, name))
165
0
            return 0;
166
0
    }
167
168
0
    if (p.digest != NULL) {
169
0
        md = ossl_prov_digest_md(&drbg->digest);
170
0
        if (md == NULL
171
0
            || !OSSL_PARAM_set_utf8_string(p.digest, EVP_MD_get0_name(md)))
172
0
            return 0;
173
0
    }
174
0
    return 1;
175
0
}
176
177
static const OSSL_PARAM *hmac_drbg_kdf_gettable_ctx_params(
178
    ossl_unused void *vctx, ossl_unused void *p_ctx)
179
0
{
180
0
    return hmac_drbg_kdf_get_ctx_params_list;
181
0
}
182
183
static int hmac_drbg_kdf_set_ctx_params(void *vctx,
184
    const OSSL_PARAM params[])
185
407
{
186
407
    KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
187
407
    PROV_DRBG_HMAC *drbg;
188
407
    OSSL_LIB_CTX *libctx;
189
407
    const EVP_MD *md;
190
407
    struct hmac_drbg_kdf_set_ctx_params_st p;
191
407
    void *ptr = NULL;
192
407
    size_t size = 0;
193
407
    int md_size;
194
195
407
    if (hmac == NULL || !hmac_drbg_kdf_set_ctx_params_decoder(params, &p))
196
0
        return 0;
197
198
407
    drbg = &hmac->base;
199
407
    libctx = PROV_LIBCTX_OF(hmac->provctx);
200
201
407
    if (p.ent != NULL) {
202
212
        if (!OSSL_PARAM_get_octet_string(p.ent, &ptr, 0, &size))
203
0
            return 0;
204
212
        OPENSSL_free(hmac->entropy);
205
212
        hmac->entropy = ptr;
206
212
        hmac->entropylen = size;
207
212
        hmac->init = 0;
208
212
        ptr = NULL;
209
212
    }
210
211
407
    if (p.nonce != NULL) {
212
212
        if (!OSSL_PARAM_get_octet_string(p.nonce, &ptr, 0, &size))
213
0
            return 0;
214
212
        OPENSSL_free(hmac->nonce);
215
212
        hmac->nonce = ptr;
216
212
        hmac->noncelen = size;
217
212
        hmac->init = 0;
218
212
    }
219
220
407
    if (p.digest != NULL) {
221
212
        if (!ossl_prov_digest_load(&drbg->digest, p.digest, p.propq, libctx))
222
10
            return 0;
223
224
        /* Confirm digest is allowed. Allow all digests that are not XOF */
225
202
        md = ossl_prov_digest_md(&drbg->digest);
226
202
        if (md != NULL) {
227
202
            if (EVP_MD_xof(md)) {
228
4
                ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
229
4
                return 0;
230
4
            }
231
198
            md_size = EVP_MD_get_size(md);
232
198
            if (md_size <= 0)
233
3
                return 0;
234
195
            drbg->blocklen = (size_t)md_size;
235
195
        }
236
195
        if (!ossl_prov_macctx_load(&drbg->ctx, NULL, NULL, p.digest, p.propq,
237
195
                "HMAC", NULL, NULL, libctx))
238
0
            return 0;
239
195
    }
240
390
    return 1;
241
407
}
242
243
static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(
244
    ossl_unused void *vctx, ossl_unused void *p_ctx)
245
399
{
246
399
    return hmac_drbg_kdf_set_ctx_params_list;
247
399
}
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
};