Refactor TLS1-PRF to create the MAC contexts early
[openssl.git] / providers / common / kdfs / tls1_prf.c
1 /*
2  * Copyright 2016-2019 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 "internal/cryptlib.h"
56 #include "internal/numbers.h"
57 #include "internal/evp_int.h"
58 #include "internal/provider_ctx.h"
59 #include "internal/providercommonerr.h"
60 #include "internal/provider_algs.h"
61 #include "internal/provider_util.h"
62 #include "e_os.h"
63
64 static OSSL_OP_kdf_newctx_fn kdf_tls1_prf_new;
65 static OSSL_OP_kdf_freectx_fn kdf_tls1_prf_free;
66 static OSSL_OP_kdf_reset_fn kdf_tls1_prf_reset;
67 static OSSL_OP_kdf_derive_fn kdf_tls1_prf_derive;
68 static OSSL_OP_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
69 static OSSL_OP_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
70
71 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
72                         const unsigned char *sec, size_t slen,
73                         const unsigned char *seed, size_t seed_len,
74                         unsigned char *out, size_t olen);
75
76 #define TLS1_PRF_MAXBUF 1024
77
78 /* TLS KDF kdf context structure */
79 typedef struct {
80     void *provctx;
81
82     /* MAC context for the main digest */
83     EVP_MAC_CTX *P_hash;
84     /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
85     EVP_MAC_CTX *P_sha1;
86
87     /* Secret value to use for PRF */
88     unsigned char *sec;
89     size_t seclen;
90     /* Buffer of concatenated seed data */
91     unsigned char seed[TLS1_PRF_MAXBUF];
92     size_t seedlen;
93 } TLS1_PRF;
94
95 static void *kdf_tls1_prf_new(void *provctx)
96 {
97     TLS1_PRF *ctx;
98
99     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
100         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
101     ctx->provctx = provctx;
102     return ctx;
103 }
104
105 static void kdf_tls1_prf_free(void *vctx)
106 {
107     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
108
109     kdf_tls1_prf_reset(ctx);
110     OPENSSL_free(ctx);
111 }
112
113 static void kdf_tls1_prf_reset(void *vctx)
114 {
115     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
116
117     EVP_MAC_CTX_free(ctx->P_hash);
118     EVP_MAC_CTX_free(ctx->P_sha1);
119     OPENSSL_clear_free(ctx->sec, ctx->seclen);
120     OPENSSL_cleanse(ctx->seed, ctx->seedlen);
121     memset(ctx, 0, sizeof(*ctx));
122 }
123
124 static int kdf_tls1_prf_derive(void *vctx, unsigned char *key,
125                                size_t keylen)
126 {
127     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
128
129     if (ctx->P_hash == NULL) {
130         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
131         return 0;
132     }
133     if (ctx->sec == NULL) {
134         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
135         return 0;
136     }
137     if (ctx->seedlen == 0) {
138         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
139         return 0;
140     }
141
142     return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
143                         ctx->sec, ctx->seclen,
144                         ctx->seed, ctx->seedlen,
145                         key, keylen);
146 }
147
148 static EVP_MAC_CTX *kdf_tls1_prf_mkmacctx(OPENSSL_CTX *libctx,
149                                           const char *mdname,
150                                           const OSSL_PARAM params[])
151 {
152     const OSSL_PARAM *p;
153     OSSL_PARAM mac_params[5], *mp = mac_params;
154     const char *properties = NULL;
155     /* TODO(3.0) rethink "flags", also see hmac.c in providers */
156     int mac_flags = EVP_MD_CTX_FLAG_NON_FIPS_ALLOW;
157     EVP_MAC_CTX *macctx = NULL;
158
159     *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
160                                              (char *)mdname, 0);
161 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
162     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ENGINE)) != NULL)
163         *mp++ = *p;
164 #endif
165     if ((p = OSSL_PARAM_locate_const(params,
166                                      OSSL_KDF_PARAM_PROPERTIES)) != NULL) {
167         properties = p->data;
168         *mp++ = *p;
169     }
170     *mp++ = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS, &mac_flags);
171     *mp = OSSL_PARAM_construct_end();
172
173     /* Implicit fetch */
174     {
175         EVP_MAC *mac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_HMAC, properties);
176
177         macctx = EVP_MAC_CTX_new(mac);
178         /* The context holds on to the MAC */
179         EVP_MAC_free(mac);
180         if (macctx == NULL)
181             goto err;
182     }
183
184     if (EVP_MAC_CTX_set_params(macctx, mac_params))
185         goto done;
186  err:
187     EVP_MAC_CTX_free(macctx);
188     macctx = NULL;
189  done:
190     return macctx;
191 }
192
193 static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
194 {
195     const OSSL_PARAM *p;
196     TLS1_PRF *ctx = vctx;
197     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
198
199     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
200         EVP_MAC_CTX_free(ctx->P_hash);
201         EVP_MAC_CTX_free(ctx->P_sha1);
202         if (strcasecmp(p->data, SN_md5_sha1) == 0) {
203             ctx->P_hash = kdf_tls1_prf_mkmacctx(libctx, SN_md5, params);
204             ctx->P_sha1 = kdf_tls1_prf_mkmacctx(libctx, SN_sha1, params);
205         } else {
206             ctx->P_hash = kdf_tls1_prf_mkmacctx(libctx, p->data, params);
207         }
208     }
209
210     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
211         OPENSSL_clear_free(ctx->sec, ctx->seclen);
212         ctx->sec = NULL;
213         if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
214             return 0;
215     }
216     /* The seed fields concatenate, so process them all */
217     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
218         OPENSSL_cleanse(ctx->seed, ctx->seedlen);
219         ctx->seedlen = 0;
220
221         for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
222                                                       OSSL_KDF_PARAM_SEED)) {
223             const void *q = ctx->seed + ctx->seedlen;
224             size_t sz = 0;
225
226             if (p->data_size != 0
227                 && p->data != NULL
228                 && !OSSL_PARAM_get_octet_string(p, (void **)&q,
229                                                 TLS1_PRF_MAXBUF - ctx->seedlen,
230                                                 &sz))
231                 return 0;
232             ctx->seedlen += sz;
233         }
234     }
235     return 1;
236 }
237
238 static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(void)
239 {
240     static const OSSL_PARAM known_settable_ctx_params[] = {
241         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
242         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
243         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
244         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
245         OSSL_PARAM_END
246     };
247     return known_settable_ctx_params;
248 }
249
250 static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
251 {
252     OSSL_PARAM *p;
253
254     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
255         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
256     return -2;
257 }
258
259 static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(void)
260 {
261     static const OSSL_PARAM known_gettable_ctx_params[] = {
262         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
263         OSSL_PARAM_END
264     };
265     return known_gettable_ctx_params;
266 }
267
268 const OSSL_DISPATCH kdf_tls1_prf_functions[] = {
269     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
270     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
271     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
272     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
273     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
274       (void(*)(void))kdf_tls1_prf_settable_ctx_params },
275     { OSSL_FUNC_KDF_SET_CTX_PARAMS,
276       (void(*)(void))kdf_tls1_prf_set_ctx_params },
277     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
278       (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
279     { OSSL_FUNC_KDF_GET_CTX_PARAMS,
280       (void(*)(void))kdf_tls1_prf_get_ctx_params },
281     { 0, NULL }
282 };
283
284 /*
285  * Refer to "The TLS Protocol Version 1.0" Section 5
286  * (https://tools.ietf.org/html/rfc2246#section-5) and
287  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
288  * (https://tools.ietf.org/html/rfc5246#section-5).
289  *
290  * P_<hash> is an expansion function that uses a single hash function to expand
291  * a secret and seed into an arbitrary quantity of output:
292  *
293  *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
294  *                            HMAC_<hash>(secret, A(2) + seed) +
295  *                            HMAC_<hash>(secret, A(3) + seed) + ...
296  *
297  * where + indicates concatenation.  P_<hash> can be iterated as many times as
298  * is necessary to produce the required quantity of data.
299  *
300  * A(i) is defined as:
301  *     A(0) = seed
302  *     A(i) = HMAC_<hash>(secret, A(i-1))
303  */
304 static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
305                            const unsigned char *sec, size_t sec_len,
306                            const unsigned char *seed, size_t seed_len,
307                            unsigned char *out, size_t olen)
308 {
309     size_t chunk;
310     EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
311     unsigned char Ai[EVP_MAX_MD_SIZE];
312     size_t Ai_len;
313     int ret = 0;
314     OSSL_PARAM params[2], *p = params;
315
316     *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
317                                              (void *)sec, sec_len);
318     *p = OSSL_PARAM_construct_end();
319     if (!EVP_MAC_CTX_set_params(ctx_init, params))
320         goto err;
321     if (!EVP_MAC_init(ctx_init))
322         goto err;
323     chunk = EVP_MAC_size(ctx_init);
324     if (chunk == 0)
325         goto err;
326     /* A(0) = seed */
327     ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
328     if (ctx_Ai == NULL)
329         goto err;
330     if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
331         goto err;
332
333     for (;;) {
334         /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
335         if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
336             goto err;
337         EVP_MAC_CTX_free(ctx_Ai);
338         ctx_Ai = NULL;
339
340         /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
341         ctx = EVP_MAC_CTX_dup(ctx_init);
342         if (ctx == NULL)
343             goto err;
344         if (!EVP_MAC_update(ctx, Ai, Ai_len))
345             goto err;
346         /* save state for calculating next A(i) value */
347         if (olen > chunk) {
348             ctx_Ai = EVP_MAC_CTX_dup(ctx);
349             if (ctx_Ai == NULL)
350                 goto err;
351         }
352         if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
353             goto err;
354         if (olen <= chunk) {
355             /* last chunk - use Ai as temp bounce buffer */
356             if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
357                 goto err;
358             memcpy(out, Ai, olen);
359             break;
360         }
361         if (!EVP_MAC_final(ctx, out, NULL, olen))
362             goto err;
363         EVP_MAC_CTX_free(ctx);
364         ctx = NULL;
365         out += chunk;
366         olen -= chunk;
367     }
368     ret = 1;
369  err:
370     EVP_MAC_CTX_free(ctx);
371     EVP_MAC_CTX_free(ctx_Ai);
372     OPENSSL_cleanse(Ai, sizeof(Ai));
373     return ret;
374 }
375
376 /*
377  * Refer to "The TLS Protocol Version 1.0" Section 5
378  * (https://tools.ietf.org/html/rfc2246#section-5) and
379  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
380  * (https://tools.ietf.org/html/rfc5246#section-5).
381  *
382  * For TLS v1.0 and TLS v1.1:
383  *
384  *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
385  *                              P_SHA-1(S2, label + seed)
386  *
387  * S1 is taken from the first half of the secret, S2 from the second half.
388  *
389  *   L_S = length in bytes of secret;
390  *   L_S1 = L_S2 = ceil(L_S / 2);
391  *
392  * For TLS v1.2:
393  *
394  *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
395  */
396 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
397                         const unsigned char *sec, size_t slen,
398                         const unsigned char *seed, size_t seed_len,
399                         unsigned char *out, size_t olen)
400 {
401     if (sha1ctx != NULL) {
402         /* TLS v1.0 and TLS v1.1 */
403         size_t i;
404         unsigned char *tmp;
405         /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
406         size_t L_S1 = (slen + 1) / 2;
407         size_t L_S2 = L_S1;
408
409         if (!tls1_prf_P_hash(mdctx, sec, L_S1,
410                              seed, seed_len, out, olen))
411             return 0;
412
413         if ((tmp = OPENSSL_malloc(olen)) == NULL) {
414             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
415             return 0;
416         }
417
418         if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
419                              seed, seed_len, tmp, olen)) {
420             OPENSSL_clear_free(tmp, olen);
421             return 0;
422         }
423         for (i = 0; i < olen; i++)
424             out[i] ^= tmp[i];
425         OPENSSL_clear_free(tmp, olen);
426         return 1;
427     }
428
429     /* TLS v1.2 */
430     if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
431         return 0;
432
433     return 1;
434 }