Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/implementations/kdfs/tls1_prf.c
Line
Count
Source
1
/*
2
 * Copyright 2016-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
/*
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
49
/*
50
 * Low level APIs (such as DH) are deprecated for public use, but still ok for
51
 * internal use.
52
 */
53
#include "internal/deprecated.h"
54
55
#include <stdio.h>
56
#include <stdarg.h>
57
#include <string.h>
58
#include <openssl/evp.h>
59
#include <openssl/kdf.h>
60
#include <openssl/core_names.h>
61
#include <openssl/params.h>
62
#include <openssl/proverr.h>
63
#include "internal/cryptlib.h"
64
#include "internal/numbers.h"
65
#include "crypto/evp.h"
66
#include "prov/provider_ctx.h"
67
#include "prov/providercommon.h"
68
#include "prov/implementations.h"
69
#include "prov/provider_util.h"
70
#include "prov/securitycheck.h"
71
#include "internal/e_os.h"
72
#include "internal/safe_math.h"
73
74
OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
75
76
static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
77
static OSSL_FUNC_kdf_dupctx_fn kdf_tls1_prf_dup;
78
static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
79
static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
80
static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
81
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
82
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
83
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
84
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
85
86
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
87
    const unsigned char *sec, size_t slen,
88
    const unsigned char *seed, size_t seed_len,
89
    unsigned char *out, size_t olen);
90
91
#define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"
92
#define TLS_MD_MASTER_SECRET_CONST_SIZE 13
93
94
/* TLS KDF kdf context structure */
95
typedef struct {
96
    void *provctx;
97
98
    /* MAC context for the main digest */
99
    EVP_MAC_CTX *P_hash;
100
    /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
101
    EVP_MAC_CTX *P_sha1;
102
103
    /* Secret value to use for PRF */
104
    unsigned char *sec;
105
    size_t seclen;
106
    /* Concatenated seed data */
107
    unsigned char *seed;
108
    size_t seedlen;
109
110
    OSSL_FIPS_IND_DECLARE
111
} TLS1_PRF;
112
113
static void *kdf_tls1_prf_new(void *provctx)
114
65.0k
{
115
65.0k
    TLS1_PRF *ctx;
116
117
65.0k
    if (!ossl_prov_is_running())
118
0
        return NULL;
119
120
65.0k
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) {
121
65.0k
        ctx->provctx = provctx;
122
65.0k
        OSSL_FIPS_IND_INIT(ctx)
123
65.0k
    }
124
65.0k
    return ctx;
