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 "internal/evp_int.h"
55 #include "kdf_local.h"
57 static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl);
58 static int tls1_prf_alg(const EVP_MD *md,
59 const unsigned char *sec, size_t slen,
60 const unsigned char *seed, size_t seed_len,
61 unsigned char *out, size_t olen);
63 #define TLS1_PRF_MAXBUF 1024
65 /* TLS KDF kdf context structure */
67 struct evp_kdf_impl_st {
68 /* Digest to use for PRF */
70 /* Secret value to use for PRF */
73 /* Buffer of concatenated seed data */
74 unsigned char seed[TLS1_PRF_MAXBUF];
78 static EVP_KDF_IMPL *kdf_tls1_prf_new(void)
82 if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
83 KDFerr(KDF_F_KDF_TLS1_PRF_NEW, ERR_R_MALLOC_FAILURE);
87 static void kdf_tls1_prf_free(EVP_KDF_IMPL *impl)
89 kdf_tls1_prf_reset(impl);
93 static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl)
95 OPENSSL_clear_free(impl->sec, impl->seclen);
96 OPENSSL_cleanse(impl->seed, impl->seedlen);
97 memset(impl, 0, sizeof(*impl));
100 static int kdf_tls1_prf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
102 const unsigned char *p;
107 case EVP_KDF_CTRL_SET_MD:
108 md = va_arg(args, const EVP_MD *);
115 case EVP_KDF_CTRL_SET_TLS_SECRET:
116 p = va_arg(args, const unsigned char *);
117 len = va_arg(args, size_t);
118 OPENSSL_clear_free(impl->sec, impl->seclen);
119 impl->sec = OPENSSL_memdup(p, len);
120 if (impl->sec == NULL)
126 case EVP_KDF_CTRL_RESET_TLS_SEED:
127 OPENSSL_cleanse(impl->seed, impl->seedlen);
131 case EVP_KDF_CTRL_ADD_TLS_SEED:
132 p = va_arg(args, const unsigned char *);
133 len = va_arg(args, size_t);
134 if (len == 0 || p == NULL)
137 if (len > (TLS1_PRF_MAXBUF - impl->seedlen))
140 memcpy(impl->seed + impl->seedlen, p, len);
141 impl->seedlen += len;
149 static int kdf_tls1_prf_ctrl_str(EVP_KDF_IMPL *impl,
150 const char *type, const char *value)
153 KDFerr(KDF_F_KDF_TLS1_PRF_CTRL_STR, KDF_R_VALUE_MISSING);
156 if (strcmp(type, "digest") == 0)
157 return kdf_md2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_SET_MD, value);
159 if (strcmp(type, "secret") == 0)
160 return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl,
161 EVP_KDF_CTRL_SET_TLS_SECRET, value);
163 if (strcmp(type, "hexsecret") == 0)
164 return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl,
165 EVP_KDF_CTRL_SET_TLS_SECRET, value);
167 if (strcmp(type, "seed") == 0)
168 return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
171 if (strcmp(type, "hexseed") == 0)
172 return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
178 static int kdf_tls1_prf_derive(EVP_KDF_IMPL *impl, unsigned char *key,
181 if (impl->md == NULL) {
182 KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
185 if (impl->sec == NULL) {
186 KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SECRET);
189 if (impl->seedlen == 0) {
190 KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SEED);
193 return tls1_prf_alg(impl->md, impl->sec, impl->seclen,
194 impl->seed, impl->seedlen,
198 const EVP_KDF tls1_prf_kdf_meth = {
204 kdf_tls1_prf_ctrl_str,
210 * Refer to "The TLS Protocol Version 1.0" Section 5
211 * (https://tools.ietf.org/html/rfc2246#section-5) and
212 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
213 * (https://tools.ietf.org/html/rfc5246#section-5).
215 * P_<hash> is an expansion function that uses a single hash function to expand
216 * a secret and seed into an arbitrary quantity of output:
218 * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
219 * HMAC_<hash>(secret, A(2) + seed) +
220 * HMAC_<hash>(secret, A(3) + seed) + ...
222 * where + indicates concatenation. P_<hash> can be iterated as many times as
223 * is necessary to produce the required quantity of data.
225 * A(i) is defined as:
227 * A(i) = HMAC_<hash>(secret, A(i-1))
229 static int tls1_prf_P_hash(const EVP_MD *md,
230 const unsigned char *sec, size_t sec_len,
231 const unsigned char *seed, size_t seed_len,
232 unsigned char *out, size_t olen)
235 EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL, *ctx_init = NULL;
236 unsigned char Ai[EVP_MAX_MD_SIZE];
240 ctx = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
241 ctx_Ai = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
242 ctx_init = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
243 if (ctx == NULL || ctx_Ai == NULL || ctx_init == NULL)
245 if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_FLAGS, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW) != 1)
247 if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_MD, md) != 1)
249 if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_KEY, sec, sec_len) != 1)
251 if (!EVP_MAC_init(ctx_init))
253 chunk = EVP_MAC_size(ctx_init);
257 if (!EVP_MAC_CTX_copy(ctx_Ai, ctx_init))
259 if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
263 /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
264 if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len))
267 /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
268 if (!EVP_MAC_CTX_copy(ctx, ctx_init))
270 if (!EVP_MAC_update(ctx, Ai, Ai_len))
272 /* save state for calculating next A(i) value */
273 if (olen > chunk && !EVP_MAC_CTX_copy(ctx_Ai, ctx))
275 if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
278 /* last chunk - use Ai as temp bounce buffer */
279 if (!EVP_MAC_final(ctx, Ai, &Ai_len))
281 memcpy(out, Ai, olen);
284 if (!EVP_MAC_final(ctx, out, NULL))
291 EVP_MAC_CTX_free(ctx);
292 EVP_MAC_CTX_free(ctx_Ai);
293 EVP_MAC_CTX_free(ctx_init);
294 OPENSSL_cleanse(Ai, sizeof(Ai));
299 * Refer to "The TLS Protocol Version 1.0" Section 5
300 * (https://tools.ietf.org/html/rfc2246#section-5) and
301 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
302 * (https://tools.ietf.org/html/rfc5246#section-5).
304 * For TLS v1.0 and TLS v1.1:
306 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
307 * P_SHA-1(S2, label + seed)
309 * S1 is taken from the first half of the secret, S2 from the second half.
311 * L_S = length in bytes of secret;
312 * L_S1 = L_S2 = ceil(L_S / 2);
316 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
318 static int tls1_prf_alg(const EVP_MD *md,
319 const unsigned char *sec, size_t slen,
320 const unsigned char *seed, size_t seed_len,
321 unsigned char *out, size_t olen)
323 if (EVP_MD_type(md) == NID_md5_sha1) {
324 /* TLS v1.0 and TLS v1.1 */
327 /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
328 size_t L_S1 = (slen + 1) / 2;
331 if (!tls1_prf_P_hash(EVP_md5(), sec, L_S1,
332 seed, seed_len, out, olen))
335 if ((tmp = OPENSSL_malloc(olen)) == NULL) {
336 KDFerr(KDF_F_TLS1_PRF_ALG, ERR_R_MALLOC_FAILURE);
339 if (!tls1_prf_P_hash(EVP_sha1(), sec + slen - L_S2, L_S2,
340 seed, seed_len, tmp, olen)) {
341 OPENSSL_clear_free(tmp, olen);
344 for (i = 0; i < olen; i++)
346 OPENSSL_clear_free(tmp, olen);
351 if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))