]> git.karo-electronics.de Git - karo-tx-linux.git/blob - security/keys/dh.c
KEYS: DH: forbid using digest_null as the KDF hash
[karo-tx-linux.git] / security / keys / dh.c
1 /* Crypto operations using stored keys
2  *
3  * Copyright (c) 2016, Intel Corporation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version
8  * 2 of the License, or (at your option) any later version.
9  */
10
11 #include <linux/mpi.h>
12 #include <linux/slab.h>
13 #include <linux/uaccess.h>
14 #include <linux/crypto.h>
15 #include <crypto/hash.h>
16 #include <keys/user-type.h>
17 #include "internal.h"
18
19 /*
20  * Public key or shared secret generation function [RFC2631 sec 2.1.1]
21  *
22  * ya = g^xa mod p;
23  * or
24  * ZZ = yb^xa mod p;
25  *
26  * where xa is the local private key, ya is the local public key, g is
27  * the generator, p is the prime, yb is the remote public key, and ZZ
28  * is the shared secret.
29  *
30  * Both are the same calculation, so g or yb are the "base" and ya or
31  * ZZ are the "result".
32  */
33 static int do_dh(MPI result, MPI base, MPI xa, MPI p)
34 {
35         return mpi_powm(result, base, xa, p);
36 }
37
38 static ssize_t mpi_from_key(key_serial_t keyid, size_t maxlen, MPI *mpi)
39 {
40         struct key *key;
41         key_ref_t key_ref;
42         long status;
43         ssize_t ret;
44
45         key_ref = lookup_user_key(keyid, 0, KEY_NEED_READ);
46         if (IS_ERR(key_ref)) {
47                 ret = -ENOKEY;
48                 goto error;
49         }
50
51         key = key_ref_to_ptr(key_ref);
52
53         ret = -EOPNOTSUPP;
54         if (key->type == &key_type_user) {
55                 down_read(&key->sem);
56                 status = key_validate(key);
57                 if (status == 0) {
58                         const struct user_key_payload *payload;
59
60                         payload = user_key_payload_locked(key);
61
62                         if (maxlen == 0) {
63                                 *mpi = NULL;
64                                 ret = payload->datalen;
65                         } else if (payload->datalen <= maxlen) {
66                                 *mpi = mpi_read_raw_data(payload->data,
67                                                          payload->datalen);
68                                 if (*mpi)
69                                         ret = payload->datalen;
70                         } else {
71                                 ret = -EINVAL;
72                         }
73                 }
74                 up_read(&key->sem);
75         }
76
77         key_put(key);
78 error:
79         return ret;
80 }
81
82 struct kdf_sdesc {
83         struct shash_desc shash;
84         char ctx[];
85 };
86
87 static int kdf_alloc(struct kdf_sdesc **sdesc_ret, char *hashname)
88 {
89         struct crypto_shash *tfm;
90         struct kdf_sdesc *sdesc;
91         int size;
92         int err;
93
94         /* allocate synchronous hash */
95         tfm = crypto_alloc_shash(hashname, 0, 0);
96         if (IS_ERR(tfm)) {
97                 pr_info("could not allocate digest TFM handle %s\n", hashname);
98                 return PTR_ERR(tfm);
99         }
100
101         err = -EINVAL;
102         if (crypto_shash_digestsize(tfm) == 0)
103                 goto out_free_tfm;
104
105         err = -ENOMEM;
106         size = sizeof(struct shash_desc) + crypto_shash_descsize(tfm);
107         sdesc = kmalloc(size, GFP_KERNEL);
108         if (!sdesc)
109                 goto out_free_tfm;
110         sdesc->shash.tfm = tfm;
111         sdesc->shash.flags = 0x0;
112
113         *sdesc_ret = sdesc;
114
115         return 0;
116
117 out_free_tfm:
118         crypto_free_shash(tfm);
119         return err;
120 }
121
122 static void kdf_dealloc(struct kdf_sdesc *sdesc)
123 {
124         if (!sdesc)
125                 return;
126
127         if (sdesc->shash.tfm)
128                 crypto_free_shash(sdesc->shash.tfm);
129
130         kzfree(sdesc);
131 }
132
133 /* convert 32 bit integer into its string representation */
134 static inline void crypto_kw_cpu_to_be32(u32 val, u8 *buf)
135 {
136         __be32 *a = (__be32 *)buf;
137
138         *a = cpu_to_be32(val);
139 }
140
141 /*
142  * Implementation of the KDF in counter mode according to SP800-108 section 5.1
143  * as well as SP800-56A section 5.8.1 (Single-step KDF).
144  *
145  * SP800-56A:
146  * The src pointer is defined as Z || other info where Z is the shared secret
147  * from DH and other info is an arbitrary string (see SP800-56A section
148  * 5.8.1.2).
149  */
150 static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
151                    u8 *dst, unsigned int dlen)
152 {
153         struct shash_desc *desc = &sdesc->shash;
154         unsigned int h = crypto_shash_digestsize(desc->tfm);
155         int err = 0;
156         u8 *dst_orig = dst;
157         u32 i = 1;
158         u8 iteration[sizeof(u32)];
159
160         while (dlen) {
161                 err = crypto_shash_init(desc);
162                 if (err)
163                         goto err;
164
165                 crypto_kw_cpu_to_be32(i, iteration);
166                 err = crypto_shash_update(desc, iteration, sizeof(u32));
167                 if (err)
168                         goto err;
169
170                 if (src && slen) {
171                         err = crypto_shash_update(desc, src, slen);
172                         if (err)
173                                 goto err;
174                 }
175
176                 if (dlen < h) {
177                         u8 tmpbuffer[h];
178
179                         err = crypto_shash_final(desc, tmpbuffer);
180                         if (err)
181                                 goto err;
182                         memcpy(dst, tmpbuffer, dlen);
183                         memzero_explicit(tmpbuffer, h);
184                         return 0;
185                 } else {
186                         err = crypto_shash_final(desc, dst);
187                         if (err)
188                                 goto err;
189
190                         dlen -= h;
191                         dst += h;
192                         i++;
193                 }
194         }
195
196         return 0;
197
198 err:
199         memzero_explicit(dst_orig, dlen);
200         return err;
201 }
202
203 static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc,
204                                  char __user *buffer, size_t buflen,
205                                  uint8_t *kbuf, size_t kbuflen)
206 {
207         uint8_t *outbuf = NULL;
208         int ret;
209
210         outbuf = kmalloc(buflen, GFP_KERNEL);
211         if (!outbuf) {
212                 ret = -ENOMEM;
213                 goto err;
214         }
215
216         ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, buflen);
217         if (ret)
218                 goto err;
219
220         ret = buflen;
221         if (copy_to_user(buffer, outbuf, buflen) != 0)
222                 ret = -EFAULT;
223
224 err:
225         kzfree(outbuf);
226         return ret;
227 }
228
229 long __keyctl_dh_compute(struct keyctl_dh_params __user *params,
230                          char __user *buffer, size_t buflen,
231                          struct keyctl_kdf_params *kdfcopy)
232 {
233         long ret;
234         MPI base, private, prime, result;
235         unsigned nbytes;
236         struct keyctl_dh_params pcopy;
237         uint8_t *kbuf;
238         ssize_t keylen;
239         size_t resultlen;
240         struct kdf_sdesc *sdesc = NULL;
241
242         if (!params || (!buffer && buflen)) {
243                 ret = -EINVAL;
244                 goto out;
245         }
246         if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) {
247                 ret = -EFAULT;
248                 goto out;
249         }
250
251         if (kdfcopy) {
252                 char *hashname;
253
254                 if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN ||
255                     kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) {
256                         ret = -EMSGSIZE;
257                         goto out;
258                 }
259
260                 /* get KDF name string */
261                 hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME);
262                 if (IS_ERR(hashname)) {
263                         ret = PTR_ERR(hashname);
264                         goto out;
265                 }
266
267                 /* allocate KDF from the kernel crypto API */
268                 ret = kdf_alloc(&sdesc, hashname);
269                 kfree(hashname);
270                 if (ret)
271                         goto out;
272         }
273
274         /*
275          * If the caller requests postprocessing with a KDF, allow an
276          * arbitrary output buffer size since the KDF ensures proper truncation.
277          */
278         keylen = mpi_from_key(pcopy.prime, kdfcopy ? SIZE_MAX : buflen, &prime);
279         if (keylen < 0 || !prime) {
280                 /* buflen == 0 may be used to query the required buffer size,
281                  * which is the prime key length.
282                  */
283                 ret = keylen;
284                 goto out;
285         }
286
287         /* The result is never longer than the prime */
288         resultlen = keylen;
289
290         keylen = mpi_from_key(pcopy.base, SIZE_MAX, &base);
291         if (keylen < 0 || !base) {
292                 ret = keylen;
293                 goto error1;
294         }
295
296         keylen = mpi_from_key(pcopy.private, SIZE_MAX, &private);
297         if (keylen < 0 || !private) {
298                 ret = keylen;
299                 goto error2;
300         }
301
302         result = mpi_alloc(0);
303         if (!result) {
304                 ret = -ENOMEM;
305                 goto error3;
306         }
307
308         /* allocate space for DH shared secret and SP800-56A otherinfo */
309         kbuf = kmalloc(kdfcopy ? (resultlen + kdfcopy->otherinfolen) : resultlen,
310                        GFP_KERNEL);
311         if (!kbuf) {
312                 ret = -ENOMEM;
313                 goto error4;
314         }
315
316         /*
317          * Concatenate SP800-56A otherinfo past DH shared secret -- the
318          * input to the KDF is (DH shared secret || otherinfo)
319          */
320         if (kdfcopy && kdfcopy->otherinfo &&
321             copy_from_user(kbuf + resultlen, kdfcopy->otherinfo,
322                            kdfcopy->otherinfolen) != 0) {
323                 ret = -EFAULT;
324                 goto error5;
325         }
326
327         ret = do_dh(result, base, private, prime);
328         if (ret)
329                 goto error5;
330
331         ret = mpi_read_buffer(result, kbuf, resultlen, &nbytes, NULL);
332         if (ret != 0)
333                 goto error5;
334
335         if (kdfcopy) {
336                 ret = keyctl_dh_compute_kdf(sdesc, buffer, buflen, kbuf,
337                                             resultlen + kdfcopy->otherinfolen);
338         } else {
339                 ret = nbytes;
340                 if (copy_to_user(buffer, kbuf, nbytes) != 0)
341                         ret = -EFAULT;
342         }
343
344 error5:
345         kzfree(kbuf);
346 error4:
347         mpi_free(result);
348 error3:
349         mpi_free(private);
350 error2:
351         mpi_free(base);
352 error1:
353         mpi_free(prime);
354 out:
355         kdf_dealloc(sdesc);
356         return ret;
357 }
358
359 long keyctl_dh_compute(struct keyctl_dh_params __user *params,
360                        char __user *buffer, size_t buflen,
361                        struct keyctl_kdf_params __user *kdf)
362 {
363         struct keyctl_kdf_params kdfcopy;
364
365         if (!kdf)
366                 return __keyctl_dh_compute(params, buffer, buflen, NULL);
367
368         if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0)
369                 return -EFAULT;
370
371         return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
372 }