Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/providers/implementations/kdfs/tls1_prf.c
Line
Count
Source
1
/*
2
 * Copyright 2016-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
/*
11
 * Refer to "The TLS Protocol Version 1.0" Section 5
12
 * (https://tools.ietf.org/html/rfc2246#section-5) and
13
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
14
 * (https://tools.ietf.org/html/rfc5246#section-5).
15
 *
16
 * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
17
 *
18
 *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
19
 *                              P_SHA-1(S2, label + seed)
20
 *
21
 * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
22
 * two halves of the secret (with the possibility of one shared byte, in the
23
 * case where the length of the original secret is odd).  S1 is taken from the
24
 * first half of the secret, S2 from the second half.
25
 *
26
 * For TLS v1.2 the TLS PRF algorithm is given by:
27
 *
28
 *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
29
 *
30
 * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
31
 * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
32
 * unless defined otherwise by the cipher suite.
33
 *
34
 * P_<hash> is an expansion function that uses a single hash function to expand
35
 * a secret and seed into an arbitrary quantity of output:
36
 *
37
 *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
38
 *                            HMAC_<hash>(secret, A(2) + seed) +
39
 *                            HMAC_<hash>(secret, A(3) + seed) + ...
40
 *
41
 * where + indicates concatenation.  P_<hash> can be iterated as many times as
42
 * is necessary to produce the required quantity of data.
43
 *
44
 * A(i) is defined as:
45
 *     A(0) = seed
46
 *     A(i) = HMAC_<hash>(secret, A(i-1))
47
 */
48
#include <stdio.h>
49
#include <stdarg.h>
50
#include <string.h>
51
#include <openssl/evp.h>
52
#include <openssl/kdf.h>
53
#include <openssl/core_names.h>
54
#include <openssl/params.h>
55
#include <openssl/proverr.h>
56
#include "internal/cryptlib.h"
57
#include "internal/numbers.h"
58
#include "crypto/evp.h"
59
#include "prov/provider_ctx.h"
60
#include "prov/providercommon.h"
61
#include "prov/implementations.h"
62
#include "prov/provider_util.h"
63
#include "e_os.h"
64
65
static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
66
static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
67
static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
68
static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
69
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
70
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
71
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
72
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
73
74
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
75
    const unsigned char *sec, size_t slen,
76
    const unsigned char *seed, size_t seed_len,
77
    unsigned char *out, size_t olen);
78
79
13.9k
#define TLS1_PRF_MAXBUF 1024
80
81
/* TLS KDF kdf context structure */
82
typedef struct {
83
    void *provctx;
84
85
    /* MAC context for the main digest */
86
    EVP_MAC_CTX *P_hash;
87
    /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
88
    EVP_MAC_CTX *P_sha1;
89
90
    /* Secret value to use for PRF */
91
    unsigned char *sec;
92
    size_t seclen;
93
    /* Buffer of concatenated seed data */
94
    unsigned char seed[TLS1_PRF_MAXBUF];
95
    size_t seedlen;
96
} TLS1_PRF;
97
98
static void *kdf_tls1_prf_new(void *provctx)
99
5.13k
{
100
5.13k
    TLS1_PRF *ctx;
101
102
5.13k
    if (!ossl_prov_is_running())
103
0
        return NULL;
104
105
5.13k
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
106
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
107
0
        return NULL;
108
0
    }
109
5.13k
    ctx->provctx = provctx;
110
5.13k
    return ctx;
111
5.13k
}
112
113
static void kdf_tls1_prf_free(void *vctx)
114
71.6k
{
115
71.6k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
116
117
71.6k
    if (ctx != NULL) {
118
71.6k
        kdf_tls1_prf_reset(ctx);
119
71.6k
        OPENSSL_free(ctx);
120
71.6k
    }
121
71.6k
}
122
123
static void kdf_tls1_prf_reset(void *vctx)
124
71.6k
{
125
71.6k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
126
71.6k
    void *provctx = ctx->provctx;
127
128
71.6k
    EVP_MAC_CTX_free(ctx->P_hash);
129
71.6k
    EVP_MAC_CTX_free(ctx->P_sha1);
130
71.6k
    OPENSSL_clear_free(ctx->sec, ctx->seclen);
131
71.6k
    OPENSSL_cleanse(ctx->seed, ctx->seedlen);
132
71.6k
    memset(ctx, 0, sizeof(*ctx));
133
71.6k
    ctx->provctx = provctx;
134
71.6k
}
135
136
static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
137
    const OSSL_PARAM params[])
