Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * algif_skcipher: User-space interface for skcipher algorithms |
| 3 | * |
| 4 | * This file provides the user-space API for symmetric key ciphers. |
| 5 | * |
| 6 | * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include <crypto/scatterwalk.h> |
| 16 | #include <crypto/skcipher.h> |
| 17 | #include <crypto/if_alg.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/net.h> |
| 24 | #include <net/sock.h> |
| 25 | |
| 26 | struct skcipher_sg_list { |
| 27 | struct list_head list; |
| 28 | |
| 29 | int cur; |
| 30 | |
| 31 | struct scatterlist sg[0]; |
| 32 | }; |
| 33 | |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 34 | struct skcipher_tfm { |
| 35 | struct crypto_skcipher *skcipher; |
| 36 | bool has_key; |
| 37 | }; |
| 38 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 39 | struct skcipher_ctx { |
| 40 | struct list_head tsgl; |
| 41 | struct af_alg_sgl rsgl; |
| 42 | |
| 43 | void *iv; |
| 44 | |
| 45 | struct af_alg_completion completion; |
| 46 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 47 | atomic_t inflight; |
LABBE Corentin | 652d5b8 | 2015-10-23 14:10:36 +0200 | [diff] [blame] | 48 | size_t used; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 49 | |
| 50 | unsigned int len; |
| 51 | bool more; |
| 52 | bool merge; |
| 53 | bool enc; |
| 54 | |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 55 | struct skcipher_request req; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 58 | struct skcipher_async_rsgl { |
| 59 | struct af_alg_sgl sgl; |
| 60 | struct list_head list; |
| 61 | }; |
| 62 | |
| 63 | struct skcipher_async_req { |
| 64 | struct kiocb *iocb; |
| 65 | struct skcipher_async_rsgl first_sgl; |
| 66 | struct list_head list; |
| 67 | struct scatterlist *tsg; |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 68 | atomic_t *inflight; |
| 69 | struct skcipher_request req; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Ondrej Kozina | e2cffb5 | 2014-08-25 11:49:54 +0200 | [diff] [blame] | 72 | #define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \ |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 73 | sizeof(struct scatterlist) - 1) |
| 74 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 75 | static void skcipher_free_async_sgls(struct skcipher_async_req *sreq) |
| 76 | { |
| 77 | struct skcipher_async_rsgl *rsgl, *tmp; |
| 78 | struct scatterlist *sgl; |
| 79 | struct scatterlist *sg; |
| 80 | int i, n; |
| 81 | |
| 82 | list_for_each_entry_safe(rsgl, tmp, &sreq->list, list) { |
| 83 | af_alg_free_sg(&rsgl->sgl); |
| 84 | if (rsgl != &sreq->first_sgl) |
| 85 | kfree(rsgl); |
| 86 | } |
| 87 | sgl = sreq->tsg; |
| 88 | n = sg_nents(sgl); |
Stephan Mueller | 9e0a643 | 2017-08-16 11:56:24 +0200 | [diff] [blame] | 89 | for_each_sg(sgl, sg, n, i) { |
| 90 | struct page *page = sg_page(sg); |
| 91 | |
| 92 | /* some SGs may not have a page mapped */ |
| 93 | if (page && page_ref_count(page)) |
| 94 | put_page(page); |
| 95 | } |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 96 | |
| 97 | kfree(sreq->tsg); |
| 98 | } |
| 99 | |
| 100 | static void skcipher_async_cb(struct crypto_async_request *req, int err) |
| 101 | { |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 102 | struct skcipher_async_req *sreq = req->data; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 103 | struct kiocb *iocb = sreq->iocb; |
| 104 | |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 105 | atomic_dec(sreq->inflight); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 106 | skcipher_free_async_sgls(sreq); |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 107 | kzfree(sreq); |
Al Viro | 237dae8 | 2015-04-09 00:00:30 -0400 | [diff] [blame] | 108 | iocb->ki_complete(iocb, err, err); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Herbert Xu | 0f6bb83 | 2010-11-30 16:49:02 +0800 | [diff] [blame] | 111 | static inline int skcipher_sndbuf(struct sock *sk) |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 112 | { |
| 113 | struct alg_sock *ask = alg_sk(sk); |
| 114 | struct skcipher_ctx *ctx = ask->private; |
| 115 | |
Herbert Xu | 0f6bb83 | 2010-11-30 16:49:02 +0800 | [diff] [blame] | 116 | return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) - |
| 117 | ctx->used, 0); |
| 118 | } |
| 119 | |
| 120 | static inline bool skcipher_writable(struct sock *sk) |
| 121 | { |
| 122 | return PAGE_SIZE <= skcipher_sndbuf(sk); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static int skcipher_alloc_sgl(struct sock *sk) |
| 126 | { |
| 127 | struct alg_sock *ask = alg_sk(sk); |
| 128 | struct skcipher_ctx *ctx = ask->private; |
| 129 | struct skcipher_sg_list *sgl; |
| 130 | struct scatterlist *sg = NULL; |
| 131 | |
| 132 | sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); |
| 133 | if (!list_empty(&ctx->tsgl)) |
| 134 | sg = sgl->sg; |
| 135 | |
| 136 | if (!sg || sgl->cur >= MAX_SGL_ENTS) { |
| 137 | sgl = sock_kmalloc(sk, sizeof(*sgl) + |
| 138 | sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1), |
| 139 | GFP_KERNEL); |
| 140 | if (!sgl) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | sg_init_table(sgl->sg, MAX_SGL_ENTS + 1); |
| 144 | sgl->cur = 0; |
| 145 | |
Stephan Mueller | e684db9 | 2017-09-21 10:16:53 +0200 | [diff] [blame] | 146 | if (sg) { |
Dan Williams | c56f6d1 | 2015-08-07 18:15:13 +0200 | [diff] [blame] | 147 | sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg); |
Stephan Mueller | e684db9 | 2017-09-21 10:16:53 +0200 | [diff] [blame] | 148 | sg_unmark_end(sg + (MAX_SGL_ENTS - 1)); |
| 149 | } |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 150 | |
| 151 | list_add_tail(&sgl->list, &ctx->tsgl); |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
LABBE Corentin | 652d5b8 | 2015-10-23 14:10:36 +0200 | [diff] [blame] | 157 | static void skcipher_pull_sgl(struct sock *sk, size_t used, int put) |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 158 | { |
| 159 | struct alg_sock *ask = alg_sk(sk); |
| 160 | struct skcipher_ctx *ctx = ask->private; |
| 161 | struct skcipher_sg_list *sgl; |
| 162 | struct scatterlist *sg; |
| 163 | int i; |
| 164 | |
| 165 | while (!list_empty(&ctx->tsgl)) { |
| 166 | sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list, |
| 167 | list); |
| 168 | sg = sgl->sg; |
| 169 | |
| 170 | for (i = 0; i < sgl->cur; i++) { |
LABBE Corentin | 652d5b8 | 2015-10-23 14:10:36 +0200 | [diff] [blame] | 171 | size_t plen = min_t(size_t, used, sg[i].length); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 172 | |
| 173 | if (!sg_page(sg + i)) |
| 174 | continue; |
| 175 | |
| 176 | sg[i].length -= plen; |
| 177 | sg[i].offset += plen; |
| 178 | |
| 179 | used -= plen; |
| 180 | ctx->used -= plen; |
| 181 | |
| 182 | if (sg[i].length) |
| 183 | return; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 184 | if (put) |
| 185 | put_page(sg_page(sg + i)); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 186 | sg_assign_page(sg + i, NULL); |
| 187 | } |
| 188 | |
| 189 | list_del(&sgl->list); |
| 190 | sock_kfree_s(sk, sgl, |
| 191 | sizeof(*sgl) + sizeof(sgl->sg[0]) * |
| 192 | (MAX_SGL_ENTS + 1)); |
| 193 | } |
| 194 | |
| 195 | if (!ctx->used) |
| 196 | ctx->merge = 0; |
| 197 | } |
| 198 | |
| 199 | static void skcipher_free_sgl(struct sock *sk) |
| 200 | { |
| 201 | struct alg_sock *ask = alg_sk(sk); |
| 202 | struct skcipher_ctx *ctx = ask->private; |
| 203 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 204 | skcipher_pull_sgl(sk, ctx->used, 1); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags) |
| 208 | { |
| 209 | long timeout; |
| 210 | DEFINE_WAIT(wait); |
| 211 | int err = -ERESTARTSYS; |
| 212 | |
| 213 | if (flags & MSG_DONTWAIT) |
| 214 | return -EAGAIN; |
| 215 | |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 216 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 217 | |
| 218 | for (;;) { |
| 219 | if (signal_pending(current)) |
| 220 | break; |
| 221 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 222 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 223 | if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) { |
| 224 | err = 0; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | finish_wait(sk_sleep(sk), &wait); |
| 229 | |
| 230 | return err; |
| 231 | } |
| 232 | |
| 233 | static void skcipher_wmem_wakeup(struct sock *sk) |
| 234 | { |
| 235 | struct socket_wq *wq; |
| 236 | |
| 237 | if (!skcipher_writable(sk)) |
| 238 | return; |
| 239 | |
| 240 | rcu_read_lock(); |
| 241 | wq = rcu_dereference(sk->sk_wq); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 242 | if (skwq_has_sleeper(wq)) |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 243 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | |
| 244 | POLLRDNORM | |
| 245 | POLLRDBAND); |
| 246 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); |
| 247 | rcu_read_unlock(); |
| 248 | } |
| 249 | |
| 250 | static int skcipher_wait_for_data(struct sock *sk, unsigned flags) |
| 251 | { |
| 252 | struct alg_sock *ask = alg_sk(sk); |
| 253 | struct skcipher_ctx *ctx = ask->private; |
| 254 | long timeout; |
| 255 | DEFINE_WAIT(wait); |
| 256 | int err = -ERESTARTSYS; |
| 257 | |
| 258 | if (flags & MSG_DONTWAIT) { |
| 259 | return -EAGAIN; |
| 260 | } |
| 261 | |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 262 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 263 | |
| 264 | for (;;) { |
| 265 | if (signal_pending(current)) |
| 266 | break; |
| 267 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
| 268 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 269 | if (sk_wait_event(sk, &timeout, ctx->used)) { |
| 270 | err = 0; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | finish_wait(sk_sleep(sk), &wait); |
| 275 | |
Eric Dumazet | 9cd3e07 | 2015-11-29 20:03:10 -0800 | [diff] [blame] | 276 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 277 | |
| 278 | return err; |
| 279 | } |
| 280 | |
| 281 | static void skcipher_data_wakeup(struct sock *sk) |
| 282 | { |
| 283 | struct alg_sock *ask = alg_sk(sk); |
| 284 | struct skcipher_ctx *ctx = ask->private; |
| 285 | struct socket_wq *wq; |
| 286 | |
| 287 | if (!ctx->used) |
| 288 | return; |
| 289 | |
| 290 | rcu_read_lock(); |
| 291 | wq = rcu_dereference(sk->sk_wq); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 292 | if (skwq_has_sleeper(wq)) |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 293 | wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | |
| 294 | POLLRDNORM | |
| 295 | POLLRDBAND); |
| 296 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
| 297 | rcu_read_unlock(); |
| 298 | } |
| 299 | |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 300 | static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg, |
| 301 | size_t size) |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 302 | { |
| 303 | struct sock *sk = sock->sk; |
| 304 | struct alg_sock *ask = alg_sk(sk); |
Herbert Xu | 6454c2b | 2016-02-03 21:39:26 +0800 | [diff] [blame] | 305 | struct sock *psk = ask->parent; |
| 306 | struct alg_sock *pask = alg_sk(psk); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 307 | struct skcipher_ctx *ctx = ask->private; |
Herbert Xu | 6454c2b | 2016-02-03 21:39:26 +0800 | [diff] [blame] | 308 | struct skcipher_tfm *skc = pask->private; |
| 309 | struct crypto_skcipher *tfm = skc->skcipher; |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 310 | unsigned ivsize = crypto_skcipher_ivsize(tfm); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 311 | struct skcipher_sg_list *sgl; |
| 312 | struct af_alg_control con = {}; |
| 313 | long copied = 0; |
| 314 | bool enc = 0; |
Stephan Mueller | f26b7b8 | 2014-11-30 10:55:26 +0100 | [diff] [blame] | 315 | bool init = 0; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 316 | int err; |
| 317 | int i; |
| 318 | |
| 319 | if (msg->msg_controllen) { |
| 320 | err = af_alg_cmsg_send(msg, &con); |
| 321 | if (err) |
| 322 | return err; |
| 323 | |
Stephan Mueller | f26b7b8 | 2014-11-30 10:55:26 +0100 | [diff] [blame] | 324 | init = 1; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 325 | switch (con.op) { |
| 326 | case ALG_OP_ENCRYPT: |
| 327 | enc = 1; |
| 328 | break; |
| 329 | case ALG_OP_DECRYPT: |
| 330 | enc = 0; |
| 331 | break; |
| 332 | default: |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | |
| 336 | if (con.iv && con.iv->ivlen != ivsize) |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | err = -EINVAL; |
| 341 | |
| 342 | lock_sock(sk); |
| 343 | if (!ctx->more && ctx->used) |
| 344 | goto unlock; |
| 345 | |
Stephan Mueller | f26b7b8 | 2014-11-30 10:55:26 +0100 | [diff] [blame] | 346 | if (init) { |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 347 | ctx->enc = enc; |
| 348 | if (con.iv) |
| 349 | memcpy(ctx->iv, con.iv->iv, ivsize); |
| 350 | } |
| 351 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 352 | while (size) { |
| 353 | struct scatterlist *sg; |
| 354 | unsigned long len = size; |
LABBE Corentin | 652d5b8 | 2015-10-23 14:10:36 +0200 | [diff] [blame] | 355 | size_t plen; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 356 | |
| 357 | if (ctx->merge) { |
| 358 | sgl = list_entry(ctx->tsgl.prev, |
| 359 | struct skcipher_sg_list, list); |
| 360 | sg = sgl->sg + sgl->cur - 1; |
| 361 | len = min_t(unsigned long, len, |
| 362 | PAGE_SIZE - sg->offset - sg->length); |
| 363 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 364 | err = memcpy_from_msg(page_address(sg_page(sg)) + |
| 365 | sg->offset + sg->length, |
| 366 | msg, len); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 367 | if (err) |
| 368 | goto unlock; |
| 369 | |
| 370 | sg->length += len; |
| 371 | ctx->merge = (sg->offset + sg->length) & |
| 372 | (PAGE_SIZE - 1); |
| 373 | |
| 374 | ctx->used += len; |
| 375 | copied += len; |
| 376 | size -= len; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 377 | continue; |
| 378 | } |
| 379 | |
Herbert Xu | 0f6bb83 | 2010-11-30 16:49:02 +0800 | [diff] [blame] | 380 | if (!skcipher_writable(sk)) { |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 381 | err = skcipher_wait_for_wmem(sk, msg->msg_flags); |
| 382 | if (err) |
| 383 | goto unlock; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 384 | } |
| 385 | |
Herbert Xu | 0f6bb83 | 2010-11-30 16:49:02 +0800 | [diff] [blame] | 386 | len = min_t(unsigned long, len, skcipher_sndbuf(sk)); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 387 | |
| 388 | err = skcipher_alloc_sgl(sk); |
| 389 | if (err) |
| 390 | goto unlock; |
| 391 | |
| 392 | sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); |
| 393 | sg = sgl->sg; |
Herbert Xu | 202736d | 2016-01-19 21:23:57 +0800 | [diff] [blame] | 394 | if (sgl->cur) |
| 395 | sg_unmark_end(sg + sgl->cur - 1); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 396 | do { |
| 397 | i = sgl->cur; |
LABBE Corentin | 652d5b8 | 2015-10-23 14:10:36 +0200 | [diff] [blame] | 398 | plen = min_t(size_t, len, PAGE_SIZE); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 399 | |
| 400 | sg_assign_page(sg + i, alloc_page(GFP_KERNEL)); |
| 401 | err = -ENOMEM; |
| 402 | if (!sg_page(sg + i)) |
| 403 | goto unlock; |
| 404 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 405 | err = memcpy_from_msg(page_address(sg_page(sg + i)), |
| 406 | msg, plen); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 407 | if (err) { |
| 408 | __free_page(sg_page(sg + i)); |
| 409 | sg_assign_page(sg + i, NULL); |
| 410 | goto unlock; |
| 411 | } |
| 412 | |
| 413 | sg[i].length = plen; |
| 414 | len -= plen; |
| 415 | ctx->used += plen; |
| 416 | copied += plen; |
| 417 | size -= plen; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 418 | sgl->cur++; |
| 419 | } while (len && sgl->cur < MAX_SGL_ENTS); |
| 420 | |
Tadeusz Struk | 0f477b6 | 2014-12-08 12:03:42 -0800 | [diff] [blame] | 421 | if (!size) |
| 422 | sg_mark_end(sg + sgl->cur - 1); |
| 423 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 424 | ctx->merge = plen & (PAGE_SIZE - 1); |
| 425 | } |
| 426 | |
| 427 | err = 0; |
| 428 | |
| 429 | ctx->more = msg->msg_flags & MSG_MORE; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 430 | |
| 431 | unlock: |
| 432 | skcipher_data_wakeup(sk); |
| 433 | release_sock(sk); |
| 434 | |
| 435 | return copied ?: err; |
| 436 | } |
| 437 | |
| 438 | static ssize_t skcipher_sendpage(struct socket *sock, struct page *page, |
| 439 | int offset, size_t size, int flags) |
| 440 | { |
| 441 | struct sock *sk = sock->sk; |
| 442 | struct alg_sock *ask = alg_sk(sk); |
| 443 | struct skcipher_ctx *ctx = ask->private; |
| 444 | struct skcipher_sg_list *sgl; |
| 445 | int err = -EINVAL; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 446 | |
Shawn Landden | d3f7d56 | 2013-11-24 22:36:28 -0800 | [diff] [blame] | 447 | if (flags & MSG_SENDPAGE_NOTLAST) |
| 448 | flags |= MSG_MORE; |
| 449 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 450 | lock_sock(sk); |
| 451 | if (!ctx->more && ctx->used) |
| 452 | goto unlock; |
| 453 | |
| 454 | if (!size) |
| 455 | goto done; |
| 456 | |
Herbert Xu | 0f6bb83 | 2010-11-30 16:49:02 +0800 | [diff] [blame] | 457 | if (!skcipher_writable(sk)) { |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 458 | err = skcipher_wait_for_wmem(sk, flags); |
| 459 | if (err) |
| 460 | goto unlock; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | err = skcipher_alloc_sgl(sk); |
| 464 | if (err) |
| 465 | goto unlock; |
| 466 | |
| 467 | ctx->merge = 0; |
| 468 | sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); |
| 469 | |
Tadeusz Struk | 0f477b6 | 2014-12-08 12:03:42 -0800 | [diff] [blame] | 470 | if (sgl->cur) |
| 471 | sg_unmark_end(sgl->sg + sgl->cur - 1); |
| 472 | |
| 473 | sg_mark_end(sgl->sg + sgl->cur); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 474 | get_page(page); |
| 475 | sg_set_page(sgl->sg + sgl->cur, page, size, offset); |
| 476 | sgl->cur++; |
| 477 | ctx->used += size; |
| 478 | |
| 479 | done: |
| 480 | ctx->more = flags & MSG_MORE; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 481 | |
| 482 | unlock: |
| 483 | skcipher_data_wakeup(sk); |
| 484 | release_sock(sk); |
| 485 | |
| 486 | return err ?: size; |
| 487 | } |
| 488 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 489 | static int skcipher_all_sg_nents(struct skcipher_ctx *ctx) |
| 490 | { |
| 491 | struct skcipher_sg_list *sgl; |
| 492 | struct scatterlist *sg; |
| 493 | int nents = 0; |
| 494 | |
| 495 | list_for_each_entry(sgl, &ctx->tsgl, list) { |
| 496 | sg = sgl->sg; |
| 497 | |
| 498 | while (!sg->length) |
| 499 | sg++; |
| 500 | |
| 501 | nents += sg_nents(sg); |
| 502 | } |
| 503 | return nents; |
| 504 | } |
| 505 | |
| 506 | static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg, |
| 507 | int flags) |
| 508 | { |
| 509 | struct sock *sk = sock->sk; |
| 510 | struct alg_sock *ask = alg_sk(sk); |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 511 | struct sock *psk = ask->parent; |
| 512 | struct alg_sock *pask = alg_sk(psk); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 513 | struct skcipher_ctx *ctx = ask->private; |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 514 | struct skcipher_tfm *skc = pask->private; |
| 515 | struct crypto_skcipher *tfm = skc->skcipher; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 516 | struct skcipher_sg_list *sgl; |
| 517 | struct scatterlist *sg; |
| 518 | struct skcipher_async_req *sreq; |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 519 | struct skcipher_request *req; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 520 | struct skcipher_async_rsgl *last_rsgl = NULL; |
Herbert Xu | 6454c2b | 2016-02-03 21:39:26 +0800 | [diff] [blame] | 521 | unsigned int txbufs = 0, len = 0, tx_nents; |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 522 | unsigned int reqsize = crypto_skcipher_reqsize(tfm); |
| 523 | unsigned int ivsize = crypto_skcipher_ivsize(tfm); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 524 | int err = -ENOMEM; |
tadeusz.struk@intel.com | 033f46b | 2015-04-01 13:53:06 -0700 | [diff] [blame] | 525 | bool mark = false; |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 526 | char *iv; |
| 527 | |
| 528 | sreq = kzalloc(sizeof(*sreq) + reqsize + ivsize, GFP_KERNEL); |
| 529 | if (unlikely(!sreq)) |
| 530 | goto out; |
| 531 | |
| 532 | req = &sreq->req; |
| 533 | iv = (char *)(req + 1) + reqsize; |
| 534 | sreq->iocb = msg->msg_iocb; |
| 535 | INIT_LIST_HEAD(&sreq->list); |
| 536 | sreq->inflight = &ctx->inflight; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 537 | |
| 538 | lock_sock(sk); |
Herbert Xu | 6454c2b | 2016-02-03 21:39:26 +0800 | [diff] [blame] | 539 | tx_nents = skcipher_all_sg_nents(ctx); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 540 | sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL); |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 541 | if (unlikely(!sreq->tsg)) |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 542 | goto unlock; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 543 | sg_init_table(sreq->tsg, tx_nents); |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 544 | memcpy(iv, ctx->iv, ivsize); |
| 545 | skcipher_request_set_tfm(req, tfm); |
Herbert Xu | dad4199 | 2016-02-03 21:39:27 +0800 | [diff] [blame] | 546 | skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 547 | skcipher_async_cb, sreq); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 548 | |
| 549 | while (iov_iter_count(&msg->msg_iter)) { |
| 550 | struct skcipher_async_rsgl *rsgl; |
tadeusz.struk@intel.com | ac110f4 | 2015-03-25 07:29:19 -0700 | [diff] [blame] | 551 | int used; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 552 | |
| 553 | if (!ctx->used) { |
| 554 | err = skcipher_wait_for_data(sk, flags); |
| 555 | if (err) |
| 556 | goto free; |
| 557 | } |
| 558 | sgl = list_first_entry(&ctx->tsgl, |
| 559 | struct skcipher_sg_list, list); |
| 560 | sg = sgl->sg; |
| 561 | |
| 562 | while (!sg->length) |
| 563 | sg++; |
| 564 | |
| 565 | used = min_t(unsigned long, ctx->used, |
| 566 | iov_iter_count(&msg->msg_iter)); |
| 567 | used = min_t(unsigned long, used, sg->length); |
| 568 | |
tadeusz.struk@intel.com | 033f46b | 2015-04-01 13:53:06 -0700 | [diff] [blame] | 569 | if (txbufs == tx_nents) { |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 570 | struct scatterlist *tmp; |
| 571 | int x; |
| 572 | /* Ran out of tx slots in async request |
| 573 | * need to expand */ |
| 574 | tmp = kcalloc(tx_nents * 2, sizeof(*tmp), |
| 575 | GFP_KERNEL); |
| 576 | if (!tmp) |
| 577 | goto free; |
| 578 | |
| 579 | sg_init_table(tmp, tx_nents * 2); |
| 580 | for (x = 0; x < tx_nents; x++) |
| 581 | sg_set_page(&tmp[x], sg_page(&sreq->tsg[x]), |
| 582 | sreq->tsg[x].length, |
| 583 | sreq->tsg[x].offset); |
| 584 | kfree(sreq->tsg); |
| 585 | sreq->tsg = tmp; |
| 586 | tx_nents *= 2; |
tadeusz.struk@intel.com | 033f46b | 2015-04-01 13:53:06 -0700 | [diff] [blame] | 587 | mark = true; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 588 | } |
| 589 | /* Need to take over the tx sgl from ctx |
| 590 | * to the asynch req - these sgls will be freed later */ |
tadeusz.struk@intel.com | 033f46b | 2015-04-01 13:53:06 -0700 | [diff] [blame] | 591 | sg_set_page(sreq->tsg + txbufs++, sg_page(sg), sg->length, |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 592 | sg->offset); |
| 593 | |
| 594 | if (list_empty(&sreq->list)) { |
| 595 | rsgl = &sreq->first_sgl; |
| 596 | list_add_tail(&rsgl->list, &sreq->list); |
| 597 | } else { |
Tadeusz Struk | 82d9292 | 2015-03-30 14:25:44 -0700 | [diff] [blame] | 598 | rsgl = kmalloc(sizeof(*rsgl), GFP_KERNEL); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 599 | if (!rsgl) { |
| 600 | err = -ENOMEM; |
| 601 | goto free; |
| 602 | } |
| 603 | list_add_tail(&rsgl->list, &sreq->list); |
| 604 | } |
| 605 | |
| 606 | used = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, used); |
| 607 | err = used; |
| 608 | if (used < 0) |
| 609 | goto free; |
| 610 | if (last_rsgl) |
| 611 | af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl); |
| 612 | |
| 613 | last_rsgl = rsgl; |
| 614 | len += used; |
| 615 | skcipher_pull_sgl(sk, used, 0); |
| 616 | iov_iter_advance(&msg->msg_iter, used); |
| 617 | } |
| 618 | |
tadeusz.struk@intel.com | 033f46b | 2015-04-01 13:53:06 -0700 | [diff] [blame] | 619 | if (mark) |
| 620 | sg_mark_end(sreq->tsg + txbufs - 1); |
| 621 | |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 622 | skcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg, |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 623 | len, iv); |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 624 | err = ctx->enc ? crypto_skcipher_encrypt(req) : |
| 625 | crypto_skcipher_decrypt(req); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 626 | if (err == -EINPROGRESS) { |
| 627 | atomic_inc(&ctx->inflight); |
| 628 | err = -EIOCBQUEUED; |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 629 | sreq = NULL; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 630 | goto unlock; |
| 631 | } |
| 632 | free: |
| 633 | skcipher_free_async_sgls(sreq); |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 634 | unlock: |
| 635 | skcipher_wmem_wakeup(sk); |
| 636 | release_sock(sk); |
Herbert Xu | ec69bbf | 2016-02-03 21:39:24 +0800 | [diff] [blame] | 637 | kzfree(sreq); |
| 638 | out: |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 639 | return err; |
| 640 | } |
| 641 | |
| 642 | static int skcipher_recvmsg_sync(struct socket *sock, struct msghdr *msg, |
| 643 | int flags) |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 644 | { |
| 645 | struct sock *sk = sock->sk; |
| 646 | struct alg_sock *ask = alg_sk(sk); |
Herbert Xu | 6454c2b | 2016-02-03 21:39:26 +0800 | [diff] [blame] | 647 | struct sock *psk = ask->parent; |
| 648 | struct alg_sock *pask = alg_sk(psk); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 649 | struct skcipher_ctx *ctx = ask->private; |
Herbert Xu | 6454c2b | 2016-02-03 21:39:26 +0800 | [diff] [blame] | 650 | struct skcipher_tfm *skc = pask->private; |
| 651 | struct crypto_skcipher *tfm = skc->skcipher; |
| 652 | unsigned bs = crypto_skcipher_blocksize(tfm); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 653 | struct skcipher_sg_list *sgl; |
| 654 | struct scatterlist *sg; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 655 | int err = -EAGAIN; |
| 656 | int used; |
| 657 | long copied = 0; |
| 658 | |
| 659 | lock_sock(sk); |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 660 | while (msg_data_left(msg)) { |
Linus Torvalds | 9399f0c | 2015-02-10 19:55:45 -0800 | [diff] [blame] | 661 | if (!ctx->used) { |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 662 | err = skcipher_wait_for_data(sk, flags); |
| 663 | if (err) |
Herbert Xu | bc97e57 | 2010-11-30 17:04:31 +0800 | [diff] [blame] | 664 | goto unlock; |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 665 | } |
Herbert Xu | bc97e57 | 2010-11-30 17:04:31 +0800 | [diff] [blame] | 666 | |
Al Viro | 01e97e6 | 2014-12-15 21:39:31 -0500 | [diff] [blame] | 667 | used = min_t(unsigned long, ctx->used, msg_data_left(msg)); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 668 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 669 | used = af_alg_make_sg(&ctx->rsgl, &msg->msg_iter, used); |
| 670 | err = used; |
| 671 | if (err < 0) |
| 672 | goto unlock; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 673 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 674 | if (ctx->more || used < ctx->used) |
| 675 | used -= used % bs; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 676 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 677 | err = -EINVAL; |
| 678 | if (!used) |
| 679 | goto free; |
| 680 | |
Herbert Xu | 4f0414e | 2016-01-18 18:46:10 +0800 | [diff] [blame] | 681 | sgl = list_first_entry(&ctx->tsgl, |
| 682 | struct skcipher_sg_list, list); |
| 683 | sg = sgl->sg; |
| 684 | |
| 685 | while (!sg->length) |
| 686 | sg++; |
| 687 | |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 688 | skcipher_request_set_crypt(&ctx->req, sg, ctx->rsgl.sg, used, |
| 689 | ctx->iv); |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 690 | |
| 691 | err = af_alg_wait_for_completion( |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 692 | ctx->enc ? |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 693 | crypto_skcipher_encrypt(&ctx->req) : |
| 694 | crypto_skcipher_decrypt(&ctx->req), |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 695 | &ctx->completion); |
| 696 | |
Herbert Xu | bc97e57 | 2010-11-30 17:04:31 +0800 | [diff] [blame] | 697 | free: |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 698 | af_alg_free_sg(&ctx->rsgl); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 699 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 700 | if (err) |
| 701 | goto unlock; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 702 | |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 703 | copied += used; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 704 | skcipher_pull_sgl(sk, used, 1); |
Al Viro | 1d10eb2 | 2014-11-28 16:39:25 -0500 | [diff] [blame] | 705 | iov_iter_advance(&msg->msg_iter, used); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | err = 0; |
| 709 | |
| 710 | unlock: |
| 711 | skcipher_wmem_wakeup(sk); |
| 712 | release_sock(sk); |
| 713 | |
| 714 | return copied ?: err; |
| 715 | } |
| 716 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 717 | static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg, |
| 718 | size_t ignored, int flags) |
| 719 | { |
| 720 | return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ? |
| 721 | skcipher_recvmsg_async(sock, msg, flags) : |
| 722 | skcipher_recvmsg_sync(sock, msg, flags); |
| 723 | } |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 724 | |
| 725 | static unsigned int skcipher_poll(struct file *file, struct socket *sock, |
| 726 | poll_table *wait) |
| 727 | { |
| 728 | struct sock *sk = sock->sk; |
| 729 | struct alg_sock *ask = alg_sk(sk); |
| 730 | struct skcipher_ctx *ctx = ask->private; |
| 731 | unsigned int mask; |
| 732 | |
| 733 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 734 | mask = 0; |
| 735 | |
| 736 | if (ctx->used) |
| 737 | mask |= POLLIN | POLLRDNORM; |
| 738 | |
| 739 | if (skcipher_writable(sk)) |
| 740 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 741 | |
| 742 | return mask; |
| 743 | } |
| 744 | |
| 745 | static struct proto_ops algif_skcipher_ops = { |
| 746 | .family = PF_ALG, |
| 747 | |
| 748 | .connect = sock_no_connect, |
| 749 | .socketpair = sock_no_socketpair, |
| 750 | .getname = sock_no_getname, |
| 751 | .ioctl = sock_no_ioctl, |
| 752 | .listen = sock_no_listen, |
| 753 | .shutdown = sock_no_shutdown, |
| 754 | .getsockopt = sock_no_getsockopt, |
| 755 | .mmap = sock_no_mmap, |
| 756 | .bind = sock_no_bind, |
| 757 | .accept = sock_no_accept, |
| 758 | .setsockopt = sock_no_setsockopt, |
| 759 | |
| 760 | .release = af_alg_release, |
| 761 | .sendmsg = skcipher_sendmsg, |
| 762 | .sendpage = skcipher_sendpage, |
| 763 | .recvmsg = skcipher_recvmsg, |
| 764 | .poll = skcipher_poll, |
| 765 | }; |
| 766 | |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 767 | static int skcipher_check_key(struct socket *sock) |
| 768 | { |
Herbert Xu | 1822793 | 2016-01-15 22:02:20 +0800 | [diff] [blame] | 769 | int err = 0; |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 770 | struct sock *psk; |
| 771 | struct alg_sock *pask; |
| 772 | struct skcipher_tfm *tfm; |
| 773 | struct sock *sk = sock->sk; |
| 774 | struct alg_sock *ask = alg_sk(sk); |
| 775 | |
Herbert Xu | 1822793 | 2016-01-15 22:02:20 +0800 | [diff] [blame] | 776 | lock_sock(sk); |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 777 | if (ask->refcnt) |
Herbert Xu | 1822793 | 2016-01-15 22:02:20 +0800 | [diff] [blame] | 778 | goto unlock_child; |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 779 | |
| 780 | psk = ask->parent; |
| 781 | pask = alg_sk(ask->parent); |
| 782 | tfm = pask->private; |
| 783 | |
| 784 | err = -ENOKEY; |
Herbert Xu | 1822793 | 2016-01-15 22:02:20 +0800 | [diff] [blame] | 785 | lock_sock_nested(psk, SINGLE_DEPTH_NESTING); |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 786 | if (!tfm->has_key) |
| 787 | goto unlock; |
| 788 | |
| 789 | if (!pask->refcnt++) |
| 790 | sock_hold(psk); |
| 791 | |
| 792 | ask->refcnt = 1; |
| 793 | sock_put(psk); |
| 794 | |
| 795 | err = 0; |
| 796 | |
| 797 | unlock: |
| 798 | release_sock(psk); |
Herbert Xu | 1822793 | 2016-01-15 22:02:20 +0800 | [diff] [blame] | 799 | unlock_child: |
| 800 | release_sock(sk); |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 801 | |
| 802 | return err; |
| 803 | } |
| 804 | |
| 805 | static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg, |
| 806 | size_t size) |
| 807 | { |
| 808 | int err; |
| 809 | |
| 810 | err = skcipher_check_key(sock); |
| 811 | if (err) |
| 812 | return err; |
| 813 | |
| 814 | return skcipher_sendmsg(sock, msg, size); |
| 815 | } |
| 816 | |
| 817 | static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page, |
| 818 | int offset, size_t size, int flags) |
| 819 | { |
| 820 | int err; |
| 821 | |
| 822 | err = skcipher_check_key(sock); |
| 823 | if (err) |
| 824 | return err; |
| 825 | |
| 826 | return skcipher_sendpage(sock, page, offset, size, flags); |
| 827 | } |
| 828 | |
| 829 | static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg, |
| 830 | size_t ignored, int flags) |
| 831 | { |
| 832 | int err; |
| 833 | |
| 834 | err = skcipher_check_key(sock); |
| 835 | if (err) |
| 836 | return err; |
| 837 | |
| 838 | return skcipher_recvmsg(sock, msg, ignored, flags); |
| 839 | } |
| 840 | |
| 841 | static struct proto_ops algif_skcipher_ops_nokey = { |
| 842 | .family = PF_ALG, |
| 843 | |
| 844 | .connect = sock_no_connect, |
| 845 | .socketpair = sock_no_socketpair, |
| 846 | .getname = sock_no_getname, |
| 847 | .ioctl = sock_no_ioctl, |
| 848 | .listen = sock_no_listen, |
| 849 | .shutdown = sock_no_shutdown, |
| 850 | .getsockopt = sock_no_getsockopt, |
| 851 | .mmap = sock_no_mmap, |
| 852 | .bind = sock_no_bind, |
| 853 | .accept = sock_no_accept, |
| 854 | .setsockopt = sock_no_setsockopt, |
| 855 | |
| 856 | .release = af_alg_release, |
| 857 | .sendmsg = skcipher_sendmsg_nokey, |
| 858 | .sendpage = skcipher_sendpage_nokey, |
| 859 | .recvmsg = skcipher_recvmsg_nokey, |
| 860 | .poll = skcipher_poll, |
| 861 | }; |
| 862 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 863 | static void *skcipher_bind(const char *name, u32 type, u32 mask) |
| 864 | { |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 865 | struct skcipher_tfm *tfm; |
| 866 | struct crypto_skcipher *skcipher; |
| 867 | |
| 868 | tfm = kzalloc(sizeof(*tfm), GFP_KERNEL); |
| 869 | if (!tfm) |
| 870 | return ERR_PTR(-ENOMEM); |
| 871 | |
| 872 | skcipher = crypto_alloc_skcipher(name, type, mask); |
| 873 | if (IS_ERR(skcipher)) { |
| 874 | kfree(tfm); |
| 875 | return ERR_CAST(skcipher); |
| 876 | } |
| 877 | |
| 878 | tfm->skcipher = skcipher; |
| 879 | |
| 880 | return tfm; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static void skcipher_release(void *private) |
| 884 | { |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 885 | struct skcipher_tfm *tfm = private; |
| 886 | |
| 887 | crypto_free_skcipher(tfm->skcipher); |
| 888 | kfree(tfm); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen) |
| 892 | { |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 893 | struct skcipher_tfm *tfm = private; |
| 894 | int err; |
| 895 | |
| 896 | err = crypto_skcipher_setkey(tfm->skcipher, key, keylen); |
| 897 | tfm->has_key = !err; |
| 898 | |
| 899 | return err; |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 900 | } |
| 901 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 902 | static void skcipher_wait(struct sock *sk) |
| 903 | { |
| 904 | struct alg_sock *ask = alg_sk(sk); |
| 905 | struct skcipher_ctx *ctx = ask->private; |
| 906 | int ctr = 0; |
| 907 | |
| 908 | while (atomic_read(&ctx->inflight) && ctr++ < 100) |
| 909 | msleep(100); |
| 910 | } |
| 911 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 912 | static void skcipher_sock_destruct(struct sock *sk) |
| 913 | { |
| 914 | struct alg_sock *ask = alg_sk(sk); |
| 915 | struct skcipher_ctx *ctx = ask->private; |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 916 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(&ctx->req); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 917 | |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 918 | if (atomic_read(&ctx->inflight)) |
| 919 | skcipher_wait(sk); |
| 920 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 921 | skcipher_free_sgl(sk); |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 922 | sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm)); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 923 | sock_kfree_s(sk, ctx, ctx->len); |
| 924 | af_alg_release_parent(sk); |
| 925 | } |
| 926 | |
Herbert Xu | d7b65ae | 2016-01-13 15:01:06 +0800 | [diff] [blame] | 927 | static int skcipher_accept_parent_nokey(void *private, struct sock *sk) |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 928 | { |
| 929 | struct skcipher_ctx *ctx; |
| 930 | struct alg_sock *ask = alg_sk(sk); |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 931 | struct skcipher_tfm *tfm = private; |
| 932 | struct crypto_skcipher *skcipher = tfm->skcipher; |
| 933 | unsigned int len = sizeof(*ctx) + crypto_skcipher_reqsize(skcipher); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 934 | |
| 935 | ctx = sock_kmalloc(sk, len, GFP_KERNEL); |
| 936 | if (!ctx) |
| 937 | return -ENOMEM; |
| 938 | |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 939 | ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher), |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 940 | GFP_KERNEL); |
| 941 | if (!ctx->iv) { |
| 942 | sock_kfree_s(sk, ctx, len); |
| 943 | return -ENOMEM; |
| 944 | } |
| 945 | |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 946 | memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher)); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 947 | |
| 948 | INIT_LIST_HEAD(&ctx->tsgl); |
| 949 | ctx->len = len; |
| 950 | ctx->used = 0; |
| 951 | ctx->more = 0; |
| 952 | ctx->merge = 0; |
| 953 | ctx->enc = 0; |
Tadeusz Struk | a596999 | 2015-03-19 12:31:40 -0700 | [diff] [blame] | 954 | atomic_set(&ctx->inflight, 0); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 955 | af_alg_init_completion(&ctx->completion); |
| 956 | |
| 957 | ask->private = ctx; |
| 958 | |
Herbert Xu | dd50458 | 2015-12-25 15:40:05 +0800 | [diff] [blame] | 959 | skcipher_request_set_tfm(&ctx->req, skcipher); |
Herbert Xu | dad4199 | 2016-02-03 21:39:27 +0800 | [diff] [blame] | 960 | skcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_SLEEP | |
| 961 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
Herbert Xu | 0d96e4b | 2015-12-18 19:16:57 +0800 | [diff] [blame] | 962 | af_alg_complete, &ctx->completion); |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 963 | |
| 964 | sk->sk_destruct = skcipher_sock_destruct; |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 969 | static int skcipher_accept_parent(void *private, struct sock *sk) |
| 970 | { |
| 971 | struct skcipher_tfm *tfm = private; |
| 972 | |
Herbert Xu | 6e8d8ec | 2016-01-11 21:29:41 +0800 | [diff] [blame] | 973 | if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher)) |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 974 | return -ENOKEY; |
| 975 | |
Herbert Xu | d7b65ae | 2016-01-13 15:01:06 +0800 | [diff] [blame] | 976 | return skcipher_accept_parent_nokey(private, sk); |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 977 | } |
| 978 | |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 979 | static const struct af_alg_type algif_type_skcipher = { |
| 980 | .bind = skcipher_bind, |
| 981 | .release = skcipher_release, |
| 982 | .setkey = skcipher_setkey, |
| 983 | .accept = skcipher_accept_parent, |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 984 | .accept_nokey = skcipher_accept_parent_nokey, |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 985 | .ops = &algif_skcipher_ops, |
Herbert Xu | a0fa2d0 | 2016-01-04 13:36:12 +0900 | [diff] [blame] | 986 | .ops_nokey = &algif_skcipher_ops_nokey, |
Herbert Xu | 8ff5909 | 2010-10-19 21:31:55 +0800 | [diff] [blame] | 987 | .name = "skcipher", |
| 988 | .owner = THIS_MODULE |
| 989 | }; |
| 990 | |
| 991 | static int __init algif_skcipher_init(void) |
| 992 | { |
| 993 | return af_alg_register_type(&algif_type_skcipher); |
| 994 | } |
| 995 | |
| 996 | static void __exit algif_skcipher_exit(void) |
| 997 | { |
| 998 | int err = af_alg_unregister_type(&algif_type_skcipher); |
| 999 | BUG_ON(err); |
| 1000 | } |
| 1001 | |
| 1002 | module_init(algif_skcipher_init); |
| 1003 | module_exit(algif_skcipher_exit); |
| 1004 | MODULE_LICENSE("GPL"); |