Get rid of the diversity of names for MAC parameters
[openssl.git] / crypto / kdf / tls1_prf.c
1 /*
2  * Copyright 2016-2018 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 "internal/cryptlib.h"
52 #include <openssl/evp.h>
53 #include <openssl/kdf.h>
54 #include <openssl/core_names.h>
55 #include <openssl/params.h>
56 #include "internal/evp_int.h"
57 #include "kdf_local.h"
58
59 static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl);
60 static int tls1_prf_alg(const EVP_MD *md,
61                         const unsigned char *sec, size_t slen,
62                         const unsigned char *seed, size_t seed_len,
63                         unsigned char *out, size_t olen);
64
65 #define TLS1_PRF_MAXBUF 1024
66
67 /* TLS KDF kdf context structure */
68
69 struct evp_kdf_impl_st {
70     /* Digest to use for PRF */
71     const EVP_MD *md;
72     /* Secret value to use for PRF */
73     unsigned char *sec;
74     size_t seclen;
75     /* Buffer of concatenated seed data */
76     unsigned char seed[TLS1_PRF_MAXBUF];
77     size_t seedlen;
78 };
79
80 static EVP_KDF_IMPL *kdf_tls1_prf_new(void)
81 {
82     EVP_KDF_IMPL *impl;
83
84     if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
85         KDFerr(KDF_F_KDF_TLS1_PRF_NEW, ERR_R_MALLOC_FAILURE);
86     return impl;
87 }
88
89 static void kdf_tls1_prf_free(EVP_KDF_IMPL *impl)
90 {
91     kdf_tls1_prf_reset(impl);
92     OPENSSL_free(impl);
93 }
94
95 static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl)
96 {
97     OPENSSL_clear_free(impl->sec, impl->seclen);
98     OPENSSL_cleanse(impl->seed, impl->seedlen);
99     memset(impl, 0, sizeof(*impl));
100 }
101
102 static int kdf_tls1_prf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
103 {
104     const unsigned char *p;
105     size_t len;
106     const EVP_MD *md;
107
108     switch (cmd) {
109     case EVP_KDF_CTRL_SET_MD:
110         md = va_arg(args, const EVP_MD *);
111         if (md == NULL)
112             return 0;
113
114         impl->md = md;
115         return 1;
116
117     case EVP_KDF_CTRL_SET_TLS_SECRET:
118         p = va_arg(args, const unsigned char *);
119         len = va_arg(args, size_t);
120         OPENSSL_clear_free(impl->sec, impl->seclen);
121         impl->sec = OPENSSL_memdup(p, len);
122         if (impl->sec == NULL)
123             return 0;
124
125         impl->seclen = len;
126         return 1;
127
128     case EVP_KDF_CTRL_RESET_TLS_SEED:
129         OPENSSL_cleanse(impl->seed, impl->seedlen);
130         impl->seedlen = 0;
131         return 1;
132
133     case EVP_KDF_CTRL_ADD_TLS_SEED:
134         p = va_arg(args, const unsigned char *);
135         len = va_arg(args, size_t);
136         if (len == 0 || p == NULL)
137             return 1;
138
139         if (len > (TLS1_PRF_MAXBUF - impl->seedlen))
140             return 0;
141
142         memcpy(impl->seed + impl->seedlen, p, len);
143         impl->seedlen += len;
144         return 1;
145
146     default:
147         return -2;
148     }
149 }
150
151 static int kdf_tls1_prf_ctrl_str(EVP_KDF_IMPL *impl,
152                                  const char *type, const char *value)
153 {
154     if (value == NULL) {
155         KDFerr(KDF_F_KDF_TLS1_PRF_CTRL_STR, KDF_R_VALUE_MISSING);
156         return 0;
157     }
158     if (strcmp(type, "digest") == 0)
159         return kdf_md2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_SET_MD, value);
160
161     if (strcmp(type, "secret") == 0)
162         return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl,
163                             EVP_KDF_CTRL_SET_TLS_SECRET, value);
164
165     if (strcmp(type, "hexsecret") == 0)
166         return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl,
167                             EVP_KDF_CTRL_SET_TLS_SECRET, value);
168
169     if (strcmp(type, "seed") == 0)
170         return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
171                             value);
172
173     if (strcmp(type, "hexseed") == 0)
174         return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
175                             value);
176
177     return -2;
178 }
179
180 static int kdf_tls1_prf_derive(EVP_KDF_IMPL *impl, unsigned char *key,
181                                size_t keylen)
182 {
183     if (impl->md == NULL) {
184         KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
185         return 0;
186     }
187     if (impl->sec == NULL) {
188         KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SECRET);
189         return 0;
190     }
191     if (impl->seedlen == 0) {
192         KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SEED);
193         return 0;
194     }
195     return tls1_prf_alg(impl->md, impl->sec, impl->seclen,
196                         impl->seed, impl->seedlen,
197                         key, keylen);
198 }
199
200 const EVP_KDF tls1_prf_kdf_meth = {
201     EVP_KDF_TLS1_PRF,
202     kdf_tls1_prf_new,
203     kdf_tls1_prf_free,
204     kdf_tls1_prf_reset,
205     kdf_tls1_prf_ctrl,
206     kdf_tls1_prf_ctrl_str,
207     NULL,
208     kdf_tls1_prf_derive
209 };
210
211 /*
212  * Refer to "The TLS Protocol Version 1.0" Section 5
213  * (https://tools.ietf.org/html/rfc2246#section-5) and
214  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
215  * (https://tools.ietf.org/html/rfc5246#section-5).
216  *
217  * P_<hash> is an expansion function that uses a single hash function to expand
218  * a secret and seed into an arbitrary quantity of output:
219  *
220  *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
221  *                            HMAC_<hash>(secret, A(2) + seed) +
222  *                            HMAC_<hash>(secret, A(3) + seed) + ...
223  *
224  * where + indicates concatenation.  P_<hash> can be iterated as many times as
225  * is necessary to produce the required quantity of data.
226  *
227  * A(i) is defined as:
228  *     A(0) = seed
229  *     A(i) = HMAC_<hash>(secret, A(i-1))
230  */
231 static int tls1_prf_P_hash(const EVP_MD *md,
232                            const unsigned char *sec, size_t sec_len,
233                            const unsigned char *seed, size_t seed_len,
234                            unsigned char *out, size_t olen)
235 {
236     size_t chunk;
237     EVP_MAC *mac = NULL;
238     EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL, *ctx_init = NULL;
239     unsigned char Ai[EVP_MAX_MD_SIZE];
240     size_t Ai_len;
241     int ret = 0;
242     OSSL_PARAM params[4];
243     int mac_flags;
244     const char *mdname = EVP_MD_name(md);
245
246     mac = EVP_MAC_fetch(NULL, OSSL_MAC_NAME_HMAC, NULL); /* Implicit fetch */
247     ctx_init = EVP_MAC_CTX_new(mac);
248     if (ctx_init == NULL)
249         goto err;
250
251     /* TODO(3.0) rethink "flags", also see hmac.c in providers */
252     mac_flags = EVP_MD_CTX_FLAG_NON_FIPS_ALLOW;
253     params[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_FLAGS, &mac_flags);
254     params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
255                                                  (char *)mdname,
256                                                  strlen(mdname) + 1);
257     params[2] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
258                                                   (void *)sec, sec_len);
259     params[3] = OSSL_PARAM_construct_end();
260     if (!EVP_MAC_CTX_set_params(ctx_init, params))
261         goto err;
262     if (!EVP_MAC_init(ctx_init))
263         goto err;
264     chunk = EVP_MAC_size(ctx_init);
265     if (chunk == 0)
266         goto err;
267     /* A(0) = seed */
268     ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
269     if (ctx_Ai == NULL)
270         goto err;
271     if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
272         goto err;
273
274     for (;;) {
275         /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
276         if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
277             goto err;
278         EVP_MAC_CTX_free(ctx_Ai);
279         ctx_Ai = NULL;
280
281         /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
282         ctx = EVP_MAC_CTX_dup(ctx_init);
283         if (ctx == NULL)
284             goto err;
285         if (!EVP_MAC_update(ctx, Ai, Ai_len))
286             goto err;
287         /* save state for calculating next A(i) value */
288         if (olen > chunk) {
289             ctx_Ai = EVP_MAC_CTX_dup(ctx);
290             if (ctx_Ai == NULL)
291                 goto err;
292         }
293         if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
294             goto err;
295         if (olen <= chunk) {
296             /* last chunk - use Ai as temp bounce buffer */
297             if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
298                 goto err;
299             memcpy(out, Ai, olen);
300             break;
301         }
302         if (!EVP_MAC_final(ctx, out, NULL, olen))
303             goto err;
304         EVP_MAC_CTX_free(ctx);
305         ctx = NULL;
306         out += chunk;
307         olen -= chunk;
308     }
309     ret = 1;
310  err:
311     EVP_MAC_CTX_free(ctx);
312     EVP_MAC_CTX_free(ctx_Ai);
313     EVP_MAC_CTX_free(ctx_init);
314     EVP_MAC_free(mac);
315     OPENSSL_cleanse(Ai, sizeof(Ai));
316     return ret;
317 }
318
319 /*
320  * Refer to "The TLS Protocol Version 1.0" Section 5
321  * (https://tools.ietf.org/html/rfc2246#section-5) and
322  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
323  * (https://tools.ietf.org/html/rfc5246#section-5).
324  *
325  * For TLS v1.0 and TLS v1.1:
326  *
327  *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
328  *                              P_SHA-1(S2, label + seed)
329  *
330  * S1 is taken from the first half of the secret, S2 from the second half.
331  *
332  *   L_S = length in bytes of secret;
333  *   L_S1 = L_S2 = ceil(L_S / 2);
334  *
335  * For TLS v1.2:
336  *
337  *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
338  */
339 static int tls1_prf_alg(const EVP_MD *md,
340                         const unsigned char *sec, size_t slen,
341                         const unsigned char *seed, size_t seed_len,
342                         unsigned char *out, size_t olen)
343 {
344     if (EVP_MD_type(md) == NID_md5_sha1) {
345         /* TLS v1.0 and TLS v1.1 */
346         size_t i;
347         unsigned char *tmp;
348         /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
349         size_t L_S1 = (slen + 1) / 2;
350         size_t L_S2 = L_S1;
351
352         if (!tls1_prf_P_hash(EVP_md5(), sec, L_S1,
353                              seed, seed_len, out, olen))
354             return 0;
355
356         if ((tmp = OPENSSL_malloc(olen)) == NULL) {
357             KDFerr(KDF_F_TLS1_PRF_ALG, ERR_R_MALLOC_FAILURE);
358             return 0;
359         }
360         if (!tls1_prf_P_hash(EVP_sha1(), sec + slen - L_S2, L_S2,
361                              seed, seed_len, tmp, olen)) {
362             OPENSSL_clear_free(tmp, olen);
363             return 0;
364         }
365         for (i = 0; i < olen; i++)
366             out[i] ^= tmp[i];
367         OPENSSL_clear_free(tmp, olen);
368         return 1;
369     }
370
371     /* TLS v1.2 */
372     if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
373         return 0;
374
375     return 1;
376 }