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)
15 #include <crypto/scatterwalk.h>
16 #include <crypto/skcipher.h>
17 #include <crypto/if_alg.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/net.h>
26 struct skcipher_sg_list
{
27 struct list_head list
;
31 struct scatterlist sg
[0];
35 struct list_head tsgl
;
36 struct af_alg_sgl rsgl
;
40 struct af_alg_completion completion
;
49 struct ablkcipher_request req
;
52 #define MAX_SGL_ENTS ((PAGE_SIZE - sizeof(struct skcipher_sg_list)) / \
53 sizeof(struct scatterlist) - 1)
55 static inline bool skcipher_writable(struct sock
*sk
)
57 struct alg_sock
*ask
= alg_sk(sk
);
58 struct skcipher_ctx
*ctx
= ask
->private;
60 return ctx
->used
+ PAGE_SIZE
<= max_t(int, sk
->sk_sndbuf
, PAGE_SIZE
);
63 static int skcipher_alloc_sgl(struct sock
*sk
)
65 struct alg_sock
*ask
= alg_sk(sk
);
66 struct skcipher_ctx
*ctx
= ask
->private;
67 struct skcipher_sg_list
*sgl
;
68 struct scatterlist
*sg
= NULL
;
70 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
71 if (!list_empty(&ctx
->tsgl
))
74 if (!sg
|| sgl
->cur
>= MAX_SGL_ENTS
) {
75 sgl
= sock_kmalloc(sk
, sizeof(*sgl
) +
76 sizeof(sgl
->sg
[0]) * (MAX_SGL_ENTS
+ 1),
81 sg_init_table(sgl
->sg
, MAX_SGL_ENTS
+ 1);
85 scatterwalk_sg_chain(sg
, MAX_SGL_ENTS
+ 1, sgl
->sg
);
87 list_add_tail(&sgl
->list
, &ctx
->tsgl
);
93 static void skcipher_pull_sgl(struct sock
*sk
, int used
)
95 struct alg_sock
*ask
= alg_sk(sk
);
96 struct skcipher_ctx
*ctx
= ask
->private;
97 struct skcipher_sg_list
*sgl
;
98 struct scatterlist
*sg
;
101 while (!list_empty(&ctx
->tsgl
)) {
102 sgl
= list_first_entry(&ctx
->tsgl
, struct skcipher_sg_list
,
106 for (i
= 0; i
< sgl
->cur
; i
++) {
107 int plen
= min_t(int, used
, sg
[i
].length
);
109 if (!sg_page(sg
+ i
))
112 sg
[i
].length
-= plen
;
113 sg
[i
].offset
+= plen
;
121 put_page(sg_page(sg
+ i
));
122 sg_assign_page(sg
+ i
, NULL
);
125 list_del(&sgl
->list
);
126 sock_kfree_s(sk
, sgl
,
127 sizeof(*sgl
) + sizeof(sgl
->sg
[0]) *
135 static void skcipher_free_sgl(struct sock
*sk
)
137 struct alg_sock
*ask
= alg_sk(sk
);
138 struct skcipher_ctx
*ctx
= ask
->private;
140 skcipher_pull_sgl(sk
, ctx
->used
);
143 static int skcipher_wait_for_wmem(struct sock
*sk
, unsigned flags
)
147 int err
= -ERESTARTSYS
;
149 if (flags
& MSG_DONTWAIT
)
152 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
155 if (signal_pending(current
))
157 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
158 timeout
= MAX_SCHEDULE_TIMEOUT
;
159 if (sk_wait_event(sk
, &timeout
, skcipher_writable(sk
))) {
164 finish_wait(sk_sleep(sk
), &wait
);
169 static void skcipher_wmem_wakeup(struct sock
*sk
)
171 struct socket_wq
*wq
;
173 if (!skcipher_writable(sk
))
177 wq
= rcu_dereference(sk
->sk_wq
);
178 if (wq_has_sleeper(wq
))
179 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
182 sk_wake_async(sk
, SOCK_WAKE_WAITD
, POLL_IN
);
186 static int skcipher_wait_for_data(struct sock
*sk
, unsigned flags
)
188 struct alg_sock
*ask
= alg_sk(sk
);
189 struct skcipher_ctx
*ctx
= ask
->private;
192 int err
= -ERESTARTSYS
;
194 if (flags
& MSG_DONTWAIT
) {
198 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
201 if (signal_pending(current
))
203 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
204 timeout
= MAX_SCHEDULE_TIMEOUT
;
205 if (sk_wait_event(sk
, &timeout
, ctx
->used
)) {
210 finish_wait(sk_sleep(sk
), &wait
);
212 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
217 static void skcipher_data_wakeup(struct sock
*sk
)
219 struct alg_sock
*ask
= alg_sk(sk
);
220 struct skcipher_ctx
*ctx
= ask
->private;
221 struct socket_wq
*wq
;
227 wq
= rcu_dereference(sk
->sk_wq
);
228 if (wq_has_sleeper(wq
))
229 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
232 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
236 static int skcipher_sendmsg(struct kiocb
*unused
, struct socket
*sock
,
237 struct msghdr
*msg
, size_t size
)
239 struct sock
*sk
= sock
->sk
;
240 struct alg_sock
*ask
= alg_sk(sk
);
241 struct skcipher_ctx
*ctx
= ask
->private;
242 struct crypto_ablkcipher
*tfm
= crypto_ablkcipher_reqtfm(&ctx
->req
);
243 unsigned ivsize
= crypto_ablkcipher_ivsize(tfm
);
244 struct skcipher_sg_list
*sgl
;
245 struct af_alg_control con
= {};
252 if (msg
->msg_controllen
) {
253 err
= af_alg_cmsg_send(msg
, &con
);
268 if (con
.iv
&& con
.iv
->ivlen
!= ivsize
)
275 if (!ctx
->more
&& ctx
->used
)
281 memcpy(ctx
->iv
, con
.iv
->iv
, ivsize
);
284 limit
= max_t(int, sk
->sk_sndbuf
, PAGE_SIZE
);
288 struct scatterlist
*sg
;
289 unsigned long len
= size
;
293 sgl
= list_entry(ctx
->tsgl
.prev
,
294 struct skcipher_sg_list
, list
);
295 sg
= sgl
->sg
+ sgl
->cur
- 1;
296 len
= min_t(unsigned long, len
,
297 PAGE_SIZE
- sg
->offset
- sg
->length
);
299 err
= memcpy_fromiovec(page_address(sg_page(sg
)) +
300 sg
->offset
+ sg
->length
,
306 ctx
->merge
= (sg
->offset
+ sg
->length
) &
316 if (limit
< PAGE_SIZE
) {
317 err
= skcipher_wait_for_wmem(sk
, msg
->msg_flags
);
321 limit
= max_t(int, sk
->sk_sndbuf
, PAGE_SIZE
);
325 len
= min_t(unsigned long, len
, limit
);
327 err
= skcipher_alloc_sgl(sk
);
331 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
335 plen
= min_t(int, len
, PAGE_SIZE
);
337 sg_assign_page(sg
+ i
, alloc_page(GFP_KERNEL
));
339 if (!sg_page(sg
+ i
))
342 err
= memcpy_fromiovec(page_address(sg_page(sg
+ i
)),
345 __free_page(sg_page(sg
+ i
));
346 sg_assign_page(sg
+ i
, NULL
);
357 } while (len
&& sgl
->cur
< MAX_SGL_ENTS
);
359 ctx
->merge
= plen
& (PAGE_SIZE
- 1);
364 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
365 if (!ctx
->more
&& !list_empty(&ctx
->tsgl
))
366 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
369 skcipher_data_wakeup(sk
);
372 return copied
?: err
;
375 static ssize_t
skcipher_sendpage(struct socket
*sock
, struct page
*page
,
376 int offset
, size_t size
, int flags
)
378 struct sock
*sk
= sock
->sk
;
379 struct alg_sock
*ask
= alg_sk(sk
);
380 struct skcipher_ctx
*ctx
= ask
->private;
381 struct skcipher_sg_list
*sgl
;
386 if (!ctx
->more
&& ctx
->used
)
392 limit
= max_t(int, sk
->sk_sndbuf
, PAGE_SIZE
);
395 if (limit
< PAGE_SIZE
) {
396 err
= skcipher_wait_for_wmem(sk
, flags
);
400 limit
= max_t(int, sk
->sk_sndbuf
, PAGE_SIZE
);
404 err
= skcipher_alloc_sgl(sk
);
409 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
412 sg_set_page(sgl
->sg
+ sgl
->cur
, page
, size
, offset
);
417 ctx
->more
= flags
& MSG_MORE
;
418 if (!ctx
->more
&& !list_empty(&ctx
->tsgl
))
419 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
422 skcipher_data_wakeup(sk
);
428 static int skcipher_recvmsg(struct kiocb
*unused
, struct socket
*sock
,
429 struct msghdr
*msg
, size_t ignored
, int flags
)
431 struct sock
*sk
= sock
->sk
;
432 struct alg_sock
*ask
= alg_sk(sk
);
433 struct skcipher_ctx
*ctx
= ask
->private;
434 unsigned bs
= crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(
436 struct skcipher_sg_list
*sgl
;
437 struct scatterlist
*sg
;
438 unsigned long iovlen
;
445 for (iov
= msg
->msg_iov
, iovlen
= msg
->msg_iovlen
; iovlen
> 0;
447 unsigned long seglen
= iov
->iov_len
;
448 char __user
*from
= iov
->iov_base
;
451 sgl
= list_first_entry(&ctx
->tsgl
,
452 struct skcipher_sg_list
, list
);
460 err
= skcipher_wait_for_data(sk
, flags
);
465 used
= min_t(unsigned long, used
, seglen
);
467 if (ctx
->more
|| used
< ctx
->used
)
474 used
= af_alg_make_sg(&ctx
->rsgl
, from
, used
, 1);
478 ablkcipher_request_set_crypt(&ctx
->req
, sg
,
482 err
= af_alg_wait_for_completion(
484 crypto_ablkcipher_encrypt(&ctx
->req
) :
485 crypto_ablkcipher_decrypt(&ctx
->req
),
488 af_alg_free_sg(&ctx
->rsgl
);
496 skcipher_pull_sgl(sk
, used
);
503 skcipher_wmem_wakeup(sk
);
506 return copied
?: err
;
510 static unsigned int skcipher_poll(struct file
*file
, struct socket
*sock
,
513 struct sock
*sk
= sock
->sk
;
514 struct alg_sock
*ask
= alg_sk(sk
);
515 struct skcipher_ctx
*ctx
= ask
->private;
518 sock_poll_wait(file
, sk_sleep(sk
), wait
);
522 mask
|= POLLIN
| POLLRDNORM
;
524 if (skcipher_writable(sk
))
525 mask
|= POLLOUT
| POLLWRNORM
| POLLWRBAND
;
530 static struct proto_ops algif_skcipher_ops
= {
533 .connect
= sock_no_connect
,
534 .socketpair
= sock_no_socketpair
,
535 .getname
= sock_no_getname
,
536 .ioctl
= sock_no_ioctl
,
537 .listen
= sock_no_listen
,
538 .shutdown
= sock_no_shutdown
,
539 .getsockopt
= sock_no_getsockopt
,
540 .mmap
= sock_no_mmap
,
541 .bind
= sock_no_bind
,
542 .accept
= sock_no_accept
,
543 .setsockopt
= sock_no_setsockopt
,
545 .release
= af_alg_release
,
546 .sendmsg
= skcipher_sendmsg
,
547 .sendpage
= skcipher_sendpage
,
548 .recvmsg
= skcipher_recvmsg
,
549 .poll
= skcipher_poll
,
552 static void *skcipher_bind(const char *name
, u32 type
, u32 mask
)
554 return crypto_alloc_ablkcipher(name
, type
, mask
);
557 static void skcipher_release(void *private)
559 crypto_free_ablkcipher(private);
562 static int skcipher_setkey(void *private, const u8
*key
, unsigned int keylen
)
564 return crypto_ablkcipher_setkey(private, key
, keylen
);
567 static void skcipher_sock_destruct(struct sock
*sk
)
569 struct alg_sock
*ask
= alg_sk(sk
);
570 struct skcipher_ctx
*ctx
= ask
->private;
571 struct crypto_ablkcipher
*tfm
= crypto_ablkcipher_reqtfm(&ctx
->req
);
573 skcipher_free_sgl(sk
);
574 sock_kfree_s(sk
, ctx
->iv
, crypto_ablkcipher_ivsize(tfm
));
575 sock_kfree_s(sk
, ctx
, ctx
->len
);
576 af_alg_release_parent(sk
);
579 static int skcipher_accept_parent(void *private, struct sock
*sk
)
581 struct skcipher_ctx
*ctx
;
582 struct alg_sock
*ask
= alg_sk(sk
);
583 unsigned int len
= sizeof(*ctx
) + crypto_ablkcipher_reqsize(private);
585 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
589 ctx
->iv
= sock_kmalloc(sk
, crypto_ablkcipher_ivsize(private),
592 sock_kfree_s(sk
, ctx
, len
);
596 memset(ctx
->iv
, 0, crypto_ablkcipher_ivsize(private));
598 INIT_LIST_HEAD(&ctx
->tsgl
);
604 af_alg_init_completion(&ctx
->completion
);
608 ablkcipher_request_set_tfm(&ctx
->req
, private);
609 ablkcipher_request_set_callback(&ctx
->req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
610 af_alg_complete
, &ctx
->completion
);
612 sk
->sk_destruct
= skcipher_sock_destruct
;
617 static const struct af_alg_type algif_type_skcipher
= {
618 .bind
= skcipher_bind
,
619 .release
= skcipher_release
,
620 .setkey
= skcipher_setkey
,
621 .accept
= skcipher_accept_parent
,
622 .ops
= &algif_skcipher_ops
,
627 static int __init
algif_skcipher_init(void)
629 return af_alg_register_type(&algif_type_skcipher
);
632 static void __exit
algif_skcipher_exit(void)
634 int err
= af_alg_unregister_type(&algif_type_skcipher
);
638 module_init(algif_skcipher_init
);
639 module_exit(algif_skcipher_exit
);
640 MODULE_LICENSE("GPL");