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