Coverage Report

Created: 2024-07-27 06:36

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