19 #include <linux/list.h>
20 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/net.h>
52 #define MAX_SGL_ENTS ((PAGE_SIZE - sizeof(struct skcipher_sg_list)) / \
53 sizeof(struct scatterlist) - 1)
55 static inline int skcipher_sndbuf(
struct sock *
sk)
64 static inline bool skcipher_writable(
struct sock *
sk)
69 static int skcipher_alloc_sgl(
struct sock *
sk)
77 if (!list_empty(&ctx->
tsgl))
99 static void skcipher_pull_sgl(
struct sock *sk,
int used)
107 while (!list_empty(&ctx->
tsgl)) {
112 for (i = 0; i < sgl->
cur; i++) {
115 if (!sg_page(sg + i))
128 sg_assign_page(sg + i,
NULL);
133 sizeof(*sgl) +
sizeof(sgl->
sg[0]) *
141 static void skcipher_free_sgl(
struct sock *sk)
146 skcipher_pull_sgl(sk, ctx->
used);
149 static int skcipher_wait_for_wmem(
struct sock *sk,
unsigned flags)
175 static void skcipher_wmem_wakeup(
struct sock *sk)
179 if (!skcipher_writable(sk))
184 if (wq_has_sleeper(wq))
192 static int skcipher_wait_for_data(
struct sock *sk,
unsigned flags)
200 if (flags & MSG_DONTWAIT) {
223 static void skcipher_data_wakeup(
struct sock *sk)
234 if (wq_has_sleeper(wq))
245 struct sock *sk = sock->
sk;
249 unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
273 if (con.
iv && con.
iv->ivlen != ivsize)
297 sg = sgl->
sg + sgl->
cur - 1;
298 len =
min_t(
unsigned long, len,
317 if (!skcipher_writable(sk)) {
318 err = skcipher_wait_for_wmem(sk, msg->
msg_flags);
323 len =
min_t(
unsigned long, len, skcipher_sndbuf(sk));
325 err = skcipher_alloc_sgl(sk);
337 if (!sg_page(sg + i))
344 sg_assign_page(sg + i,
NULL);
362 if (!ctx->
more && !list_empty(&ctx->
tsgl))
366 skcipher_data_wakeup(sk);
369 return copied ?:
err;
373 int offset,
size_t size,
int flags)
375 struct sock *sk = sock->
sk;
388 if (!skcipher_writable(sk)) {
389 err = skcipher_wait_for_wmem(sk, flags);
394 err = skcipher_alloc_sgl(sk);
402 sg_set_page(sgl->
sg + sgl->
cur, page, size, offset);
408 if (!ctx->
more && !list_empty(&ctx->
tsgl))
412 skcipher_data_wakeup(sk);
418 static int skcipher_recvmsg(
struct kiocb *unused,
struct socket *sock,
421 struct sock *sk = sock->
sk;
424 unsigned bs = crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(
428 unsigned long iovlen;
437 unsigned long seglen = iov->
iov_len;
450 err = skcipher_wait_for_data(sk, flags);
455 used =
min_t(
unsigned long, used, seglen);
462 if (ctx->
more || used < ctx->used)
469 ablkcipher_request_set_crypt(&ctx->
req, sg,
475 crypto_ablkcipher_encrypt(&ctx->
req) :
476 crypto_ablkcipher_decrypt(&ctx->
req),
488 skcipher_pull_sgl(sk, used);
495 skcipher_wmem_wakeup(sk);
498 return copied ?:
err;
502 static unsigned int skcipher_poll(
struct file *
file,
struct socket *sock,
505 struct sock *sk = sock->
sk;
510 sock_poll_wait(file, sk_sleep(sk), wait);
516 if (skcipher_writable(sk))
522 static struct proto_ops algif_skcipher_ops = {
538 .sendmsg = skcipher_sendmsg,
539 .sendpage = skcipher_sendpage,
540 .recvmsg = skcipher_recvmsg,
541 .poll = skcipher_poll,
549 static void skcipher_release(
void *
private)
551 crypto_free_ablkcipher(
private);
554 static int skcipher_setkey(
void *
private,
const u8 *
key,
unsigned int keylen)
556 return crypto_ablkcipher_setkey(
private, key, keylen);
559 static void skcipher_sock_destruct(
struct sock *sk)
565 skcipher_free_sgl(sk);
568 af_alg_release_parent(sk);
571 static int skcipher_accept_parent(
void *
private,
struct sock *sk)
575 unsigned int len =
sizeof(*ctx) + crypto_ablkcipher_reqsize(
private);
588 memset(ctx->
iv, 0, crypto_ablkcipher_ivsize(
private));
590 INIT_LIST_HEAD(&ctx->
tsgl);
600 ablkcipher_request_set_tfm(&ctx->
req,
private);
609 static const struct af_alg_type algif_type_skcipher = {
610 .bind = skcipher_bind,
611 .release = skcipher_release,
612 .setkey = skcipher_setkey,
613 .accept = skcipher_accept_parent,
614 .ops = &algif_skcipher_ops,
619 static int __init algif_skcipher_init(
void)
624 static void __exit algif_skcipher_exit(
void)