125
65.0k
}
126
127
static void kdf_tls1_prf_free(void *vctx)
128
70.4k
{
129
70.4k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
130
131
70.4k
    if (ctx != NULL) {
132
70.4k
        kdf_tls1_prf_reset(ctx);
133
70.4k
        OPENSSL_free(ctx);
134
70.4k
    }
135
70.4k
}
136
137
static void kdf_tls1_prf_reset(void *vctx)
138
70.4k
{
139
70.4k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
140
70.4k
    void *provctx = ctx->provctx;
141
142
70.4k
    EVP_MAC_CTX_free(ctx->P_hash);
143
70.4k
    EVP_MAC_CTX_free(ctx->P_sha1);
144
70.4k
    OPENSSL_clear_free(ctx->sec, ctx->seclen);
145
70.4k
    OPENSSL_clear_free(ctx->seed, ctx->seedlen);
146
70.4k
    memset(ctx, 0, sizeof(*ctx));
147
70.4k
    ctx->provctx = provctx;
148
70.4k
}
149
150
static void *kdf_tls1_prf_dup(void *vctx)
151
0
{
152
0
    const TLS1_PRF *src = (const TLS1_PRF *)vctx;
153
0
    TLS1_PRF *dest;
154
155
0
    dest = kdf_tls1_prf_new(src->provctx);
156
0
    if (dest != NULL) {
157
0
        if (src->P_hash != NULL
158
0
            && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
159
0
            goto err;
160
0
        if (src->P_sha1 != NULL
161
0
            && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
162
0
            goto err;
163
0
        if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
164
0
            goto err;
165
0
        if (!ossl_prov_memdup(src->seed, src->seedlen, &dest->seed,
166
0
                &dest->seedlen))
167
0
            goto err;
168
0
        OSSL_FIPS_IND_COPY(dest, src)
169
0
    }
170
0
    return dest;
171
172
0
err:
173
0
    kdf_tls1_prf_free(dest);
174
0
    return NULL;
175
0
}
176
177
#ifdef FIPS_MODULE
178
179
static int fips_ems_check_passed(TLS1_PRF *ctx)
180
{
181
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
182
    /*
183
     * Check that TLS is using EMS.
184
     *
185
     * The seed buffer is prepended with a label.
186
     * If EMS mode is enforced then the label "master secret" is not allowed,
187
     * We do the check this way since the PRF is used for other purposes, as well
188
     * as "extended master secret".
189
     */
190
    int ems_approved = (ctx->seedlen < TLS_MD_MASTER_SECRET_CONST_SIZE
191
        || memcmp(ctx->seed, TLS_MD_MASTER_SECRET_CONST,
192
               TLS_MD_MASTER_SECRET_CONST_SIZE)
193
            != 0);
194
195
    if (!ems_approved) {
196
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
197
                libctx, "TLS_PRF", "EMS",
198
                ossl_fips_config_tls1_prf_ems_check)) {
199
            ERR_raise(ERR_LIB_PROV, PROV_R_EMS_NOT_ENABLED);
200
            return 0;
201
        }
202
    }
203
    return 1;
204
}
205
206
static int fips_digest_check_passed(TLS1_PRF *ctx, const EVP_MD *md)
207
{
208
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
209
    /*
210
     * Perform digest check
211
     *
212
     * According to NIST SP 800-135r1 section 5.2, the valid hash functions are
213
     * specified in FIPS 180-3. ACVP also only lists the same set of hash
214
     * functions.
215
     */
216
    int digest_unapproved = !EVP_MD_is_a(md, SN_sha256)
217
        && !EVP_MD_is_a(md, SN_sha384)
218
        && !EVP_MD_is_a(md, SN_sha512);
219
220
    if (digest_unapproved) {
221
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1,
222
                libctx, "TLS_PRF", "Digest",
223
                ossl_fips_config_tls1_prf_digest_check)) {
224
            ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
225
            return 0;
226
        }
227
    }
228
    return 1;
229
}
230
231
static int fips_key_check_passed(TLS1_PRF *ctx)
232
{
233
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
234
    int key_approved = ossl_kdf_check_key_size(ctx->seclen);
235
236
    if (!key_approved) {
237
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,
238
                libctx, "TLS_PRF", "Key size",
239
                ossl_fips_config_tls1_prf_key_check)) {
240
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
241
            return 0;
242
        }
243
    }
244
    return 1;
245
}
246
#endif
247
248
static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
249
    const OSSL_PARAM params[])
250
55.3k
{
251
55.3k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
252
253
55.3k
    if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
254
0
        return 0;
255
256
55.3k
    if (ctx->P_hash == NULL) {
257
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
258
0
        return 0;
259
0
    }
260
55.3k
    if (ctx->sec == NULL) {
261
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
262
0
        return 0;
263
0
    }
264
55.3k
    if (ctx->seedlen == 0) {
265
512
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
266
512
        return 0;
267
512
    }
268
54.8k
    if (keylen == 0) {
269
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
270
0
        return 0;
271
0
    }
272
273
#ifdef FIPS_MODULE
274
    if (!fips_ems_check_passed(ctx))
275
        return 0;
276
#endif
277
278
54.8k
    return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
279
54.8k
        ctx->sec, ctx->seclen,
280
54.8k
        ctx->seed, ctx->seedlen,
281
54.8k
        key, keylen);
