Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 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 | |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/uaccess.h> |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 13 | #include <linux/scatterlist.h> |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 14 | #include <linux/crypto.h> |
| 15 | #include <crypto/hash.h> |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 16 | #include <crypto/kpp.h> |
| 17 | #include <crypto/dh.h> |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 18 | #include <keys/user-type.h> |
| 19 | #include "internal.h" |
| 20 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 21 | static ssize_t dh_data_from_key(key_serial_t keyid, void **data) |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 22 | { |
| 23 | struct key *key; |
| 24 | key_ref_t key_ref; |
| 25 | long status; |
| 26 | ssize_t ret; |
| 27 | |
| 28 | key_ref = lookup_user_key(keyid, 0, KEY_NEED_READ); |
| 29 | if (IS_ERR(key_ref)) { |
| 30 | ret = -ENOKEY; |
| 31 | goto error; |
| 32 | } |
| 33 | |
| 34 | key = key_ref_to_ptr(key_ref); |
| 35 | |
| 36 | ret = -EOPNOTSUPP; |
| 37 | if (key->type == &key_type_user) { |
| 38 | down_read(&key->sem); |
| 39 | status = key_validate(key); |
| 40 | if (status == 0) { |
| 41 | const struct user_key_payload *payload; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 42 | uint8_t *duplicate; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 43 | |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 44 | payload = user_key_payload_locked(key); |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 45 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 46 | duplicate = kmemdup(payload->data, payload->datalen, |
| 47 | GFP_KERNEL); |
| 48 | if (duplicate) { |
| 49 | *data = duplicate; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 50 | ret = payload->datalen; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 51 | } else { |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 52 | ret = -ENOMEM; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | up_read(&key->sem); |
| 56 | } |
| 57 | |
| 58 | key_put(key); |
| 59 | error: |
| 60 | return ret; |
| 61 | } |
| 62 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 63 | static void dh_free_data(struct dh *dh) |
| 64 | { |
| 65 | kzfree(dh->key); |
| 66 | kzfree(dh->p); |
| 67 | kzfree(dh->g); |
| 68 | } |
| 69 | |
| 70 | struct dh_completion { |
| 71 | struct completion completion; |
| 72 | int err; |
| 73 | }; |
| 74 | |
| 75 | static void dh_crypto_done(struct crypto_async_request *req, int err) |
| 76 | { |
| 77 | struct dh_completion *compl = req->data; |
| 78 | |
| 79 | if (err == -EINPROGRESS) |
| 80 | return; |
| 81 | |
| 82 | compl->err = err; |
| 83 | complete(&compl->completion); |
| 84 | } |
| 85 | |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 86 | struct kdf_sdesc { |
| 87 | struct shash_desc shash; |
| 88 | char ctx[]; |
| 89 | }; |
| 90 | |
| 91 | static int kdf_alloc(struct kdf_sdesc **sdesc_ret, char *hashname) |
| 92 | { |
| 93 | struct crypto_shash *tfm; |
| 94 | struct kdf_sdesc *sdesc; |
| 95 | int size; |
Eric Biggers | bbe2404 | 2017-06-08 14:49:34 +0100 | [diff] [blame] | 96 | int err; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 97 | |
| 98 | /* allocate synchronous hash */ |
| 99 | tfm = crypto_alloc_shash(hashname, 0, 0); |
| 100 | if (IS_ERR(tfm)) { |
| 101 | pr_info("could not allocate digest TFM handle %s\n", hashname); |
| 102 | return PTR_ERR(tfm); |
| 103 | } |
| 104 | |
Eric Biggers | bbe2404 | 2017-06-08 14:49:34 +0100 | [diff] [blame] | 105 | err = -EINVAL; |
| 106 | if (crypto_shash_digestsize(tfm) == 0) |
| 107 | goto out_free_tfm; |
| 108 | |
| 109 | err = -ENOMEM; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 110 | size = sizeof(struct shash_desc) + crypto_shash_descsize(tfm); |
| 111 | sdesc = kmalloc(size, GFP_KERNEL); |
| 112 | if (!sdesc) |
Eric Biggers | bbe2404 | 2017-06-08 14:49:34 +0100 | [diff] [blame] | 113 | goto out_free_tfm; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 114 | sdesc->shash.tfm = tfm; |
| 115 | sdesc->shash.flags = 0x0; |
| 116 | |
| 117 | *sdesc_ret = sdesc; |
| 118 | |
| 119 | return 0; |
Eric Biggers | bbe2404 | 2017-06-08 14:49:34 +0100 | [diff] [blame] | 120 | |
| 121 | out_free_tfm: |
| 122 | crypto_free_shash(tfm); |
| 123 | return err; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void kdf_dealloc(struct kdf_sdesc *sdesc) |
| 127 | { |
| 128 | if (!sdesc) |
| 129 | return; |
| 130 | |
| 131 | if (sdesc->shash.tfm) |
| 132 | crypto_free_shash(sdesc->shash.tfm); |
| 133 | |
| 134 | kzfree(sdesc); |
| 135 | } |
| 136 | |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 137 | /* |
| 138 | * Implementation of the KDF in counter mode according to SP800-108 section 5.1 |
| 139 | * as well as SP800-56A section 5.8.1 (Single-step KDF). |
| 140 | * |
| 141 | * SP800-56A: |
| 142 | * The src pointer is defined as Z || other info where Z is the shared secret |
| 143 | * from DH and other info is an arbitrary string (see SP800-56A section |
| 144 | * 5.8.1.2). |
Eric Biggers | 3619dec | 2018-06-26 16:59:46 +0100 | [diff] [blame] | 145 | * |
| 146 | * 'dlen' must be a multiple of the digest size. |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 147 | */ |
| 148 | static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen, |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 149 | u8 *dst, unsigned int dlen, unsigned int zlen) |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 150 | { |
| 151 | struct shash_desc *desc = &sdesc->shash; |
| 152 | unsigned int h = crypto_shash_digestsize(desc->tfm); |
| 153 | int err = 0; |
| 154 | u8 *dst_orig = dst; |
Eric Biggers | 0ddd9f1 | 2017-06-08 14:49:49 +0100 | [diff] [blame] | 155 | __be32 counter = cpu_to_be32(1); |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 156 | |
| 157 | while (dlen) { |
| 158 | err = crypto_shash_init(desc); |
| 159 | if (err) |
| 160 | goto err; |
| 161 | |
Eric Biggers | 0ddd9f1 | 2017-06-08 14:49:49 +0100 | [diff] [blame] | 162 | err = crypto_shash_update(desc, (u8 *)&counter, sizeof(__be32)); |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 163 | if (err) |
| 164 | goto err; |
| 165 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 166 | if (zlen && h) { |
Tycho Andersen | 890e2ab | 2018-04-24 14:26:39 -0600 | [diff] [blame] | 167 | u8 tmpbuffer[32]; |
| 168 | size_t chunk = min_t(size_t, zlen, sizeof(tmpbuffer)); |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 169 | memset(tmpbuffer, 0, chunk); |
| 170 | |
| 171 | do { |
| 172 | err = crypto_shash_update(desc, tmpbuffer, |
| 173 | chunk); |
| 174 | if (err) |
| 175 | goto err; |
| 176 | |
| 177 | zlen -= chunk; |
Tycho Andersen | 890e2ab | 2018-04-24 14:26:39 -0600 | [diff] [blame] | 178 | chunk = min_t(size_t, zlen, sizeof(tmpbuffer)); |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 179 | } while (zlen); |
| 180 | } |
| 181 | |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 182 | if (src && slen) { |
| 183 | err = crypto_shash_update(desc, src, slen); |
| 184 | if (err) |
| 185 | goto err; |
| 186 | } |
| 187 | |
Tycho Andersen | 383203e | 2018-04-24 14:26:38 -0600 | [diff] [blame] | 188 | err = crypto_shash_final(desc, dst); |
| 189 | if (err) |
| 190 | goto err; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 191 | |
Tycho Andersen | 383203e | 2018-04-24 14:26:38 -0600 | [diff] [blame] | 192 | dlen -= h; |
| 193 | dst += h; |
| 194 | counter = cpu_to_be32(be32_to_cpu(counter) + 1); |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | |
| 199 | err: |
| 200 | memzero_explicit(dst_orig, dlen); |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc, |
| 205 | char __user *buffer, size_t buflen, |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 206 | uint8_t *kbuf, size_t kbuflen, size_t lzero) |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 207 | { |
| 208 | uint8_t *outbuf = NULL; |
| 209 | int ret; |
Eric Biggers | 3619dec | 2018-06-26 16:59:46 +0100 | [diff] [blame] | 210 | size_t outbuf_len = roundup(buflen, |
| 211 | crypto_shash_digestsize(sdesc->shash.tfm)); |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 212 | |
Tycho Andersen | 383203e | 2018-04-24 14:26:38 -0600 | [diff] [blame] | 213 | outbuf = kmalloc(outbuf_len, GFP_KERNEL); |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 214 | if (!outbuf) { |
| 215 | ret = -ENOMEM; |
| 216 | goto err; |
| 217 | } |
| 218 | |
Tycho Andersen | 383203e | 2018-04-24 14:26:38 -0600 | [diff] [blame] | 219 | ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, outbuf_len, lzero); |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 220 | if (ret) |
| 221 | goto err; |
| 222 | |
| 223 | ret = buflen; |
| 224 | if (copy_to_user(buffer, outbuf, buflen) != 0) |
| 225 | ret = -EFAULT; |
| 226 | |
| 227 | err: |
| 228 | kzfree(outbuf); |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | long __keyctl_dh_compute(struct keyctl_dh_params __user *params, |
| 233 | char __user *buffer, size_t buflen, |
| 234 | struct keyctl_kdf_params *kdfcopy) |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 235 | { |
| 236 | long ret; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 237 | ssize_t dlen; |
| 238 | int secretlen; |
| 239 | int outlen; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 240 | struct keyctl_dh_params pcopy; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 241 | struct dh dh_inputs; |
| 242 | struct scatterlist outsg; |
| 243 | struct dh_completion compl; |
| 244 | struct crypto_kpp *tfm; |
| 245 | struct kpp_request *req; |
| 246 | uint8_t *secret; |
| 247 | uint8_t *outbuf; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 248 | struct kdf_sdesc *sdesc = NULL; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 249 | |
| 250 | if (!params || (!buffer && buflen)) { |
| 251 | ret = -EINVAL; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 252 | goto out1; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 253 | } |
| 254 | if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) { |
| 255 | ret = -EFAULT; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 256 | goto out1; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 259 | if (kdfcopy) { |
| 260 | char *hashname; |
| 261 | |
Eric Biggers | 4f9dabf | 2017-07-13 13:16:56 +0100 | [diff] [blame] | 262 | if (memchr_inv(kdfcopy->__spare, 0, sizeof(kdfcopy->__spare))) { |
| 263 | ret = -EINVAL; |
| 264 | goto out1; |
| 265 | } |
| 266 | |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 267 | if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN || |
| 268 | kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) { |
| 269 | ret = -EMSGSIZE; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 270 | goto out1; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* get KDF name string */ |
| 274 | hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME); |
| 275 | if (IS_ERR(hashname)) { |
| 276 | ret = PTR_ERR(hashname); |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 277 | goto out1; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* allocate KDF from the kernel crypto API */ |
| 281 | ret = kdf_alloc(&sdesc, hashname); |
| 282 | kfree(hashname); |
| 283 | if (ret) |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 284 | goto out1; |
Stephan Mueller | 4693fc7 | 2016-05-26 23:38:12 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 287 | memset(&dh_inputs, 0, sizeof(dh_inputs)); |
| 288 | |
| 289 | dlen = dh_data_from_key(pcopy.prime, &dh_inputs.p); |
| 290 | if (dlen < 0) { |
| 291 | ret = dlen; |
| 292 | goto out1; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 293 | } |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 294 | dh_inputs.p_size = dlen; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 295 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 296 | dlen = dh_data_from_key(pcopy.base, &dh_inputs.g); |
| 297 | if (dlen < 0) { |
| 298 | ret = dlen; |
| 299 | goto out2; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 300 | } |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 301 | dh_inputs.g_size = dlen; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 302 | |
Lubomir Rintel | 8c0f9f5 | 2018-09-24 13:18:34 +0100 | [diff] [blame^] | 303 | dlen = dh_data_from_key(pcopy.private, &dh_inputs.key); |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 304 | if (dlen < 0) { |
| 305 | ret = dlen; |
| 306 | goto out2; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 307 | } |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 308 | dh_inputs.key_size = dlen; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 309 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 310 | secretlen = crypto_dh_key_len(&dh_inputs); |
| 311 | secret = kmalloc(secretlen, GFP_KERNEL); |
| 312 | if (!secret) { |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 313 | ret = -ENOMEM; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 314 | goto out2; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 315 | } |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 316 | ret = crypto_dh_encode_key(secret, secretlen, &dh_inputs); |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 317 | if (ret) |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 318 | goto out3; |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 319 | |
Eric Biggers | 85d7311 | 2018-06-30 15:16:16 -0700 | [diff] [blame] | 320 | tfm = crypto_alloc_kpp("dh", 0, 0); |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 321 | if (IS_ERR(tfm)) { |
| 322 | ret = PTR_ERR(tfm); |
| 323 | goto out3; |
| 324 | } |
| 325 | |
| 326 | ret = crypto_kpp_set_secret(tfm, secret, secretlen); |
| 327 | if (ret) |
| 328 | goto out4; |
| 329 | |
| 330 | outlen = crypto_kpp_maxsize(tfm); |
| 331 | |
| 332 | if (!kdfcopy) { |
| 333 | /* |
| 334 | * When not using a KDF, buflen 0 is used to read the |
| 335 | * required buffer length |
| 336 | */ |
| 337 | if (buflen == 0) { |
| 338 | ret = outlen; |
| 339 | goto out4; |
| 340 | } else if (outlen > buflen) { |
| 341 | ret = -EOVERFLOW; |
| 342 | goto out4; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | outbuf = kzalloc(kdfcopy ? (outlen + kdfcopy->otherinfolen) : outlen, |
| 347 | GFP_KERNEL); |
| 348 | if (!outbuf) { |
| 349 | ret = -ENOMEM; |
| 350 | goto out4; |
| 351 | } |
| 352 | |
| 353 | sg_init_one(&outsg, outbuf, outlen); |
| 354 | |
| 355 | req = kpp_request_alloc(tfm, GFP_KERNEL); |
| 356 | if (!req) { |
| 357 | ret = -ENOMEM; |
| 358 | goto out5; |
| 359 | } |
| 360 | |
| 361 | kpp_request_set_input(req, NULL, 0); |
| 362 | kpp_request_set_output(req, &outsg, outlen); |
| 363 | init_completion(&compl.completion); |
| 364 | kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 365 | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 366 | dh_crypto_done, &compl); |
| 367 | |
| 368 | /* |
| 369 | * For DH, generate_public_key and generate_shared_secret are |
| 370 | * the same calculation |
| 371 | */ |
| 372 | ret = crypto_kpp_generate_public_key(req); |
| 373 | if (ret == -EINPROGRESS) { |
| 374 | wait_for_completion(&compl.completion); |
| 375 | ret = compl.err; |
| 376 | if (ret) |
| 377 | goto out6; |
| 378 | } |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 379 | |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 380 | if (kdfcopy) { |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 381 | /* |
| 382 | * Concatenate SP800-56A otherinfo past DH shared secret -- the |
| 383 | * input to the KDF is (DH shared secret || otherinfo) |
| 384 | */ |
| 385 | if (copy_from_user(outbuf + req->dst_len, kdfcopy->otherinfo, |
| 386 | kdfcopy->otherinfolen) != 0) { |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 387 | ret = -EFAULT; |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 388 | goto out6; |
| 389 | } |
| 390 | |
| 391 | ret = keyctl_dh_compute_kdf(sdesc, buffer, buflen, outbuf, |
| 392 | req->dst_len + kdfcopy->otherinfolen, |
| 393 | outlen - req->dst_len); |
| 394 | } else if (copy_to_user(buffer, outbuf, req->dst_len) == 0) { |
| 395 | ret = req->dst_len; |
| 396 | } else { |
| 397 | ret = -EFAULT; |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 398 | } |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 399 | |
Mat Martineau | 7cbe093 | 2017-06-08 14:50:11 +0100 | [diff] [blame] | 400 | out6: |
| 401 | kpp_request_free(req); |
| 402 | out5: |
| 403 | kzfree(outbuf); |
| 404 | out4: |
| 405 | crypto_free_kpp(tfm); |
| 406 | out3: |
| 407 | kzfree(secret); |
| 408 | out2: |
| 409 | dh_free_data(&dh_inputs); |
| 410 | out1: |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 411 | kdf_dealloc(sdesc); |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 412 | return ret; |
| 413 | } |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 414 | |
| 415 | long keyctl_dh_compute(struct keyctl_dh_params __user *params, |
| 416 | char __user *buffer, size_t buflen, |
| 417 | struct keyctl_kdf_params __user *kdf) |
| 418 | { |
| 419 | struct keyctl_kdf_params kdfcopy; |
| 420 | |
| 421 | if (!kdf) |
| 422 | return __keyctl_dh_compute(params, buffer, buflen, NULL); |
| 423 | |
| 424 | if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0) |
| 425 | return -EFAULT; |
| 426 | |
| 427 | return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy); |
| 428 | } |