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