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