282
54.8k
}
283
284
static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
285
17.4k
{
286
17.4k
    const OSSL_PARAM *p;
287
17.4k
    TLS1_PRF *ctx = vctx;
288
17.4k
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
289
290
17.4k
    if (ossl_param_is_empty(params))
291
363
        return 1;
292
293
17.0k
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,
294
17.0k
            OSSL_KDF_PARAM_FIPS_EMS_CHECK))
295
0
        return 0;
296
17.0k
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, params,
297
17.0k
            OSSL_KDF_PARAM_FIPS_DIGEST_CHECK))
298
0
        return 0;
299
17.0k
    if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE2, params,
300
17.0k
            OSSL_KDF_PARAM_FIPS_KEY_CHECK))
301
0
        return 0;
302
303
17.0k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
304
17.0k
        PROV_DIGEST digest;
305
17.0k
        const EVP_MD *md = NULL;
306
307
17.0k
        if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
308
4.03k
            if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
309
4.03k
                    OSSL_MAC_NAME_HMAC,
310
4.03k
                    NULL, SN_md5, libctx)
311
4.03k
                || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
312
4.03k
                    OSSL_MAC_NAME_HMAC,
313
4.03k
                    NULL, SN_sha1, libctx))
314
3
                return 0;
315
13.0k
        } else {
316
13.0k
            EVP_MAC_CTX_free(ctx->P_sha1);
317
13.0k
            if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
318
13.0k
                    OSSL_MAC_NAME_HMAC,
319
13.0k
                    NULL, NULL, libctx))
320
439
                return 0;
321
13.0k
        }
322
323
16.5k
        memset(&digest, 0, sizeof(digest));
324
16.5k
        if (!ossl_prov_digest_load_from_params(&digest, params, libctx))
325
0
            return 0;
326
327
16.5k
        md = ossl_prov_digest_md(&digest);
328
16.5k
        if (EVP_MD_xof(md)) {
329
3
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
330
3
            ossl_prov_digest_reset(&digest);
331
3
            return 0;
332
3
        }
333
334
#ifdef FIPS_MODULE
335
        if (!fips_digest_check_passed(ctx, md)) {
336
            ossl_prov_digest_reset(&digest);
337
            return 0;
338
        }
339
#endif
340
341
16.5k
        ossl_prov_digest_reset(&digest);
342
16.5k
    }
343
344
16.5k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
345
16.5k
        OPENSSL_clear_free(ctx->sec, ctx->seclen);
346
16.5k
        ctx->sec = NULL;
347
16.5k
        if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
348
0
            return 0;
349
350
#ifdef FIPS_MODULE
351
        if (!fips_key_check_passed(ctx))
352
            return 0;
353
#endif
354
16.5k
    }
355
    /* The seed fields concatenate, so process them all */
356
16.5k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
357
98.1k
        for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
358
81.5k
                              OSSL_KDF_PARAM_SEED)) {
359
81.5k
            if (p->data_size != 0 && p->data != NULL) {
360
43.9k
                const void *val = NULL;
361
43.9k
                size_t sz = 0;
362
43.9k
                unsigned char *seed;
363
43.9k
                size_t seedlen;
364
43.9k
                int err = 0;
365
366
43.9k
                if (!OSSL_PARAM_get_octet_string_ptr(p, &val, &sz))
367
0
                    return 0;
368
369
43.9k
                seedlen = safe_add_size_t(ctx->seedlen, sz, &err);
370
43.9k
                if (err)
371
0
                    return 0;
372
373
43.9k
                seed = OPENSSL_clear_realloc(ctx->seed, ctx->seedlen, seedlen);
374
43.9k
                if (!seed)
375
0
                    return 0;
376
377
43.9k
                ctx->seed = seed;
378
43.9k
                if (ossl_assert(sz != 0))
379
43.9k
                    memcpy(ctx->seed + ctx->seedlen, val, sz);
380
43.9k
                ctx->seedlen = seedlen;
381
43.9k
            }
382
81.5k
        }
