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