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