138
54.0k
{
139
54.0k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
140
141
54.0k
    if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
142
0
        return 0;
143
144
54.0k
    if (ctx->P_hash == NULL) {
145
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
146
0
        return 0;
147
0
    }
148
54.0k
    if (ctx->sec == NULL) {
149
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
150
0
        return 0;
151
0
    }
152
54.0k
    if (ctx->seedlen == 0) {
153
467
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
154
467
        return 0;
155
467
    }
156
53.6k
    if (keylen == 0) {
157
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
158
0
        return 0;
159
0
    }
160
161
53.6k
    return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
162
53.6k
        ctx->sec, ctx->seclen,
163
53.6k
        ctx->seed, ctx->seedlen,
164
53.6k
        key, keylen);
165
53.6k
}
166
167
static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
168
5.13k
{
169
5.13k
    const OSSL_PARAM *p;
170
5.13k
    TLS1_PRF *ctx = vctx;
171
5.13k
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
172
173
5.13k
    if (params == NULL)
174
0
        return 1;
175
176
5.13k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
177
5.13k
        if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
178
1.29k
            if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
179
1.29k
                    OSSL_MAC_NAME_HMAC,
180
1.29k
                    NULL, SN_md5, libctx)
181
1.29k
                || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
182
1.29k
                    OSSL_MAC_NAME_HMAC,
183
1.29k
                    NULL, SN_sha1, libctx))
184
0
                return 0;
185
3.84k
        } else {
186
3.84k
            EVP_MAC_CTX_free(ctx->P_sha1);
187
3.84k
            ctx->P_sha1 = NULL;
188
3.84k
            if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
189
3.84k
                    OSSL_MAC_NAME_HMAC,
190
3.84k
                    NULL, NULL, libctx))
191
0
                return 0;
192
3.84k
        }
193
5.13k
    }
194
195
5.13k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
196
5.13k
        OPENSSL_clear_free(ctx->sec, ctx->seclen);
197
5.13k
        ctx->sec = NULL;
198
5.13k
        if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
199
0
            return 0;
200
5.13k
    }
201
    /* The seed fields concatenate, so process them all */
202
5.13k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
203
30.8k
        for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
204
25.6k
                              OSSL_KDF_PARAM_SEED)) {
205
25.6k
            const void *q = ctx->seed + ctx->seedlen;
206
25.6k
            size_t sz = 0;
207
208
25.6k
            if (p->data_size != 0
209
13.9k
                && p->data != NULL
210
13.9k
                && !OSSL_PARAM_get_octet_string(p, (void **)&q,
211
13.9k
                    TLS1_PRF_MAXBUF - ctx->seedlen,
212
13.9k
                    &sz))
213
0
                return 0;
214
25.6k
            ctx->seedlen += sz;
215
25.6k
        }
216
5.13k
    }
217
5.13k
    return 1;
218
5.13k
}
219
220
static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
221
    ossl_unused void *ctx, ossl_unused void *provctx)
222
2.42k
{
223
2.42k
    static const OSSL_PARAM known_settable_ctx_params[] = {
224
2.42k
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
225
2.42k
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
226
2.42k
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
227
2.42k
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
228
2.42k
        OSSL_PARAM_END
229
2.42k
    };
230
2.42k
    return known_settable_ctx_params;
231
2.42k
}
232
233
static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
234
0
{
235
0
    OSSL_PARAM *p;
236
237
0
    if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
238
0
        return OSSL_PARAM_set_size_t(p, SIZE_MAX);
239
0
    return -2;
240
0
}
241
242
static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
243
    ossl_unused void *ctx, ossl_unused void *provctx)
244
0
{
245
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
246
0
        OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
247
0
        OSSL_PARAM_END
248
0
    };
249
0
    return known_gettable_ctx_params;
250
0
}
251
252
const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
253
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_tls1_prf_new },
254
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_tls1_prf_free },
255
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_tls1_prf_reset },
256
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_tls1_prf_derive },
257
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
258
        (void (*)(void))kdf_tls1_prf_settable_ctx_params },
259
    { OSSL_FUNC_KDF_SET_CTX_PARAMS,
260
        (void (*)(void))kdf_tls1_prf_set_ctx_params },
261
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
262
        (void (*)(void))kdf_tls1_prf_gettable_ctx_params },
263
    { OSSL_FUNC_KDF_GET_CTX_PARAMS,
264
        (void (*)(void))kdf_tls1_prf_get_ctx_params },
265
    { 0, NULL }
266
};
267
268
/*
269
 * Refer to "The TLS Protocol Version 1.0" Section 5
270
 * (https://tools.ietf.org/html/rfc2246#section-5) and
271
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
272
 * (https://tools.ietf.org/html/rfc5246#section-5).
273
 *
274
 * P_<hash> is an expansion function that uses a single hash function to expand
275
 * a secret and seed into an arbitrary quantity of output:
276
 *
277
 *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
278
 *                            HMAC_<hash>(secret, A(2) + seed) +
279
 *                            HMAC_<hash>(secret, A(3) + seed) + ...
280
 *
281
 * where + indicates concatenation.  P_<hash> can be iterated as many times as
282
 * is necessary to produce the required quantity of data.
283
 *
284
 * A(i) is defined as:
285
 *     A(0) = seed
286
 *     A(i) = HMAC_<hash>(secret, A(i-1))
287
 */
