Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
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
0
{
45
0
    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
0
    if (!ossl_prov_is_running())
54
0
        return NULL;
55
56
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
57
0
    if (ctx == NULL) {
58
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
59
0
        return NULL;
60
0
    }
61
0
    ctx->provctx = provctx;
62
0
    return ctx;
63
0
}
64
65
static void hmac_drbg_kdf_reset(void *vctx)
66
0
{
67
0
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
68
0
    PROV_DRBG_HMAC *drbg = &ctx->base;
69
0
    void *provctx = ctx->provctx;
70
71
0
    EVP_MAC_CTX_free(drbg->ctx);
72
0
    ossl_prov_digest_reset(&drbg->digest);
73
0
    OPENSSL_clear_free(ctx->entropy, ctx->entropylen);
74
0
    OPENSSL_clear_free(ctx->nonce, ctx->noncelen);
75
0
    OPENSSL_cleanse(ctx, sizeof(*ctx));
76
0
    ctx->provctx = provctx;
77
0
}
78
79
static void hmac_drbg_kdf_free(void *vctx)
80
0
{
81
0
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
82
83
0
    if (ctx != NULL) {
84
0
        hmac_drbg_kdf_reset(ctx);
85
0
        OPENSSL_free(ctx);
86
0
    }
87
0
}
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
0
{
129
0
    KDF_HMAC_DRBG *ctx = (KDF_HMAC_DRBG *)vctx;
130
0
    PROV_DRBG_HMAC *drbg = &ctx->base;
131
132
0
    if (!ossl_prov_is_running()
133
0
        || !hmac_drbg_kdf_set_ctx_params(vctx, params))
134
0
        return 0;
135
0
    if (!ctx->init) {
136
0
        if (ctx->entropy == NULL
137
0
            || ctx->entropylen == 0
138
0
            || ctx->nonce == NULL
139
0
            || ctx->noncelen == 0
140
0
            || !ossl_drbg_hmac_init(drbg, ctx->entropy, ctx->entropylen,
141
0
                ctx->nonce, ctx->noncelen, NULL, 0))
142
0
            return 0;
143
0
        ctx->init = 1;
144
0
    }
145
146
0
    return ossl_drbg_hmac_generate(drbg, out, outlen, NULL, 0);
147
0
}
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
0
{
186
0
    KDF_HMAC_DRBG *hmac = (KDF_HMAC_DRBG *)vctx;
187
0
    PROV_DRBG_HMAC *drbg;
188
0
    OSSL_LIB_CTX *libctx;
189
0
    const EVP_MD *md;
190
0
    struct hmac_drbg_kdf_set_ctx_params_st p;
191
0
    void *ptr = NULL;
192
0
    size_t size = 0;
193
0
    int md_size;
194
195
0
    if (hmac == NULL || !hmac_drbg_kdf_set_ctx_params_decoder(params, &p))
196
0
        return 0;
197
198
0
    drbg = &hmac->base;
199
0
    libctx = PROV_LIBCTX_OF(hmac->provctx);
200
201
0
    if (p.ent != NULL) {
202
0
        if (!OSSL_PARAM_get_octet_string(p.ent, &ptr, 0, &size))
203
0
            return 0;
204
0
        OPENSSL_free(hmac->entropy);
205
0
        hmac->entropy = ptr;
206
0
        hmac->entropylen = size;
207
0
        hmac->init = 0;
208
0
        ptr = NULL;
209
0
    }
210
211
0
    if (p.nonce != NULL) {
212
0
        if (!OSSL_PARAM_get_octet_string(p.nonce, &ptr, 0, &size))
213
0
            return 0;
214
0
        OPENSSL_free(hmac->nonce);
215
0
        hmac->nonce = ptr;
216
0
        hmac->noncelen = size;
217
0
        hmac->init = 0;
218
0
    }
219
220
0
    if (p.digest != NULL) {
221
0
        if (!ossl_prov_digest_load(&drbg->digest, p.digest, p.propq, libctx))
222
0
            return 0;
223
224
        /* Confirm digest is allowed. Allow all digests that are not XOF */
225
0
        md = ossl_prov_digest_md(&drbg->digest);
226
0
        if (md != NULL) {
227
0
            if (EVP_MD_xof(md)) {
228
0
                ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
229
0
                return 0;
230
0
            }
231
0
            md_size = EVP_MD_get_size(md);
232
0
            if (md_size <= 0)
233
0
                return 0;
234
0
            drbg->blocklen = (size_t)md_size;
235
0
        }
236
0
        if (!ossl_prov_macctx_load(&drbg->ctx, NULL, NULL, p.digest, p.propq,
237
0
                "HMAC", NULL, NULL, libctx))
238
0
            return 0;
239
0
    }
240
0
    return 1;
241
0
}
242
243
static const OSSL_PARAM *hmac_drbg_kdf_settable_ctx_params(
244
    ossl_unused void *vctx, ossl_unused void *p_ctx)
245
0
{
246
0
    return hmac_drbg_kdf_set_ctx_params_list;
247
0
}
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
};