Don't forget our provider ctx when resetting
[openssl.git] / providers / implementations / kdfs / tls1_prf.c
1 /*
2  * Copyright 2016-2020 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 "crypto/evp.h"
58 #include "prov/provider_ctx.h"
59 #include "prov/providercommonerr.h"
60 #include "prov/implementations.h"
61 #include "prov/provider_util.h"
62 #include "e_os.h"
63
64 static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
65 static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
66 static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
67 static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
68 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
69 static OSSL_FUNC_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     if (ctx != NULL) {
110         kdf_tls1_prf_reset(ctx);
111         OPENSSL_free(ctx);
112     }
113 }
114
115 static void kdf_tls1_prf_reset(void *vctx)
116 {
117     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
118     void *provctx = ctx->provctx;
119
120     EVP_MAC_free_ctx(ctx->P_hash);
121     EVP_MAC_free_ctx(ctx->P_sha1);
122     OPENSSL_clear_free(ctx->sec, ctx->seclen);
123     OPENSSL_cleanse(ctx->seed, ctx->seedlen);
124     memset(ctx, 0, sizeof(*ctx));
125     ctx->provctx = provctx;
126 }
127
128 static int kdf_tls1_prf_derive(void *vctx, unsigned char *key,
129                                size_t keylen)
130 {
131     TLS1_PRF *ctx = (TLS1_PRF *)vctx;
132
133     if (ctx->P_hash == NULL) {
134         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
135         return 0;
136     }
137     if (ctx->sec == NULL) {
138         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
139         return 0;
140     }
141     if (ctx->seedlen == 0) {
142         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
143         return 0;
144     }
145
146     return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
147                         ctx->sec, ctx->seclen,
148                         ctx->seed, ctx->seedlen,
149                         key, keylen);
150 }
151
152 static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
153 {
154     const OSSL_PARAM *p;
155     TLS1_PRF *ctx = vctx;
156     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
157
158     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
159         if (strcasecmp(p->data, SN_md5_sha1) == 0) {
160             if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
161                                                    OSSL_MAC_NAME_HMAC,
162                                                    NULL, SN_md5, libctx)
163                 || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
164                                                       OSSL_MAC_NAME_HMAC,
165                                                       NULL, SN_sha1, libctx))
166                 return 0;
167         } else {
168             EVP_MAC_free_ctx(ctx->P_sha1);
169             if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
170                                                    OSSL_MAC_NAME_HMAC,
171                                                    NULL, NULL, libctx))
172                 return 0;
173         }
174     }
175
176     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
177         OPENSSL_clear_free(ctx->sec, ctx->seclen);
178         ctx->sec = NULL;
179         if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
180             return 0;
181     }
182     /* The seed fields concatenate, so process them all */
183     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
184         OPENSSL_cleanse(ctx->seed, ctx->seedlen);
185         ctx->seedlen = 0;
186
187         for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
188                                                       OSSL_KDF_PARAM_SEED)) {
189             const void *q = ctx->seed + ctx->seedlen;
190             size_t sz = 0;
191
192             if (p->data_size != 0
193                 && p->data != NULL
194                 && !OSSL_PARAM_get_octet_string(p, (void **)&q,
195                                                 TLS1_PRF_MAXBUF - ctx->seedlen,
196                                                 &sz))
197                 return 0;
198             ctx->seedlen += sz;
199         }
200     }
201     return 1;
202 }
203
204 static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(void)
205 {
206     static const OSSL_PARAM known_settable_ctx_params[] = {
207         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
208         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
209         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
210         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
211         OSSL_PARAM_END
212     };
213     return known_settable_ctx_params;
214 }
215
216 static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
217 {
218     OSSL_PARAM *p;
219
220     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
221         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
222     return -2;
223 }
224
225 static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(void)
226 {
227     static const OSSL_PARAM known_gettable_ctx_params[] = {
228         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
229         OSSL_PARAM_END
230     };
231     return known_gettable_ctx_params;
232 }
233
234 const OSSL_DISPATCH kdf_tls1_prf_functions[] = {
235     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
236     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
237     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
238     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
239     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
240       (void(*)(void))kdf_tls1_prf_settable_ctx_params },
241     { OSSL_FUNC_KDF_SET_CTX_PARAMS,
242       (void(*)(void))kdf_tls1_prf_set_ctx_params },
243     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
244       (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
245     { OSSL_FUNC_KDF_GET_CTX_PARAMS,
246       (void(*)(void))kdf_tls1_prf_get_ctx_params },
247     { 0, NULL }
248 };
249
250 /*
251  * Refer to "The TLS Protocol Version 1.0" Section 5
252  * (https://tools.ietf.org/html/rfc2246#section-5) and
253  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
254  * (https://tools.ietf.org/html/rfc5246#section-5).
255  *
256  * P_<hash> is an expansion function that uses a single hash function to expand
257  * a secret and seed into an arbitrary quantity of output:
258  *
259  *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
260  *                            HMAC_<hash>(secret, A(2) + seed) +
261  *                            HMAC_<hash>(secret, A(3) + seed) + ...
262  *
263  * where + indicates concatenation.  P_<hash> can be iterated as many times as
264  * is necessary to produce the required quantity of data.
265  *
266  * A(i) is defined as:
267  *     A(0) = seed
268  *     A(i) = HMAC_<hash>(secret, A(i-1))
269  */
270 static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
271                            const unsigned char *sec, size_t sec_len,
272                            const unsigned char *seed, size_t seed_len,
273                            unsigned char *out, size_t olen)
274 {
275     size_t chunk;
276     EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
277     unsigned char Ai[EVP_MAX_MD_SIZE];
278     size_t Ai_len;
279     int ret = 0;
280     OSSL_PARAM params[2], *p = params;
281
282     *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
283                                              (void *)sec, sec_len);
284     *p = OSSL_PARAM_construct_end();
285     if (!EVP_MAC_set_ctx_params(ctx_init, params))
286         goto err;
287     if (!EVP_MAC_init(ctx_init))
288         goto err;
289     chunk = EVP_MAC_size(ctx_init);
290     if (chunk == 0)
291         goto err;
292     /* A(0) = seed */
293     ctx_Ai = EVP_MAC_dup_ctx(ctx_init);
294     if (ctx_Ai == NULL)
295         goto err;
296     if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
297         goto err;
298
299     for (;;) {
300         /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
301         if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
302             goto err;
303         EVP_MAC_free_ctx(ctx_Ai);
304         ctx_Ai = NULL;
305
306         /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
307         ctx = EVP_MAC_dup_ctx(ctx_init);
308         if (ctx == NULL)
309             goto err;
310         if (!EVP_MAC_update(ctx, Ai, Ai_len))
311             goto err;
312         /* save state for calculating next A(i) value */
313         if (olen > chunk) {
314             ctx_Ai = EVP_MAC_dup_ctx(ctx);
315             if (ctx_Ai == NULL)
316                 goto err;
317         }
318         if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
319             goto err;
320         if (olen <= chunk) {
321             /* last chunk - use Ai as temp bounce buffer */
322             if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
323                 goto err;
324             memcpy(out, Ai, olen);
325             break;
326         }
327         if (!EVP_MAC_final(ctx, out, NULL, olen))
328             goto err;
329         EVP_MAC_free_ctx(ctx);
330         ctx = NULL;
331         out += chunk;
332         olen -= chunk;
333     }
334     ret = 1;
335  err:
336     EVP_MAC_free_ctx(ctx);
337     EVP_MAC_free_ctx(ctx_Ai);
338     OPENSSL_cleanse(Ai, sizeof(Ai));
339     return ret;
340 }
341
342 /*
343  * Refer to "The TLS Protocol Version 1.0" Section 5
344  * (https://tools.ietf.org/html/rfc2246#section-5) and
345  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
346  * (https://tools.ietf.org/html/rfc5246#section-5).
347  *
348  * For TLS v1.0 and TLS v1.1:
349  *
350  *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
351  *                              P_SHA-1(S2, label + seed)
352  *
353  * S1 is taken from the first half of the secret, S2 from the second half.
354  *
355  *   L_S = length in bytes of secret;
356  *   L_S1 = L_S2 = ceil(L_S / 2);
357  *
358  * For TLS v1.2:
359  *
360  *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
361  */
362 static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
363                         const unsigned char *sec, size_t slen,
364                         const unsigned char *seed, size_t seed_len,
365                         unsigned char *out, size_t olen)
366 {
367     if (sha1ctx != NULL) {
368         /* TLS v1.0 and TLS v1.1 */
369         size_t i;
370         unsigned char *tmp;
371         /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
372         size_t L_S1 = (slen + 1) / 2;
373         size_t L_S2 = L_S1;
374
375         if (!tls1_prf_P_hash(mdctx, sec, L_S1,
376                              seed, seed_len, out, olen))
377             return 0;
378
379         if ((tmp = OPENSSL_malloc(olen)) == NULL) {
380             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
381             return 0;
382         }
383
384         if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
385                              seed, seed_len, tmp, olen)) {
386             OPENSSL_clear_free(tmp, olen);
387             return 0;
388         }
389         for (i = 0; i < olen; i++)
390             out[i] ^= tmp[i];
391         OPENSSL_clear_free(tmp, olen);
392         return 1;
393     }
394
395     /* TLS v1.2 */
396     if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
397         return 0;
398
399     return 1;
400 }