1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos /*
11*b0d17251Schristos * Refer to "The TLS Protocol Version 1.0" Section 5
12*b0d17251Schristos * (https://tools.ietf.org/html/rfc2246#section-5) and
13*b0d17251Schristos * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
14*b0d17251Schristos * (https://tools.ietf.org/html/rfc5246#section-5).
15*b0d17251Schristos *
16*b0d17251Schristos * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
17*b0d17251Schristos *
18*b0d17251Schristos * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
19*b0d17251Schristos * P_SHA-1(S2, label + seed)
20*b0d17251Schristos *
21*b0d17251Schristos * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
22*b0d17251Schristos * two halves of the secret (with the possibility of one shared byte, in the
23*b0d17251Schristos * case where the length of the original secret is odd). S1 is taken from the
24*b0d17251Schristos * first half of the secret, S2 from the second half.
25*b0d17251Schristos *
26*b0d17251Schristos * For TLS v1.2 the TLS PRF algorithm is given by:
27*b0d17251Schristos *
28*b0d17251Schristos * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
29*b0d17251Schristos *
30*b0d17251Schristos * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
31*b0d17251Schristos * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
32*b0d17251Schristos * unless defined otherwise by the cipher suite.
33*b0d17251Schristos *
34*b0d17251Schristos * P_<hash> is an expansion function that uses a single hash function to expand
35*b0d17251Schristos * a secret and seed into an arbitrary quantity of output:
36*b0d17251Schristos *
37*b0d17251Schristos * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
38*b0d17251Schristos * HMAC_<hash>(secret, A(2) + seed) +
39*b0d17251Schristos * HMAC_<hash>(secret, A(3) + seed) + ...
40*b0d17251Schristos *
41*b0d17251Schristos * where + indicates concatenation. P_<hash> can be iterated as many times as
42*b0d17251Schristos * is necessary to produce the required quantity of data.
43*b0d17251Schristos *
44*b0d17251Schristos * A(i) is defined as:
45*b0d17251Schristos * A(0) = seed
46*b0d17251Schristos * A(i) = HMAC_<hash>(secret, A(i-1))
47*b0d17251Schristos */
48*b0d17251Schristos #include <stdio.h>
49*b0d17251Schristos #include <stdarg.h>
50*b0d17251Schristos #include <string.h>
51*b0d17251Schristos #include <openssl/evp.h>
52*b0d17251Schristos #include <openssl/kdf.h>
53*b0d17251Schristos #include <openssl/core_names.h>
54*b0d17251Schristos #include <openssl/params.h>
55*b0d17251Schristos #include <openssl/proverr.h>
56*b0d17251Schristos #include "internal/cryptlib.h"
57*b0d17251Schristos #include "internal/numbers.h"
58*b0d17251Schristos #include "crypto/evp.h"
59*b0d17251Schristos #include "prov/provider_ctx.h"
60*b0d17251Schristos #include "prov/providercommon.h"
61*b0d17251Schristos #include "prov/implementations.h"
62*b0d17251Schristos #include "prov/provider_util.h"
63*b0d17251Schristos #include "e_os.h"
64*b0d17251Schristos
65*b0d17251Schristos static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
66*b0d17251Schristos static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
67*b0d17251Schristos static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
68*b0d17251Schristos static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
69*b0d17251Schristos static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
70*b0d17251Schristos static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
71*b0d17251Schristos static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
72*b0d17251Schristos static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
73*b0d17251Schristos
74*b0d17251Schristos static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
75*b0d17251Schristos const unsigned char *sec, size_t slen,
76*b0d17251Schristos const unsigned char *seed, size_t seed_len,
77*b0d17251Schristos unsigned char *out, size_t olen);
78*b0d17251Schristos
79*b0d17251Schristos #define TLS1_PRF_MAXBUF 1024
80*b0d17251Schristos
81*b0d17251Schristos /* TLS KDF kdf context structure */
82*b0d17251Schristos typedef struct {
83*b0d17251Schristos void *provctx;
84*b0d17251Schristos
85*b0d17251Schristos /* MAC context for the main digest */
86*b0d17251Schristos EVP_MAC_CTX *P_hash;
87*b0d17251Schristos /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
88*b0d17251Schristos EVP_MAC_CTX *P_sha1;
89*b0d17251Schristos
90*b0d17251Schristos /* Secret value to use for PRF */
91*b0d17251Schristos unsigned char *sec;
92*b0d17251Schristos size_t seclen;
93*b0d17251Schristos /* Buffer of concatenated seed data */
94*b0d17251Schristos unsigned char seed[TLS1_PRF_MAXBUF];
95*b0d17251Schristos size_t seedlen;
96*b0d17251Schristos } TLS1_PRF;
97*b0d17251Schristos
kdf_tls1_prf_new(void * provctx)98*b0d17251Schristos static void *kdf_tls1_prf_new(void *provctx)
99*b0d17251Schristos {
100*b0d17251Schristos TLS1_PRF *ctx;
101*b0d17251Schristos
102*b0d17251Schristos if (!ossl_prov_is_running())
103*b0d17251Schristos return NULL;
104*b0d17251Schristos
105*b0d17251Schristos if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
106*b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
107*b0d17251Schristos return NULL;
108*b0d17251Schristos }
109*b0d17251Schristos ctx->provctx = provctx;
110*b0d17251Schristos return ctx;
111*b0d17251Schristos }
112*b0d17251Schristos
kdf_tls1_prf_free(void * vctx)113*b0d17251Schristos static void kdf_tls1_prf_free(void *vctx)
114*b0d17251Schristos {
115*b0d17251Schristos TLS1_PRF *ctx = (TLS1_PRF *)vctx;
116*b0d17251Schristos
117*b0d17251Schristos if (ctx != NULL) {
118*b0d17251Schristos kdf_tls1_prf_reset(ctx);
119*b0d17251Schristos OPENSSL_free(ctx);
120*b0d17251Schristos }
121*b0d17251Schristos }
122*b0d17251Schristos
kdf_tls1_prf_reset(void * vctx)123*b0d17251Schristos static void kdf_tls1_prf_reset(void *vctx)
124*b0d17251Schristos {
125*b0d17251Schristos TLS1_PRF *ctx = (TLS1_PRF *)vctx;
126*b0d17251Schristos void *provctx = ctx->provctx;
127*b0d17251Schristos
128*b0d17251Schristos EVP_MAC_CTX_free(ctx->P_hash);
129*b0d17251Schristos EVP_MAC_CTX_free(ctx->P_sha1);
130*b0d17251Schristos OPENSSL_clear_free(ctx->sec, ctx->seclen);
131*b0d17251Schristos OPENSSL_cleanse(ctx->seed, ctx->seedlen);
132*b0d17251Schristos memset(ctx, 0, sizeof(*ctx));
133*b0d17251Schristos ctx->provctx = provctx;
134*b0d17251Schristos }
135*b0d17251Schristos
kdf_tls1_prf_derive(void * vctx,unsigned char * key,size_t keylen,const OSSL_PARAM params[])136*b0d17251Schristos static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
137*b0d17251Schristos const OSSL_PARAM params[])
138*b0d17251Schristos {
139*b0d17251Schristos TLS1_PRF *ctx = (TLS1_PRF *)vctx;
140*b0d17251Schristos
141*b0d17251Schristos if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
142*b0d17251Schristos return 0;
143*b0d17251Schristos
144*b0d17251Schristos if (ctx->P_hash == NULL) {
145*b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
146*b0d17251Schristos return 0;
147*b0d17251Schristos }
148*b0d17251Schristos if (ctx->sec == NULL) {
149*b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
150*b0d17251Schristos return 0;
151*b0d17251Schristos }
152*b0d17251Schristos if (ctx->seedlen == 0) {
153*b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
154*b0d17251Schristos return 0;
155*b0d17251Schristos }
156*b0d17251Schristos if (keylen == 0) {
157*b0d17251Schristos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
158*b0d17251Schristos return 0;
159*b0d17251Schristos }
160*b0d17251Schristos
161*b0d17251Schristos return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
162*b0d17251Schristos ctx->sec, ctx->seclen,
163*b0d17251Schristos ctx->seed, ctx->seedlen,
164*b0d17251Schristos key, keylen);
165*b0d17251Schristos }
166*b0d17251Schristos
kdf_tls1_prf_set_ctx_params(void * vctx,const OSSL_PARAM params[])167*b0d17251Schristos static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
168*b0d17251Schristos {
169*b0d17251Schristos const OSSL_PARAM *p;
170*b0d17251Schristos TLS1_PRF *ctx = vctx;
171*b0d17251Schristos OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
172*b0d17251Schristos
173*b0d17251Schristos if (params == NULL)
174*b0d17251Schristos return 1;
175*b0d17251Schristos
176*b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
177*b0d17251Schristos if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
178*b0d17251Schristos if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
179*b0d17251Schristos OSSL_MAC_NAME_HMAC,
180*b0d17251Schristos NULL, SN_md5, libctx)
181*b0d17251Schristos || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
182*b0d17251Schristos OSSL_MAC_NAME_HMAC,
183*b0d17251Schristos NULL, SN_sha1, libctx))
184*b0d17251Schristos return 0;
185*b0d17251Schristos } else {
186*b0d17251Schristos EVP_MAC_CTX_free(ctx->P_sha1);
187*b0d17251Schristos if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
188*b0d17251Schristos OSSL_MAC_NAME_HMAC,
189*b0d17251Schristos NULL, NULL, libctx))
190*b0d17251Schristos return 0;
191*b0d17251Schristos }
192*b0d17251Schristos }
193*b0d17251Schristos
194*b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
195*b0d17251Schristos OPENSSL_clear_free(ctx->sec, ctx->seclen);
196*b0d17251Schristos ctx->sec = NULL;
197*b0d17251Schristos if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
198*b0d17251Schristos return 0;
199*b0d17251Schristos }
200*b0d17251Schristos /* The seed fields concatenate, so process them all */
201*b0d17251Schristos if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
202*b0d17251Schristos for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
203*b0d17251Schristos OSSL_KDF_PARAM_SEED)) {
204*b0d17251Schristos const void *q = ctx->seed + ctx->seedlen;
205*b0d17251Schristos size_t sz = 0;
206*b0d17251Schristos
207*b0d17251Schristos if (p->data_size != 0
208*b0d17251Schristos && p->data != NULL
209*b0d17251Schristos && !OSSL_PARAM_get_octet_string(p, (void **)&q,
210*b0d17251Schristos TLS1_PRF_MAXBUF - ctx->seedlen,
211*b0d17251Schristos &sz))
212*b0d17251Schristos return 0;
213*b0d17251Schristos ctx->seedlen += sz;
214*b0d17251Schristos }
215*b0d17251Schristos }
216*b0d17251Schristos return 1;
217*b0d17251Schristos }
218*b0d17251Schristos
kdf_tls1_prf_settable_ctx_params(ossl_unused void * ctx,ossl_unused void * provctx)219*b0d17251Schristos static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
220*b0d17251Schristos ossl_unused void *ctx, ossl_unused void *provctx)
221*b0d17251Schristos {
222*b0d17251Schristos static const OSSL_PARAM known_settable_ctx_params[] = {
223*b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
224*b0d17251Schristos OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
225*b0d17251Schristos OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
226*b0d17251Schristos OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
227*b0d17251Schristos OSSL_PARAM_END
228*b0d17251Schristos };
229*b0d17251Schristos return known_settable_ctx_params;
230*b0d17251Schristos }
231*b0d17251Schristos
kdf_tls1_prf_get_ctx_params(void * vctx,OSSL_PARAM params[])232*b0d17251Schristos static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
233*b0d17251Schristos {
234*b0d17251Schristos OSSL_PARAM *p;
235*b0d17251Schristos
236*b0d17251Schristos if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
237*b0d17251Schristos return OSSL_PARAM_set_size_t(p, SIZE_MAX);
238*b0d17251Schristos return -2;
239*b0d17251Schristos }
240*b0d17251Schristos
kdf_tls1_prf_gettable_ctx_params(ossl_unused void * ctx,ossl_unused void * provctx)241*b0d17251Schristos static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
242*b0d17251Schristos ossl_unused void *ctx, ossl_unused void *provctx)
243*b0d17251Schristos {
244*b0d17251Schristos static const OSSL_PARAM known_gettable_ctx_params[] = {
245*b0d17251Schristos OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
246*b0d17251Schristos OSSL_PARAM_END
247*b0d17251Schristos };
248*b0d17251Schristos return known_gettable_ctx_params;
249*b0d17251Schristos }
250*b0d17251Schristos
251*b0d17251Schristos const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
252*b0d17251Schristos { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
253*b0d17251Schristos { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
254*b0d17251Schristos { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
255*b0d17251Schristos { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
256*b0d17251Schristos { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
257*b0d17251Schristos (void(*)(void))kdf_tls1_prf_settable_ctx_params },
258*b0d17251Schristos { OSSL_FUNC_KDF_SET_CTX_PARAMS,
259*b0d17251Schristos (void(*)(void))kdf_tls1_prf_set_ctx_params },
260*b0d17251Schristos { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
261*b0d17251Schristos (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
262*b0d17251Schristos { OSSL_FUNC_KDF_GET_CTX_PARAMS,
263*b0d17251Schristos (void(*)(void))kdf_tls1_prf_get_ctx_params },
264*b0d17251Schristos { 0, NULL }
265*b0d17251Schristos };
266*b0d17251Schristos
267*b0d17251Schristos /*
268*b0d17251Schristos * Refer to "The TLS Protocol Version 1.0" Section 5
269*b0d17251Schristos * (https://tools.ietf.org/html/rfc2246#section-5) and
270*b0d17251Schristos * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
271*b0d17251Schristos * (https://tools.ietf.org/html/rfc5246#section-5).
272*b0d17251Schristos *
273*b0d17251Schristos * P_<hash> is an expansion function that uses a single hash function to expand
274*b0d17251Schristos * a secret and seed into an arbitrary quantity of output:
275*b0d17251Schristos *
276*b0d17251Schristos * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
277*b0d17251Schristos * HMAC_<hash>(secret, A(2) + seed) +
278*b0d17251Schristos * HMAC_<hash>(secret, A(3) + seed) + ...
279*b0d17251Schristos *
280*b0d17251Schristos * where + indicates concatenation. P_<hash> can be iterated as many times as
281*b0d17251Schristos * is necessary to produce the required quantity of data.
282*b0d17251Schristos *
283*b0d17251Schristos * A(i) is defined as:
284*b0d17251Schristos * A(0) = seed
285*b0d17251Schristos * A(i) = HMAC_<hash>(secret, A(i-1))
286*b0d17251Schristos */
tls1_prf_P_hash(EVP_MAC_CTX * ctx_init,const unsigned char * sec,size_t sec_len,const unsigned char * seed,size_t seed_len,unsigned char * out,size_t olen)287*b0d17251Schristos static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
288*b0d17251Schristos const unsigned char *sec, size_t sec_len,
289*b0d17251Schristos const unsigned char *seed, size_t seed_len,
290*b0d17251Schristos unsigned char *out, size_t olen)
291*b0d17251Schristos {
292*b0d17251Schristos size_t chunk;
293*b0d17251Schristos EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
294*b0d17251Schristos unsigned char Ai[EVP_MAX_MD_SIZE];
295*b0d17251Schristos size_t Ai_len;
296*b0d17251Schristos int ret = 0;
297*b0d17251Schristos
298*b0d17251Schristos if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
299*b0d17251Schristos goto err;
300*b0d17251Schristos chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
301*b0d17251Schristos if (chunk == 0)
302*b0d17251Schristos goto err;
303*b0d17251Schristos /* A(0) = seed */
304*b0d17251Schristos ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
305*b0d17251Schristos if (ctx_Ai == NULL)
306*b0d17251Schristos goto err;
307*b0d17251Schristos if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
308*b0d17251Schristos goto err;
309*b0d17251Schristos
310*b0d17251Schristos for (;;) {
311*b0d17251Schristos /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
312*b0d17251Schristos if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
313*b0d17251Schristos goto err;
314*b0d17251Schristos EVP_MAC_CTX_free(ctx_Ai);
315*b0d17251Schristos ctx_Ai = NULL;
316*b0d17251Schristos
317*b0d17251Schristos /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
318*b0d17251Schristos ctx = EVP_MAC_CTX_dup(ctx_init);
319*b0d17251Schristos if (ctx == NULL)
320*b0d17251Schristos goto err;
321*b0d17251Schristos if (!EVP_MAC_update(ctx, Ai, Ai_len))
322*b0d17251Schristos goto err;
323*b0d17251Schristos /* save state for calculating next A(i) value */
324*b0d17251Schristos if (olen > chunk) {
325*b0d17251Schristos ctx_Ai = EVP_MAC_CTX_dup(ctx);
326*b0d17251Schristos if (ctx_Ai == NULL)
327*b0d17251Schristos goto err;
328*b0d17251Schristos }
329*b0d17251Schristos if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
330*b0d17251Schristos goto err;
331*b0d17251Schristos if (olen <= chunk) {
332*b0d17251Schristos /* last chunk - use Ai as temp bounce buffer */
333*b0d17251Schristos if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
334*b0d17251Schristos goto err;
335*b0d17251Schristos memcpy(out, Ai, olen);
336*b0d17251Schristos break;
337*b0d17251Schristos }
338*b0d17251Schristos if (!EVP_MAC_final(ctx, out, NULL, olen))
339*b0d17251Schristos goto err;
340*b0d17251Schristos EVP_MAC_CTX_free(ctx);
341*b0d17251Schristos ctx = NULL;
342*b0d17251Schristos out += chunk;
343*b0d17251Schristos olen -= chunk;
344*b0d17251Schristos }
345*b0d17251Schristos ret = 1;
346*b0d17251Schristos err:
347*b0d17251Schristos EVP_MAC_CTX_free(ctx);
348*b0d17251Schristos EVP_MAC_CTX_free(ctx_Ai);
349*b0d17251Schristos OPENSSL_cleanse(Ai, sizeof(Ai));
350*b0d17251Schristos return ret;
351*b0d17251Schristos }
352*b0d17251Schristos
353*b0d17251Schristos /*
354*b0d17251Schristos * Refer to "The TLS Protocol Version 1.0" Section 5
355*b0d17251Schristos * (https://tools.ietf.org/html/rfc2246#section-5) and
356*b0d17251Schristos * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
357*b0d17251Schristos * (https://tools.ietf.org/html/rfc5246#section-5).
358*b0d17251Schristos *
359*b0d17251Schristos * For TLS v1.0 and TLS v1.1:
360*b0d17251Schristos *
361*b0d17251Schristos * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
362*b0d17251Schristos * P_SHA-1(S2, label + seed)
363*b0d17251Schristos *
364*b0d17251Schristos * S1 is taken from the first half of the secret, S2 from the second half.
365*b0d17251Schristos *
366*b0d17251Schristos * L_S = length in bytes of secret;
367*b0d17251Schristos * L_S1 = L_S2 = ceil(L_S / 2);
368*b0d17251Schristos *
369*b0d17251Schristos * For TLS v1.2:
370*b0d17251Schristos *
371*b0d17251Schristos * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
372*b0d17251Schristos */
tls1_prf_alg(EVP_MAC_CTX * mdctx,EVP_MAC_CTX * sha1ctx,const unsigned char * sec,size_t slen,const unsigned char * seed,size_t seed_len,unsigned char * out,size_t olen)373*b0d17251Schristos static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
374*b0d17251Schristos const unsigned char *sec, size_t slen,
375*b0d17251Schristos const unsigned char *seed, size_t seed_len,
376*b0d17251Schristos unsigned char *out, size_t olen)
377*b0d17251Schristos {
378*b0d17251Schristos if (sha1ctx != NULL) {
379*b0d17251Schristos /* TLS v1.0 and TLS v1.1 */
380*b0d17251Schristos size_t i;
381*b0d17251Schristos unsigned char *tmp;
382*b0d17251Schristos /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
383*b0d17251Schristos size_t L_S1 = (slen + 1) / 2;
384*b0d17251Schristos size_t L_S2 = L_S1;
385*b0d17251Schristos
386*b0d17251Schristos if (!tls1_prf_P_hash(mdctx, sec, L_S1,
387*b0d17251Schristos seed, seed_len, out, olen))
388*b0d17251Schristos return 0;
389*b0d17251Schristos
390*b0d17251Schristos if ((tmp = OPENSSL_malloc(olen)) == NULL) {
391*b0d17251Schristos ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
392*b0d17251Schristos return 0;
393*b0d17251Schristos }
394*b0d17251Schristos
395*b0d17251Schristos if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
396*b0d17251Schristos seed, seed_len, tmp, olen)) {
397*b0d17251Schristos OPENSSL_clear_free(tmp, olen);
398*b0d17251Schristos return 0;
399*b0d17251Schristos }
400*b0d17251Schristos for (i = 0; i < olen; i++)
401*b0d17251Schristos out[i] ^= tmp[i];
402*b0d17251Schristos OPENSSL_clear_free(tmp, olen);
403*b0d17251Schristos return 1;
404*b0d17251Schristos }
405*b0d17251Schristos
406*b0d17251Schristos /* TLS v1.2 */
407*b0d17251Schristos if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
408*b0d17251Schristos return 0;
409*b0d17251Schristos
410*b0d17251Schristos return 1;
411*b0d17251Schristos }
412