Coverage Report

Created: 2025-12-31 06:58

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