Coverage Report

Created: 2025-12-10 06:24

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