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 34dd504589SHerbert Xu struct skcipher_tfm { 35dd504589SHerbert Xu struct crypto_skcipher *skcipher; 36dd504589SHerbert Xu bool has_key; 37dd504589SHerbert Xu }; 38dd504589SHerbert Xu 398ff59090SHerbert Xu struct skcipher_ctx { 408ff59090SHerbert Xu struct list_head tsgl; 418ff59090SHerbert Xu struct af_alg_sgl rsgl; 428ff59090SHerbert Xu 438ff59090SHerbert Xu void *iv; 448ff59090SHerbert Xu 458ff59090SHerbert Xu struct af_alg_completion completion; 468ff59090SHerbert Xu 47a596999bSTadeusz Struk atomic_t inflight; 48652d5b8aSLABBE Corentin size_t used; 498ff59090SHerbert Xu 508ff59090SHerbert Xu unsigned int len; 518ff59090SHerbert Xu bool more; 528ff59090SHerbert Xu bool merge; 538ff59090SHerbert Xu bool enc; 548ff59090SHerbert Xu 550d96e4baSHerbert Xu struct skcipher_request req; 568ff59090SHerbert Xu }; 578ff59090SHerbert Xu 58a596999bSTadeusz Struk struct skcipher_async_rsgl { 59a596999bSTadeusz Struk struct af_alg_sgl sgl; 60a596999bSTadeusz Struk struct list_head list; 61a596999bSTadeusz Struk }; 62a596999bSTadeusz Struk 63a596999bSTadeusz Struk struct skcipher_async_req { 64a596999bSTadeusz Struk struct kiocb *iocb; 65a596999bSTadeusz Struk struct skcipher_async_rsgl first_sgl; 66a596999bSTadeusz Struk struct list_head list; 67a596999bSTadeusz Struk struct scatterlist *tsg; 68a596999bSTadeusz Struk char iv[]; 69a596999bSTadeusz Struk }; 70a596999bSTadeusz Struk 71a596999bSTadeusz Struk #define GET_SREQ(areq, ctx) (struct skcipher_async_req *)((char *)areq + \ 720d96e4baSHerbert Xu crypto_skcipher_reqsize(crypto_skcipher_reqtfm(&ctx->req))) 73a596999bSTadeusz Struk 74a596999bSTadeusz Struk #define GET_REQ_SIZE(ctx) \ 750d96e4baSHerbert Xu crypto_skcipher_reqsize(crypto_skcipher_reqtfm(&ctx->req)) 76a596999bSTadeusz Struk 77a596999bSTadeusz Struk #define GET_IV_SIZE(ctx) \ 780d96e4baSHerbert Xu crypto_skcipher_ivsize(crypto_skcipher_reqtfm(&ctx->req)) 79a596999bSTadeusz Struk 80e2cffb5fSOndrej Kozina #define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \ 818ff59090SHerbert Xu sizeof(struct scatterlist) - 1) 828ff59090SHerbert Xu 83a596999bSTadeusz Struk static void skcipher_free_async_sgls(struct skcipher_async_req *sreq) 84a596999bSTadeusz Struk { 85a596999bSTadeusz Struk struct skcipher_async_rsgl *rsgl, *tmp; 86a596999bSTadeusz Struk struct scatterlist *sgl; 87a596999bSTadeusz Struk struct scatterlist *sg; 88a596999bSTadeusz Struk int i, n; 89a596999bSTadeusz Struk 90a596999bSTadeusz Struk list_for_each_entry_safe(rsgl, tmp, &sreq->list, list) { 91a596999bSTadeusz Struk af_alg_free_sg(&rsgl->sgl); 92a596999bSTadeusz Struk if (rsgl != &sreq->first_sgl) 93a596999bSTadeusz Struk kfree(rsgl); 94a596999bSTadeusz Struk } 95a596999bSTadeusz Struk sgl = sreq->tsg; 96a596999bSTadeusz Struk n = sg_nents(sgl); 97a596999bSTadeusz Struk for_each_sg(sgl, sg, n, i) 98a596999bSTadeusz Struk put_page(sg_page(sg)); 99a596999bSTadeusz Struk 100a596999bSTadeusz Struk kfree(sreq->tsg); 101a596999bSTadeusz Struk } 102a596999bSTadeusz Struk 103a596999bSTadeusz Struk static void skcipher_async_cb(struct crypto_async_request *req, int err) 104a596999bSTadeusz Struk { 105a596999bSTadeusz Struk struct sock *sk = req->data; 106a596999bSTadeusz Struk struct alg_sock *ask = alg_sk(sk); 107a596999bSTadeusz Struk struct skcipher_ctx *ctx = ask->private; 108a596999bSTadeusz Struk struct skcipher_async_req *sreq = GET_SREQ(req, ctx); 109a596999bSTadeusz Struk struct kiocb *iocb = sreq->iocb; 110a596999bSTadeusz Struk 111a596999bSTadeusz Struk atomic_dec(&ctx->inflight); 112a596999bSTadeusz Struk skcipher_free_async_sgls(sreq); 113a596999bSTadeusz Struk kfree(req); 114237dae88SAl Viro iocb->ki_complete(iocb, err, err); 115a596999bSTadeusz Struk } 116a596999bSTadeusz Struk 1170f6bb83cSHerbert Xu static inline int skcipher_sndbuf(struct sock *sk) 1188ff59090SHerbert Xu { 1198ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 1208ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 1218ff59090SHerbert Xu 1220f6bb83cSHerbert Xu return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) - 1230f6bb83cSHerbert Xu ctx->used, 0); 1240f6bb83cSHerbert Xu } 1250f6bb83cSHerbert Xu 1260f6bb83cSHerbert Xu static inline bool skcipher_writable(struct sock *sk) 1270f6bb83cSHerbert Xu { 1280f6bb83cSHerbert Xu return PAGE_SIZE <= skcipher_sndbuf(sk); 1298ff59090SHerbert Xu } 1308ff59090SHerbert Xu 1318ff59090SHerbert Xu static int skcipher_alloc_sgl(struct sock *sk) 1328ff59090SHerbert Xu { 1338ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 1348ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 1358ff59090SHerbert Xu struct skcipher_sg_list *sgl; 1368ff59090SHerbert Xu struct scatterlist *sg = NULL; 1378ff59090SHerbert Xu 1388ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); 1398ff59090SHerbert Xu if (!list_empty(&ctx->tsgl)) 1408ff59090SHerbert Xu sg = sgl->sg; 1418ff59090SHerbert Xu 1428ff59090SHerbert Xu if (!sg || sgl->cur >= MAX_SGL_ENTS) { 1438ff59090SHerbert Xu sgl = sock_kmalloc(sk, sizeof(*sgl) + 1448ff59090SHerbert Xu sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1), 1458ff59090SHerbert Xu GFP_KERNEL); 1468ff59090SHerbert Xu if (!sgl) 1478ff59090SHerbert Xu return -ENOMEM; 1488ff59090SHerbert Xu 1498ff59090SHerbert Xu sg_init_table(sgl->sg, MAX_SGL_ENTS + 1); 1508ff59090SHerbert Xu sgl->cur = 0; 1518ff59090SHerbert Xu 1528ff59090SHerbert Xu if (sg) 153c56f6d12SDan Williams sg_chain(sg, MAX_SGL_ENTS + 1, sgl->sg); 1548ff59090SHerbert Xu 1558ff59090SHerbert Xu list_add_tail(&sgl->list, &ctx->tsgl); 1568ff59090SHerbert Xu } 1578ff59090SHerbert Xu 1588ff59090SHerbert Xu return 0; 1598ff59090SHerbert Xu } 1608ff59090SHerbert Xu 161652d5b8aSLABBE Corentin static void skcipher_pull_sgl(struct sock *sk, size_t used, int put) 1628ff59090SHerbert Xu { 1638ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 1648ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 1658ff59090SHerbert Xu struct skcipher_sg_list *sgl; 1668ff59090SHerbert Xu struct scatterlist *sg; 1678ff59090SHerbert Xu int i; 1688ff59090SHerbert Xu 1698ff59090SHerbert Xu while (!list_empty(&ctx->tsgl)) { 1708ff59090SHerbert Xu sgl = list_first_entry(&ctx->tsgl, struct skcipher_sg_list, 1718ff59090SHerbert Xu list); 1728ff59090SHerbert Xu sg = sgl->sg; 1738ff59090SHerbert Xu 1748ff59090SHerbert Xu for (i = 0; i < sgl->cur; i++) { 175652d5b8aSLABBE Corentin size_t plen = min_t(size_t, used, sg[i].length); 1768ff59090SHerbert Xu 1778ff59090SHerbert Xu if (!sg_page(sg + i)) 1788ff59090SHerbert Xu continue; 1798ff59090SHerbert Xu 1808ff59090SHerbert Xu sg[i].length -= plen; 1818ff59090SHerbert Xu sg[i].offset += plen; 1828ff59090SHerbert Xu 1838ff59090SHerbert Xu used -= plen; 1848ff59090SHerbert Xu ctx->used -= plen; 1858ff59090SHerbert Xu 1868ff59090SHerbert Xu if (sg[i].length) 1878ff59090SHerbert Xu return; 188a596999bSTadeusz Struk if (put) 1898ff59090SHerbert Xu put_page(sg_page(sg + i)); 1908ff59090SHerbert Xu sg_assign_page(sg + i, NULL); 1918ff59090SHerbert Xu } 1928ff59090SHerbert Xu 1938ff59090SHerbert Xu list_del(&sgl->list); 1948ff59090SHerbert Xu sock_kfree_s(sk, sgl, 1958ff59090SHerbert Xu sizeof(*sgl) + sizeof(sgl->sg[0]) * 1968ff59090SHerbert Xu (MAX_SGL_ENTS + 1)); 1978ff59090SHerbert Xu } 1988ff59090SHerbert Xu 1998ff59090SHerbert Xu if (!ctx->used) 2008ff59090SHerbert Xu ctx->merge = 0; 2018ff59090SHerbert Xu } 2028ff59090SHerbert Xu 2038ff59090SHerbert Xu static void skcipher_free_sgl(struct sock *sk) 2048ff59090SHerbert Xu { 2058ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 2068ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 2078ff59090SHerbert Xu 208a596999bSTadeusz Struk skcipher_pull_sgl(sk, ctx->used, 1); 2098ff59090SHerbert Xu } 2108ff59090SHerbert Xu 2118ff59090SHerbert Xu static int skcipher_wait_for_wmem(struct sock *sk, unsigned flags) 2128ff59090SHerbert Xu { 2138ff59090SHerbert Xu long timeout; 2148ff59090SHerbert Xu DEFINE_WAIT(wait); 2158ff59090SHerbert Xu int err = -ERESTARTSYS; 2168ff59090SHerbert Xu 2178ff59090SHerbert Xu if (flags & MSG_DONTWAIT) 2188ff59090SHerbert Xu return -EAGAIN; 2198ff59090SHerbert Xu 2209cd3e072SEric Dumazet sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 2218ff59090SHerbert Xu 2228ff59090SHerbert Xu for (;;) { 2238ff59090SHerbert Xu if (signal_pending(current)) 2248ff59090SHerbert Xu break; 2258ff59090SHerbert Xu prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 2268ff59090SHerbert Xu timeout = MAX_SCHEDULE_TIMEOUT; 2278ff59090SHerbert Xu if (sk_wait_event(sk, &timeout, skcipher_writable(sk))) { 2288ff59090SHerbert Xu err = 0; 2298ff59090SHerbert Xu break; 2308ff59090SHerbert Xu } 2318ff59090SHerbert Xu } 2328ff59090SHerbert Xu finish_wait(sk_sleep(sk), &wait); 2338ff59090SHerbert Xu 2348ff59090SHerbert Xu return err; 2358ff59090SHerbert Xu } 2368ff59090SHerbert Xu 2378ff59090SHerbert Xu static void skcipher_wmem_wakeup(struct sock *sk) 2388ff59090SHerbert Xu { 2398ff59090SHerbert Xu struct socket_wq *wq; 2408ff59090SHerbert Xu 2418ff59090SHerbert Xu if (!skcipher_writable(sk)) 2428ff59090SHerbert Xu return; 2438ff59090SHerbert Xu 2448ff59090SHerbert Xu rcu_read_lock(); 2458ff59090SHerbert Xu wq = rcu_dereference(sk->sk_wq); 2468ff59090SHerbert Xu if (wq_has_sleeper(wq)) 2478ff59090SHerbert Xu wake_up_interruptible_sync_poll(&wq->wait, POLLIN | 2488ff59090SHerbert Xu POLLRDNORM | 2498ff59090SHerbert Xu POLLRDBAND); 2508ff59090SHerbert Xu sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 2518ff59090SHerbert Xu rcu_read_unlock(); 2528ff59090SHerbert Xu } 2538ff59090SHerbert Xu 2548ff59090SHerbert Xu static int skcipher_wait_for_data(struct sock *sk, unsigned flags) 2558ff59090SHerbert Xu { 2568ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 2578ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 2588ff59090SHerbert Xu long timeout; 2598ff59090SHerbert Xu DEFINE_WAIT(wait); 2608ff59090SHerbert Xu int err = -ERESTARTSYS; 2618ff59090SHerbert Xu 2628ff59090SHerbert Xu if (flags & MSG_DONTWAIT) { 2638ff59090SHerbert Xu return -EAGAIN; 2648ff59090SHerbert Xu } 2658ff59090SHerbert Xu 2669cd3e072SEric Dumazet sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 2678ff59090SHerbert Xu 2688ff59090SHerbert Xu for (;;) { 2698ff59090SHerbert Xu if (signal_pending(current)) 2708ff59090SHerbert Xu break; 2718ff59090SHerbert Xu prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 2728ff59090SHerbert Xu timeout = MAX_SCHEDULE_TIMEOUT; 2738ff59090SHerbert Xu if (sk_wait_event(sk, &timeout, ctx->used)) { 2748ff59090SHerbert Xu err = 0; 2758ff59090SHerbert Xu break; 2768ff59090SHerbert Xu } 2778ff59090SHerbert Xu } 2788ff59090SHerbert Xu finish_wait(sk_sleep(sk), &wait); 2798ff59090SHerbert Xu 2809cd3e072SEric Dumazet sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 2818ff59090SHerbert Xu 2828ff59090SHerbert Xu return err; 2838ff59090SHerbert Xu } 2848ff59090SHerbert Xu 2858ff59090SHerbert Xu static void skcipher_data_wakeup(struct sock *sk) 2868ff59090SHerbert Xu { 2878ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 2888ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 2898ff59090SHerbert Xu struct socket_wq *wq; 2908ff59090SHerbert Xu 2918ff59090SHerbert Xu if (!ctx->used) 2928ff59090SHerbert Xu return; 2938ff59090SHerbert Xu 2948ff59090SHerbert Xu rcu_read_lock(); 2958ff59090SHerbert Xu wq = rcu_dereference(sk->sk_wq); 2968ff59090SHerbert Xu if (wq_has_sleeper(wq)) 2978ff59090SHerbert Xu wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | 2988ff59090SHerbert Xu POLLRDNORM | 2998ff59090SHerbert Xu POLLRDBAND); 3008ff59090SHerbert Xu sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); 3018ff59090SHerbert Xu rcu_read_unlock(); 3028ff59090SHerbert Xu } 3038ff59090SHerbert Xu 3041b784140SYing Xue static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg, 3051b784140SYing Xue size_t size) 3068ff59090SHerbert Xu { 3078ff59090SHerbert Xu struct sock *sk = sock->sk; 3088ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 3098ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 3100d96e4baSHerbert Xu struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(&ctx->req); 3110d96e4baSHerbert Xu unsigned ivsize = crypto_skcipher_ivsize(tfm); 3128ff59090SHerbert Xu struct skcipher_sg_list *sgl; 3138ff59090SHerbert Xu struct af_alg_control con = {}; 3148ff59090SHerbert Xu long copied = 0; 3158ff59090SHerbert Xu bool enc = 0; 316f26b7b80SStephan Mueller bool init = 0; 3178ff59090SHerbert Xu int err; 3188ff59090SHerbert Xu int i; 3198ff59090SHerbert Xu 3208ff59090SHerbert Xu if (msg->msg_controllen) { 3218ff59090SHerbert Xu err = af_alg_cmsg_send(msg, &con); 3228ff59090SHerbert Xu if (err) 3238ff59090SHerbert Xu return err; 3248ff59090SHerbert Xu 325f26b7b80SStephan Mueller init = 1; 3268ff59090SHerbert Xu switch (con.op) { 3278ff59090SHerbert Xu case ALG_OP_ENCRYPT: 3288ff59090SHerbert Xu enc = 1; 3298ff59090SHerbert Xu break; 3308ff59090SHerbert Xu case ALG_OP_DECRYPT: 3318ff59090SHerbert Xu enc = 0; 3328ff59090SHerbert Xu break; 3338ff59090SHerbert Xu default: 3348ff59090SHerbert Xu return -EINVAL; 3358ff59090SHerbert Xu } 3368ff59090SHerbert Xu 3378ff59090SHerbert Xu if (con.iv && con.iv->ivlen != ivsize) 3388ff59090SHerbert Xu return -EINVAL; 3398ff59090SHerbert Xu } 3408ff59090SHerbert Xu 3418ff59090SHerbert Xu err = -EINVAL; 3428ff59090SHerbert Xu 3438ff59090SHerbert Xu lock_sock(sk); 3448ff59090SHerbert Xu if (!ctx->more && ctx->used) 3458ff59090SHerbert Xu goto unlock; 3468ff59090SHerbert Xu 347f26b7b80SStephan Mueller if (init) { 3488ff59090SHerbert Xu ctx->enc = enc; 3498ff59090SHerbert Xu if (con.iv) 3508ff59090SHerbert Xu memcpy(ctx->iv, con.iv->iv, ivsize); 3518ff59090SHerbert Xu } 3528ff59090SHerbert Xu 3538ff59090SHerbert Xu while (size) { 3548ff59090SHerbert Xu struct scatterlist *sg; 3558ff59090SHerbert Xu unsigned long len = size; 356652d5b8aSLABBE Corentin size_t plen; 3578ff59090SHerbert Xu 3588ff59090SHerbert Xu if (ctx->merge) { 3598ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, 3608ff59090SHerbert Xu struct skcipher_sg_list, list); 3618ff59090SHerbert Xu sg = sgl->sg + sgl->cur - 1; 3628ff59090SHerbert Xu len = min_t(unsigned long, len, 3638ff59090SHerbert Xu PAGE_SIZE - sg->offset - sg->length); 3648ff59090SHerbert Xu 3656ce8e9ceSAl Viro err = memcpy_from_msg(page_address(sg_page(sg)) + 3668ff59090SHerbert Xu sg->offset + sg->length, 3676ce8e9ceSAl Viro msg, len); 3688ff59090SHerbert Xu if (err) 3698ff59090SHerbert Xu goto unlock; 3708ff59090SHerbert Xu 3718ff59090SHerbert Xu sg->length += len; 3728ff59090SHerbert Xu ctx->merge = (sg->offset + sg->length) & 3738ff59090SHerbert Xu (PAGE_SIZE - 1); 3748ff59090SHerbert Xu 3758ff59090SHerbert Xu ctx->used += len; 3768ff59090SHerbert Xu copied += len; 3778ff59090SHerbert Xu size -= len; 3788ff59090SHerbert Xu continue; 3798ff59090SHerbert Xu } 3808ff59090SHerbert Xu 3810f6bb83cSHerbert Xu if (!skcipher_writable(sk)) { 3828ff59090SHerbert Xu err = skcipher_wait_for_wmem(sk, msg->msg_flags); 3838ff59090SHerbert Xu if (err) 3848ff59090SHerbert Xu goto unlock; 3858ff59090SHerbert Xu } 3868ff59090SHerbert Xu 3870f6bb83cSHerbert Xu len = min_t(unsigned long, len, skcipher_sndbuf(sk)); 3888ff59090SHerbert Xu 3898ff59090SHerbert Xu err = skcipher_alloc_sgl(sk); 3908ff59090SHerbert Xu if (err) 3918ff59090SHerbert Xu goto unlock; 3928ff59090SHerbert Xu 3938ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); 3948ff59090SHerbert Xu sg = sgl->sg; 3950f477b65STadeusz Struk sg_unmark_end(sg + sgl->cur); 3968ff59090SHerbert Xu do { 3978ff59090SHerbert Xu i = sgl->cur; 398652d5b8aSLABBE Corentin plen = min_t(size_t, len, PAGE_SIZE); 3998ff59090SHerbert Xu 4008ff59090SHerbert Xu sg_assign_page(sg + i, alloc_page(GFP_KERNEL)); 4018ff59090SHerbert Xu err = -ENOMEM; 4028ff59090SHerbert Xu if (!sg_page(sg + i)) 4038ff59090SHerbert Xu goto unlock; 4048ff59090SHerbert Xu 4056ce8e9ceSAl Viro err = memcpy_from_msg(page_address(sg_page(sg + i)), 4066ce8e9ceSAl Viro msg, plen); 4078ff59090SHerbert Xu if (err) { 4088ff59090SHerbert Xu __free_page(sg_page(sg + i)); 4098ff59090SHerbert Xu sg_assign_page(sg + i, NULL); 4108ff59090SHerbert Xu goto unlock; 4118ff59090SHerbert Xu } 4128ff59090SHerbert Xu 4138ff59090SHerbert Xu sg[i].length = plen; 4148ff59090SHerbert Xu len -= plen; 4158ff59090SHerbert Xu ctx->used += plen; 4168ff59090SHerbert Xu copied += plen; 4178ff59090SHerbert Xu size -= plen; 4188ff59090SHerbert Xu sgl->cur++; 4198ff59090SHerbert Xu } while (len && sgl->cur < MAX_SGL_ENTS); 4208ff59090SHerbert Xu 4210f477b65STadeusz Struk if (!size) 4220f477b65STadeusz Struk sg_mark_end(sg + sgl->cur - 1); 4230f477b65STadeusz Struk 4248ff59090SHerbert Xu ctx->merge = plen & (PAGE_SIZE - 1); 4258ff59090SHerbert Xu } 4268ff59090SHerbert Xu 4278ff59090SHerbert Xu err = 0; 4288ff59090SHerbert Xu 4298ff59090SHerbert Xu ctx->more = msg->msg_flags & MSG_MORE; 4308ff59090SHerbert Xu 4318ff59090SHerbert Xu unlock: 4328ff59090SHerbert Xu skcipher_data_wakeup(sk); 4338ff59090SHerbert Xu release_sock(sk); 4348ff59090SHerbert Xu 4358ff59090SHerbert Xu return copied ?: err; 4368ff59090SHerbert Xu } 4378ff59090SHerbert Xu 4388ff59090SHerbert Xu static ssize_t skcipher_sendpage(struct socket *sock, struct page *page, 4398ff59090SHerbert Xu int offset, size_t size, int flags) 4408ff59090SHerbert Xu { 4418ff59090SHerbert Xu struct sock *sk = sock->sk; 4428ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 4438ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 4448ff59090SHerbert Xu struct skcipher_sg_list *sgl; 4458ff59090SHerbert Xu int err = -EINVAL; 4468ff59090SHerbert Xu 447d3f7d56aSShawn Landden if (flags & MSG_SENDPAGE_NOTLAST) 448d3f7d56aSShawn Landden flags |= MSG_MORE; 449d3f7d56aSShawn Landden 4508ff59090SHerbert Xu lock_sock(sk); 4518ff59090SHerbert Xu if (!ctx->more && ctx->used) 4528ff59090SHerbert Xu goto unlock; 4538ff59090SHerbert Xu 4548ff59090SHerbert Xu if (!size) 4558ff59090SHerbert Xu goto done; 4568ff59090SHerbert Xu 4570f6bb83cSHerbert Xu if (!skcipher_writable(sk)) { 4588ff59090SHerbert Xu err = skcipher_wait_for_wmem(sk, flags); 4598ff59090SHerbert Xu if (err) 4608ff59090SHerbert Xu goto unlock; 4618ff59090SHerbert Xu } 4628ff59090SHerbert Xu 4638ff59090SHerbert Xu err = skcipher_alloc_sgl(sk); 4648ff59090SHerbert Xu if (err) 4658ff59090SHerbert Xu goto unlock; 4668ff59090SHerbert Xu 4678ff59090SHerbert Xu ctx->merge = 0; 4688ff59090SHerbert Xu sgl = list_entry(ctx->tsgl.prev, struct skcipher_sg_list, list); 4698ff59090SHerbert Xu 4700f477b65STadeusz Struk if (sgl->cur) 4710f477b65STadeusz Struk sg_unmark_end(sgl->sg + sgl->cur - 1); 4720f477b65STadeusz Struk 4730f477b65STadeusz Struk sg_mark_end(sgl->sg + sgl->cur); 4748ff59090SHerbert Xu get_page(page); 4758ff59090SHerbert Xu sg_set_page(sgl->sg + sgl->cur, page, size, offset); 4768ff59090SHerbert Xu sgl->cur++; 4778ff59090SHerbert Xu ctx->used += size; 4788ff59090SHerbert Xu 4798ff59090SHerbert Xu done: 4808ff59090SHerbert Xu ctx->more = flags & MSG_MORE; 4818ff59090SHerbert Xu 4828ff59090SHerbert Xu unlock: 4838ff59090SHerbert Xu skcipher_data_wakeup(sk); 4848ff59090SHerbert Xu release_sock(sk); 4858ff59090SHerbert Xu 4868ff59090SHerbert Xu return err ?: size; 4878ff59090SHerbert Xu } 4888ff59090SHerbert Xu 489a596999bSTadeusz Struk static int skcipher_all_sg_nents(struct skcipher_ctx *ctx) 490a596999bSTadeusz Struk { 491a596999bSTadeusz Struk struct skcipher_sg_list *sgl; 492a596999bSTadeusz Struk struct scatterlist *sg; 493a596999bSTadeusz Struk int nents = 0; 494a596999bSTadeusz Struk 495a596999bSTadeusz Struk list_for_each_entry(sgl, &ctx->tsgl, list) { 496a596999bSTadeusz Struk sg = sgl->sg; 497a596999bSTadeusz Struk 498a596999bSTadeusz Struk while (!sg->length) 499a596999bSTadeusz Struk sg++; 500a596999bSTadeusz Struk 501a596999bSTadeusz Struk nents += sg_nents(sg); 502a596999bSTadeusz Struk } 503a596999bSTadeusz Struk return nents; 504a596999bSTadeusz Struk } 505a596999bSTadeusz Struk 506a596999bSTadeusz Struk static int skcipher_recvmsg_async(struct socket *sock, struct msghdr *msg, 507a596999bSTadeusz Struk int flags) 508a596999bSTadeusz Struk { 509a596999bSTadeusz Struk struct sock *sk = sock->sk; 510a596999bSTadeusz Struk struct alg_sock *ask = alg_sk(sk); 511a596999bSTadeusz Struk struct skcipher_ctx *ctx = ask->private; 512a596999bSTadeusz Struk struct skcipher_sg_list *sgl; 513a596999bSTadeusz Struk struct scatterlist *sg; 514a596999bSTadeusz Struk struct skcipher_async_req *sreq; 5150d96e4baSHerbert Xu struct skcipher_request *req; 516a596999bSTadeusz Struk struct skcipher_async_rsgl *last_rsgl = NULL; 517033f46b3Stadeusz.struk@intel.com unsigned int txbufs = 0, len = 0, tx_nents = skcipher_all_sg_nents(ctx); 518a596999bSTadeusz Struk unsigned int reqlen = sizeof(struct skcipher_async_req) + 519a596999bSTadeusz Struk GET_REQ_SIZE(ctx) + GET_IV_SIZE(ctx); 520a596999bSTadeusz Struk int err = -ENOMEM; 521033f46b3Stadeusz.struk@intel.com bool mark = false; 522a596999bSTadeusz Struk 523a596999bSTadeusz Struk lock_sock(sk); 524a596999bSTadeusz Struk req = kmalloc(reqlen, GFP_KERNEL); 525a596999bSTadeusz Struk if (unlikely(!req)) 526a596999bSTadeusz Struk goto unlock; 527a596999bSTadeusz Struk 528a596999bSTadeusz Struk sreq = GET_SREQ(req, ctx); 529a596999bSTadeusz Struk sreq->iocb = msg->msg_iocb; 530a596999bSTadeusz Struk memset(&sreq->first_sgl, '\0', sizeof(struct skcipher_async_rsgl)); 531a596999bSTadeusz Struk INIT_LIST_HEAD(&sreq->list); 532a596999bSTadeusz Struk sreq->tsg = kcalloc(tx_nents, sizeof(*sg), GFP_KERNEL); 533a596999bSTadeusz Struk if (unlikely(!sreq->tsg)) { 534a596999bSTadeusz Struk kfree(req); 535a596999bSTadeusz Struk goto unlock; 536a596999bSTadeusz Struk } 537a596999bSTadeusz Struk sg_init_table(sreq->tsg, tx_nents); 538a596999bSTadeusz Struk memcpy(sreq->iv, ctx->iv, GET_IV_SIZE(ctx)); 5390d96e4baSHerbert Xu skcipher_request_set_tfm(req, crypto_skcipher_reqtfm(&ctx->req)); 5400d96e4baSHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 541a596999bSTadeusz Struk skcipher_async_cb, sk); 542a596999bSTadeusz Struk 543a596999bSTadeusz Struk while (iov_iter_count(&msg->msg_iter)) { 544a596999bSTadeusz Struk struct skcipher_async_rsgl *rsgl; 545ac110f49Stadeusz.struk@intel.com int used; 546a596999bSTadeusz Struk 547a596999bSTadeusz Struk if (!ctx->used) { 548a596999bSTadeusz Struk err = skcipher_wait_for_data(sk, flags); 549a596999bSTadeusz Struk if (err) 550a596999bSTadeusz Struk goto free; 551a596999bSTadeusz Struk } 552a596999bSTadeusz Struk sgl = list_first_entry(&ctx->tsgl, 553a596999bSTadeusz Struk struct skcipher_sg_list, list); 554a596999bSTadeusz Struk sg = sgl->sg; 555a596999bSTadeusz Struk 556a596999bSTadeusz Struk while (!sg->length) 557a596999bSTadeusz Struk sg++; 558a596999bSTadeusz Struk 559a596999bSTadeusz Struk used = min_t(unsigned long, ctx->used, 560a596999bSTadeusz Struk iov_iter_count(&msg->msg_iter)); 561a596999bSTadeusz Struk used = min_t(unsigned long, used, sg->length); 562a596999bSTadeusz Struk 563033f46b3Stadeusz.struk@intel.com if (txbufs == tx_nents) { 564a596999bSTadeusz Struk struct scatterlist *tmp; 565a596999bSTadeusz Struk int x; 566a596999bSTadeusz Struk /* Ran out of tx slots in async request 567a596999bSTadeusz Struk * need to expand */ 568a596999bSTadeusz Struk tmp = kcalloc(tx_nents * 2, sizeof(*tmp), 569a596999bSTadeusz Struk GFP_KERNEL); 570a596999bSTadeusz Struk if (!tmp) 571a596999bSTadeusz Struk goto free; 572a596999bSTadeusz Struk 573a596999bSTadeusz Struk sg_init_table(tmp, tx_nents * 2); 574a596999bSTadeusz Struk for (x = 0; x < tx_nents; x++) 575a596999bSTadeusz Struk sg_set_page(&tmp[x], sg_page(&sreq->tsg[x]), 576a596999bSTadeusz Struk sreq->tsg[x].length, 577a596999bSTadeusz Struk sreq->tsg[x].offset); 578a596999bSTadeusz Struk kfree(sreq->tsg); 579a596999bSTadeusz Struk sreq->tsg = tmp; 580a596999bSTadeusz Struk tx_nents *= 2; 581033f46b3Stadeusz.struk@intel.com mark = true; 582a596999bSTadeusz Struk } 583a596999bSTadeusz Struk /* Need to take over the tx sgl from ctx 584a596999bSTadeusz Struk * to the asynch req - these sgls will be freed later */ 585033f46b3Stadeusz.struk@intel.com sg_set_page(sreq->tsg + txbufs++, sg_page(sg), sg->length, 586a596999bSTadeusz Struk sg->offset); 587a596999bSTadeusz Struk 588a596999bSTadeusz Struk if (list_empty(&sreq->list)) { 589a596999bSTadeusz Struk rsgl = &sreq->first_sgl; 590a596999bSTadeusz Struk list_add_tail(&rsgl->list, &sreq->list); 591a596999bSTadeusz Struk } else { 59282d92920STadeusz Struk rsgl = kmalloc(sizeof(*rsgl), GFP_KERNEL); 593a596999bSTadeusz Struk if (!rsgl) { 594a596999bSTadeusz Struk err = -ENOMEM; 595a596999bSTadeusz Struk goto free; 596a596999bSTadeusz Struk } 597a596999bSTadeusz Struk list_add_tail(&rsgl->list, &sreq->list); 598a596999bSTadeusz Struk } 599a596999bSTadeusz Struk 600a596999bSTadeusz Struk used = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, used); 601a596999bSTadeusz Struk err = used; 602a596999bSTadeusz Struk if (used < 0) 603a596999bSTadeusz Struk goto free; 604a596999bSTadeusz Struk if (last_rsgl) 605a596999bSTadeusz Struk af_alg_link_sg(&last_rsgl->sgl, &rsgl->sgl); 606a596999bSTadeusz Struk 607a596999bSTadeusz Struk last_rsgl = rsgl; 608a596999bSTadeusz Struk len += used; 609a596999bSTadeusz Struk skcipher_pull_sgl(sk, used, 0); 610a596999bSTadeusz Struk iov_iter_advance(&msg->msg_iter, used); 611a596999bSTadeusz Struk } 612a596999bSTadeusz Struk 613033f46b3Stadeusz.struk@intel.com if (mark) 614033f46b3Stadeusz.struk@intel.com sg_mark_end(sreq->tsg + txbufs - 1); 615033f46b3Stadeusz.struk@intel.com 6160d96e4baSHerbert Xu skcipher_request_set_crypt(req, sreq->tsg, sreq->first_sgl.sgl.sg, 617a596999bSTadeusz Struk len, sreq->iv); 6180d96e4baSHerbert Xu err = ctx->enc ? crypto_skcipher_encrypt(req) : 6190d96e4baSHerbert Xu crypto_skcipher_decrypt(req); 620a596999bSTadeusz Struk if (err == -EINPROGRESS) { 621a596999bSTadeusz Struk atomic_inc(&ctx->inflight); 622a596999bSTadeusz Struk err = -EIOCBQUEUED; 623a596999bSTadeusz Struk goto unlock; 624a596999bSTadeusz Struk } 625a596999bSTadeusz Struk free: 626a596999bSTadeusz Struk skcipher_free_async_sgls(sreq); 627a596999bSTadeusz Struk kfree(req); 628a596999bSTadeusz Struk unlock: 629a596999bSTadeusz Struk skcipher_wmem_wakeup(sk); 630a596999bSTadeusz Struk release_sock(sk); 631a596999bSTadeusz Struk return err; 632a596999bSTadeusz Struk } 633a596999bSTadeusz Struk 634a596999bSTadeusz Struk static int skcipher_recvmsg_sync(struct socket *sock, struct msghdr *msg, 635a596999bSTadeusz Struk int flags) 6368ff59090SHerbert Xu { 6378ff59090SHerbert Xu struct sock *sk = sock->sk; 6388ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 6398ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 6400d96e4baSHerbert Xu unsigned bs = crypto_skcipher_blocksize(crypto_skcipher_reqtfm( 6418ff59090SHerbert Xu &ctx->req)); 6428ff59090SHerbert Xu struct skcipher_sg_list *sgl; 6438ff59090SHerbert Xu struct scatterlist *sg; 6448ff59090SHerbert Xu int err = -EAGAIN; 6458ff59090SHerbert Xu int used; 6468ff59090SHerbert Xu long copied = 0; 6478ff59090SHerbert Xu 6488ff59090SHerbert Xu lock_sock(sk); 64901e97e65SAl Viro while (msg_data_left(msg)) { 6508ff59090SHerbert Xu sgl = list_first_entry(&ctx->tsgl, 6518ff59090SHerbert Xu struct skcipher_sg_list, list); 6528ff59090SHerbert Xu sg = sgl->sg; 6538ff59090SHerbert Xu 6548ff59090SHerbert Xu while (!sg->length) 6558ff59090SHerbert Xu sg++; 6568ff59090SHerbert Xu 6579399f0c5SLinus Torvalds if (!ctx->used) { 6588ff59090SHerbert Xu err = skcipher_wait_for_data(sk, flags); 6598ff59090SHerbert Xu if (err) 6608ff59090SHerbert Xu goto unlock; 6618ff59090SHerbert Xu } 6628ff59090SHerbert Xu 66301e97e65SAl Viro used = min_t(unsigned long, ctx->used, msg_data_left(msg)); 6648ff59090SHerbert Xu 6651d10eb2fSAl Viro used = af_alg_make_sg(&ctx->rsgl, &msg->msg_iter, used); 666bc97e57eSHerbert Xu err = used; 667bc97e57eSHerbert Xu if (err < 0) 668bc97e57eSHerbert Xu goto unlock; 669bc97e57eSHerbert Xu 6708ff59090SHerbert Xu if (ctx->more || used < ctx->used) 6718ff59090SHerbert Xu used -= used % bs; 6728ff59090SHerbert Xu 6738ff59090SHerbert Xu err = -EINVAL; 6748ff59090SHerbert Xu if (!used) 675bc97e57eSHerbert Xu goto free; 6768ff59090SHerbert Xu 6770d96e4baSHerbert Xu skcipher_request_set_crypt(&ctx->req, sg, ctx->rsgl.sg, used, 6788ff59090SHerbert Xu ctx->iv); 6798ff59090SHerbert Xu 6808ff59090SHerbert Xu err = af_alg_wait_for_completion( 6818ff59090SHerbert Xu ctx->enc ? 6820d96e4baSHerbert Xu crypto_skcipher_encrypt(&ctx->req) : 6830d96e4baSHerbert Xu crypto_skcipher_decrypt(&ctx->req), 6848ff59090SHerbert Xu &ctx->completion); 6858ff59090SHerbert Xu 686bc97e57eSHerbert Xu free: 6878ff59090SHerbert Xu af_alg_free_sg(&ctx->rsgl); 6888ff59090SHerbert Xu 6898ff59090SHerbert Xu if (err) 6908ff59090SHerbert Xu goto unlock; 6918ff59090SHerbert Xu 6928ff59090SHerbert Xu copied += used; 693a596999bSTadeusz Struk skcipher_pull_sgl(sk, used, 1); 6941d10eb2fSAl Viro iov_iter_advance(&msg->msg_iter, used); 6958ff59090SHerbert Xu } 6968ff59090SHerbert Xu 6978ff59090SHerbert Xu err = 0; 6988ff59090SHerbert Xu 6998ff59090SHerbert Xu unlock: 7008ff59090SHerbert Xu skcipher_wmem_wakeup(sk); 7018ff59090SHerbert Xu release_sock(sk); 7028ff59090SHerbert Xu 7038ff59090SHerbert Xu return copied ?: err; 7048ff59090SHerbert Xu } 7058ff59090SHerbert Xu 706a596999bSTadeusz Struk static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg, 707a596999bSTadeusz Struk size_t ignored, int flags) 708a596999bSTadeusz Struk { 709a596999bSTadeusz Struk return (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) ? 710a596999bSTadeusz Struk skcipher_recvmsg_async(sock, msg, flags) : 711a596999bSTadeusz Struk skcipher_recvmsg_sync(sock, msg, flags); 712a596999bSTadeusz Struk } 7138ff59090SHerbert Xu 7148ff59090SHerbert Xu static unsigned int skcipher_poll(struct file *file, struct socket *sock, 7158ff59090SHerbert Xu poll_table *wait) 7168ff59090SHerbert Xu { 7178ff59090SHerbert Xu struct sock *sk = sock->sk; 7188ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 7198ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 7208ff59090SHerbert Xu unsigned int mask; 7218ff59090SHerbert Xu 7228ff59090SHerbert Xu sock_poll_wait(file, sk_sleep(sk), wait); 7238ff59090SHerbert Xu mask = 0; 7248ff59090SHerbert Xu 7258ff59090SHerbert Xu if (ctx->used) 7268ff59090SHerbert Xu mask |= POLLIN | POLLRDNORM; 7278ff59090SHerbert Xu 7288ff59090SHerbert Xu if (skcipher_writable(sk)) 7298ff59090SHerbert Xu mask |= POLLOUT | POLLWRNORM | POLLWRBAND; 7308ff59090SHerbert Xu 7318ff59090SHerbert Xu return mask; 7328ff59090SHerbert Xu } 7338ff59090SHerbert Xu 7348ff59090SHerbert Xu static struct proto_ops algif_skcipher_ops = { 7358ff59090SHerbert Xu .family = PF_ALG, 7368ff59090SHerbert Xu 7378ff59090SHerbert Xu .connect = sock_no_connect, 7388ff59090SHerbert Xu .socketpair = sock_no_socketpair, 7398ff59090SHerbert Xu .getname = sock_no_getname, 7408ff59090SHerbert Xu .ioctl = sock_no_ioctl, 7418ff59090SHerbert Xu .listen = sock_no_listen, 7428ff59090SHerbert Xu .shutdown = sock_no_shutdown, 7438ff59090SHerbert Xu .getsockopt = sock_no_getsockopt, 7448ff59090SHerbert Xu .mmap = sock_no_mmap, 7458ff59090SHerbert Xu .bind = sock_no_bind, 7468ff59090SHerbert Xu .accept = sock_no_accept, 7478ff59090SHerbert Xu .setsockopt = sock_no_setsockopt, 7488ff59090SHerbert Xu 7498ff59090SHerbert Xu .release = af_alg_release, 7508ff59090SHerbert Xu .sendmsg = skcipher_sendmsg, 7518ff59090SHerbert Xu .sendpage = skcipher_sendpage, 7528ff59090SHerbert Xu .recvmsg = skcipher_recvmsg, 7538ff59090SHerbert Xu .poll = skcipher_poll, 7548ff59090SHerbert Xu }; 7558ff59090SHerbert Xu 756a0fa2d03SHerbert Xu static int skcipher_check_key(struct socket *sock) 757a0fa2d03SHerbert Xu { 758a0fa2d03SHerbert Xu int err; 759a0fa2d03SHerbert Xu struct sock *psk; 760a0fa2d03SHerbert Xu struct alg_sock *pask; 761a0fa2d03SHerbert Xu struct skcipher_tfm *tfm; 762a0fa2d03SHerbert Xu struct sock *sk = sock->sk; 763a0fa2d03SHerbert Xu struct alg_sock *ask = alg_sk(sk); 764a0fa2d03SHerbert Xu 765a0fa2d03SHerbert Xu if (ask->refcnt) 766a0fa2d03SHerbert Xu return 0; 767a0fa2d03SHerbert Xu 768a0fa2d03SHerbert Xu psk = ask->parent; 769a0fa2d03SHerbert Xu pask = alg_sk(ask->parent); 770a0fa2d03SHerbert Xu tfm = pask->private; 771a0fa2d03SHerbert Xu 772a0fa2d03SHerbert Xu err = -ENOKEY; 773a0fa2d03SHerbert Xu lock_sock(psk); 774a0fa2d03SHerbert Xu if (!tfm->has_key) 775a0fa2d03SHerbert Xu goto unlock; 776a0fa2d03SHerbert Xu 777a0fa2d03SHerbert Xu if (!pask->refcnt++) 778a0fa2d03SHerbert Xu sock_hold(psk); 779a0fa2d03SHerbert Xu 780a0fa2d03SHerbert Xu ask->refcnt = 1; 781a0fa2d03SHerbert Xu sock_put(psk); 782a0fa2d03SHerbert Xu 783a0fa2d03SHerbert Xu err = 0; 784a0fa2d03SHerbert Xu 785a0fa2d03SHerbert Xu unlock: 786a0fa2d03SHerbert Xu release_sock(psk); 787a0fa2d03SHerbert Xu 788a0fa2d03SHerbert Xu return err; 789a0fa2d03SHerbert Xu } 790a0fa2d03SHerbert Xu 791a0fa2d03SHerbert Xu static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg, 792a0fa2d03SHerbert Xu size_t size) 793a0fa2d03SHerbert Xu { 794a0fa2d03SHerbert Xu int err; 795a0fa2d03SHerbert Xu 796a0fa2d03SHerbert Xu err = skcipher_check_key(sock); 797a0fa2d03SHerbert Xu if (err) 798a0fa2d03SHerbert Xu return err; 799a0fa2d03SHerbert Xu 800a0fa2d03SHerbert Xu return skcipher_sendmsg(sock, msg, size); 801a0fa2d03SHerbert Xu } 802a0fa2d03SHerbert Xu 803a0fa2d03SHerbert Xu static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page, 804a0fa2d03SHerbert Xu int offset, size_t size, int flags) 805a0fa2d03SHerbert Xu { 806a0fa2d03SHerbert Xu int err; 807a0fa2d03SHerbert Xu 808a0fa2d03SHerbert Xu err = skcipher_check_key(sock); 809a0fa2d03SHerbert Xu if (err) 810a0fa2d03SHerbert Xu return err; 811a0fa2d03SHerbert Xu 812a0fa2d03SHerbert Xu return skcipher_sendpage(sock, page, offset, size, flags); 813a0fa2d03SHerbert Xu } 814a0fa2d03SHerbert Xu 815a0fa2d03SHerbert Xu static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg, 816a0fa2d03SHerbert Xu size_t ignored, int flags) 817a0fa2d03SHerbert Xu { 818a0fa2d03SHerbert Xu int err; 819a0fa2d03SHerbert Xu 820a0fa2d03SHerbert Xu err = skcipher_check_key(sock); 821a0fa2d03SHerbert Xu if (err) 822a0fa2d03SHerbert Xu return err; 823a0fa2d03SHerbert Xu 824a0fa2d03SHerbert Xu return skcipher_recvmsg(sock, msg, ignored, flags); 825a0fa2d03SHerbert Xu } 826a0fa2d03SHerbert Xu 827a0fa2d03SHerbert Xu static struct proto_ops algif_skcipher_ops_nokey = { 828a0fa2d03SHerbert Xu .family = PF_ALG, 829a0fa2d03SHerbert Xu 830a0fa2d03SHerbert Xu .connect = sock_no_connect, 831a0fa2d03SHerbert Xu .socketpair = sock_no_socketpair, 832a0fa2d03SHerbert Xu .getname = sock_no_getname, 833a0fa2d03SHerbert Xu .ioctl = sock_no_ioctl, 834a0fa2d03SHerbert Xu .listen = sock_no_listen, 835a0fa2d03SHerbert Xu .shutdown = sock_no_shutdown, 836a0fa2d03SHerbert Xu .getsockopt = sock_no_getsockopt, 837a0fa2d03SHerbert Xu .mmap = sock_no_mmap, 838a0fa2d03SHerbert Xu .bind = sock_no_bind, 839a0fa2d03SHerbert Xu .accept = sock_no_accept, 840a0fa2d03SHerbert Xu .setsockopt = sock_no_setsockopt, 841a0fa2d03SHerbert Xu 842a0fa2d03SHerbert Xu .release = af_alg_release, 843a0fa2d03SHerbert Xu .sendmsg = skcipher_sendmsg_nokey, 844a0fa2d03SHerbert Xu .sendpage = skcipher_sendpage_nokey, 845a0fa2d03SHerbert Xu .recvmsg = skcipher_recvmsg_nokey, 846a0fa2d03SHerbert Xu .poll = skcipher_poll, 847a0fa2d03SHerbert Xu }; 848a0fa2d03SHerbert Xu 8498ff59090SHerbert Xu static void *skcipher_bind(const char *name, u32 type, u32 mask) 8508ff59090SHerbert Xu { 851dd504589SHerbert Xu struct skcipher_tfm *tfm; 852dd504589SHerbert Xu struct crypto_skcipher *skcipher; 853dd504589SHerbert Xu 854dd504589SHerbert Xu tfm = kzalloc(sizeof(*tfm), GFP_KERNEL); 855dd504589SHerbert Xu if (!tfm) 856dd504589SHerbert Xu return ERR_PTR(-ENOMEM); 857dd504589SHerbert Xu 858dd504589SHerbert Xu skcipher = crypto_alloc_skcipher(name, type, mask); 859dd504589SHerbert Xu if (IS_ERR(skcipher)) { 860dd504589SHerbert Xu kfree(tfm); 861dd504589SHerbert Xu return ERR_CAST(skcipher); 862dd504589SHerbert Xu } 863dd504589SHerbert Xu 864dd504589SHerbert Xu tfm->skcipher = skcipher; 865dd504589SHerbert Xu 866dd504589SHerbert Xu return tfm; 8678ff59090SHerbert Xu } 8688ff59090SHerbert Xu 8698ff59090SHerbert Xu static void skcipher_release(void *private) 8708ff59090SHerbert Xu { 871dd504589SHerbert Xu struct skcipher_tfm *tfm = private; 872dd504589SHerbert Xu 873dd504589SHerbert Xu crypto_free_skcipher(tfm->skcipher); 874dd504589SHerbert Xu kfree(tfm); 8758ff59090SHerbert Xu } 8768ff59090SHerbert Xu 8778ff59090SHerbert Xu static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen) 8788ff59090SHerbert Xu { 879dd504589SHerbert Xu struct skcipher_tfm *tfm = private; 880dd504589SHerbert Xu int err; 881dd504589SHerbert Xu 882dd504589SHerbert Xu err = crypto_skcipher_setkey(tfm->skcipher, key, keylen); 883dd504589SHerbert Xu tfm->has_key = !err; 884dd504589SHerbert Xu 885dd504589SHerbert Xu return err; 8868ff59090SHerbert Xu } 8878ff59090SHerbert Xu 888a596999bSTadeusz Struk static void skcipher_wait(struct sock *sk) 889a596999bSTadeusz Struk { 890a596999bSTadeusz Struk struct alg_sock *ask = alg_sk(sk); 891a596999bSTadeusz Struk struct skcipher_ctx *ctx = ask->private; 892a596999bSTadeusz Struk int ctr = 0; 893a596999bSTadeusz Struk 894a596999bSTadeusz Struk while (atomic_read(&ctx->inflight) && ctr++ < 100) 895a596999bSTadeusz Struk msleep(100); 896a596999bSTadeusz Struk } 897a596999bSTadeusz Struk 898*d7b65aeeSHerbert Xu static void skcipher_sock_destruct(struct sock *sk) 8998ff59090SHerbert Xu { 9008ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 9018ff59090SHerbert Xu struct skcipher_ctx *ctx = ask->private; 9020d96e4baSHerbert Xu struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(&ctx->req); 9038ff59090SHerbert Xu 904a596999bSTadeusz Struk if (atomic_read(&ctx->inflight)) 905a596999bSTadeusz Struk skcipher_wait(sk); 906a596999bSTadeusz Struk 9078ff59090SHerbert Xu skcipher_free_sgl(sk); 9080d96e4baSHerbert Xu sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm)); 9098ff59090SHerbert Xu sock_kfree_s(sk, ctx, ctx->len); 9108ff59090SHerbert Xu af_alg_release_parent(sk); 9118ff59090SHerbert Xu } 9128ff59090SHerbert Xu 913*d7b65aeeSHerbert Xu static int skcipher_accept_parent_nokey(void *private, struct sock *sk) 9148ff59090SHerbert Xu { 9158ff59090SHerbert Xu struct skcipher_ctx *ctx; 9168ff59090SHerbert Xu struct alg_sock *ask = alg_sk(sk); 917dd504589SHerbert Xu struct skcipher_tfm *tfm = private; 918dd504589SHerbert Xu struct crypto_skcipher *skcipher = tfm->skcipher; 919dd504589SHerbert Xu unsigned int len = sizeof(*ctx) + crypto_skcipher_reqsize(skcipher); 920dd504589SHerbert Xu 9218ff59090SHerbert Xu ctx = sock_kmalloc(sk, len, GFP_KERNEL); 9228ff59090SHerbert Xu if (!ctx) 9238ff59090SHerbert Xu return -ENOMEM; 9248ff59090SHerbert Xu 925dd504589SHerbert Xu ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher), 9268ff59090SHerbert Xu GFP_KERNEL); 9278ff59090SHerbert Xu if (!ctx->iv) { 9288ff59090SHerbert Xu sock_kfree_s(sk, ctx, len); 9298ff59090SHerbert Xu return -ENOMEM; 9308ff59090SHerbert Xu } 9318ff59090SHerbert Xu 932dd504589SHerbert Xu memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher)); 9338ff59090SHerbert Xu 9348ff59090SHerbert Xu INIT_LIST_HEAD(&ctx->tsgl); 9358ff59090SHerbert Xu ctx->len = len; 9368ff59090SHerbert Xu ctx->used = 0; 9378ff59090SHerbert Xu ctx->more = 0; 9388ff59090SHerbert Xu ctx->merge = 0; 9398ff59090SHerbert Xu ctx->enc = 0; 940a596999bSTadeusz Struk atomic_set(&ctx->inflight, 0); 9418ff59090SHerbert Xu af_alg_init_completion(&ctx->completion); 9428ff59090SHerbert Xu 9438ff59090SHerbert Xu ask->private = ctx; 9448ff59090SHerbert Xu 945dd504589SHerbert Xu skcipher_request_set_tfm(&ctx->req, skcipher); 9460d96e4baSHerbert Xu skcipher_request_set_callback(&ctx->req, CRYPTO_TFM_REQ_MAY_BACKLOG, 9478ff59090SHerbert Xu af_alg_complete, &ctx->completion); 9488ff59090SHerbert Xu 9498ff59090SHerbert Xu sk->sk_destruct = skcipher_sock_destruct; 9508ff59090SHerbert Xu 9518ff59090SHerbert Xu return 0; 9528ff59090SHerbert Xu } 9538ff59090SHerbert Xu 954a0fa2d03SHerbert Xu static int skcipher_accept_parent(void *private, struct sock *sk) 955a0fa2d03SHerbert Xu { 956a0fa2d03SHerbert Xu struct skcipher_tfm *tfm = private; 957a0fa2d03SHerbert Xu 9586e8d8ecfSHerbert Xu if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher)) 959a0fa2d03SHerbert Xu return -ENOKEY; 960a0fa2d03SHerbert Xu 961*d7b65aeeSHerbert Xu return skcipher_accept_parent_nokey(private, sk); 962a0fa2d03SHerbert Xu } 963a0fa2d03SHerbert Xu 9648ff59090SHerbert Xu static const struct af_alg_type algif_type_skcipher = { 9658ff59090SHerbert Xu .bind = skcipher_bind, 9668ff59090SHerbert Xu .release = skcipher_release, 9678ff59090SHerbert Xu .setkey = skcipher_setkey, 9688ff59090SHerbert Xu .accept = skcipher_accept_parent, 969a0fa2d03SHerbert Xu .accept_nokey = skcipher_accept_parent_nokey, 9708ff59090SHerbert Xu .ops = &algif_skcipher_ops, 971a0fa2d03SHerbert Xu .ops_nokey = &algif_skcipher_ops_nokey, 9728ff59090SHerbert Xu .name = "skcipher", 9738ff59090SHerbert Xu .owner = THIS_MODULE 9748ff59090SHerbert Xu }; 9758ff59090SHerbert Xu 9768ff59090SHerbert Xu static int __init algif_skcipher_init(void) 9778ff59090SHerbert Xu { 9788ff59090SHerbert Xu return af_alg_register_type(&algif_type_skcipher); 9798ff59090SHerbert Xu } 9808ff59090SHerbert Xu 9818ff59090SHerbert Xu static void __exit algif_skcipher_exit(void) 9828ff59090SHerbert Xu { 9838ff59090SHerbert Xu int err = af_alg_unregister_type(&algif_type_skcipher); 9848ff59090SHerbert Xu BUG_ON(err); 9858ff59090SHerbert Xu } 9868ff59090SHerbert Xu 9878ff59090SHerbert Xu module_init(algif_skcipher_init); 9888ff59090SHerbert Xu module_exit(algif_skcipher_exit); 9898ff59090SHerbert Xu MODULE_LICENSE("GPL"); 990