Coverage Report

Created: 2025-10-28 06:56

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