xref: /OK3568_Linux_fs/kernel/crypto/algif_skcipher.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * algif_skcipher: User-space interface for skcipher algorithms
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This file provides the user-space API for symmetric key ciphers.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * The following concept of the memory management is used:
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
12*4882a593Smuzhiyun  * filled by user space with the data submitted via sendpage/sendmsg. Filling
13*4882a593Smuzhiyun  * up the TX SGL does not cause a crypto operation -- the data will only be
14*4882a593Smuzhiyun  * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
15*4882a593Smuzhiyun  * provide a buffer which is tracked with the RX SGL.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * During the processing of the recvmsg operation, the cipher request is
18*4882a593Smuzhiyun  * allocated and prepared. As part of the recvmsg operation, the processed
19*4882a593Smuzhiyun  * TX buffers are extracted from the TX SGL into a separate SGL.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * After the completion of the crypto operation, the RX SGL and the cipher
22*4882a593Smuzhiyun  * request is released. The extracted TX SGL parts are released together with
23*4882a593Smuzhiyun  * the RX SGL release.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <crypto/scatterwalk.h>
27*4882a593Smuzhiyun #include <crypto/skcipher.h>
28*4882a593Smuzhiyun #include <crypto/if_alg.h>
29*4882a593Smuzhiyun #include <linux/init.h>
30*4882a593Smuzhiyun #include <linux/list.h>
31*4882a593Smuzhiyun #include <linux/kernel.h>
32*4882a593Smuzhiyun #include <linux/mm.h>
33*4882a593Smuzhiyun #include <linux/module.h>
34*4882a593Smuzhiyun #include <linux/net.h>
35*4882a593Smuzhiyun #include <net/sock.h>
36*4882a593Smuzhiyun 
skcipher_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)37*4882a593Smuzhiyun static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
38*4882a593Smuzhiyun 			    size_t size)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	struct sock *sk = sock->sk;
41*4882a593Smuzhiyun 	struct alg_sock *ask = alg_sk(sk);
42*4882a593Smuzhiyun 	struct sock *psk = ask->parent;
43*4882a593Smuzhiyun 	struct alg_sock *pask = alg_sk(psk);
44*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = pask->private;
45*4882a593Smuzhiyun 	unsigned ivsize = crypto_skcipher_ivsize(tfm);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	return af_alg_sendmsg(sock, msg, size, ivsize);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
_skcipher_recvmsg(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)50*4882a593Smuzhiyun static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
51*4882a593Smuzhiyun 			     size_t ignored, int flags)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	struct sock *sk = sock->sk;
54*4882a593Smuzhiyun 	struct alg_sock *ask = alg_sk(sk);
55*4882a593Smuzhiyun 	struct sock *psk = ask->parent;
56*4882a593Smuzhiyun 	struct alg_sock *pask = alg_sk(psk);
57*4882a593Smuzhiyun 	struct af_alg_ctx *ctx = ask->private;
58*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = pask->private;
59*4882a593Smuzhiyun 	unsigned int bs = crypto_skcipher_chunksize(tfm);
60*4882a593Smuzhiyun 	struct af_alg_async_req *areq;
61*4882a593Smuzhiyun 	int err = 0;
62*4882a593Smuzhiyun 	size_t len = 0;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	if (!ctx->init || (ctx->more && ctx->used < bs)) {
65*4882a593Smuzhiyun 		err = af_alg_wait_for_data(sk, flags, bs);
66*4882a593Smuzhiyun 		if (err)
67*4882a593Smuzhiyun 			return err;
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/* Allocate cipher request for current operation. */
71*4882a593Smuzhiyun 	areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
72*4882a593Smuzhiyun 				     crypto_skcipher_reqsize(tfm));
73*4882a593Smuzhiyun 	if (IS_ERR(areq))
74*4882a593Smuzhiyun 		return PTR_ERR(areq);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	/* convert iovecs of output buffers into RX SGL */
77*4882a593Smuzhiyun 	err = af_alg_get_rsgl(sk, msg, flags, areq, ctx->used, &len);
78*4882a593Smuzhiyun 	if (err)
79*4882a593Smuzhiyun 		goto free;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/*
82*4882a593Smuzhiyun 	 * If more buffers are to be expected to be processed, process only
83*4882a593Smuzhiyun 	 * full block size buffers.
84*4882a593Smuzhiyun 	 */
85*4882a593Smuzhiyun 	if (ctx->more || len < ctx->used)
86*4882a593Smuzhiyun 		len -= len % bs;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	/*
89*4882a593Smuzhiyun 	 * Create a per request TX SGL for this request which tracks the
90*4882a593Smuzhiyun 	 * SG entries from the global TX SGL.
91*4882a593Smuzhiyun 	 */
92*4882a593Smuzhiyun 	areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0);
93*4882a593Smuzhiyun 	if (!areq->tsgl_entries)
94*4882a593Smuzhiyun 		areq->tsgl_entries = 1;
95*4882a593Smuzhiyun 	areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
96*4882a593Smuzhiyun 						 areq->tsgl_entries),
97*4882a593Smuzhiyun 				  GFP_KERNEL);
98*4882a593Smuzhiyun 	if (!areq->tsgl) {
99*4882a593Smuzhiyun 		err = -ENOMEM;
100*4882a593Smuzhiyun 		goto free;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 	sg_init_table(areq->tsgl, areq->tsgl_entries);
103*4882a593Smuzhiyun 	af_alg_pull_tsgl(sk, len, areq->tsgl, 0);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* Initialize the crypto operation */
106*4882a593Smuzhiyun 	skcipher_request_set_tfm(&areq->cra_u.skcipher_req, tfm);
107*4882a593Smuzhiyun 	skcipher_request_set_crypt(&areq->cra_u.skcipher_req, areq->tsgl,
108*4882a593Smuzhiyun 				   areq->first_rsgl.sgl.sg, len, ctx->iv);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
111*4882a593Smuzhiyun 		/* AIO operation */
112*4882a593Smuzhiyun 		sock_hold(sk);
113*4882a593Smuzhiyun 		areq->iocb = msg->msg_iocb;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		/* Remember output size that will be generated. */
116*4882a593Smuzhiyun 		areq->outlen = len;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 		skcipher_request_set_callback(&areq->cra_u.skcipher_req,
119*4882a593Smuzhiyun 					      CRYPTO_TFM_REQ_MAY_SLEEP,
120*4882a593Smuzhiyun 					      af_alg_async_cb, areq);
121*4882a593Smuzhiyun 		err = ctx->enc ?
122*4882a593Smuzhiyun 			crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
123*4882a593Smuzhiyun 			crypto_skcipher_decrypt(&areq->cra_u.skcipher_req);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 		/* AIO operation in progress */
126*4882a593Smuzhiyun 		if (err == -EINPROGRESS)
127*4882a593Smuzhiyun 			return -EIOCBQUEUED;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 		sock_put(sk);
130*4882a593Smuzhiyun 	} else {
131*4882a593Smuzhiyun 		/* Synchronous operation */
132*4882a593Smuzhiyun 		skcipher_request_set_callback(&areq->cra_u.skcipher_req,
133*4882a593Smuzhiyun 					      CRYPTO_TFM_REQ_MAY_SLEEP |
134*4882a593Smuzhiyun 					      CRYPTO_TFM_REQ_MAY_BACKLOG,
135*4882a593Smuzhiyun 					      crypto_req_done, &ctx->wait);
136*4882a593Smuzhiyun 		err = crypto_wait_req(ctx->enc ?
137*4882a593Smuzhiyun 			crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
138*4882a593Smuzhiyun 			crypto_skcipher_decrypt(&areq->cra_u.skcipher_req),
139*4882a593Smuzhiyun 						 &ctx->wait);
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun free:
144*4882a593Smuzhiyun 	af_alg_free_resources(areq);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	return err ? err : len;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
skcipher_recvmsg(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)149*4882a593Smuzhiyun static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
150*4882a593Smuzhiyun 			    size_t ignored, int flags)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	struct sock *sk = sock->sk;
153*4882a593Smuzhiyun 	int ret = 0;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	lock_sock(sk);
156*4882a593Smuzhiyun 	while (msg_data_left(msg)) {
157*4882a593Smuzhiyun 		int err = _skcipher_recvmsg(sock, msg, ignored, flags);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 		/*
160*4882a593Smuzhiyun 		 * This error covers -EIOCBQUEUED which implies that we can
161*4882a593Smuzhiyun 		 * only handle one AIO request. If the caller wants to have
162*4882a593Smuzhiyun 		 * multiple AIO requests in parallel, he must make multiple
163*4882a593Smuzhiyun 		 * separate AIO calls.
164*4882a593Smuzhiyun 		 *
165*4882a593Smuzhiyun 		 * Also return the error if no data has been processed so far.
166*4882a593Smuzhiyun 		 */
167*4882a593Smuzhiyun 		if (err <= 0) {
168*4882a593Smuzhiyun 			if (err == -EIOCBQUEUED || !ret)
169*4882a593Smuzhiyun 				ret = err;
170*4882a593Smuzhiyun 			goto out;
171*4882a593Smuzhiyun 		}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		ret += err;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun out:
177*4882a593Smuzhiyun 	af_alg_wmem_wakeup(sk);
178*4882a593Smuzhiyun 	release_sock(sk);
179*4882a593Smuzhiyun 	return ret;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun static struct proto_ops algif_skcipher_ops = {
183*4882a593Smuzhiyun 	.family		=	PF_ALG,
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	.connect	=	sock_no_connect,
186*4882a593Smuzhiyun 	.socketpair	=	sock_no_socketpair,
187*4882a593Smuzhiyun 	.getname	=	sock_no_getname,
188*4882a593Smuzhiyun 	.ioctl		=	sock_no_ioctl,
189*4882a593Smuzhiyun 	.listen		=	sock_no_listen,
190*4882a593Smuzhiyun 	.shutdown	=	sock_no_shutdown,
191*4882a593Smuzhiyun 	.mmap		=	sock_no_mmap,
192*4882a593Smuzhiyun 	.bind		=	sock_no_bind,
193*4882a593Smuzhiyun 	.accept		=	sock_no_accept,
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	.release	=	af_alg_release,
196*4882a593Smuzhiyun 	.sendmsg	=	skcipher_sendmsg,
197*4882a593Smuzhiyun 	.sendpage	=	af_alg_sendpage,
198*4882a593Smuzhiyun 	.recvmsg	=	skcipher_recvmsg,
199*4882a593Smuzhiyun 	.poll		=	af_alg_poll,
200*4882a593Smuzhiyun };
201*4882a593Smuzhiyun 
skcipher_check_key(struct socket * sock)202*4882a593Smuzhiyun static int skcipher_check_key(struct socket *sock)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	int err = 0;
205*4882a593Smuzhiyun 	struct sock *psk;
206*4882a593Smuzhiyun 	struct alg_sock *pask;
207*4882a593Smuzhiyun 	struct crypto_skcipher *tfm;
208*4882a593Smuzhiyun 	struct sock *sk = sock->sk;
209*4882a593Smuzhiyun 	struct alg_sock *ask = alg_sk(sk);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	lock_sock(sk);
212*4882a593Smuzhiyun 	if (!atomic_read(&ask->nokey_refcnt))
213*4882a593Smuzhiyun 		goto unlock_child;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	psk = ask->parent;
216*4882a593Smuzhiyun 	pask = alg_sk(ask->parent);
217*4882a593Smuzhiyun 	tfm = pask->private;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	err = -ENOKEY;
220*4882a593Smuzhiyun 	lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
221*4882a593Smuzhiyun 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
222*4882a593Smuzhiyun 		goto unlock;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	atomic_dec(&pask->nokey_refcnt);
225*4882a593Smuzhiyun 	atomic_set(&ask->nokey_refcnt, 0);
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	err = 0;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun unlock:
230*4882a593Smuzhiyun 	release_sock(psk);
231*4882a593Smuzhiyun unlock_child:
232*4882a593Smuzhiyun 	release_sock(sk);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return err;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
skcipher_sendmsg_nokey(struct socket * sock,struct msghdr * msg,size_t size)237*4882a593Smuzhiyun static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
238*4882a593Smuzhiyun 				  size_t size)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	int err;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	err = skcipher_check_key(sock);
243*4882a593Smuzhiyun 	if (err)
244*4882a593Smuzhiyun 		return err;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	return skcipher_sendmsg(sock, msg, size);
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
skcipher_sendpage_nokey(struct socket * sock,struct page * page,int offset,size_t size,int flags)249*4882a593Smuzhiyun static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
250*4882a593Smuzhiyun 				       int offset, size_t size, int flags)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	int err;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	err = skcipher_check_key(sock);
255*4882a593Smuzhiyun 	if (err)
256*4882a593Smuzhiyun 		return err;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	return af_alg_sendpage(sock, page, offset, size, flags);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
skcipher_recvmsg_nokey(struct socket * sock,struct msghdr * msg,size_t ignored,int flags)261*4882a593Smuzhiyun static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
262*4882a593Smuzhiyun 				  size_t ignored, int flags)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	int err;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	err = skcipher_check_key(sock);
267*4882a593Smuzhiyun 	if (err)
268*4882a593Smuzhiyun 		return err;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	return skcipher_recvmsg(sock, msg, ignored, flags);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun static struct proto_ops algif_skcipher_ops_nokey = {
274*4882a593Smuzhiyun 	.family		=	PF_ALG,
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	.connect	=	sock_no_connect,
277*4882a593Smuzhiyun 	.socketpair	=	sock_no_socketpair,
278*4882a593Smuzhiyun 	.getname	=	sock_no_getname,
279*4882a593Smuzhiyun 	.ioctl		=	sock_no_ioctl,
280*4882a593Smuzhiyun 	.listen		=	sock_no_listen,
281*4882a593Smuzhiyun 	.shutdown	=	sock_no_shutdown,
282*4882a593Smuzhiyun 	.mmap		=	sock_no_mmap,
283*4882a593Smuzhiyun 	.bind		=	sock_no_bind,
284*4882a593Smuzhiyun 	.accept		=	sock_no_accept,
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	.release	=	af_alg_release,
287*4882a593Smuzhiyun 	.sendmsg	=	skcipher_sendmsg_nokey,
288*4882a593Smuzhiyun 	.sendpage	=	skcipher_sendpage_nokey,
289*4882a593Smuzhiyun 	.recvmsg	=	skcipher_recvmsg_nokey,
290*4882a593Smuzhiyun 	.poll		=	af_alg_poll,
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun 
skcipher_bind(const char * name,u32 type,u32 mask)293*4882a593Smuzhiyun static void *skcipher_bind(const char *name, u32 type, u32 mask)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	return crypto_alloc_skcipher(name, type, mask);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
skcipher_release(void * private)298*4882a593Smuzhiyun static void skcipher_release(void *private)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	crypto_free_skcipher(private);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
skcipher_setkey(void * private,const u8 * key,unsigned int keylen)303*4882a593Smuzhiyun static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	return crypto_skcipher_setkey(private, key, keylen);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun 
skcipher_sock_destruct(struct sock * sk)308*4882a593Smuzhiyun static void skcipher_sock_destruct(struct sock *sk)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun 	struct alg_sock *ask = alg_sk(sk);
311*4882a593Smuzhiyun 	struct af_alg_ctx *ctx = ask->private;
312*4882a593Smuzhiyun 	struct sock *psk = ask->parent;
313*4882a593Smuzhiyun 	struct alg_sock *pask = alg_sk(psk);
314*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = pask->private;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
317*4882a593Smuzhiyun 	sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
318*4882a593Smuzhiyun 	sock_kfree_s(sk, ctx, ctx->len);
319*4882a593Smuzhiyun 	af_alg_release_parent(sk);
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun 
skcipher_accept_parent_nokey(void * private,struct sock * sk)322*4882a593Smuzhiyun static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun 	struct af_alg_ctx *ctx;
325*4882a593Smuzhiyun 	struct alg_sock *ask = alg_sk(sk);
326*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = private;
327*4882a593Smuzhiyun 	unsigned int len = sizeof(*ctx);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
330*4882a593Smuzhiyun 	if (!ctx)
331*4882a593Smuzhiyun 		return -ENOMEM;
332*4882a593Smuzhiyun 	memset(ctx, 0, len);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(tfm),
335*4882a593Smuzhiyun 			       GFP_KERNEL);
336*4882a593Smuzhiyun 	if (!ctx->iv) {
337*4882a593Smuzhiyun 		sock_kfree_s(sk, ctx, len);
338*4882a593Smuzhiyun 		return -ENOMEM;
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun 	memset(ctx->iv, 0, crypto_skcipher_ivsize(tfm));
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	INIT_LIST_HEAD(&ctx->tsgl_list);
343*4882a593Smuzhiyun 	ctx->len = len;
344*4882a593Smuzhiyun 	crypto_init_wait(&ctx->wait);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	ask->private = ctx;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	sk->sk_destruct = skcipher_sock_destruct;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	return 0;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
skcipher_accept_parent(void * private,struct sock * sk)353*4882a593Smuzhiyun static int skcipher_accept_parent(void *private, struct sock *sk)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	struct crypto_skcipher *tfm = private;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
358*4882a593Smuzhiyun 		return -ENOKEY;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	return skcipher_accept_parent_nokey(private, sk);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun static const struct af_alg_type algif_type_skcipher = {
364*4882a593Smuzhiyun 	.bind		=	skcipher_bind,
365*4882a593Smuzhiyun 	.release	=	skcipher_release,
366*4882a593Smuzhiyun 	.setkey		=	skcipher_setkey,
367*4882a593Smuzhiyun 	.accept		=	skcipher_accept_parent,
368*4882a593Smuzhiyun 	.accept_nokey	=	skcipher_accept_parent_nokey,
369*4882a593Smuzhiyun 	.ops		=	&algif_skcipher_ops,
370*4882a593Smuzhiyun 	.ops_nokey	=	&algif_skcipher_ops_nokey,
371*4882a593Smuzhiyun 	.name		=	"skcipher",
372*4882a593Smuzhiyun 	.owner		=	THIS_MODULE
373*4882a593Smuzhiyun };
374*4882a593Smuzhiyun 
algif_skcipher_init(void)375*4882a593Smuzhiyun static int __init algif_skcipher_init(void)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	return af_alg_register_type(&algif_type_skcipher);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
algif_skcipher_exit(void)380*4882a593Smuzhiyun static void __exit algif_skcipher_exit(void)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	int err = af_alg_unregister_type(&algif_type_skcipher);
383*4882a593Smuzhiyun 	BUG_ON(err);
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun module_init(algif_skcipher_init);
387*4882a593Smuzhiyun module_exit(algif_skcipher_exit);
388*4882a593Smuzhiyun MODULE_LICENSE("GPL");
389