2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
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
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).
16 * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
18 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
19 * P_SHA-1(S2, label + seed)
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.
26 * For TLS v1.2 the TLS PRF algorithm is given by:
28 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
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.
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:
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) + ...
41 * where + indicates concatenation. P_<hash> can be iterated as many times as
42 * is necessary to produce the required quantity of data.
46 * A(i) = HMAC_<hash>(secret, A(i-1))
51 #include <openssl/evp.h>
52 #include <openssl/kdf.h>
53 #include <openssl/core_names.h>
54 #include <openssl/params.h>
55 #include <openssl/proverr.h>
56 #include "internal/cryptlib.h"
57 #include "internal/numbers.h"
58 #include "crypto/evp.h"
59 #include "prov/provider_ctx.h"
60 #include "prov/providercommon.h"
61 #include "prov/implementations.h"
62 #include "prov/provider_util.h"
63 #include "internal/e_os.h"
65 static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new
;
66 static OSSL_FUNC_kdf_dupctx_fn kdf_tls1_prf_dup
;
67 static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free
;
68 static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset
;
69 static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive
;
70 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params
;
71 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params
;
72 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params
;
73 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params
;
75 static int tls1_prf_alg(EVP_MAC_CTX
*mdctx
, EVP_MAC_CTX
*sha1ctx
,
76 const unsigned char *sec
, size_t slen
,
77 const unsigned char *seed
, size_t seed_len
,
78 unsigned char *out
, size_t olen
);
80 #define TLS1_PRF_MAXBUF 1024
82 /* TLS KDF kdf context structure */
86 /* MAC context for the main digest */
88 /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
91 /* Secret value to use for PRF */
94 /* Buffer of concatenated seed data */
95 unsigned char seed
[TLS1_PRF_MAXBUF
];
99 static void *kdf_tls1_prf_new(void *provctx
)
103 if (!ossl_prov_is_running())
106 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
)
107 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
108 ctx
->provctx
= provctx
;
112 static void kdf_tls1_prf_free(void *vctx
)
114 TLS1_PRF
*ctx
= (TLS1_PRF
*)vctx
;
117 kdf_tls1_prf_reset(ctx
);
122 static void kdf_tls1_prf_reset(void *vctx
)
124 TLS1_PRF
*ctx
= (TLS1_PRF
*)vctx
;
125 void *provctx
= ctx
->provctx
;
127 EVP_MAC_CTX_free(ctx
->P_hash
);
128 EVP_MAC_CTX_free(ctx
->P_sha1
);
129 OPENSSL_clear_free(ctx
->sec
, ctx
->seclen
);
130 OPENSSL_cleanse(ctx
->seed
, ctx
->seedlen
);
131 memset(ctx
, 0, sizeof(*ctx
));
132 ctx
->provctx
= provctx
;
135 static void *kdf_tls1_prf_dup(void *vctx
)
137 const TLS1_PRF
*src
= (const TLS1_PRF
*)vctx
;
140 dest
= kdf_tls1_prf_new(src
->provctx
);
142 if (src
->P_hash
!= NULL
143 && (dest
->P_hash
= EVP_MAC_CTX_dup(src
->P_hash
)) == NULL
)
145 if (src
->P_sha1
!= NULL
146 && (dest
->P_sha1
= EVP_MAC_CTX_dup(src
->P_sha1
)) == NULL
)
148 if (!ossl_prov_memdup(src
->sec
, src
->seclen
, &dest
->sec
, &dest
->seclen
))
150 memcpy(dest
->seed
, src
->seed
, src
->seedlen
);
151 dest
->seedlen
= src
->seedlen
;
156 kdf_tls1_prf_free(dest
);
160 static int kdf_tls1_prf_derive(void *vctx
, unsigned char *key
, size_t keylen
,
161 const OSSL_PARAM params
[])
163 TLS1_PRF
*ctx
= (TLS1_PRF
*)vctx
;
165 if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx
, params
))
168 if (ctx
->P_hash
== NULL
) {
169 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_MESSAGE_DIGEST
);
172 if (ctx
->sec
== NULL
) {
173 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_SECRET
);
176 if (ctx
->seedlen
== 0) {
177 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_SEED
);
181 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
185 return tls1_prf_alg(ctx
->P_hash
, ctx
->P_sha1
,
186 ctx
->sec
, ctx
->seclen
,
187 ctx
->seed
, ctx
->seedlen
,
191 static int kdf_tls1_prf_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
194 TLS1_PRF
*ctx
= vctx
;
195 OSSL_LIB_CTX
*libctx
= PROV_LIBCTX_OF(ctx
->provctx
);
200 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_DIGEST
)) != NULL
) {
201 if (OPENSSL_strcasecmp(p
->data
, SN_md5_sha1
) == 0) {
202 if (!ossl_prov_macctx_load_from_params(&ctx
->P_hash
, params
,
204 NULL
, SN_md5
, libctx
)
205 || !ossl_prov_macctx_load_from_params(&ctx
->P_sha1
, params
,
207 NULL
, SN_sha1
, libctx
))
210 EVP_MAC_CTX_free(ctx
->P_sha1
);
211 if (!ossl_prov_macctx_load_from_params(&ctx
->P_hash
, params
,
218 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SECRET
)) != NULL
) {
219 OPENSSL_clear_free(ctx
->sec
, ctx
->seclen
);
221 if (!OSSL_PARAM_get_octet_string(p
, (void **)&ctx
->sec
, 0, &ctx
->seclen
))
224 /* The seed fields concatenate, so process them all */
225 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_KDF_PARAM_SEED
)) != NULL
) {
226 for (; p
!= NULL
; p
= OSSL_PARAM_locate_const(p
+ 1,
227 OSSL_KDF_PARAM_SEED
)) {
228 const void *q
= ctx
->seed
+ ctx
->seedlen
;
231 if (p
->data_size
!= 0
233 && !OSSL_PARAM_get_octet_string(p
, (void **)&q
,
234 TLS1_PRF_MAXBUF
- ctx
->seedlen
,
243 static const OSSL_PARAM
*kdf_tls1_prf_settable_ctx_params(
244 ossl_unused
void *ctx
, ossl_unused
void *provctx
)
246 static const OSSL_PARAM known_settable_ctx_params
[] = {
247 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES
, NULL
, 0),
248 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST
, NULL
, 0),
249 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET
, NULL
, 0),
250 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED
, NULL
, 0),
253 return known_settable_ctx_params
;
256 static int kdf_tls1_prf_get_ctx_params(void *vctx
, OSSL_PARAM params
[])
260 if ((p
= OSSL_PARAM_locate(params
, OSSL_KDF_PARAM_SIZE
)) != NULL
)
261 return OSSL_PARAM_set_size_t(p
, SIZE_MAX
);
265 static const OSSL_PARAM
*kdf_tls1_prf_gettable_ctx_params(
266 ossl_unused
void *ctx
, ossl_unused
void *provctx
)
268 static const OSSL_PARAM known_gettable_ctx_params
[] = {
269 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE
, NULL
),
272 return known_gettable_ctx_params
;
275 const OSSL_DISPATCH ossl_kdf_tls1_prf_functions
[] = {
276 { OSSL_FUNC_KDF_NEWCTX
, (void(*)(void))kdf_tls1_prf_new
},
277 { OSSL_FUNC_KDF_DUPCTX
, (void(*)(void))kdf_tls1_prf_dup
},
278 { OSSL_FUNC_KDF_FREECTX
, (void(*)(void))kdf_tls1_prf_free
},
279 { OSSL_FUNC_KDF_RESET
, (void(*)(void))kdf_tls1_prf_reset
},
280 { OSSL_FUNC_KDF_DERIVE
, (void(*)(void))kdf_tls1_prf_derive
},
281 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS
,
282 (void(*)(void))kdf_tls1_prf_settable_ctx_params
},
283 { OSSL_FUNC_KDF_SET_CTX_PARAMS
,
284 (void(*)(void))kdf_tls1_prf_set_ctx_params
},
285 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS
,
286 (void(*)(void))kdf_tls1_prf_gettable_ctx_params
},
287 { OSSL_FUNC_KDF_GET_CTX_PARAMS
,
288 (void(*)(void))kdf_tls1_prf_get_ctx_params
},
293 * Refer to "The TLS Protocol Version 1.0" Section 5
294 * (https://tools.ietf.org/html/rfc2246#section-5) and
295 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
296 * (https://tools.ietf.org/html/rfc5246#section-5).
298 * P_<hash> is an expansion function that uses a single hash function to expand
299 * a secret and seed into an arbitrary quantity of output:
301 * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
302 * HMAC_<hash>(secret, A(2) + seed) +
303 * HMAC_<hash>(secret, A(3) + seed) + ...
305 * where + indicates concatenation. P_<hash> can be iterated as many times as
306 * is necessary to produce the required quantity of data.
308 * A(i) is defined as:
310 * A(i) = HMAC_<hash>(secret, A(i-1))
312 static int tls1_prf_P_hash(EVP_MAC_CTX
*ctx_init
,
313 const unsigned char *sec
, size_t sec_len
,
314 const unsigned char *seed
, size_t seed_len
,
315 unsigned char *out
, size_t olen
)
318 EVP_MAC_CTX
*ctx
= NULL
, *ctx_Ai
= NULL
;
319 unsigned char Ai
[EVP_MAX_MD_SIZE
];
323 if (!EVP_MAC_init(ctx_init
, sec
, sec_len
, NULL
))
325 chunk
= EVP_MAC_CTX_get_mac_size(ctx_init
);
329 ctx_Ai
= EVP_MAC_CTX_dup(ctx_init
);
332 if (seed
!= NULL
&& !EVP_MAC_update(ctx_Ai
, seed
, seed_len
))
336 /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
337 if (!EVP_MAC_final(ctx_Ai
, Ai
, &Ai_len
, sizeof(Ai
)))
339 EVP_MAC_CTX_free(ctx_Ai
);
342 /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
343 ctx
= EVP_MAC_CTX_dup(ctx_init
);
346 if (!EVP_MAC_update(ctx
, Ai
, Ai_len
))
348 /* save state for calculating next A(i) value */
350 ctx_Ai
= EVP_MAC_CTX_dup(ctx
);
354 if (seed
!= NULL
&& !EVP_MAC_update(ctx
, seed
, seed_len
))
357 /* last chunk - use Ai as temp bounce buffer */
358 if (!EVP_MAC_final(ctx
, Ai
, &Ai_len
, sizeof(Ai
)))
360 memcpy(out
, Ai
, olen
);
363 if (!EVP_MAC_final(ctx
, out
, NULL
, olen
))
365 EVP_MAC_CTX_free(ctx
);
372 EVP_MAC_CTX_free(ctx
);
373 EVP_MAC_CTX_free(ctx_Ai
);
374 OPENSSL_cleanse(Ai
, sizeof(Ai
));
379 * Refer to "The TLS Protocol Version 1.0" Section 5
380 * (https://tools.ietf.org/html/rfc2246#section-5) and
381 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
382 * (https://tools.ietf.org/html/rfc5246#section-5).
384 * For TLS v1.0 and TLS v1.1:
386 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
387 * P_SHA-1(S2, label + seed)
389 * S1 is taken from the first half of the secret, S2 from the second half.
391 * L_S = length in bytes of secret;
392 * L_S1 = L_S2 = ceil(L_S / 2);
396 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
398 static int tls1_prf_alg(EVP_MAC_CTX
*mdctx
, EVP_MAC_CTX
*sha1ctx
,
399 const unsigned char *sec
, size_t slen
,
400 const unsigned char *seed
, size_t seed_len
,
401 unsigned char *out
, size_t olen
)
403 if (sha1ctx
!= NULL
) {
404 /* TLS v1.0 and TLS v1.1 */
407 /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
408 size_t L_S1
= (slen
+ 1) / 2;
411 if (!tls1_prf_P_hash(mdctx
, sec
, L_S1
,
412 seed
, seed_len
, out
, olen
))
415 if ((tmp
= OPENSSL_malloc(olen
)) == NULL
) {
416 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
420 if (!tls1_prf_P_hash(sha1ctx
, sec
+ slen
- L_S2
, L_S2
,
421 seed
, seed_len
, tmp
, olen
)) {
422 OPENSSL_clear_free(tmp
, olen
);
425 for (i
= 0; i
< olen
; i
++)
427 OPENSSL_clear_free(tmp
, olen
);
432 if (!tls1_prf_P_hash(mdctx
, sec
, slen
, seed
, seed_len
, out
, olen
))