1 /* 2 * Copyright 2016-2026 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) 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 { 251 TLS1_PRF *ctx = (TLS1_PRF *)vctx; 252 253 if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params)) 254 return 0; 255 256 if (ctx->P_hash == NULL) { 257 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); 258 return 0; 259 } 260 if (ctx->sec == NULL) { 261 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); 262 return 0; 263 } 264 if (ctx->seedlen == 0) { 265 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED); 266 return 0; 267 } 268 if (keylen == 0) { 269 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); 270 return 0; 271 } 272 273 #ifdef FIPS_MODULE 274 if (!fips_ems_check_passed(ctx)) 275 return 0; 276 #endif 277 278 return tls1_prf_alg(ctx->P_hash, ctx->P_sha1, 279 ctx->sec, ctx->seclen, 280 ctx->seed, ctx->seedlen, 281 key, keylen); 282 } 283 284 static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) 285 { 286 const OSSL_PARAM *p; 287 TLS1_PRF *ctx = vctx; 288 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); 289 290 if (ossl_param_is_empty(params)) 291 return 1; 292 293 if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params, 294 OSSL_KDF_PARAM_FIPS_EMS_CHECK)) 295 return 0; 296 if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, params, 297 OSSL_KDF_PARAM_FIPS_DIGEST_CHECK)) 298 return 0; 299 if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE2, params, 300 OSSL_KDF_PARAM_FIPS_KEY_CHECK)) 301 return 0; 302 303 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) { 304 PROV_DIGEST digest; 305 const EVP_MD *md = NULL; 306 307 if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) { 308 if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params, 309 OSSL_MAC_NAME_HMAC, 310 NULL, SN_md5, libctx) 311 || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params, 312 OSSL_MAC_NAME_HMAC, 313 NULL, SN_sha1, libctx)) 314 return 0; 315 } else { 316 EVP_MAC_CTX_free(ctx->P_sha1); 317 ctx->P_sha1 = NULL; 318 if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params, 319 OSSL_MAC_NAME_HMAC, 320 NULL, NULL, libctx)) 321 return 0; 322 } 323 324 memset(&digest, 0, sizeof(digest)); 325 if (!ossl_prov_digest_load_from_params(&digest, params, libctx)) 326 return 0; 327 328 md = ossl_prov_digest_md(&digest); 329 if (EVP_MD_xof(md)) { 330 ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); 331 ossl_prov_digest_reset(&digest); 332 return 0; 333 } 334 335 #ifdef FIPS_MODULE 336 if (!fips_digest_check_passed(ctx, md)) { 337 ossl_prov_digest_reset(&digest); 338 return 0; 339 } 340 #endif 341 342 ossl_prov_digest_reset(&digest); 343 } 344 345 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) { 346 OPENSSL_clear_free(ctx->sec, ctx->seclen); 347 ctx->sec = NULL; 348 if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen)) 349 return 0; 350 351 #ifdef FIPS_MODULE 352 if (!fips_key_check_passed(ctx)) 353 return 0; 354 #endif 355 } 356 /* The seed fields concatenate, so process them all */ 357 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) { 358 for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, 359 OSSL_KDF_PARAM_SEED)) { 360 if (p->data_size != 0 && p->data != NULL) { 361 const void *val = NULL; 362 size_t sz = 0; 363 unsigned char *seed; 364 size_t seedlen; 365 int err = 0; 366 367 if (!OSSL_PARAM_get_octet_string_ptr(p, &val, &sz)) 368 return 0; 369 370 seedlen = safe_add_size_t(ctx->seedlen, sz, &err); 371 if (err) 372 return 0; 373 374 seed = OPENSSL_clear_realloc(ctx->seed, ctx->seedlen, seedlen); 375 if (!seed) 376 return 0; 377 378 ctx->seed = seed; 379 if (ossl_assert(sz != 0)) 380 memcpy(ctx->seed + ctx->seedlen, val, sz); 381 ctx->seedlen = seedlen; 382 } 383 } 384 } 385 return 1; 386 } 387 388 static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params( 389 ossl_unused void *ctx, ossl_unused void *provctx) 390 { 391 static const OSSL_PARAM known_settable_ctx_params[] = { 392 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), 393 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), 394 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0), 395 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0), 396 OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_EMS_CHECK) 397 OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK) 398 OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK) 399 OSSL_PARAM_END 400 }; 401 return known_settable_ctx_params; 402 } 403 404 static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[]) 405 { 406 OSSL_PARAM *p; 407 408 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) { 409 if (!OSSL_PARAM_set_size_t(p, SIZE_MAX)) 410 return 0; 411 } 412 if (!OSSL_FIPS_IND_GET_CTX_PARAM(((TLS1_PRF *)vctx), params)) 413 return 0; 414 return 1; 415 } 416 417 static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params( 418 ossl_unused void *ctx, ossl_unused void *provctx) 419 { 420 static const OSSL_PARAM known_gettable_ctx_params[] = { 421 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), 422 OSSL_FIPS_IND_GETTABLE_CTX_PARAM() 423 OSSL_PARAM_END 424 }; 425 return known_gettable_ctx_params; 426 } 427 428 const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = { 429 { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_tls1_prf_new }, 430 { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_tls1_prf_dup }, 431 { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_tls1_prf_free }, 432 { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_tls1_prf_reset }, 433 { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_tls1_prf_derive }, 434 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, 435 (void (*)(void))kdf_tls1_prf_settable_ctx_params }, 436 { OSSL_FUNC_KDF_SET_CTX_PARAMS, 437 (void (*)(void))kdf_tls1_prf_set_ctx_params }, 438 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, 439 (void (*)(void))kdf_tls1_prf_gettable_ctx_params }, 440 { OSSL_FUNC_KDF_GET_CTX_PARAMS, 441 (void (*)(void))kdf_tls1_prf_get_ctx_params }, 442 OSSL_DISPATCH_END 443 }; 444 445 /* 446 * Refer to "The TLS Protocol Version 1.0" Section 5 447 * (https://tools.ietf.org/html/rfc2246#section-5) and 448 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 449 * (https://tools.ietf.org/html/rfc5246#section-5). 450 * 451 * P_<hash> is an expansion function that uses a single hash function to expand 452 * a secret and seed into an arbitrary quantity of output: 453 * 454 * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) + 455 * HMAC_<hash>(secret, A(2) + seed) + 456 * HMAC_<hash>(secret, A(3) + seed) + ... 457 * 458 * where + indicates concatenation. P_<hash> can be iterated as many times as 459 * is necessary to produce the required quantity of data. 460 * 461 * A(i) is defined as: 462 * A(0) = seed 463 * A(i) = HMAC_<hash>(secret, A(i-1)) 464 */ 465 static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init, 466 const unsigned char *sec, size_t sec_len, 467 const unsigned char *seed, size_t seed_len, 468 unsigned char *out, size_t olen) 469 { 470 size_t chunk; 471 EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL; 472 unsigned char Ai[EVP_MAX_MD_SIZE]; 473 size_t Ai_len; 474 int ret = 0; 475 476 if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL)) 477 goto err; 478 chunk = EVP_MAC_CTX_get_mac_size(ctx_init); 479 if (chunk == 0) 480 goto err; 481 /* A(0) = seed */ 482 ctx_Ai = EVP_MAC_CTX_dup(ctx_init); 483 if (ctx_Ai == NULL) 484 goto err; 485 if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len)) 486 goto err; 487 488 for (;;) { 489 /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */ 490 if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai))) 491 goto err; 492 EVP_MAC_CTX_free(ctx_Ai); 493 ctx_Ai = NULL; 494 495 /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */ 496 ctx = EVP_MAC_CTX_dup(ctx_init); 497 if (ctx == NULL) 498 goto err; 499 if (!EVP_MAC_update(ctx, Ai, Ai_len)) 500 goto err; 501 /* save state for calculating next A(i) value */ 502 if (olen > chunk) { 503 ctx_Ai = EVP_MAC_CTX_dup(ctx); 504 if (ctx_Ai == NULL) 505 goto err; 506 } 507 if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len)) 508 goto err; 509 if (olen <= chunk) { 510 /* last chunk - use Ai as temp bounce buffer */ 511 if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai))) 512 goto err; 513 memcpy(out, Ai, olen); 514 break; 515 } 516 if (!EVP_MAC_final(ctx, out, NULL, olen)) 517 goto err; 518 EVP_MAC_CTX_free(ctx); 519 ctx = NULL; 520 out += chunk; 521 olen -= chunk; 522 } 523 ret = 1; 524 err: 525 EVP_MAC_CTX_free(ctx); 526 EVP_MAC_CTX_free(ctx_Ai); 527 OPENSSL_cleanse(Ai, sizeof(Ai)); 528 return ret; 529 } 530 531 /* 532 * Refer to "The TLS Protocol Version 1.0" Section 5 533 * (https://tools.ietf.org/html/rfc2246#section-5) and 534 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 535 * (https://tools.ietf.org/html/rfc5246#section-5). 536 * 537 * For TLS v1.0 and TLS v1.1: 538 * 539 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR 540 * P_SHA-1(S2, label + seed) 541 * 542 * S1 is taken from the first half of the secret, S2 from the second half. 543 * 544 * L_S = length in bytes of secret; 545 * L_S1 = L_S2 = ceil(L_S / 2); 546 * 547 * For TLS v1.2: 548 * 549 * PRF(secret, label, seed) = P_<hash>(secret, label + seed) 550 */ 551 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx, 552 const unsigned char *sec, size_t slen, 553 const unsigned char *seed, size_t seed_len, 554 unsigned char *out, size_t olen) 555 { 556 if (sha1ctx != NULL) { 557 /* TLS v1.0 and TLS v1.1 */ 558 size_t i; 559 unsigned char *tmp; 560 /* calc: L_S1 = L_S2 = ceil(L_S / 2) */ 561 size_t L_S1 = (slen + 1) / 2; 562 size_t L_S2 = L_S1; 563 564 if (!tls1_prf_P_hash(mdctx, sec, L_S1, 565 seed, seed_len, out, olen)) 566 return 0; 567 568 if ((tmp = OPENSSL_malloc(olen)) == NULL) 569 return 0; 570 571 if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2, 572 seed, seed_len, tmp, olen)) { 573 OPENSSL_clear_free(tmp, olen); 574 return 0; 575 } 576 for (i = 0; i < olen; i++) 577 out[i] ^= tmp[i]; 578 OPENSSL_clear_free(tmp, olen); 579 return 1; 580 } 581 582 /* TLS v1.2 */ 583 if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen)) 584 return 0; 585 586 return 1; 587 } 588