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 crypto_ablkcipher
*skcipher
;
40 struct list_head tsgl
;
41 struct af_alg_sgl rsgl
;
45 struct af_alg_completion completion
;
54 struct ablkcipher_request req
;
57 #define MAX_SGL_ENTS ((4096 - sizeof(struct skcipher_sg_list)) / \
58 sizeof(struct scatterlist) - 1)
60 static inline int skcipher_sndbuf(struct sock
*sk
)
62 struct alg_sock
*ask
= alg_sk(sk
);
63 struct skcipher_ctx
*ctx
= ask
->private;
65 return max_t(int, max_t(int, sk
->sk_sndbuf
& PAGE_MASK
, PAGE_SIZE
) -
69 static inline bool skcipher_writable(struct sock
*sk
)
71 return PAGE_SIZE
<= skcipher_sndbuf(sk
);
74 static int skcipher_alloc_sgl(struct sock
*sk
)
76 struct alg_sock
*ask
= alg_sk(sk
);
77 struct skcipher_ctx
*ctx
= ask
->private;
78 struct skcipher_sg_list
*sgl
;
79 struct scatterlist
*sg
= NULL
;
81 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
82 if (!list_empty(&ctx
->tsgl
))
85 if (!sg
|| sgl
->cur
>= MAX_SGL_ENTS
) {
86 sgl
= sock_kmalloc(sk
, sizeof(*sgl
) +
87 sizeof(sgl
->sg
[0]) * (MAX_SGL_ENTS
+ 1),
92 sg_init_table(sgl
->sg
, MAX_SGL_ENTS
+ 1);
96 scatterwalk_sg_chain(sg
, MAX_SGL_ENTS
+ 1, sgl
->sg
);
98 list_add_tail(&sgl
->list
, &ctx
->tsgl
);
104 static void skcipher_pull_sgl(struct sock
*sk
, int used
)
106 struct alg_sock
*ask
= alg_sk(sk
);
107 struct skcipher_ctx
*ctx
= ask
->private;
108 struct skcipher_sg_list
*sgl
;
109 struct scatterlist
*sg
;
112 while (!list_empty(&ctx
->tsgl
)) {
113 sgl
= list_first_entry(&ctx
->tsgl
, struct skcipher_sg_list
,
117 for (i
= 0; i
< sgl
->cur
; i
++) {
118 int plen
= min_t(int, used
, sg
[i
].length
);
120 if (!sg_page(sg
+ i
))
123 sg
[i
].length
-= plen
;
124 sg
[i
].offset
+= plen
;
132 put_page(sg_page(sg
+ i
));
133 sg_assign_page(sg
+ i
, NULL
);
136 list_del(&sgl
->list
);
137 sock_kfree_s(sk
, sgl
,
138 sizeof(*sgl
) + sizeof(sgl
->sg
[0]) *
146 static void skcipher_free_sgl(struct sock
*sk
)
148 struct alg_sock
*ask
= alg_sk(sk
);
149 struct skcipher_ctx
*ctx
= ask
->private;
151 skcipher_pull_sgl(sk
, ctx
->used
);
154 static int skcipher_wait_for_wmem(struct sock
*sk
, unsigned flags
)
158 int err
= -ERESTARTSYS
;
160 if (flags
& MSG_DONTWAIT
)
163 set_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
);
166 if (signal_pending(current
))
168 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
169 timeout
= MAX_SCHEDULE_TIMEOUT
;
170 if (sk_wait_event(sk
, &timeout
, skcipher_writable(sk
))) {
175 finish_wait(sk_sleep(sk
), &wait
);
180 static void skcipher_wmem_wakeup(struct sock
*sk
)
182 struct socket_wq
*wq
;
184 if (!skcipher_writable(sk
))
188 wq
= rcu_dereference(sk
->sk_wq
);
189 if (wq_has_sleeper(wq
))
190 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
193 sk_wake_async(sk
, SOCK_WAKE_WAITD
, POLL_IN
);
197 static int skcipher_wait_for_data(struct sock
*sk
, unsigned flags
)
199 struct alg_sock
*ask
= alg_sk(sk
);
200 struct skcipher_ctx
*ctx
= ask
->private;
203 int err
= -ERESTARTSYS
;
205 if (flags
& MSG_DONTWAIT
) {
209 set_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
212 if (signal_pending(current
))
214 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
215 timeout
= MAX_SCHEDULE_TIMEOUT
;
216 if (sk_wait_event(sk
, &timeout
, ctx
->used
)) {
221 finish_wait(sk_sleep(sk
), &wait
);
223 clear_bit(SOCK_ASYNC_WAITDATA
, &sk
->sk_socket
->flags
);
228 static void skcipher_data_wakeup(struct sock
*sk
)
230 struct alg_sock
*ask
= alg_sk(sk
);
231 struct skcipher_ctx
*ctx
= ask
->private;
232 struct socket_wq
*wq
;
238 wq
= rcu_dereference(sk
->sk_wq
);
239 if (wq_has_sleeper(wq
))
240 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
243 sk_wake_async(sk
, SOCK_WAKE_SPACE
, POLL_OUT
);
247 static int skcipher_sendmsg(struct kiocb
*unused
, struct socket
*sock
,
248 struct msghdr
*msg
, size_t size
)
250 struct sock
*sk
= sock
->sk
;
251 struct alg_sock
*ask
= alg_sk(sk
);
252 struct skcipher_ctx
*ctx
= ask
->private;
253 struct crypto_ablkcipher
*tfm
= crypto_ablkcipher_reqtfm(&ctx
->req
);
254 unsigned ivsize
= crypto_ablkcipher_ivsize(tfm
);
255 struct skcipher_sg_list
*sgl
;
256 struct af_alg_control con
= {};
262 if (msg
->msg_controllen
) {
263 err
= af_alg_cmsg_send(msg
, &con
);
278 if (con
.iv
&& con
.iv
->ivlen
!= ivsize
)
285 if (!ctx
->more
&& ctx
->used
)
291 memcpy(ctx
->iv
, con
.iv
->iv
, ivsize
);
295 struct scatterlist
*sg
;
296 unsigned long len
= size
;
300 sgl
= list_entry(ctx
->tsgl
.prev
,
301 struct skcipher_sg_list
, list
);
302 sg
= sgl
->sg
+ sgl
->cur
- 1;
303 len
= min_t(unsigned long, len
,
304 PAGE_SIZE
- sg
->offset
- sg
->length
);
306 err
= memcpy_fromiovec(page_address(sg_page(sg
)) +
307 sg
->offset
+ sg
->length
,
313 ctx
->merge
= (sg
->offset
+ sg
->length
) &
322 if (!skcipher_writable(sk
)) {
323 err
= skcipher_wait_for_wmem(sk
, msg
->msg_flags
);
328 len
= min_t(unsigned long, len
, skcipher_sndbuf(sk
));
330 err
= skcipher_alloc_sgl(sk
);
334 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
338 plen
= min_t(int, len
, PAGE_SIZE
);
340 sg_assign_page(sg
+ i
, alloc_page(GFP_KERNEL
));
342 if (!sg_page(sg
+ i
))
345 err
= memcpy_fromiovec(page_address(sg_page(sg
+ i
)),
348 __free_page(sg_page(sg
+ i
));
349 sg_assign_page(sg
+ i
, NULL
);
359 } while (len
&& sgl
->cur
< MAX_SGL_ENTS
);
361 ctx
->merge
= plen
& (PAGE_SIZE
- 1);
366 ctx
->more
= msg
->msg_flags
& MSG_MORE
;
367 if (!ctx
->more
&& !list_empty(&ctx
->tsgl
))
368 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
371 skcipher_data_wakeup(sk
);
374 return copied
?: err
;
377 static ssize_t
skcipher_sendpage(struct socket
*sock
, struct page
*page
,
378 int offset
, size_t size
, int flags
)
380 struct sock
*sk
= sock
->sk
;
381 struct alg_sock
*ask
= alg_sk(sk
);
382 struct skcipher_ctx
*ctx
= ask
->private;
383 struct skcipher_sg_list
*sgl
;
386 if (flags
& MSG_SENDPAGE_NOTLAST
)
390 if (!ctx
->more
&& ctx
->used
)
396 if (!skcipher_writable(sk
)) {
397 err
= skcipher_wait_for_wmem(sk
, flags
);
402 err
= skcipher_alloc_sgl(sk
);
407 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
410 sg_set_page(sgl
->sg
+ sgl
->cur
, page
, size
, offset
);
415 ctx
->more
= flags
& MSG_MORE
;
416 if (!ctx
->more
&& !list_empty(&ctx
->tsgl
))
417 sgl
= list_entry(ctx
->tsgl
.prev
, struct skcipher_sg_list
, list
);
420 skcipher_data_wakeup(sk
);
426 static int skcipher_recvmsg(struct kiocb
*unused
, struct socket
*sock
,
427 struct msghdr
*msg
, size_t ignored
, int flags
)
429 struct sock
*sk
= sock
->sk
;
430 struct alg_sock
*ask
= alg_sk(sk
);
431 struct skcipher_ctx
*ctx
= ask
->private;
432 unsigned bs
= crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(
434 struct skcipher_sg_list
*sgl
;
435 struct scatterlist
*sg
;
436 unsigned long iovlen
;
443 for (iov
= msg
->msg_iov
, iovlen
= msg
->msg_iovlen
; iovlen
> 0;
445 unsigned long seglen
= iov
->iov_len
;
446 char __user
*from
= iov
->iov_base
;
451 err
= skcipher_wait_for_data(sk
, flags
);
456 used
= min_t(unsigned long, used
, seglen
);
458 used
= af_alg_make_sg(&ctx
->rsgl
, from
, used
, 1);
463 if (ctx
->more
|| used
< ctx
->used
)
470 sgl
= list_first_entry(&ctx
->tsgl
,
471 struct skcipher_sg_list
, list
);
477 ablkcipher_request_set_crypt(&ctx
->req
, sg
,
481 err
= af_alg_wait_for_completion(
483 crypto_ablkcipher_encrypt(&ctx
->req
) :
484 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 int skcipher_check_key(struct socket
*sock
)
556 struct alg_sock
*pask
;
557 struct skcipher_tfm
*tfm
;
558 struct sock
*sk
= sock
->sk
;
559 struct alg_sock
*ask
= alg_sk(sk
);
566 pask
= alg_sk(ask
->parent
);
570 lock_sock_nested(psk
, SINGLE_DEPTH_NESTING
);
590 static int skcipher_sendmsg_nokey(struct kiocb
*unused
, struct socket
*sock
,
591 struct msghdr
*msg
, size_t size
)
595 err
= skcipher_check_key(sock
);
599 return skcipher_sendmsg(unused
, sock
, msg
, size
);
602 static ssize_t
skcipher_sendpage_nokey(struct socket
*sock
, struct page
*page
,
603 int offset
, size_t size
, int flags
)
607 err
= skcipher_check_key(sock
);
611 return skcipher_sendpage(sock
, page
, offset
, size
, flags
);
614 static int skcipher_recvmsg_nokey(struct kiocb
*unused
, struct socket
*sock
,
615 struct msghdr
*msg
, size_t ignored
, int flags
)
619 err
= skcipher_check_key(sock
);
623 return skcipher_recvmsg(unused
, sock
, msg
, ignored
, flags
);
626 static struct proto_ops algif_skcipher_ops_nokey
= {
629 .connect
= sock_no_connect
,
630 .socketpair
= sock_no_socketpair
,
631 .getname
= sock_no_getname
,
632 .ioctl
= sock_no_ioctl
,
633 .listen
= sock_no_listen
,
634 .shutdown
= sock_no_shutdown
,
635 .getsockopt
= sock_no_getsockopt
,
636 .mmap
= sock_no_mmap
,
637 .bind
= sock_no_bind
,
638 .accept
= sock_no_accept
,
639 .setsockopt
= sock_no_setsockopt
,
641 .release
= af_alg_release
,
642 .sendmsg
= skcipher_sendmsg_nokey
,
643 .sendpage
= skcipher_sendpage_nokey
,
644 .recvmsg
= skcipher_recvmsg_nokey
,
645 .poll
= skcipher_poll
,
648 static void *skcipher_bind(const char *name
, u32 type
, u32 mask
)
650 struct skcipher_tfm
*tfm
;
651 struct crypto_ablkcipher
*skcipher
;
653 tfm
= kzalloc(sizeof(*tfm
), GFP_KERNEL
);
655 return ERR_PTR(-ENOMEM
);
657 skcipher
= crypto_alloc_ablkcipher(name
, type
, mask
);
658 if (IS_ERR(skcipher
)) {
660 return ERR_CAST(skcipher
);
663 tfm
->skcipher
= skcipher
;
668 static void skcipher_release(void *private)
670 struct skcipher_tfm
*tfm
= private;
672 crypto_free_ablkcipher(tfm
->skcipher
);
676 static int skcipher_setkey(void *private, const u8
*key
, unsigned int keylen
)
678 struct skcipher_tfm
*tfm
= private;
681 err
= crypto_ablkcipher_setkey(tfm
->skcipher
, key
, keylen
);
687 static void skcipher_sock_destruct(struct sock
*sk
)
689 struct alg_sock
*ask
= alg_sk(sk
);
690 struct skcipher_ctx
*ctx
= ask
->private;
691 struct crypto_ablkcipher
*tfm
= crypto_ablkcipher_reqtfm(&ctx
->req
);
693 skcipher_free_sgl(sk
);
694 sock_kfree_s(sk
, ctx
->iv
, crypto_ablkcipher_ivsize(tfm
));
695 sock_kfree_s(sk
, ctx
, ctx
->len
);
696 af_alg_release_parent(sk
);
699 static int skcipher_accept_parent_nokey(void *private, struct sock
*sk
)
701 struct skcipher_ctx
*ctx
;
702 struct alg_sock
*ask
= alg_sk(sk
);
703 struct skcipher_tfm
*tfm
= private;
704 struct crypto_ablkcipher
*skcipher
= tfm
->skcipher
;
705 unsigned int len
= sizeof(*ctx
) + crypto_ablkcipher_reqsize(skcipher
);
707 ctx
= sock_kmalloc(sk
, len
, GFP_KERNEL
);
711 ctx
->iv
= sock_kmalloc(sk
, crypto_ablkcipher_ivsize(skcipher
),
714 sock_kfree_s(sk
, ctx
, len
);
718 memset(ctx
->iv
, 0, crypto_ablkcipher_ivsize(skcipher
));
720 INIT_LIST_HEAD(&ctx
->tsgl
);
726 af_alg_init_completion(&ctx
->completion
);
730 ablkcipher_request_set_tfm(&ctx
->req
, skcipher
);
731 ablkcipher_request_set_callback(&ctx
->req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
732 af_alg_complete
, &ctx
->completion
);
734 sk
->sk_destruct
= skcipher_sock_destruct
;
739 static int skcipher_accept_parent(void *private, struct sock
*sk
)
741 struct skcipher_tfm
*tfm
= private;
743 if (!tfm
->has_key
&& crypto_ablkcipher_has_setkey(tfm
->skcipher
))
746 return skcipher_accept_parent_nokey(private, sk
);
749 static const struct af_alg_type algif_type_skcipher
= {
750 .bind
= skcipher_bind
,
751 .release
= skcipher_release
,
752 .setkey
= skcipher_setkey
,
753 .accept
= skcipher_accept_parent
,
754 .accept_nokey
= skcipher_accept_parent_nokey
,
755 .ops
= &algif_skcipher_ops
,
756 .ops_nokey
= &algif_skcipher_ops_nokey
,
761 static int __init
algif_skcipher_init(void)
763 return af_alg_register_type(&algif_type_skcipher
);
766 static void __exit
algif_skcipher_exit(void)
768 int err
= af_alg_unregister_type(&algif_type_skcipher
);
772 module_init(algif_skcipher_init
);
773 module_exit(algif_skcipher_exit
);
774 MODULE_LICENSE("GPL");