2 * Copyright 2016-2021 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"
65 static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
66 static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
67 static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
68 static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
69 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
70 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
71 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
72 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
74 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
75 const unsigned char *sec, size_t slen,
76 const unsigned char *seed, size_t seed_len,
77 unsigned char *out, size_t olen);
79 #define TLS1_PRF_MAXBUF 1024
81 /* TLS KDF kdf context structure */
85 /* MAC context for the main digest */
87 /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
90 /* Secret value to use for PRF */
93 /* Buffer of concatenated seed data */
94 unsigned char seed[TLS1_PRF_MAXBUF];
98 static void *kdf_tls1_prf_new(void *provctx)
102 if (!ossl_prov_is_running())
105 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
106 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
107 ctx->provctx = provctx;
111 static void kdf_tls1_prf_free(void *vctx)
113 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
116 kdf_tls1_prf_reset(ctx);
121 static void kdf_tls1_prf_reset(void *vctx)
123 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
124 void *provctx = ctx->provctx;
126 EVP_MAC_CTX_free(ctx->P_hash);
127 EVP_MAC_CTX_free(ctx->P_sha1);
128 OPENSSL_clear_free(ctx->sec, ctx->seclen);
129 OPENSSL_cleanse(ctx->seed, ctx->seedlen);
130 memset(ctx, 0, sizeof(*ctx));
131 ctx->provctx = provctx;
134 static int kdf_tls1_prf_derive(void *vctx, unsigned char *key,
137 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
139 if (!ossl_prov_is_running())
142 if (ctx->P_hash == NULL) {
143 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
146 if (ctx->sec == NULL) {
147 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
150 if (ctx->seedlen == 0) {
151 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
155 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
159 return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
160 ctx->sec, ctx->seclen,
161 ctx->seed, ctx->seedlen,
165 static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
168 TLS1_PRF *ctx = vctx;
169 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
171 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
172 if (strcasecmp(p->data, SN_md5_sha1) == 0) {
173 if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
175 NULL, SN_md5, libctx)
176 || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
178 NULL, SN_sha1, libctx))
181 EVP_MAC_CTX_free(ctx->P_sha1);
182 if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
189 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
190 OPENSSL_clear_free(ctx->sec, ctx->seclen);
192 if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
195 /* The seed fields concatenate, so process them all */
196 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
197 for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
198 OSSL_KDF_PARAM_SEED)) {
199 const void *q = ctx->seed + ctx->seedlen;
202 if (p->data_size != 0
204 && !OSSL_PARAM_get_octet_string(p, (void **)&q,
205 TLS1_PRF_MAXBUF - ctx->seedlen,
214 static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
215 ossl_unused void *ctx, ossl_unused void *provctx)
217 static const OSSL_PARAM known_settable_ctx_params[] = {
218 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
219 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
220 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
221 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
224 return known_settable_ctx_params;
227 static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
231 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
232 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
236 static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
237 ossl_unused void *ctx, ossl_unused void *provctx)
239 static const OSSL_PARAM known_gettable_ctx_params[] = {
240 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
243 return known_gettable_ctx_params;
246 const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
247 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
248 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
249 { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
250 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
251 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
252 (void(*)(void))kdf_tls1_prf_settable_ctx_params },
253 { OSSL_FUNC_KDF_SET_CTX_PARAMS,
254 (void(*)(void))kdf_tls1_prf_set_ctx_params },
255 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
256 (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
257 { OSSL_FUNC_KDF_GET_CTX_PARAMS,
258 (void(*)(void))kdf_tls1_prf_get_ctx_params },
263 * Refer to "The TLS Protocol Version 1.0" Section 5
264 * (https://tools.ietf.org/html/rfc2246#section-5) and
265 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
266 * (https://tools.ietf.org/html/rfc5246#section-5).
268 * P_<hash> is an expansion function that uses a single hash function to expand
269 * a secret and seed into an arbitrary quantity of output:
271 * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
272 * HMAC_<hash>(secret, A(2) + seed) +
273 * HMAC_<hash>(secret, A(3) + seed) + ...
275 * where + indicates concatenation. P_<hash> can be iterated as many times as
276 * is necessary to produce the required quantity of data.
278 * A(i) is defined as:
280 * A(i) = HMAC_<hash>(secret, A(i-1))
282 static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
283 const unsigned char *sec, size_t sec_len,
284 const unsigned char *seed, size_t seed_len,
285 unsigned char *out, size_t olen)
288 EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
289 unsigned char Ai[EVP_MAX_MD_SIZE];
292 OSSL_PARAM params[2], *p = params;
294 *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
295 (void *)sec, sec_len);
296 *p = OSSL_PARAM_construct_end();
297 if (!EVP_MAC_CTX_set_params(ctx_init, params))
299 if (!EVP_MAC_init(ctx_init))
301 chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
305 ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
308 if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
312 /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
313 if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
315 EVP_MAC_CTX_free(ctx_Ai);
318 /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
319 ctx = EVP_MAC_CTX_dup(ctx_init);
322 if (!EVP_MAC_update(ctx, Ai, Ai_len))
324 /* save state for calculating next A(i) value */
326 ctx_Ai = EVP_MAC_CTX_dup(ctx);
330 if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
333 /* last chunk - use Ai as temp bounce buffer */
334 if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
336 memcpy(out, Ai, olen);
339 if (!EVP_MAC_final(ctx, out, NULL, olen))
341 EVP_MAC_CTX_free(ctx);
348 EVP_MAC_CTX_free(ctx);
349 EVP_MAC_CTX_free(ctx_Ai);
350 OPENSSL_cleanse(Ai, sizeof(Ai));
355 * Refer to "The TLS Protocol Version 1.0" Section 5
356 * (https://tools.ietf.org/html/rfc2246#section-5) and
357 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
358 * (https://tools.ietf.org/html/rfc5246#section-5).
360 * For TLS v1.0 and TLS v1.1:
362 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
363 * P_SHA-1(S2, label + seed)
365 * S1 is taken from the first half of the secret, S2 from the second half.
367 * L_S = length in bytes of secret;
368 * L_S1 = L_S2 = ceil(L_S / 2);
372 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
374 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
375 const unsigned char *sec, size_t slen,
376 const unsigned char *seed, size_t seed_len,
377 unsigned char *out, size_t olen)
379 if (sha1ctx != NULL) {
380 /* TLS v1.0 and TLS v1.1 */
383 /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
384 size_t L_S1 = (slen + 1) / 2;
387 if (!tls1_prf_P_hash(mdctx, sec, L_S1,
388 seed, seed_len, out, olen))
391 if ((tmp = OPENSSL_malloc(olen)) == NULL) {
392 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
396 if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
397 seed, seed_len, tmp, olen)) {
398 OPENSSL_clear_free(tmp, olen);
401 for (i = 0; i < olen; i++)
403 OPENSSL_clear_free(tmp, olen);
408 if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))