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