383
16.5k
    }
384
16.5k
    return 1;
385
16.5k
}
386
387
static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
388
    ossl_unused void *ctx, ossl_unused void *provctx)
389
2.39k
{
390
2.39k
    static const OSSL_PARAM known_settable_ctx_params[] = {
391
2.39k
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
392
2.39k
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
393
2.39k
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
394
2.39k
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
395
2.39k
        OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_EMS_CHECK)
396
2.39k
            OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK)
397
2.39k
                OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)
398
2.39k
                    OSSL_PARAM_END
399
2.39k
    };
400
2.39k
    return known_settable_ctx_params;
401
2.39k
}
402
403
static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
404
0
{
405
0
    OSSL_PARAM *p;
406
407
0
    if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) {
408
0
        if (!OSSL_PARAM_set_size_t(p, SIZE_MAX))
409
0
            return 0;
410
0
    }
411
0
    if (!OSSL_FIPS_IND_GET_CTX_PARAM(((TLS1_PRF *)vctx), params))
412
0
        return 0;
413
0
    return 1;
414
0
}
415
416
static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
417
    ossl_unused void *ctx, ossl_unused void *provctx)
418
0
{
419
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
420
0
        OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
421
0
        OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
422
0
            OSSL_PARAM_END
423
0
    };
424
0
    return known_gettable_ctx_params;
425
0
}
426
427
const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
428
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_tls1_prf_new },
429
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_tls1_prf_dup },
430
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_tls1_prf_free },
431
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_tls1_prf_reset },
432
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_tls1_prf_derive },
433
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
434
        (void (*)(void))kdf_tls1_prf_settable_ctx_params },
435
    { OSSL_FUNC_KDF_SET_CTX_PARAMS,
436
        (void (*)(void))kdf_tls1_prf_set_ctx_params },
437
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
438
        (void (*)(void))kdf_tls1_prf_gettable_ctx_params },
439
    { OSSL_FUNC_KDF_GET_CTX_PARAMS,
440
        (void (*)(void))kdf_tls1_prf_get_ctx_params },
441
    OSSL_DISPATCH_END
442
};
443
444
/*
445
 * Refer to "The TLS Protocol Version 1.0" Section 5
446
 * (https://tools.ietf.org/html/rfc2246#section-5) and
447
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
448
 * (https://tools.ietf.org/html/rfc5246#section-5).
449
 *
450
 * P_<hash> is an expansion function that uses a single hash function to expand
451
 * a secret and seed into an arbitrary quantity of output:
452
 *
453
 *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
454
 *                            HMAC_<hash>(secret, A(2) + seed) +
455
 *                            HMAC_<hash>(secret, A(3) + seed) + ...
456
 *
457
 * where + indicates concatenation.  P_<hash> can be iterated as many times as
458
 * is necessary to produce the required quantity of data.
459
 *
460
 * A(i) is defined as:
461
 *     A(0) = seed
462
 *     A(i) = HMAC_<hash>(secret, A(i-1))
463
 */
464
static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
465
    const unsigned char *sec, size_t sec_len,
466
    const unsigned char *seed, size_t seed_len,
467
    unsigned char *out, size_t olen)
