2 * Copyright 2016-2018 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 "internal/cryptlib.h"
52 #include <openssl/evp.h>
53 #include <openssl/kdf.h>
54 #include <openssl/core_names.h>
55 #include <openssl/params.h>
56 #include "internal/evp_int.h"
57 #include "kdf_local.h"
59 static void kdf_tls1_prf_reset(EVP_KDF_IMPL
*impl
);
60 static int tls1_prf_alg(const EVP_MD
*md
,
61 const unsigned char *sec
, size_t slen
,
62 const unsigned char *seed
, size_t seed_len
,
63 unsigned char *out
, size_t olen
);
65 #define TLS1_PRF_MAXBUF 1024
67 /* TLS KDF kdf context structure */
69 struct evp_kdf_impl_st
{
70 /* Digest to use for PRF */
72 /* Secret value to use for PRF */
75 /* Buffer of concatenated seed data */
76 unsigned char seed
[TLS1_PRF_MAXBUF
];
80 static EVP_KDF_IMPL
*kdf_tls1_prf_new(void)
84 if ((impl
= OPENSSL_zalloc(sizeof(*impl
))) == NULL
)
85 KDFerr(KDF_F_KDF_TLS1_PRF_NEW
, ERR_R_MALLOC_FAILURE
);
89 static void kdf_tls1_prf_free(EVP_KDF_IMPL
*impl
)
91 kdf_tls1_prf_reset(impl
);
95 static void kdf_tls1_prf_reset(EVP_KDF_IMPL
*impl
)
97 OPENSSL_clear_free(impl
->sec
, impl
->seclen
);
98 OPENSSL_cleanse(impl
->seed
, impl
->seedlen
);
99 memset(impl
, 0, sizeof(*impl
));
102 static int kdf_tls1_prf_ctrl(EVP_KDF_IMPL
*impl
, int cmd
, va_list args
)
104 const unsigned char *p
;
109 case EVP_KDF_CTRL_SET_MD
:
110 md
= va_arg(args
, const EVP_MD
*);
117 case EVP_KDF_CTRL_SET_TLS_SECRET
:
118 p
= va_arg(args
, const unsigned char *);
119 len
= va_arg(args
, size_t);
120 OPENSSL_clear_free(impl
->sec
, impl
->seclen
);
121 impl
->sec
= OPENSSL_memdup(p
, len
);
122 if (impl
->sec
== NULL
)
128 /* TODO: This is only ever called from pkey_kdf and only as part of setting the TLS secret
129 consider merging the twe two?? */
130 case EVP_KDF_CTRL_RESET_TLS_SEED
:
131 OPENSSL_cleanse(impl
->seed
, impl
->seedlen
);
135 case EVP_KDF_CTRL_ADD_TLS_SEED
:
136 p
= va_arg(args
, const unsigned char *);
137 len
= va_arg(args
, size_t);
138 if (len
== 0 || p
== NULL
)
141 if (len
> (TLS1_PRF_MAXBUF
- impl
->seedlen
))
144 memcpy(impl
->seed
+ impl
->seedlen
, p
, len
);
145 impl
->seedlen
+= len
;
153 static int kdf_tls1_prf_ctrl_str(EVP_KDF_IMPL
*impl
,
154 const char *type
, const char *value
)
157 KDFerr(KDF_F_KDF_TLS1_PRF_CTRL_STR
, KDF_R_VALUE_MISSING
);
160 if (strcmp(type
, "digest") == 0)
161 return kdf_md2ctrl(impl
, kdf_tls1_prf_ctrl
, EVP_KDF_CTRL_SET_MD
, value
);
163 if (strcmp(type
, "secret") == 0)
164 return kdf_str2ctrl(impl
, kdf_tls1_prf_ctrl
,
165 EVP_KDF_CTRL_SET_TLS_SECRET
, value
);
167 if (strcmp(type
, "hexsecret") == 0)
168 return kdf_hex2ctrl(impl
, kdf_tls1_prf_ctrl
,
169 EVP_KDF_CTRL_SET_TLS_SECRET
, value
);
171 if (strcmp(type
, "seed") == 0)
172 return kdf_str2ctrl(impl
, kdf_tls1_prf_ctrl
, EVP_KDF_CTRL_ADD_TLS_SEED
,
175 if (strcmp(type
, "hexseed") == 0)
176 return kdf_hex2ctrl(impl
, kdf_tls1_prf_ctrl
, EVP_KDF_CTRL_ADD_TLS_SEED
,
182 static int kdf_tls1_prf_derive(EVP_KDF_IMPL
*impl
, unsigned char *key
,
185 if (impl
->md
== NULL
) {
186 KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE
, KDF_R_MISSING_MESSAGE_DIGEST
);
189 if (impl
->sec
== NULL
) {
190 KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE
, KDF_R_MISSING_SECRET
);
193 if (impl
->seedlen
== 0) {
194 KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE
, KDF_R_MISSING_SEED
);
197 return tls1_prf_alg(impl
->md
, impl
->sec
, impl
->seclen
,
198 impl
->seed
, impl
->seedlen
,
202 const EVP_KDF tls1_prf_kdf_meth
= {
208 kdf_tls1_prf_ctrl_str
,
214 * Refer to "The TLS Protocol Version 1.0" Section 5
215 * (https://tools.ietf.org/html/rfc2246#section-5) and
216 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
217 * (https://tools.ietf.org/html/rfc5246#section-5).
219 * P_<hash> is an expansion function that uses a single hash function to expand
220 * a secret and seed into an arbitrary quantity of output:
222 * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
223 * HMAC_<hash>(secret, A(2) + seed) +
224 * HMAC_<hash>(secret, A(3) + seed) + ...
226 * where + indicates concatenation. P_<hash> can be iterated as many times as
227 * is necessary to produce the required quantity of data.
229 * A(i) is defined as:
231 * A(i) = HMAC_<hash>(secret, A(i-1))
233 static int tls1_prf_P_hash(const EVP_MD
*md
,
234 const unsigned char *sec
, size_t sec_len
,
235 const unsigned char *seed
, size_t seed_len
,
236 unsigned char *out
, size_t olen
)
240 EVP_MAC_CTX
*ctx
= NULL
, *ctx_Ai
= NULL
, *ctx_init
= NULL
;
241 unsigned char Ai
[EVP_MAX_MD_SIZE
];
244 OSSL_PARAM params
[4];
246 const char *mdname
= EVP_MD_name(md
);
248 mac
= EVP_MAC_fetch(NULL
, OSSL_MAC_NAME_HMAC
, NULL
); /* Implicit fetch */
249 ctx_init
= EVP_MAC_CTX_new(mac
);
250 if (ctx_init
== NULL
)
253 /* TODO(3.0) rethink "flags", also see hmac.c in providers */
254 mac_flags
= EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
;
255 params
[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS
, &mac_flags
);
256 params
[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST
,
258 params
[2] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
259 (void *)sec
, sec_len
);
260 params
[3] = OSSL_PARAM_construct_end();
261 if (!EVP_MAC_CTX_set_params(ctx_init
, params
))
263 if (!EVP_MAC_init(ctx_init
))
265 chunk
= EVP_MAC_size(ctx_init
);
269 ctx_Ai
= EVP_MAC_CTX_dup(ctx_init
);
272 if (seed
!= NULL
&& !EVP_MAC_update(ctx_Ai
, seed
, seed_len
))
276 /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
277 if (!EVP_MAC_final(ctx_Ai
, Ai
, &Ai_len
, sizeof(Ai
)))
279 EVP_MAC_CTX_free(ctx_Ai
);
282 /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
283 ctx
= EVP_MAC_CTX_dup(ctx_init
);
286 if (!EVP_MAC_update(ctx
, Ai
, Ai_len
))
288 /* save state for calculating next A(i) value */
290 ctx_Ai
= EVP_MAC_CTX_dup(ctx
);
294 if (seed
!= NULL
&& !EVP_MAC_update(ctx
, seed
, seed_len
))
297 /* last chunk - use Ai as temp bounce buffer */
298 if (!EVP_MAC_final(ctx
, Ai
, &Ai_len
, sizeof(Ai
)))
300 memcpy(out
, Ai
, olen
);
303 if (!EVP_MAC_final(ctx
, out
, NULL
, olen
))
305 EVP_MAC_CTX_free(ctx
);
312 EVP_MAC_CTX_free(ctx
);
313 EVP_MAC_CTX_free(ctx_Ai
);
314 EVP_MAC_CTX_free(ctx_init
);
316 OPENSSL_cleanse(Ai
, sizeof(Ai
));
321 * Refer to "The TLS Protocol Version 1.0" Section 5
322 * (https://tools.ietf.org/html/rfc2246#section-5) and
323 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
324 * (https://tools.ietf.org/html/rfc5246#section-5).
326 * For TLS v1.0 and TLS v1.1:
328 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
329 * P_SHA-1(S2, label + seed)
331 * S1 is taken from the first half of the secret, S2 from the second half.
333 * L_S = length in bytes of secret;
334 * L_S1 = L_S2 = ceil(L_S / 2);
338 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
340 static int tls1_prf_alg(const EVP_MD
*md
,
341 const unsigned char *sec
, size_t slen
,
342 const unsigned char *seed
, size_t seed_len
,
343 unsigned char *out
, size_t olen
)
345 if (EVP_MD_type(md
) == NID_md5_sha1
) {
346 /* TLS v1.0 and TLS v1.1 */
349 /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
350 size_t L_S1
= (slen
+ 1) / 2;
353 if (!tls1_prf_P_hash(EVP_md5(), sec
, L_S1
,
354 seed
, seed_len
, out
, olen
))
357 if ((tmp
= OPENSSL_malloc(olen
)) == NULL
) {
358 KDFerr(KDF_F_TLS1_PRF_ALG
, ERR_R_MALLOC_FAILURE
);
361 if (!tls1_prf_P_hash(EVP_sha1(), sec
+ slen
- L_S2
, L_S2
,
362 seed
, seed_len
, tmp
, olen
)) {
363 OPENSSL_clear_free(tmp
, olen
);
366 for (i
= 0; i
< olen
; i
++)
368 OPENSSL_clear_free(tmp
, olen
);
373 if (!tls1_prf_P_hash(md
, sec
, slen
, seed
, seed_len
, out
, olen
))