288
static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
289
    const unsigned char *sec, size_t sec_len,
290
    const unsigned char *seed, size_t seed_len,
291
    unsigned char *out, size_t olen)
292
93.8k
{
293
93.8k
    size_t chunk;
294
93.8k
    EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
295
93.8k
    unsigned char Ai[EVP_MAX_MD_SIZE];
296
93.8k
    size_t Ai_len;
297
93.8k
    int ret = 0;
298
299
93.8k
    if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
300
0
        goto err;
301
93.8k
    chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
302
93.8k
    if (chunk == 0)
303
8
        goto err;
304
    /* A(0) = seed */
305
93.8k
    ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
306
93.8k
    if (ctx_Ai == NULL)
307
0
        goto err;
308
93.8k
    if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
309
0
        goto err;
310
311
245k
    for (;;) {
312
        /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
313
245k
        if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
314
0
            goto err;
315
245k
        EVP_MAC_CTX_free(ctx_Ai);
316
245k
        ctx_Ai = NULL;
317
318
        /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
319
245k
        ctx = EVP_MAC_CTX_dup(ctx_init);
320
245k
        if (ctx == NULL)
321
0
            goto err;
322
245k
        if (!EVP_MAC_update(ctx, Ai, Ai_len))
323
0
            goto err;
324
        /* save state for calculating next A(i) value */
325
245k
        if (olen > chunk) {
326
152k
            ctx_Ai = EVP_MAC_CTX_dup(ctx);
327
152k
            if (ctx_Ai == NULL)
328
0
                goto err;
329
152k
        }
330
245k
        if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
331
0
            goto err;
332
245k
        if (olen <= chunk) {
333
            /* last chunk - use Ai as temp bounce buffer */
334
93.8k
            if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
335
0
                goto err;
336
93.8k
            memcpy(out, Ai, olen);
337
93.8k
            break;
338
93.8k
        }
339
152k
        if (!EVP_MAC_final(ctx, out, NULL, olen))
340
0
            goto err;
341
152k
        EVP_MAC_CTX_free(ctx);
342
152k
        ctx = NULL;
343
152k
        out += chunk;
344
152k
        olen -= chunk;
345
152k
    }
346
93.8k
    ret = 1;
347
93.8k
err:
348
93.8k
    EVP_MAC_CTX_free(ctx);
349
93.8k
    EVP_MAC_CTX_free(ctx_Ai);
350
93.8k
    OPENSSL_cleanse(Ai, sizeof(Ai));
351
93.8k
    return ret;
352
93.8k
}
353
354
/*
355
 * Refer to "The TLS Protocol Version 1.0" Section 5
356
 * (https://tools.ietf.org/html/rfc2246#section-5) and
357
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
358
 * (https://tools.ietf.org/html/rfc5246#section-5).
359
 *
360
 * For TLS v1.0 and TLS v1.1:
361
 *
362
 *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
363
 *                              P_SHA-1(S2, label + seed)
364
 *
365
 * S1 is taken from the first half of the secret, S2 from the second half.
366
 *
367
 *   L_S = length in bytes of secret;
368
 *   L_S1 = L_S2 = ceil(L_S / 2);
369
 *
370
 * For TLS v1.2:
371
 *
372
 *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
373
 */
374
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
375
    const unsigned char *sec, size_t slen,
376
    const unsigned char *seed, size_t seed_len,
377
    unsigned char *out, size_t olen)
378
69.7k
{
379
69.7k
    if (sha1ctx != NULL) {
380
        /* TLS v1.0 and TLS v1.1 */
381
24.0k
        size_t i;
382
24.0k
        unsigned char *tmp;
383
        /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
384
24.0k
        size_t L_S1 = (slen + 1) / 2;
385
24.0k
        size_t L_S2 = L_S1;
386
387
24.0k
        if (!tls1_prf_P_hash(mdctx, sec, L_S1,
388
24.0k
                seed, seed_len, out, olen))
389
0
            return 0;
390
391
24.0k
        if ((tmp = OPENSSL_malloc(olen)) == NULL) {
392
0
            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
393
0
            return 0;
394
0
        }
395
396
24.0k
        if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
397
24.0k
                seed, seed_len, tmp, olen)) {
398
0
            OPENSSL_clear_free(tmp, olen);
399
0
            return 0;
400
0
        }
401
1.24M
        for (i = 0; i < olen; i++)
402
1.22M
            out[i] ^= tmp[i];
403
24.0k
        OPENSSL_clear_free(tmp, olen);
404
24.0k
        return 1;
405
24.0k
    }
406
407
    /* TLS v1.2 */
408
45.7k
    if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
409
8
        return 0;
410
411
45.7k
    return 1;
412
45.7k
}