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