2 * algif_skcipher: User-space interface for skcipher algorithms
4 * This file provides the user-space API for symmetric key ciphers.
6 * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * The following concept of the memory management is used:
15 * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
16 * filled by user space with the data submitted via sendpage/sendmsg. Filling
17 * up the TX SGL does not cause a crypto operation -- the data will only be
18 * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
19 * provide a buffer which is tracked with the RX SGL.
21 * During the processing of the recvmsg operation, the cipher request is
22 * allocated and prepared. As part of the recvmsg operation, the processed
23 * TX buffers are extracted from the TX SGL into a separate SGL.
25 * After the completion of the crypto operation, the RX SGL and the cipher
26 * request is released. The extracted TX SGL parts are released together with
30 #include <crypto/scatterwalk.h>
31 #include <crypto/skcipher.h>
32 #include <crypto/if_alg.h>
33 #include <linux/init.h>
34 #include <linux/list.h>
35 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/net.h>
42 struct crypto_skcipher *skcipher;
46 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
49 struct sock *sk = sock->sk;
50 struct alg_sock *ask = alg_sk(sk);
51 struct sock *psk = ask->parent;
52 struct alg_sock *pask = alg_sk(psk);
53 struct skcipher_tfm *skc = pask->private;
54 struct crypto_skcipher *tfm = skc->skcipher;
55 unsigned ivsize = crypto_skcipher_ivsize(tfm);
57 return af_alg_sendmsg(sock, msg, size, ivsize);
60 static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
61 size_t ignored, int flags)
63 struct sock *sk = sock->sk;
64 struct alg_sock *ask = alg_sk(sk);
65 struct sock *psk = ask->parent;
66 struct alg_sock *pask = alg_sk(psk);
67 struct af_alg_ctx *ctx = ask->private;
68 struct skcipher_tfm *skc = pask->private;
69 struct crypto_skcipher *tfm = skc->skcipher;
70 unsigned int bs = crypto_skcipher_blocksize(tfm);
71 struct af_alg_async_req *areq;
76 err = af_alg_wait_for_data(sk, flags);
81 /* Allocate cipher request for current operation. */
82 areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
83 crypto_skcipher_reqsize(tfm));
87 /* convert iovecs of output buffers into RX SGL */
88 err = af_alg_get_rsgl(sk, msg, flags, areq, -1, &len);
92 /* Process only as much RX buffers for which we have TX data */
97 * If more buffers are to be expected to be processed, process only
98 * full block size buffers.
100 if (ctx->more || len < ctx->used)
104 * Create a per request TX SGL for this request which tracks the
105 * SG entries from the global TX SGL.
107 areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0);
108 if (!areq->tsgl_entries)
109 areq->tsgl_entries = 1;
110 areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * areq->tsgl_entries,
116 sg_init_table(areq->tsgl, areq->tsgl_entries);
117 af_alg_pull_tsgl(sk, len, areq->tsgl, 0);
119 /* Initialize the crypto operation */
120 skcipher_request_set_tfm(&areq->cra_u.skcipher_req, tfm);
121 skcipher_request_set_crypt(&areq->cra_u.skcipher_req, areq->tsgl,
122 areq->first_rsgl.sgl.sg, len, ctx->iv);
124 if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
127 areq->iocb = msg->msg_iocb;
129 /* Remember output size that will be generated. */
132 skcipher_request_set_callback(&areq->cra_u.skcipher_req,
133 CRYPTO_TFM_REQ_MAY_SLEEP,
134 af_alg_async_cb, areq);
136 crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
137 crypto_skcipher_decrypt(&areq->cra_u.skcipher_req);
139 /* AIO operation in progress */
140 if (err == -EINPROGRESS || err == -EBUSY)
145 /* Synchronous operation */
146 skcipher_request_set_callback(&areq->cra_u.skcipher_req,
147 CRYPTO_TFM_REQ_MAY_SLEEP |
148 CRYPTO_TFM_REQ_MAY_BACKLOG,
151 err = af_alg_wait_for_completion(ctx->enc ?
152 crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
153 crypto_skcipher_decrypt(&areq->cra_u.skcipher_req),
159 af_alg_free_resources(areq);
161 return err ? err : len;
164 static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
165 size_t ignored, int flags)
167 struct sock *sk = sock->sk;
171 while (msg_data_left(msg)) {
172 int err = _skcipher_recvmsg(sock, msg, ignored, flags);
175 * This error covers -EIOCBQUEUED which implies that we can
176 * only handle one AIO request. If the caller wants to have
177 * multiple AIO requests in parallel, he must make multiple
178 * separate AIO calls.
180 * Also return the error if no data has been processed so far.
183 if (err == -EIOCBQUEUED || !ret)
192 af_alg_wmem_wakeup(sk);
198 static struct proto_ops algif_skcipher_ops = {
201 .connect = sock_no_connect,
202 .socketpair = sock_no_socketpair,
203 .getname = sock_no_getname,
204 .ioctl = sock_no_ioctl,
205 .listen = sock_no_listen,
206 .shutdown = sock_no_shutdown,
207 .getsockopt = sock_no_getsockopt,
208 .mmap = sock_no_mmap,
209 .bind = sock_no_bind,
210 .accept = sock_no_accept,
211 .setsockopt = sock_no_setsockopt,
213 .release = af_alg_release,
214 .sendmsg = skcipher_sendmsg,
215 .sendpage = af_alg_sendpage,
216 .recvmsg = skcipher_recvmsg,
220 static int skcipher_check_key(struct socket *sock)
224 struct alg_sock *pask;
225 struct skcipher_tfm *tfm;
226 struct sock *sk = sock->sk;
227 struct alg_sock *ask = alg_sk(sk);
234 pask = alg_sk(ask->parent);
238 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
258 static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
263 err = skcipher_check_key(sock);
267 return skcipher_sendmsg(sock, msg, size);
270 static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
271 int offset, size_t size, int flags)
275 err = skcipher_check_key(sock);
279 return af_alg_sendpage(sock, page, offset, size, flags);
282 static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
283 size_t ignored, int flags)
287 err = skcipher_check_key(sock);
291 return skcipher_recvmsg(sock, msg, ignored, flags);
294 static struct proto_ops algif_skcipher_ops_nokey = {
297 .connect = sock_no_connect,
298 .socketpair = sock_no_socketpair,
299 .getname = sock_no_getname,
300 .ioctl = sock_no_ioctl,
301 .listen = sock_no_listen,
302 .shutdown = sock_no_shutdown,
303 .getsockopt = sock_no_getsockopt,
304 .mmap = sock_no_mmap,
305 .bind = sock_no_bind,
306 .accept = sock_no_accept,
307 .setsockopt = sock_no_setsockopt,
309 .release = af_alg_release,
310 .sendmsg = skcipher_sendmsg_nokey,
311 .sendpage = skcipher_sendpage_nokey,
312 .recvmsg = skcipher_recvmsg_nokey,
316 static void *skcipher_bind(const char *name, u32 type, u32 mask)
318 struct skcipher_tfm *tfm;
319 struct crypto_skcipher *skcipher;
321 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
323 return ERR_PTR(-ENOMEM);
325 skcipher = crypto_alloc_skcipher(name, type, mask);
326 if (IS_ERR(skcipher)) {
328 return ERR_CAST(skcipher);
331 tfm->skcipher = skcipher;
336 static void skcipher_release(void *private)
338 struct skcipher_tfm *tfm = private;
340 crypto_free_skcipher(tfm->skcipher);
344 static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
346 struct skcipher_tfm *tfm = private;
349 err = crypto_skcipher_setkey(tfm->skcipher, key, keylen);
355 static void skcipher_sock_destruct(struct sock *sk)
357 struct alg_sock *ask = alg_sk(sk);
358 struct af_alg_ctx *ctx = ask->private;
359 struct sock *psk = ask->parent;
360 struct alg_sock *pask = alg_sk(psk);
361 struct skcipher_tfm *skc = pask->private;
362 struct crypto_skcipher *tfm = skc->skcipher;
364 af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
365 sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
366 sock_kfree_s(sk, ctx, ctx->len);
367 af_alg_release_parent(sk);
370 static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
372 struct af_alg_ctx *ctx;
373 struct alg_sock *ask = alg_sk(sk);
374 struct skcipher_tfm *tfm = private;
375 struct crypto_skcipher *skcipher = tfm->skcipher;
376 unsigned int len = sizeof(*ctx);
378 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
382 ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher),
385 sock_kfree_s(sk, ctx, len);
389 memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher));
391 INIT_LIST_HEAD(&ctx->tsgl_list);
394 atomic_set(&ctx->rcvused, 0);
398 af_alg_init_completion(&ctx->completion);
402 sk->sk_destruct = skcipher_sock_destruct;
407 static int skcipher_accept_parent(void *private, struct sock *sk)
409 struct skcipher_tfm *tfm = private;
411 if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher))
414 return skcipher_accept_parent_nokey(private, sk);
417 static const struct af_alg_type algif_type_skcipher = {
418 .bind = skcipher_bind,
419 .release = skcipher_release,
420 .setkey = skcipher_setkey,
421 .accept = skcipher_accept_parent,
422 .accept_nokey = skcipher_accept_parent_nokey,
423 .ops = &algif_skcipher_ops,
424 .ops_nokey = &algif_skcipher_ops_nokey,
429 static int __init algif_skcipher_init(void)
431 return af_alg_register_type(&algif_type_skcipher);
434 static void __exit algif_skcipher_exit(void)
436 int err = af_alg_unregister_type(&algif_type_skcipher);
440 module_init(algif_skcipher_init);
441 module_exit(algif_skcipher_exit);
442 MODULE_LICENSE("GPL");