]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/kdfs/tls1_prf.c
Update copyright year
[thirdparty/openssl.git] / providers / implementations / kdfs / tls1_prf.c
1 /*
2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /*
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).
15 *
16 * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
17 *
18 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
19 * P_SHA-1(S2, label + seed)
20 *
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.
25 *
26 * For TLS v1.2 the TLS PRF algorithm is given by:
27 *
28 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
29 *
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.
33 *
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:
36 *
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) + ...
40 *
41 * where + indicates concatenation. P_<hash> can be iterated as many times as
42 * is necessary to produce the required quantity of data.
43 *
44 * A(i) is defined as:
45 * A(0) = seed
46 * A(i) = HMAC_<hash>(secret, A(i-1))
47 */
48 #include <stdio.h>
49 #include <stdarg.h>
50 #include <string.h>
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"
64
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;
74
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);
79
80 #define TLS1_PRF_MAXBUF 1024
81
82 /* TLS KDF kdf context structure */
83 typedef struct {
84 void *provctx;
85
86 /* MAC context for the main digest */
87 EVP_MAC_CTX *P_hash;
88 /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
89 EVP_MAC_CTX *P_sha1;
90
91 /* Secret value to use for PRF */
92 unsigned char *sec;
93 size_t seclen;
94 /* Buffer of concatenated seed data */
95 unsigned char seed[TLS1_PRF_MAXBUF];
96 size_t seedlen;
97 } TLS1_PRF;
98
99 static void *kdf_tls1_prf_new(void *provctx)
100 {
101 TLS1_PRF *ctx;
102
103 if (!ossl_prov_is_running())
104 return NULL;
105
106 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
107 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
108 ctx->provctx = provctx;
109 return ctx;
110 }
111
112 static void kdf_tls1_prf_free(void *vctx)
113 {
114 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
115
116 if (ctx != NULL) {
117 kdf_tls1_prf_reset(ctx);
118 OPENSSL_free(ctx);
119 }
120 }
121
122 static void kdf_tls1_prf_reset(void *vctx)
123 {
124 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
125 void *provctx = ctx->provctx;
126
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;
133 }
134
135 static void *kdf_tls1_prf_dup(void *vctx)
136 {
137 const TLS1_PRF *src = (const TLS1_PRF *)vctx;
138 TLS1_PRF *dest;
139
140 dest = kdf_tls1_prf_new(src->provctx);
141 if (dest != NULL) {
142 if (src->P_hash != NULL
143 && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
144 goto err;
145 if (src->P_sha1 != NULL
146 && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
147 goto err;
148 if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
149 goto err;
150 memcpy(dest->seed, src->seed, src->seedlen);
151 dest->seedlen = src->seedlen;
152 }
153 return dest;
154
155 err:
156 kdf_tls1_prf_free(dest);
157 return NULL;
158 }
159
160 static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
161 const OSSL_PARAM params[])
162 {
163 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
164
165 if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
166 return 0;
167
168 if (ctx->P_hash == NULL) {
169 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
170 return 0;
171 }
172 if (ctx->sec == NULL) {
173 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
174 return 0;
175 }
176 if (ctx->seedlen == 0) {
177 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
178 return 0;
179 }
180 if (keylen == 0) {
181 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
182 return 0;
183 }
184
185 return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
186 ctx->sec, ctx->seclen,
187 ctx->seed, ctx->seedlen,
188 key, keylen);
189 }
190
191 static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
192 {
193 const OSSL_PARAM *p;
194 TLS1_PRF *ctx = vctx;
195 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
196
197 if (params == NULL)
198 return 1;
199
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,
203 OSSL_MAC_NAME_HMAC,
204 NULL, SN_md5, libctx)
205 || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
206 OSSL_MAC_NAME_HMAC,
207 NULL, SN_sha1, libctx))
208 return 0;
209 } else {
210 EVP_MAC_CTX_free(ctx->P_sha1);
211 if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
212 OSSL_MAC_NAME_HMAC,
213 NULL, NULL, libctx))
214 return 0;
215 }
216 }
217
218 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
219 OPENSSL_clear_free(ctx->sec, ctx->seclen);
220 ctx->sec = NULL;
221 if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
222 return 0;
223 }
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;
229 size_t sz = 0;
230
231 if (p->data_size != 0
232 && p->data != NULL
233 && !OSSL_PARAM_get_octet_string(p, (void **)&q,
234 TLS1_PRF_MAXBUF - ctx->seedlen,
235 &sz))
236 return 0;
237 ctx->seedlen += sz;
238 }
239 }
240 return 1;
241 }
242
243 static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
244 ossl_unused void *ctx, ossl_unused void *provctx)
245 {
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),
251 OSSL_PARAM_END
252 };
253 return known_settable_ctx_params;
254 }
255
256 static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
257 {
258 OSSL_PARAM *p;
259
260 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
261 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
262 return -2;
263 }
264
265 static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
266 ossl_unused void *ctx, ossl_unused void *provctx)
267 {
268 static const OSSL_PARAM known_gettable_ctx_params[] = {
269 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
270 OSSL_PARAM_END
271 };
272 return known_gettable_ctx_params;
273 }
274
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 },
289 { 0, NULL }
290 };
291
292 /*
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).
297 *
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:
300 *
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) + ...
304 *
305 * where + indicates concatenation. P_<hash> can be iterated as many times as
306 * is necessary to produce the required quantity of data.
307 *
308 * A(i) is defined as:
309 * A(0) = seed
310 * A(i) = HMAC_<hash>(secret, A(i-1))
311 */
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)
316 {
317 size_t chunk;
318 EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
319 unsigned char Ai[EVP_MAX_MD_SIZE];
320 size_t Ai_len;
321 int ret = 0;
322
323 if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
324 goto err;
325 chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
326 if (chunk == 0)
327 goto err;
328 /* A(0) = seed */
329 ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
330 if (ctx_Ai == NULL)
331 goto err;
332 if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
333 goto err;
334
335 for (;;) {
336 /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
337 if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
338 goto err;
339 EVP_MAC_CTX_free(ctx_Ai);
340 ctx_Ai = NULL;
341
342 /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
343 ctx = EVP_MAC_CTX_dup(ctx_init);
344 if (ctx == NULL)
345 goto err;
346 if (!EVP_MAC_update(ctx, Ai, Ai_len))
347 goto err;
348 /* save state for calculating next A(i) value */
349 if (olen > chunk) {
350 ctx_Ai = EVP_MAC_CTX_dup(ctx);
351 if (ctx_Ai == NULL)
352 goto err;
353 }
354 if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
355 goto err;
356 if (olen <= chunk) {
357 /* last chunk - use Ai as temp bounce buffer */
358 if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
359 goto err;
360 memcpy(out, Ai, olen);
361 break;
362 }
363 if (!EVP_MAC_final(ctx, out, NULL, olen))
364 goto err;
365 EVP_MAC_CTX_free(ctx);
366 ctx = NULL;
367 out += chunk;
368 olen -= chunk;
369 }
370 ret = 1;
371 err:
372 EVP_MAC_CTX_free(ctx);
373 EVP_MAC_CTX_free(ctx_Ai);
374 OPENSSL_cleanse(Ai, sizeof(Ai));
375 return ret;
376 }
377
378 /*
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).
383 *
384 * For TLS v1.0 and TLS v1.1:
385 *
386 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
387 * P_SHA-1(S2, label + seed)
388 *
389 * S1 is taken from the first half of the secret, S2 from the second half.
390 *
391 * L_S = length in bytes of secret;
392 * L_S1 = L_S2 = ceil(L_S / 2);
393 *
394 * For TLS v1.2:
395 *
396 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
397 */
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)
402 {
403 if (sha1ctx != NULL) {
404 /* TLS v1.0 and TLS v1.1 */
405 size_t i;
406 unsigned char *tmp;
407 /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
408 size_t L_S1 = (slen + 1) / 2;
409 size_t L_S2 = L_S1;
410
411 if (!tls1_prf_P_hash(mdctx, sec, L_S1,
412 seed, seed_len, out, olen))
413 return 0;
414
415 if ((tmp = OPENSSL_malloc(olen)) == NULL) {
416 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
417 return 0;
418 }
419
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);
423 return 0;
424 }
425 for (i = 0; i < olen; i++)
426 out[i] ^= tmp[i];
427 OPENSSL_clear_free(tmp, olen);
428 return 1;
429 }
430
431 /* TLS v1.2 */
432 if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
433 return 0;
434
435 return 1;
436 }