18ff59090SHerbert Xu /* 28ff59090SHerbert Xu * algif_skcipher: User-space interface for skcipher algorithms 38ff59090SHerbert Xu * 48ff59090SHerbert Xu * This file provides the user-space API for symmetric key ciphers. 58ff59090SHerbert Xu * 68ff59090SHerbert Xu * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> 78ff59090SHerbert Xu * 88ff59090SHerbert Xu * This program is free software; you can redistribute it and/or modify it 98ff59090SHerbert Xu * under the terms of the GNU General Public License as published by the Free 108ff59090SHerbert Xu * Software Foundation; either version 2 of the License, or (at your option) 118ff59090SHerbert Xu * any later version. 128ff59090SHerbert Xu * 138ff59090SHerbert Xu */ 148ff59090SHerbert Xu 158ff59090SHerbert Xu #include <crypto/scatterwalk.h> 168ff59090SHerbert Xu #include <crypto/skcipher.h> 178ff59090SHerbert Xu #include <crypto/if_alg.h> 188ff59090SHerbert Xu #include <linux/init.h> 198ff59090SHerbert Xu #include <linux/list.h> 208ff59090SHerbert Xu #include <linux/kernel.h> 218ff59090SHerbert Xu #include <linux/mm.h> 228ff59090SHerbert Xu #include <linux/module.h> 238ff59090SHerbert Xu #include <linux/net.h> 248ff59090SHerbert Xu #include <net/sock.h> 258ff59090SHerbert Xu 268ff59090SHerbert Xu struct skcipher_sg_list { 278ff59090SHerbert Xu struct list_head list; 288ff59090SHerbert Xu 298ff59090SHerbert Xu int cur; 308ff59090SHerbert Xu 318ff59090SHerbert Xu struct scatterlist sg[0]; 328ff59090SHerbert Xu }; 338ff59090SHerbert Xu 348ff59090SHerbert Xu struct skcipher_ctx { 358ff59090SHerbert Xu struct list_head tsgl; 368ff59090SHerbert Xu struct af_alg_sgl rsgl; 378ff59090SHerbert Xu 388ff59090SHerbert Xu void *iv; 398ff59090SHerbert Xu 408ff59090SHerbert Xu struct af_alg_completion completion; 418ff59090SHerbert Xu 42a596999bSTadeusz Struk atomic_t inflight; 438ff59090SHerbert Xu unsigned used; 448ff59090SHerbert Xu 458ff59090SHerbert Xu unsigned int len; 468ff59090SHerbert Xu bool more; 478ff59090SHerbert Xu bool merge; 488ff59090SHerbert Xu bool enc; 498ff59090SHerbert Xu 508ff59090SHerbert Xu struct ablkcipher_request req; 518ff59090SHerbert Xu }; 528ff59090SHerbert Xu 53a596999bSTadeusz Struk struct skcipher_async_rsgl { 54a596999bSTadeusz Struk struct af_alg_sgl sgl; 55a596999bSTadeusz Struk struct list_head list; 56a596999bSTadeusz Struk }; 57a596999bSTadeusz Struk 58a596999bSTadeusz Struk struct skcipher_async_req { 59a596999bSTadeusz Struk struct kiocb *iocb; 60a596999bSTadeusz Struk struct skcipher_async_rsgl first_sgl; 61a596999bSTadeusz Struk struct list_head list; 62a596999bSTadeusz Struk struct scatterlist *tsg; 63a596999bSTadeusz Struk char iv[]; 64a596999bSTadeusz Struk }; 65a596999bSTadeusz Struk 66a596999bSTadeusz Struk #define GET_SREQ(areq, ctx) (struct skcipher_async_req *)((char *)areq + \ 67a596999bSTadeusz Struk crypto_ablkcipher_reqsize(crypto_ablkcipher_reqtfm(&ctx->req))) 68a596999bSTadeusz Struk 69a596999bSTadeusz Struk #define GET_REQ_SIZE(ctx) \ 70a596999bSTadeusz Struk crypto_ablkcipher_reqsize(crypto_ablkcipher_reqtfm(&ctx->req)) 71a596999bSTadeusz Struk 72a596999bSTadeusz Struk #define GET_IV_SIZE(ctx) \ 73a596999bSTadeusz Struk crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(&ctx->req)) 74a596999bSTadeusz Struk 75e2cffb5fSOndrej Kozina #define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \ 768ff59090SHerbert Xu sizeof(struct scatterlist) - 1) 778ff59090SHerbert Xu 78a596999bSTadeusz Struk static void skcipher_free_async_sgls(struct skcipher_async_req *sreq) 79a596999bSTadeusz Struk { 80a596999bSTadeusz Struk struct skcipher_async_rsgl *rsgl, *tmp; 81a596999bSTadeusz Struk struct scatterlist *sgl; 82a596999bSTadeusz Struk struct scatterlist *sg; 83a596999bSTadeusz Struk int i, n; 84a596999bSTadeusz Struk 85a596999bSTadeusz Struk list_for_each_entry_safe(rsgl, tmp, &sreq->list, list) { 86a596999bSTadeusz Struk af_alg_free_sg(&rsgl->sgl); 87a596999bSTadeusz Struk if (rsgl != &sreq->first_sgl) 88a596999bSTadeusz Struk kfree(rsgl); 89a596999bSTadeusz Struk } 90a596999bSTadeusz Struk sgl = sreq->tsg; 91a596999bSTadeusz Struk n = sg_nents(sgl); 92a596999bSTadeusz Struk for_each_sg(sgl, sg, n, i) 93a596999bSTadeusz Struk put_page(sg_page(sg)); 94a596999bSTadeusz Struk 95a596999bSTadeusz Struk kfree(sreq->tsg); 96a596999bSTadeusz Struk } 97a596999bSTadeusz Struk 98a596999bSTadeusz Struk static void skcipher_async_cb(struct crypto_async_request *req, int err) 99a596999bSTadeusz Struk { 100a596999bSTadeusz Struk struct sock *sk = req->data; 101a596999bSTadeusz Struk struct alg_sock *ask = alg_sk(sk); 102a596999bSTadeusz Struk struct skcipher_ctx *ctx = ask->private; 103a596999bSTadeusz Struk struct skcipher_async_req *sreq = GET_SREQ(req, ctx); 104a596999bSTadeusz Struk struct kiocb *iocb = sreq->iocb; 105a596999bSTadeusz Struk 106a596999bSTadeusz Struk atomic_dec(&ctx->inflight); 107a596999bSTadeusz Struk skcipher_free_async_sgls(sreq); 108a596999bSTadeusz Struk kfree(req); 109237dae88SAl Viro iocb->ki_complete(iocb, err, err); 110a596999bSTadeusz Struk } 111a596999bSTadeusz Struk 1120f6bb83cSHerbert Xu static inline int skcipher_sndbuf(struct sock *sk) 1138ff59090SHerbert Xu { 1148ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 1158ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 1168ff59090SHerbert Xu 1170f6bb83cSHerbert Xu return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) - 1180f6bb83cSHerbert Xu ctx->used, 0); 1190f6bb83cSHerbert Xu } 1200f6bb83cSHerbert Xu 1210f6bb83cSHerbert Xu static inline bool skcipher_writable(struct sock *sk) 1220f6bb83cSHerbert Xu { 1230f6bb83cSHerbert Xu return PAGE_SIZE <= skcipher_sndbuf(sk); 1248ff59090SHerbert Xu } 1258ff59090SHerbert Xu 1268ff59090SHerbert Xu static int skcipher_alloc_sgl(struct sock *sk) 1278ff59090SHerbert Xu { 1288ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 1298ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 1308ff59090SHerbert Xu struct skcipher_sg_list *sgl; 1318ff59090SHerbert Xu struct scatterlist *sg = NULL; 1328ff59090SHerbert Xu 1338ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); 1348ff59090SHerbert Xu if (!list_empty(&ctx->tsgl)) 1358ff59090SHerbert Xu sg = sgl->sg; 1368ff59090SHerbert Xu 1378ff59090SHerbert Xu if (!sg || sgl->cur >= MAX_SGL_ENTS) { 1388ff59090SHerbert Xu sgl = sock_kmalloc(sk, sizeof(*sgl) + 1398ff59090SHerbert Xu sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1), 1408ff59090SHerbert Xu GFP_KERNEL); 1418ff59090SHerbert Xu if (!sgl) 1428ff59090SHerbert Xu return -ENOMEM; 1438ff59090SHerbert Xu 1448ff59090SHerbert Xu sg_init_table(sgl->sg, MAX_SGL_ENTS + 1); 1458ff59090SHerbert Xu sgl->cur = 0; 1468ff59090SHerbert Xu 1478ff59090SHerbert Xu if (sg) 148*c56f6d12SDan Williams sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg); 1498ff59090SHerbert Xu 1508ff59090SHerbert Xu list_add_tail(&sgl->list, &ctx->tsgl); 1518ff59090SHerbert Xu } 1528ff59090SHerbert Xu 1538ff59090SHerbert Xu return 0; 1548ff59090SHerbert Xu } 1558ff59090SHerbert Xu 156a596999bSTadeusz Struk static void skcipher_pull_sgl(struct sock *sk, int used, int put) 1578ff59090SHerbert Xu { 1588ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 1598ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 1608ff59090SHerbert Xu struct skcipher_sg_list *sgl; 1618ff59090SHerbert Xu struct scatterlist *sg; 1628ff59090SHerbert Xu int i; 1638ff59090SHerbert Xu 1648ff59090SHerbert Xu while (!list_empty(&ctx->tsgl)) { 1658ff59090SHerbert Xu sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list, 1668ff59090SHerbert Xu list); 1678ff59090SHerbert Xu sg = sgl->sg; 1688ff59090SHerbert Xu 1698ff59090SHerbert Xu for (i = 0; i < sgl->cur; i++) { 1708ff59090SHerbert Xu int plen = min_t(int, used, sg[i].length); 1718ff59090SHerbert Xu 1728ff59090SHerbert Xu if (!sg_page(sg + i)) 1738ff59090SHerbert Xu continue; 1748ff59090SHerbert Xu 1758ff59090SHerbert Xu sg[i].length -= plen; 1768ff59090SHerbert Xu sg[i].offset += plen; 1778ff59090SHerbert Xu 1788ff59090SHerbert Xu used -= plen; 1798ff59090SHerbert Xu ctx->used -= plen; 1808ff59090SHerbert Xu 1818ff59090SHerbert Xu if (sg[i].length) 1828ff59090SHerbert Xu return; 183a596999bSTadeusz Struk if (put) 1848ff59090SHerbert Xu put_page(sg_page(sg + i)); 1858ff59090SHerbert Xu sg_assign_page(sg + i, NULL); 1868ff59090SHerbert Xu } 1878ff59090SHerbert Xu 1888ff59090SHerbert Xu list_del(&sgl->list); 1898ff59090SHerbert Xu sock_kfree_s(sk, sgl, 1908ff59090SHerbert Xu sizeof(*sgl) + sizeof(sgl->sg[0]) * 1918ff59090SHerbert Xu (MAX_SGL_ENTS + 1)); 1928ff59090SHerbert Xu } 1938ff59090SHerbert Xu 1948ff59090SHerbert Xu if (!ctx->used) 1958ff59090SHerbert Xu ctx->merge = 0; 1968ff59090SHerbert Xu } 1978ff59090SHerbert Xu 1988ff59090SHerbert Xu static void skcipher_free_sgl(struct sock *sk) 1998ff59090SHerbert Xu { 2008ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 2018ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 2028ff59090SHerbert Xu 203a596999bSTadeusz Struk skcipher_pull_sgl(sk, ctx->used, 1); 2048ff59090SHerbert Xu } 2058ff59090SHerbert Xu 2068ff59090SHerbert Xu static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags) 2078ff59090SHerbert Xu { 2088ff59090SHerbert Xu long timeout; 2098ff59090SHerbert Xu DEFINE_WAIT(wait); 2108ff59090SHerbert Xu int err = -ERESTARTSYS; 2118ff59090SHerbert Xu 2128ff59090SHerbert Xu if (flags & MSG_DONTWAIT) 2138ff59090SHerbert Xu return -EAGAIN; 2148ff59090SHerbert Xu 2158ff59090SHerbert Xu set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); 2168ff59090SHerbert Xu 2178ff59090SHerbert Xu for (;;) { 2188ff59090SHerbert Xu if (signal_pending(current)) 2198ff59090SHerbert Xu break; 2208ff59090SHerbert Xu prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 2218ff59090SHerbert Xu timeout = MAX_SCHEDULE_TIMEOUT; 2228ff59090SHerbert Xu if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) { 2238ff59090SHerbert Xu err = 0; 2248ff59090SHerbert Xu break; 2258ff59090SHerbert Xu } 2268ff59090SHerbert Xu } 2278ff59090SHerbert Xu finish_wait(sk_sleep(sk), &wait); 2288ff59090SHerbert Xu 2298ff59090SHerbert Xu return err; 2308ff59090SHerbert Xu } 2318ff59090SHerbert Xu 2328ff59090SHerbert Xu static void skcipher_wmem_wakeup(struct sock *sk) 2338ff59090SHerbert Xu { 2348ff59090SHerbert Xu struct socket_wq *wq; 2358ff59090SHerbert Xu 2368ff59090SHerbert Xu if (!skcipher_writable(sk)) 2378ff59090SHerbert Xu return; 2388ff59090SHerbert Xu 2398ff59090SHerbert Xu rcu_read_lock(); 2408ff59090SHerbert Xu wq = rcu_dereference(sk->sk_wq); 2418ff59090SHerbert Xu if (wq_has_sleeper(wq)) 2428ff59090SHerbert Xu wake_up_interruptible_sync_poll(&wq->wait, POLLIN | 2438ff59090SHerbert Xu POLLRDNORM | 2448ff59090SHerbert Xu POLLRDBAND); 2458ff59090SHerbert Xu sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 2468ff59090SHerbert Xu rcu_read_unlock(); 2478ff59090SHerbert Xu } 2488ff59090SHerbert Xu 2498ff59090SHerbert Xu static int skcipher_wait_for_data(struct sock *sk, unsigned flags) 2508ff59090SHerbert Xu { 2518ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 2528ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 2538ff59090SHerbert Xu long timeout; 2548ff59090SHerbert Xu DEFINE_WAIT(wait); 2558ff59090SHerbert Xu int err = -ERESTARTSYS; 2568ff59090SHerbert Xu 2578ff59090SHerbert Xu if (flags & MSG_DONTWAIT) { 2588ff59090SHerbert Xu return -EAGAIN; 2598ff59090SHerbert Xu } 2608ff59090SHerbert Xu 2618ff59090SHerbert Xu set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 2628ff59090SHerbert Xu 2638ff59090SHerbert Xu for (;;) { 2648ff59090SHerbert Xu if (signal_pending(current)) 2658ff59090SHerbert Xu break; 2668ff59090SHerbert Xu prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 2678ff59090SHerbert Xu timeout = MAX_SCHEDULE_TIMEOUT; 2688ff59090SHerbert Xu if (sk_wait_event(sk, &timeout, ctx->used)) { 2698ff59090SHerbert Xu err = 0; 2708ff59090SHerbert Xu break; 2718ff59090SHerbert Xu } 2728ff59090SHerbert Xu } 2738ff59090SHerbert Xu finish_wait(sk_sleep(sk), &wait); 2748ff59090SHerbert Xu 2758ff59090SHerbert Xu clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); 2768ff59090SHerbert Xu 2778ff59090SHerbert Xu return err; 2788ff59090SHerbert Xu } 2798ff59090SHerbert Xu 2808ff59090SHerbert Xu static void skcipher_data_wakeup(struct sock *sk) 2818ff59090SHerbert Xu { 2828ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 2838ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 2848ff59090SHerbert Xu struct socket_wq *wq; 2858ff59090SHerbert Xu 2868ff59090SHerbert Xu if (!ctx->used) 2878ff59090SHerbert Xu return; 2888ff59090SHerbert Xu 2898ff59090SHerbert Xu rcu_read_lock(); 2908ff59090SHerbert Xu wq = rcu_dereference(sk->sk_wq); 2918ff59090SHerbert Xu if (wq_has_sleeper(wq)) 2928ff59090SHerbert Xu wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | 2938ff59090SHerbert Xu POLLRDNORM | 2948ff59090SHerbert Xu POLLRDBAND); 2958ff59090SHerbert Xu sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); 2968ff59090SHerbert Xu rcu_read_unlock(); 2978ff59090SHerbert Xu } 2988ff59090SHerbert Xu 2991b784140SYing Xue static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg, 3001b784140SYing Xue size_t size) 3018ff59090SHerbert Xu { 3028ff59090SHerbert Xu struct sock *sk = sock->sk; 3038ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 3048ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 3058ff59090SHerbert Xu struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req); 3068ff59090SHerbert Xu unsigned ivsize = crypto_ablkcipher_ivsize(tfm); 3078ff59090SHerbert Xu struct skcipher_sg_list *sgl; 3088ff59090SHerbert Xu struct af_alg_control con = {}; 3098ff59090SHerbert Xu long copied = 0; 3108ff59090SHerbert Xu bool enc = 0; 311f26b7b80SStephan Mueller bool init = 0; 3128ff59090SHerbert Xu int err; 3138ff59090SHerbert Xu int i; 3148ff59090SHerbert Xu 3158ff59090SHerbert Xu if (msg->msg_controllen) { 3168ff59090SHerbert Xu err = af_alg_cmsg_send(msg, &con); 3178ff59090SHerbert Xu if (err) 3188ff59090SHerbert Xu return err; 3198ff59090SHerbert Xu 320f26b7b80SStephan Mueller init = 1; 3218ff59090SHerbert Xu switch (con.op) { 3228ff59090SHerbert Xu case ALG_OP_ENCRYPT: 3238ff59090SHerbert Xu enc = 1; 3248ff59090SHerbert Xu break; 3258ff59090SHerbert Xu case ALG_OP_DECRYPT: 3268ff59090SHerbert Xu enc = 0; 3278ff59090SHerbert Xu break; 3288ff59090SHerbert Xu default: 3298ff59090SHerbert Xu return -EINVAL; 3308ff59090SHerbert Xu } 3318ff59090SHerbert Xu 3328ff59090SHerbert Xu if (con.iv && con.iv->ivlen != ivsize) 3338ff59090SHerbert Xu return -EINVAL; 3348ff59090SHerbert Xu } 3358ff59090SHerbert Xu 3368ff59090SHerbert Xu err = -EINVAL; 3378ff59090SHerbert Xu 3388ff59090SHerbert Xu lock_sock(sk); 3398ff59090SHerbert Xu if (!ctx->more && ctx->used) 3408ff59090SHerbert Xu goto unlock; 3418ff59090SHerbert Xu 342f26b7b80SStephan Mueller if (init) { 3438ff59090SHerbert Xu ctx->enc = enc; 3448ff59090SHerbert Xu if (con.iv) 3458ff59090SHerbert Xu memcpy(ctx->iv, con.iv->iv, ivsize); 3468ff59090SHerbert Xu } 3478ff59090SHerbert Xu 3488ff59090SHerbert Xu while (size) { 3498ff59090SHerbert Xu struct scatterlist *sg; 3508ff59090SHerbert Xu unsigned long len = size; 3518ff59090SHerbert Xu int plen; 3528ff59090SHerbert Xu 3538ff59090SHerbert Xu if (ctx->merge) { 3548ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, 3558ff59090SHerbert Xu struct skcipher_sg_list, list); 3568ff59090SHerbert Xu sg = sgl->sg + sgl->cur - 1; 3578ff59090SHerbert Xu len = min_t(unsigned long, len, 3588ff59090SHerbert Xu PAGE_SIZE - sg->offset - sg->length); 3598ff59090SHerbert Xu 3606ce8e9ceSAl Viro err = memcpy_from_msg(page_address(sg_page(sg)) + 3618ff59090SHerbert Xu sg->offset + sg->length, 3626ce8e9ceSAl Viro msg, len); 3638ff59090SHerbert Xu if (err) 3648ff59090SHerbert Xu goto unlock; 3658ff59090SHerbert Xu 3668ff59090SHerbert Xu sg->length += len; 3678ff59090SHerbert Xu ctx->merge = (sg->offset + sg->length) & 3688ff59090SHerbert Xu (PAGE_SIZE - 1); 3698ff59090SHerbert Xu 3708ff59090SHerbert Xu ctx->used += len; 3718ff59090SHerbert Xu copied += len; 3728ff59090SHerbert Xu size -= len; 3738ff59090SHerbert Xu continue; 3748ff59090SHerbert Xu } 3758ff59090SHerbert Xu 3760f6bb83cSHerbert Xu if (!skcipher_writable(sk)) { 3778ff59090SHerbert Xu err = skcipher_wait_for_wmem(sk, msg->msg_flags); 3788ff59090SHerbert Xu if (err) 3798ff59090SHerbert Xu goto unlock; 3808ff59090SHerbert Xu } 3818ff59090SHerbert Xu 3820f6bb83cSHerbert Xu len = min_t(unsigned long, len, skcipher_sndbuf(sk)); 3838ff59090SHerbert Xu 3848ff59090SHerbert Xu err = skcipher_alloc_sgl(sk); 3858ff59090SHerbert Xu if (err) 3868ff59090SHerbert Xu goto unlock; 3878ff59090SHerbert Xu 3888ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); 3898ff59090SHerbert Xu sg = sgl->sg; 3900f477b65STadeusz Struk sg_unmark_end(sg + sgl->cur); 3918ff59090SHerbert Xu do { 3928ff59090SHerbert Xu i = sgl->cur; 3938ff59090SHerbert Xu plen = min_t(int, len, PAGE_SIZE); 3948ff59090SHerbert Xu 3958ff59090SHerbert Xu sg_assign_page(sg + i, alloc_page(GFP_KERNEL)); 3968ff59090SHerbert Xu err = -ENOMEM; 3978ff59090SHerbert Xu if (!sg_page(sg + i)) 3988ff59090SHerbert Xu goto unlock; 3998ff59090SHerbert Xu 4006ce8e9ceSAl Viro err = memcpy_from_msg(page_address(sg_page(sg + i)), 4016ce8e9ceSAl Viro msg, plen); 4028ff59090SHerbert Xu if (err) { 4038ff59090SHerbert Xu __free_page(sg_page(sg + i)); 4048ff59090SHerbert Xu sg_assign_page(sg + i, NULL); 4058ff59090SHerbert Xu goto unlock; 4068ff59090SHerbert Xu } 4078ff59090SHerbert Xu 4088ff59090SHerbert Xu sg[i].length = plen; 4098ff59090SHerbert Xu len -= plen; 4108ff59090SHerbert Xu ctx->used += plen; 4118ff59090SHerbert Xu copied += plen; 4128ff59090SHerbert Xu size -= plen; 4138ff59090SHerbert Xu sgl->cur++; 4148ff59090SHerbert Xu } while (len && sgl->cur < MAX_SGL_ENTS); 4158ff59090SHerbert Xu 4160f477b65STadeusz Struk if (!size) 4170f477b65STadeusz Struk sg_mark_end(sg + sgl->cur - 1); 4180f477b65STadeusz Struk 4198ff59090SHerbert Xu ctx->merge = plen & (PAGE_SIZE - 1); 4208ff59090SHerbert Xu } 4218ff59090SHerbert Xu 4228ff59090SHerbert Xu err = 0; 4238ff59090SHerbert Xu 4248ff59090SHerbert Xu ctx->more = msg->msg_flags & MSG_MORE; 4258ff59090SHerbert Xu 4268ff59090SHerbert Xu unlock: 4278ff59090SHerbert Xu skcipher_data_wakeup(sk); 4288ff59090SHerbert Xu release_sock(sk); 4298ff59090SHerbert Xu 4308ff59090SHerbert Xu return copied ?: err; 4318ff59090SHerbert Xu } 4328ff59090SHerbert Xu 4338ff59090SHerbert Xu static ssize_t skcipher_sendpage(struct socket *sock, struct page *page, 4348ff59090SHerbert Xu int offset, size_t size, int flags) 4358ff59090SHerbert Xu { 4368ff59090SHerbert Xu struct sock *sk = sock->sk; 4378ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 4388ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 4398ff59090SHerbert Xu struct skcipher_sg_list *sgl; 4408ff59090SHerbert Xu int err = -EINVAL; 4418ff59090SHerbert Xu 442d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 443d3f7d56aSShawn Landden flags |= MSG_MORE; 444d3f7d56aSShawn Landden 4458ff59090SHerbert Xu lock_sock(sk); 4468ff59090SHerbert Xu if (!ctx->more && ctx->used) 4478ff59090SHerbert Xu goto unlock; 4488ff59090SHerbert Xu 4498ff59090SHerbert Xu if (!size) 4508ff59090SHerbert Xu goto done; 4518ff59090SHerbert Xu 4520f6bb83cSHerbert Xu if (!skcipher_writable(sk)) { 4538ff59090SHerbert Xu err = skcipher_wait_for_wmem(sk, flags); 4548ff59090SHerbert Xu if (err) 4558ff59090SHerbert Xu goto unlock; 4568ff59090SHerbert Xu } 4578ff59090SHerbert Xu 4588ff59090SHerbert Xu err = skcipher_alloc_sgl(sk); 4598ff59090SHerbert Xu if (err) 4608ff59090SHerbert Xu goto unlock; 4618ff59090SHerbert Xu 4628ff59090SHerbert Xu ctx->merge = 0; 4638ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); 4648ff59090SHerbert Xu 4650f477b65STadeusz Struk if (sgl->cur) 4660f477b65STadeusz Struk sg_unmark_end(sgl->sg + sgl->cur - 1); 4670f477b65STadeusz Struk 4680f477b65STadeusz Struk sg_mark_end(sgl->sg + sgl->cur); 4698ff59090SHerbert Xu get_page(page); 4708ff59090SHerbert Xu sg_set_page(sgl->sg + sgl->cur, page, size, offset); 4718ff59090SHerbert Xu sgl->cur++; 4728ff59090SHerbert Xu ctx->used += size; 4738ff59090SHerbert Xu 4748ff59090SHerbert Xu done: 4758ff59090SHerbert Xu ctx->more = flags & MSG_MORE; 4768ff59090SHerbert Xu 4778ff59090SHerbert Xu unlock: 4788ff59090SHerbert Xu skcipher_data_wakeup(sk); 4798ff59090SHerbert Xu release_sock(sk); 4808ff59090SHerbert Xu 4818ff59090SHerbert Xu return err ?: size; 4828ff59090SHerbert Xu } 4838ff59090SHerbert Xu 484a596999bSTadeusz Struk static int skcipher_all_sg_nents(struct skcipher_ctx *ctx) 485a596999bSTadeusz Struk { 486a596999bSTadeusz Struk struct skcipher_sg_list *sgl; 487a596999bSTadeusz Struk struct scatterlist *sg; 488a596999bSTadeusz Struk int nents = 0; 489a596999bSTadeusz Struk 490a596999bSTadeusz Struk list_for_each_entry(sgl, &ctx->tsgl, list) { 491a596999bSTadeusz Struk sg = sgl->sg; 492a596999bSTadeusz Struk 493a596999bSTadeusz Struk while (!sg->length) 494a596999bSTadeusz Struk sg++; 495a596999bSTadeusz Struk 496a596999bSTadeusz Struk nents += sg_nents(sg); 497a596999bSTadeusz Struk } 498a596999bSTadeusz Struk return nents; 499a596999bSTadeusz Struk } 500a596999bSTadeusz Struk 501a596999bSTadeusz Struk static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg, 502a596999bSTadeusz Struk int flags) 503a596999bSTadeusz Struk { 504a596999bSTadeusz Struk struct sock *sk = sock->sk; 505a596999bSTadeusz Struk struct alg_sock *ask = alg_sk(sk); 506a596999bSTadeusz Struk struct skcipher_ctx *ctx = ask->private; 507a596999bSTadeusz Struk struct skcipher_sg_list *sgl; 508a596999bSTadeusz Struk struct scatterlist *sg; 509a596999bSTadeusz Struk struct skcipher_async_req *sreq; 510a596999bSTadeusz Struk struct ablkcipher_request *req; 511a596999bSTadeusz Struk struct skcipher_async_rsgl *last_rsgl = NULL; 512033f46b3Stadeusz.struk@intel.com unsigned int txbufs = 0, len = 0, tx_nents = skcipher_all_sg_nents(ctx); 513a596999bSTadeusz Struk unsigned int reqlen = sizeof(struct skcipher_async_req) + 514a596999bSTadeusz Struk GET_REQ_SIZE(ctx) + GET_IV_SIZE(ctx); 515a596999bSTadeusz Struk int err = -ENOMEM; 516033f46b3Stadeusz.struk@intel.com bool mark = false; 517a596999bSTadeusz Struk 518a596999bSTadeusz Struk lock_sock(sk); 519a596999bSTadeusz Struk req = kmalloc(reqlen, GFP_KERNEL); 520a596999bSTadeusz Struk if (unlikely(!req)) 521a596999bSTadeusz Struk goto unlock; 522a596999bSTadeusz Struk 523a596999bSTadeusz Struk sreq = GET_SREQ(req, ctx); 524a596999bSTadeusz Struk sreq->iocb = msg->msg_iocb; 525a596999bSTadeusz Struk memset(&sreq->first_sgl, '\0', sizeof(struct skcipher_async_rsgl)); 526a596999bSTadeusz Struk INIT_LIST_HEAD(&sreq->list); 527a596999bSTadeusz Struk sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL); 528a596999bSTadeusz Struk if (unlikely(!sreq->tsg)) { 529a596999bSTadeusz Struk kfree(req); 530a596999bSTadeusz Struk goto unlock; 531a596999bSTadeusz Struk } 532a596999bSTadeusz Struk sg_init_table(sreq->tsg, tx_nents); 533a596999bSTadeusz Struk memcpy(sreq->iv, ctx->iv, GET_IV_SIZE(ctx)); 534a596999bSTadeusz Struk ablkcipher_request_set_tfm(req, crypto_ablkcipher_reqtfm(&ctx->req)); 535a596999bSTadeusz Struk ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 536a596999bSTadeusz Struk skcipher_async_cb, sk); 537a596999bSTadeusz Struk 538a596999bSTadeusz Struk while (iov_iter_count(&msg->msg_iter)) { 539a596999bSTadeusz Struk struct skcipher_async_rsgl *rsgl; 540ac110f49Stadeusz.struk@intel.com int used; 541a596999bSTadeusz Struk 542a596999bSTadeusz Struk if (!ctx->used) { 543a596999bSTadeusz Struk err = skcipher_wait_for_data(sk, flags); 544a596999bSTadeusz Struk if (err) 545a596999bSTadeusz Struk goto free; 546a596999bSTadeusz Struk } 547a596999bSTadeusz Struk sgl = list_first_entry(&ctx->tsgl, 548a596999bSTadeusz Struk struct skcipher_sg_list, list); 549a596999bSTadeusz Struk sg = sgl->sg; 550a596999bSTadeusz Struk 551a596999bSTadeusz Struk while (!sg->length) 552a596999bSTadeusz Struk sg++; 553a596999bSTadeusz Struk 554a596999bSTadeusz Struk used = min_t(unsigned long, ctx->used, 555a596999bSTadeusz Struk iov_iter_count(&msg->msg_iter)); 556a596999bSTadeusz Struk used = min_t(unsigned long, used, sg->length); 557a596999bSTadeusz Struk 558033f46b3Stadeusz.struk@intel.com if (txbufs == tx_nents) { 559a596999bSTadeusz Struk struct scatterlist *tmp; 560a596999bSTadeusz Struk int x; 561a596999bSTadeusz Struk /* Ran out of tx slots in async request 562a596999bSTadeusz Struk * need to expand */ 563a596999bSTadeusz Struk tmp = kcalloc(tx_nents * 2, sizeof(*tmp), 564a596999bSTadeusz Struk GFP_KERNEL); 565a596999bSTadeusz Struk if (!tmp) 566a596999bSTadeusz Struk goto free; 567a596999bSTadeusz Struk 568a596999bSTadeusz Struk sg_init_table(tmp, tx_nents * 2); 569a596999bSTadeusz Struk for (x = 0; x < tx_nents; x++) 570a596999bSTadeusz Struk sg_set_page(&tmp[x], sg_page(&sreq->tsg[x]), 571a596999bSTadeusz Struk sreq->tsg[x].length, 572a596999bSTadeusz Struk sreq->tsg[x].offset); 573a596999bSTadeusz Struk kfree(sreq->tsg); 574a596999bSTadeusz Struk sreq->tsg = tmp; 575a596999bSTadeusz Struk tx_nents *= 2; 576033f46b3Stadeusz.struk@intel.com mark = true; 577a596999bSTadeusz Struk } 578a596999bSTadeusz Struk /* Need to take over the tx sgl from ctx 579a596999bSTadeusz Struk * to the asynch req - these sgls will be freed later */ 580033f46b3Stadeusz.struk@intel.com sg_set_page(sreq->tsg + txbufs++, sg_page(sg), sg->length, 581a596999bSTadeusz Struk sg->offset); 582a596999bSTadeusz Struk 583a596999bSTadeusz Struk if (list_empty(&sreq->list)) { 584a596999bSTadeusz Struk rsgl = &sreq->first_sgl; 585a596999bSTadeusz Struk list_add_tail(&rsgl->list, &sreq->list); 586a596999bSTadeusz Struk } else { 58782d92920STadeusz Struk rsgl = kmalloc(sizeof(*rsgl), GFP_KERNEL); 588a596999bSTadeusz Struk if (!rsgl) { 589a596999bSTadeusz Struk err = -ENOMEM; 590a596999bSTadeusz Struk goto free; 591a596999bSTadeusz Struk } 592a596999bSTadeusz Struk list_add_tail(&rsgl->list, &sreq->list); 593a596999bSTadeusz Struk } 594a596999bSTadeusz Struk 595a596999bSTadeusz Struk used = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, used); 596a596999bSTadeusz Struk err = used; 597a596999bSTadeusz Struk if (used < 0) 598a596999bSTadeusz Struk goto free; 599a596999bSTadeusz Struk if (last_rsgl) 600a596999bSTadeusz Struk af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl); 601a596999bSTadeusz Struk 602a596999bSTadeusz Struk last_rsgl = rsgl; 603a596999bSTadeusz Struk len += used; 604a596999bSTadeusz Struk skcipher_pull_sgl(sk, used, 0); 605a596999bSTadeusz Struk iov_iter_advance(&msg->msg_iter, used); 606a596999bSTadeusz Struk } 607a596999bSTadeusz Struk 608033f46b3Stadeusz.struk@intel.com if (mark) 609033f46b3Stadeusz.struk@intel.com sg_mark_end(sreq->tsg + txbufs - 1); 610033f46b3Stadeusz.struk@intel.com 611a596999bSTadeusz Struk ablkcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg, 612a596999bSTadeusz Struk len, sreq->iv); 613a596999bSTadeusz Struk err = ctx->enc ? crypto_ablkcipher_encrypt(req) : 614a596999bSTadeusz Struk crypto_ablkcipher_decrypt(req); 615a596999bSTadeusz Struk if (err == -EINPROGRESS) { 616a596999bSTadeusz Struk atomic_inc(&ctx->inflight); 617a596999bSTadeusz Struk err = -EIOCBQUEUED; 618a596999bSTadeusz Struk goto unlock; 619a596999bSTadeusz Struk } 620a596999bSTadeusz Struk free: 621a596999bSTadeusz Struk skcipher_free_async_sgls(sreq); 622a596999bSTadeusz Struk kfree(req); 623a596999bSTadeusz Struk unlock: 624a596999bSTadeusz Struk skcipher_wmem_wakeup(sk); 625a596999bSTadeusz Struk release_sock(sk); 626a596999bSTadeusz Struk return err; 627a596999bSTadeusz Struk } 628a596999bSTadeusz Struk 629a596999bSTadeusz Struk static int skcipher_recvmsg_sync(struct socket *sock, struct msghdr *msg, 630a596999bSTadeusz Struk int flags) 6318ff59090SHerbert Xu { 6328ff59090SHerbert Xu struct sock *sk = sock->sk; 6338ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 6348ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 6358ff59090SHerbert Xu unsigned bs = crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm( 6368ff59090SHerbert Xu &ctx->req)); 6378ff59090SHerbert Xu struct skcipher_sg_list *sgl; 6388ff59090SHerbert Xu struct scatterlist *sg; 6398ff59090SHerbert Xu int err = -EAGAIN; 6408ff59090SHerbert Xu int used; 6418ff59090SHerbert Xu long copied = 0; 6428ff59090SHerbert Xu 6438ff59090SHerbert Xu lock_sock(sk); 64401e97e65SAl Viro while (msg_data_left(msg)) { 6458ff59090SHerbert Xu sgl = list_first_entry(&ctx->tsgl, 6468ff59090SHerbert Xu struct skcipher_sg_list, list); 6478ff59090SHerbert Xu sg = sgl->sg; 6488ff59090SHerbert Xu 6498ff59090SHerbert Xu while (!sg->length) 6508ff59090SHerbert Xu sg++; 6518ff59090SHerbert Xu 6529399f0c5SLinus Torvalds if (!ctx->used) { 6538ff59090SHerbert Xu err = skcipher_wait_for_data(sk, flags); 6548ff59090SHerbert Xu if (err) 6558ff59090SHerbert Xu goto unlock; 6568ff59090SHerbert Xu } 6578ff59090SHerbert Xu 65801e97e65SAl Viro used = min_t(unsigned long, ctx->used, msg_data_left(msg)); 6598ff59090SHerbert Xu 6601d10eb2fSAl Viro used = af_alg_make_sg(&ctx->rsgl, &msg->msg_iter, used); 661bc97e57eSHerbert Xu err = used; 662bc97e57eSHerbert Xu if (err < 0) 663bc97e57eSHerbert Xu goto unlock; 664bc97e57eSHerbert Xu 6658ff59090SHerbert Xu if (ctx->more || used < ctx->used) 6668ff59090SHerbert Xu used -= used % bs; 6678ff59090SHerbert Xu 6688ff59090SHerbert Xu err = -EINVAL; 6698ff59090SHerbert Xu if (!used) 670bc97e57eSHerbert Xu goto free; 6718ff59090SHerbert Xu 6728ff59090SHerbert Xu ablkcipher_request_set_crypt(&ctx->req, sg, 6738ff59090SHerbert Xu ctx->rsgl.sg, used, 6748ff59090SHerbert Xu ctx->iv); 6758ff59090SHerbert Xu 6768ff59090SHerbert Xu err = af_alg_wait_for_completion( 6778ff59090SHerbert Xu ctx->enc ? 6788ff59090SHerbert Xu crypto_ablkcipher_encrypt(&ctx->req) : 6798ff59090SHerbert Xu crypto_ablkcipher_decrypt(&ctx->req), 6808ff59090SHerbert Xu &ctx->completion); 6818ff59090SHerbert Xu 682bc97e57eSHerbert Xu free: 6838ff59090SHerbert Xu af_alg_free_sg(&ctx->rsgl); 6848ff59090SHerbert Xu 6858ff59090SHerbert Xu if (err) 6868ff59090SHerbert Xu goto unlock; 6878ff59090SHerbert Xu 6888ff59090SHerbert Xu copied += used; 689a596999bSTadeusz Struk skcipher_pull_sgl(sk, used, 1); 6901d10eb2fSAl Viro iov_iter_advance(&msg->msg_iter, used); 6918ff59090SHerbert Xu } 6928ff59090SHerbert Xu 6938ff59090SHerbert Xu err = 0; 6948ff59090SHerbert Xu 6958ff59090SHerbert Xu unlock: 6968ff59090SHerbert Xu skcipher_wmem_wakeup(sk); 6978ff59090SHerbert Xu release_sock(sk); 6988ff59090SHerbert Xu 6998ff59090SHerbert Xu return copied ?: err; 7008ff59090SHerbert Xu } 7018ff59090SHerbert Xu 702a596999bSTadeusz Struk static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg, 703a596999bSTadeusz Struk size_t ignored, int flags) 704a596999bSTadeusz Struk { 705a596999bSTadeusz Struk return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ? 706a596999bSTadeusz Struk skcipher_recvmsg_async(sock, msg, flags) : 707a596999bSTadeusz Struk skcipher_recvmsg_sync(sock, msg, flags); 708a596999bSTadeusz Struk } 7098ff59090SHerbert Xu 7108ff59090SHerbert Xu static unsigned int skcipher_poll(struct file *file, struct socket *sock, 7118ff59090SHerbert Xu poll_table *wait) 7128ff59090SHerbert Xu { 7138ff59090SHerbert Xu struct sock *sk = sock->sk; 7148ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 7158ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 7168ff59090SHerbert Xu unsigned int mask; 7178ff59090SHerbert Xu 7188ff59090SHerbert Xu sock_poll_wait(file, sk_sleep(sk), wait); 7198ff59090SHerbert Xu mask = 0; 7208ff59090SHerbert Xu 7218ff59090SHerbert Xu if (ctx->used) 7228ff59090SHerbert Xu mask |= POLLIN | POLLRDNORM; 7238ff59090SHerbert Xu 7248ff59090SHerbert Xu if (skcipher_writable(sk)) 7258ff59090SHerbert Xu mask |= POLLOUT | POLLWRNORM | POLLWRBAND; 7268ff59090SHerbert Xu 7278ff59090SHerbert Xu return mask; 7288ff59090SHerbert Xu } 7298ff59090SHerbert Xu 7308ff59090SHerbert Xu static struct proto_ops algif_skcipher_ops = { 7318ff59090SHerbert Xu .family = PF_ALG, 7328ff59090SHerbert Xu 7338ff59090SHerbert Xu .connect = sock_no_connect, 7348ff59090SHerbert Xu .socketpair = sock_no_socketpair, 7358ff59090SHerbert Xu .getname = sock_no_getname, 7368ff59090SHerbert Xu .ioctl = sock_no_ioctl, 7378ff59090SHerbert Xu .listen = sock_no_listen, 7388ff59090SHerbert Xu .shutdown = sock_no_shutdown, 7398ff59090SHerbert Xu .getsockopt = sock_no_getsockopt, 7408ff59090SHerbert Xu .mmap = sock_no_mmap, 7418ff59090SHerbert Xu .bind = sock_no_bind, 7428ff59090SHerbert Xu .accept = sock_no_accept, 7438ff59090SHerbert Xu .setsockopt = sock_no_setsockopt, 7448ff59090SHerbert Xu 7458ff59090SHerbert Xu .release = af_alg_release, 7468ff59090SHerbert Xu .sendmsg = skcipher_sendmsg, 7478ff59090SHerbert Xu .sendpage = skcipher_sendpage, 7488ff59090SHerbert Xu .recvmsg = skcipher_recvmsg, 7498ff59090SHerbert Xu .poll = skcipher_poll, 7508ff59090SHerbert Xu }; 7518ff59090SHerbert Xu 7528ff59090SHerbert Xu static void *skcipher_bind(const char *name, u32 type, u32 mask) 7538ff59090SHerbert Xu { 7548ff59090SHerbert Xu return crypto_alloc_ablkcipher(name, type, mask); 7558ff59090SHerbert Xu } 7568ff59090SHerbert Xu 7578ff59090SHerbert Xu static void skcipher_release(void *private) 7588ff59090SHerbert Xu { 7598ff59090SHerbert Xu crypto_free_ablkcipher(private); 7608ff59090SHerbert Xu } 7618ff59090SHerbert Xu 7628ff59090SHerbert Xu static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen) 7638ff59090SHerbert Xu { 7648ff59090SHerbert Xu return crypto_ablkcipher_setkey(private, key, keylen); 7658ff59090SHerbert Xu } 7668ff59090SHerbert Xu 767a596999bSTadeusz Struk static void skcipher_wait(struct sock *sk) 768a596999bSTadeusz Struk { 769a596999bSTadeusz Struk struct alg_sock *ask = alg_sk(sk); 770a596999bSTadeusz Struk struct skcipher_ctx *ctx = ask->private; 771a596999bSTadeusz Struk int ctr = 0; 772a596999bSTadeusz Struk 773a596999bSTadeusz Struk while (atomic_read(&ctx->inflight) && ctr++ < 100) 774a596999bSTadeusz Struk msleep(100); 775a596999bSTadeusz Struk } 776a596999bSTadeusz Struk 7778ff59090SHerbert Xu static void skcipher_sock_destruct(struct sock *sk) 7788ff59090SHerbert Xu { 7798ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 7808ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 7818ff59090SHerbert Xu struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(&ctx->req); 7828ff59090SHerbert Xu 783a596999bSTadeusz Struk if (atomic_read(&ctx->inflight)) 784a596999bSTadeusz Struk skcipher_wait(sk); 785a596999bSTadeusz Struk 7868ff59090SHerbert Xu skcipher_free_sgl(sk); 78779e88659SDaniel Borkmann sock_kzfree_s(sk, ctx->iv, crypto_ablkcipher_ivsize(tfm)); 7888ff59090SHerbert Xu sock_kfree_s(sk, ctx, ctx->len); 7898ff59090SHerbert Xu af_alg_release_parent(sk); 7908ff59090SHerbert Xu } 7918ff59090SHerbert Xu 7928ff59090SHerbert Xu static int skcipher_accept_parent(void *private, struct sock *sk) 7938ff59090SHerbert Xu { 7948ff59090SHerbert Xu struct skcipher_ctx *ctx; 7958ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 7968ff59090SHerbert Xu unsigned int len = sizeof(*ctx) + crypto_ablkcipher_reqsize(private); 7978ff59090SHerbert Xu 7988ff59090SHerbert Xu ctx = sock_kmalloc(sk, len, GFP_KERNEL); 7998ff59090SHerbert Xu if (!ctx) 8008ff59090SHerbert Xu return -ENOMEM; 8018ff59090SHerbert Xu 8028ff59090SHerbert Xu ctx->iv = sock_kmalloc(sk, crypto_ablkcipher_ivsize(private), 8038ff59090SHerbert Xu GFP_KERNEL); 8048ff59090SHerbert Xu if (!ctx->iv) { 8058ff59090SHerbert Xu sock_kfree_s(sk, ctx, len); 8068ff59090SHerbert Xu return -ENOMEM; 8078ff59090SHerbert Xu } 8088ff59090SHerbert Xu 8098ff59090SHerbert Xu memset(ctx->iv, 0, crypto_ablkcipher_ivsize(private)); 8108ff59090SHerbert Xu 8118ff59090SHerbert Xu INIT_LIST_HEAD(&ctx->tsgl); 8128ff59090SHerbert Xu ctx->len = len; 8138ff59090SHerbert Xu ctx->used = 0; 8148ff59090SHerbert Xu ctx->more = 0; 8158ff59090SHerbert Xu ctx->merge = 0; 8168ff59090SHerbert Xu ctx->enc = 0; 817a596999bSTadeusz Struk atomic_set(&ctx->inflight, 0); 8188ff59090SHerbert Xu af_alg_init_completion(&ctx->completion); 8198ff59090SHerbert Xu 8208ff59090SHerbert Xu ask->private = ctx; 8218ff59090SHerbert Xu 8228ff59090SHerbert Xu ablkcipher_request_set_tfm(&ctx->req, private); 8238ff59090SHerbert Xu ablkcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG, 8248ff59090SHerbert Xu af_alg_complete, &ctx->completion); 8258ff59090SHerbert Xu 8268ff59090SHerbert Xu sk->sk_destruct = skcipher_sock_destruct; 8278ff59090SHerbert Xu 8288ff59090SHerbert Xu return 0; 8298ff59090SHerbert Xu } 8308ff59090SHerbert Xu 8318ff59090SHerbert Xu static const struct af_alg_type algif_type_skcipher = { 8328ff59090SHerbert Xu .bind = skcipher_bind, 8338ff59090SHerbert Xu .release = skcipher_release, 8348ff59090SHerbert Xu .setkey = skcipher_setkey, 8358ff59090SHerbert Xu .accept = skcipher_accept_parent, 8368ff59090SHerbert Xu .ops = &algif_skcipher_ops, 8378ff59090SHerbert Xu .name = "skcipher", 8388ff59090SHerbert Xu .owner = THIS_MODULE 8398ff59090SHerbert Xu }; 8408ff59090SHerbert Xu 8418ff59090SHerbert Xu static int __init algif_skcipher_init(void) 8428ff59090SHerbert Xu { 8438ff59090SHerbert Xu return af_alg_register_type(&algif_type_skcipher); 8448ff59090SHerbert Xu } 8458ff59090SHerbert Xu 8468ff59090SHerbert Xu static void __exit algif_skcipher_exit(void) 8478ff59090SHerbert Xu { 8488ff59090SHerbert Xu int err = af_alg_unregister_type(&algif_type_skcipher); 8498ff59090SHerbert Xu BUG_ON(err); 8508ff59090SHerbert Xu } 8518ff59090SHerbert Xu 8528ff59090SHerbert Xu module_init(algif_skcipher_init); 8538ff59090SHerbert Xu module_exit(algif_skcipher_exit); 8548ff59090SHerbert Xu MODULE_LICENSE("GPL"); 855