468
90.1k
{
469
90.1k
    size_t chunk;
470
90.1k
    EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
471
90.1k
    unsigned char Ai[EVP_MAX_MD_SIZE];
472
90.1k
    size_t Ai_len;
473
90.1k
    int ret = 0;
474
475
90.1k
    if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
476
0
        goto err;
477
90.1k
    chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
478
90.1k
    if (chunk == 0)
479
10
        goto err;
480
    /* A(0) = seed */
481
90.1k
    ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
482
90.1k
    if (ctx_Ai == NULL)
483
0
        goto err;
484
90.1k
    if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
485
0
        goto err;
486
487
238k
    for (;;) {
488
        /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
489
238k
        if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
490
0
            goto err;
491
238k
        EVP_MAC_CTX_free(ctx_Ai);
492
238k
        ctx_Ai = NULL;
493
494
        /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
495
238k
        ctx = EVP_MAC_CTX_dup(ctx_init);
496
238k
        if (ctx == NULL)
497
0
            goto err;
498
238k
        if (!EVP_MAC_update(ctx, Ai, Ai_len))
499
0
            goto err;
500
        /* save state for calculating next A(i) value */
501
238k
        if (olen > chunk) {
502
147k
            ctx_Ai = EVP_MAC_CTX_dup(ctx);
503
147k
            if (ctx_Ai == NULL)
504
0
                goto err;
505
147k
        }
506
238k
        if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
507
0
            goto err;
508
238k
        if (olen <= chunk) {
509
            /* last chunk - use Ai as temp bounce buffer */
510
90.1k
            if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
511
0
                goto err;
512
90.1k
            memcpy(out, Ai, olen);
513
90.1k
            break;
514
90.1k
        }
515
147k
        if (!EVP_MAC_final(ctx, out, NULL, olen))
516
0
            goto err;
517
147k
        EVP_MAC_CTX_free(ctx);
518
147k
        ctx = NULL;
519
147k
        out += chunk;
520
147k
        olen -= chunk;
521
147k
    }
522
90.1k
    ret = 1;
523
90.1k
err:
524
90.1k
    EVP_MAC_CTX_free(ctx);
525
90.1k
    EVP_MAC_CTX_free(ctx_Ai);
526
90.1k
    OPENSSL_cleanse(Ai, sizeof(Ai));
527
90.1k
    return ret;
528
90.1k
}
529
530
/*
531
 * Refer to "The TLS Protocol Version 1.0" Section 5
532
 * (https://tools.ietf.org/html/rfc2246#section-5) and
533
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
534
 * (https://tools.ietf.org/html/rfc5246#section-5).
535
 *
536
 * For TLS v1.0 and TLS v1.1:
537
 *
538
 *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
539
 *                              P_SHA-1(S2, label + seed)
540
 *
541
 * S1 is taken from the first half of the secret, S2 from the second half.
542
 *
543
 *   L_S = length in bytes of secret;
544
 *   L_S1 = L_S2 = ceil(L_S / 2);
545
 *
546
 * For TLS v1.2:
547
 *
548
 *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
549
 */
550
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
551
    const unsigned char *sec, size_t slen,
552
    const unsigned char *seed, size_t seed_len,
553
    unsigned char *out, size_t olen)
554
68.6k
{
555
68.6k
    if (sha1ctx != NULL) {
556
        /* TLS v1.0 and TLS v1.1 */
557
21.5k
        size_t i;
558
21.5k
        unsigned char *tmp;
559
        /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
560
21.5k
        size_t L_S1 = (slen + 1) / 2;
561
21.5k
        size_t L_S2 = L_S1;
562
563
21.5k
        if (!tls1_prf_P_hash(mdctx, sec, L_S1,
564
21.5k
                seed, seed_len, out, olen))
565
0
            return 0;
566
567
21.5k
        if ((tmp = OPENSSL_malloc(olen)) == NULL)
568
0
            return 0;
569
570
21.5k
        if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
571
21.5k
                seed, seed_len, tmp, olen)) {
572
0
            OPENSSL_clear_free(tmp, olen);
573
0
            return 0;
574
0
        }
575
1.17M
        for (i = 0; i < olen; i++)
576
1.14M
            out[i] ^= tmp[i];
577
21.5k
        OPENSSL_clear_free(tmp, olen);
578
21.5k
        return 1;
579
21.5k
    }
580
581
    /* TLS v1.2 */
582
47.1k
    if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
583
10
        return 0;
584
585
47.0k
    return 1;
586
47.1k
}