Debian Codegraph @

kernel-patches/4.15-rc3/asym/v10-0003-crypto-AF_ALG-add-asymmetric-cipher.patch

raw
1 From 376b0c4f625df9c1326c994ae8e3ce7ff74a9456 Mon Sep 17 00:00:00 2001
2 From: Stephan Mueller <[email protected]>
3 Date: Sun, 7 Jan 2018 18:14:21 +0100
4 Subject: [PATCH v10 3/3] crypto: AF_ALG -- add asymmetric cipher
5
6 This patch adds the user space interface for asymmetric ciphers. The
7 interface allows the use of sendmsg as well as vmsplice to provide data.
8
9 The akcipher interface implementation uses the common AF_ALG interface
10 code regarding TX and RX SGL handling.
11
12 Signed-off-by: Stephan Mueller <[email protected]>
13 ---
14 crypto/Kconfig | 9 +
15 crypto/Makefile | 1 +
16 crypto/algif_akcipher.c | 474 ++++++++++++++++++++++++++++++++++++++++++++++++
17 include/crypto/if_alg.h | 2 +
18 4 files changed, 486 insertions(+)
19 create mode 100644 crypto/algif_akcipher.c
20
21 diff --git a/crypto/Kconfig b/crypto/Kconfig
22 index 9327fbfccf5a..1785679b27f8 100644
23 --- a/crypto/Kconfig
24 +++ b/crypto/Kconfig
25 @@ -1772,6 +1772,15 @@ config CRYPTO_USER_API_AEAD
26 This option enables the user-spaces interface for AEAD
27 cipher algorithms.
28
29 +config CRYPTO_USER_API_AKCIPHER
30 + tristate "User-space interface for asymmetric key cipher algorithms"
31 + depends on NET
32 + select CRYPTO_AKCIPHER2
33 + select CRYPTO_USER_API
34 + help
35 + This option enables the user-space interface for asymmetric
36 + key cipher algorithms.
37 +
38 config CRYPTO_HASH_INFO
39 bool
40
41 diff --git a/crypto/Makefile b/crypto/Makefile
42 index d674884b2d51..526a4e5336af 100644
43 --- a/crypto/Makefile
44 +++ b/crypto/Makefile
45 @@ -135,6 +135,7 @@ obj-$(CONFIG_CRYPTO_USER_API_HASH) += algif_hash.o
46 obj-$(CONFIG_CRYPTO_USER_API_SKCIPHER) += algif_skcipher.o
47 obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o
48 obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
49 +obj-$(CONFIG_CRYPTO_USER_API_AKCIPHER) += algif_akcipher.o
50
51 ecdh_generic-y := ecc.o
52 ecdh_generic-y += ecdh.o
53 diff --git a/crypto/algif_akcipher.c b/crypto/algif_akcipher.c
54 new file mode 100644
55 index 000000000000..f7d7582eef67
56 --- /dev/null
57 +++ b/crypto/algif_akcipher.c
58 @@ -0,0 +1,474 @@
59 +/*
60 + * algif_akcipher: User-space interface for asymmetric cipher algorithms
61 + *
62 + * Copyright (C) 2018 - 2022, Stephan Mueller <[email protected]>
63 + *
64 + * This file provides the user-space API for asymmetric ciphers.
65 + *
66 + * This program is free software; you can redistribute it and/or modify it
67 + * under the terms of the GNU General Public License as published by the Free
68 + * Software Foundation; either version 2 of the License, or (at your option)
69 + * any later version.
70 + *
71 + * The following concept of the memory management is used:
72 + *
73 + * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
74 + * filled by user space with the data submitted via sendpage/sendmsg. Filling
75 + * up the TX SGL does not cause a crypto operation -- the data will only be
76 + * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
77 + * provide a buffer which is tracked with the RX SGL.
78 + *
79 + * During the processing of the recvmsg operation, the cipher request is
80 + * allocated and prepared. As part of the recvmsg operation, the processed
81 + * TX buffers are extracted from the TX SGL into a separate SGL.
82 + *
83 + * After the completion of the crypto operation, the RX SGL and the cipher
84 + * request is released. The extracted TX SGL parts are released together with
85 + * the RX SGL release.
86 + */
87 +
88 +#include <crypto/akcipher.h>
89 +#include <crypto/if_alg.h>
90 +#include <crypto/scatterwalk.h>
91 +#include <linux/init.h>
92 +#include <linux/list.h>
93 +#include <linux/kernel.h>
94 +#include <linux/mm.h>
95 +#include <linux/module.h>
96 +#include <linux/net.h>
97 +#include <net/sock.h>
98 +
99 +struct akcipher_tfm {
100 + struct crypto_akcipher *akcipher;
101 + bool has_key;
102 +};
103 +
104 +static int akcipher_sendmsg(struct socket *sock, struct msghdr *msg,
105 + size_t size)
106 +{
107 + return af_alg_sendmsg(sock, msg, size, 0);
108 +}
109 +
110 +static inline int akcipher_cipher_op(struct af_alg_ctx *ctx,
111 + struct af_alg_async_req *areq)
112 +{
113 + switch (ctx->op) {
114 + case ALG_OP_ENCRYPT:
115 + return crypto_akcipher_encrypt(&areq->cra_u.akcipher_req);
116 + case ALG_OP_DECRYPT:
117 + return crypto_akcipher_decrypt(&areq->cra_u.akcipher_req);
118 + case ALG_OP_SIGN:
119 + return crypto_akcipher_sign(&areq->cra_u.akcipher_req);
120 + case ALG_OP_VERIFY:
121 + return crypto_akcipher_verify(&areq->cra_u.akcipher_req);
122 + default:
123 + return -EOPNOTSUPP;
124 + }
125 +}
126 +
127 +static int _akcipher_recvmsg(struct socket *sock, struct msghdr *msg,
128 + size_t ignored, int flags)
129 +{
130 + struct sock *sk = sock->sk;
131 + struct alg_sock *ask = alg_sk(sk);
132 + struct sock *psk = ask->parent;
133 + struct alg_sock *pask = alg_sk(psk);
134 + struct af_alg_ctx *ctx = ask->private;
135 + struct akcipher_tfm *akc = pask->private;
136 + struct crypto_akcipher *tfm = akc->akcipher;
137 + struct af_alg_async_req *areq;
138 + size_t len;
139 + size_t used;
140 + int err;
141 + int maxsize;
142 +
143 + if (!ctx->used) {
144 + err = af_alg_wait_for_data(sk, flags);
145 + if (err)
146 + return err;
147 + }
148 +
149 + maxsize = crypto_akcipher_maxsize(tfm);
150 + if (maxsize < 0)
151 + return maxsize;
152 +
153 + /* Allocate cipher request for current operation. */
154 + areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
155 + crypto_akcipher_reqsize(tfm));
156 + if (IS_ERR(areq))
157 + return PTR_ERR(areq);
158 +
159 + /* convert iovecs of output buffers into RX SGL */
160 + err = af_alg_get_rsgl(sk, msg, flags, areq, maxsize, &len);
161 + if (err)
162 + goto free;
163 +
164 + /* ensure output buffer is sufficiently large */
165 + if (len < maxsize) {
166 + err = -EMSGSIZE;
167 + goto free;
168 + }
169 +
170 + /*
171 + * Create a per request TX SGL for this request which tracks the
172 + * SG entries from the global TX SGL.
173 + */
174 + used = ctx->used;
175 + areq->tsgl_entries = af_alg_count_tsgl(sk, used, 0);
176 + if (!areq->tsgl_entries)
177 + areq->tsgl_entries = 1;
178 + areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * areq->tsgl_entries,
179 + GFP_KERNEL);
180 + if (!areq->tsgl) {
181 + err = -ENOMEM;
182 + goto free;
183 + }
184 + sg_init_table(areq->tsgl, areq->tsgl_entries);
185 + af_alg_pull_tsgl(sk, used, areq->tsgl, 0);
186 +
187 + /* Initialize the crypto operation */
188 + akcipher_request_set_tfm(&areq->cra_u.akcipher_req, tfm);
189 + akcipher_request_set_crypt(&areq->cra_u.akcipher_req, areq->tsgl,
190 + areq->first_rsgl.sgl.sg, used, len);
191 +
192 + if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
193 + /* AIO operation */
194 + sock_hold(sk);
195 + areq->iocb = msg->msg_iocb;
196 +
197 + /* Remember output size that will be generated. */
198 + areq->outlen = areq->cra_u.akcipher_req.dst_len ?
199 + areq->cra_u.akcipher_req.dst_len : len;
200 +
201 + akcipher_request_set_callback(&areq->cra_u.akcipher_req,
202 + CRYPTO_TFM_REQ_MAY_SLEEP,
203 + af_alg_async_cb, areq);
204 + err = akcipher_cipher_op(ctx, areq);
205 +
206 + /* AIO operation in progress */
207 + if (err == -EINPROGRESS || err == -EBUSY)
208 + return -EIOCBQUEUED;
209 +
210 + sock_put(sk);
211 + } else {
212 + /* Synchronous operation */
213 + akcipher_request_set_callback(&areq->cra_u.akcipher_req,
214 + CRYPTO_TFM_REQ_MAY_SLEEP |
215 + CRYPTO_TFM_REQ_MAY_BACKLOG,
216 + crypto_req_done,
217 + &ctx->wait);
218 + err = crypto_wait_req(akcipher_cipher_op(ctx, areq),
219 + &ctx->wait);
220 + }
221 +
222 +free:
223 + af_alg_free_resources(areq);
224 +
225 + return err ? err : areq->cra_u.akcipher_req.dst_len;
226 +}
227 +
228 +static int akcipher_recvmsg(struct socket *sock, struct msghdr *msg,
229 + size_t ignored, int flags)
230 +{
231 + struct sock *sk = sock->sk;
232 + struct alg_sock *ask = alg_sk(sk);
233 + struct sock *psk = ask->parent;
234 + struct alg_sock *pask = alg_sk(psk);
235 + struct akcipher_tfm *akc = pask->private;
236 + struct crypto_akcipher *tfm = akc->akcipher;
237 + int ret = 0;
238 + int err;
239 +
240 + lock_sock(sk);
241 +
242 + while (msg_data_left(msg)) {
243 + err = _akcipher_recvmsg(sock, msg, ignored, flags);
244 +
245 + /*
246 + * This error covers -EIOCBQUEUED which implies that we can
247 + * only handle one AIO request. If the caller wants to have
248 + * multiple AIO requests in parallel, he must make multiple
249 + * separate AIO calls.
250 + */
251 + if (err <= 0) {
252 + if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
253 + ret = err;
254 + goto out;
255 + }
256 +
257 + ret += err;
258 +
259 + /*
260 + * The caller must provide crypto_akcipher_maxsize per request.
261 + * If he provides more, we conclude that multiple akcipher
262 + * operations are requested.
263 + */
264 + iov_iter_advance(&msg->msg_iter,
265 + crypto_akcipher_maxsize(tfm) - err);
266 + }
267 +
268 +out:
269 + af_alg_wmem_wakeup(sk);
270 + release_sock(sk);
271 + return ret;
272 +}
273 +
274 +static struct proto_ops algif_akcipher_ops = {
275 + .family = PF_ALG,
276 +
277 + .connect = sock_no_connect,
278 + .socketpair = sock_no_socketpair,
279 + .getname = sock_no_getname,
280 + .ioctl = sock_no_ioctl,
281 + .listen = sock_no_listen,
282 + .shutdown = sock_no_shutdown,
283 + .getsockopt = sock_no_getsockopt,
284 + .mmap = sock_no_mmap,
285 + .bind = sock_no_bind,
286 + .accept = sock_no_accept,
287 + .setsockopt = sock_no_setsockopt,
288 +
289 + .release = af_alg_release,
290 + .sendmsg = akcipher_sendmsg,
291 + .sendpage = af_alg_sendpage,
292 + .recvmsg = akcipher_recvmsg,
293 + .poll = af_alg_poll,
294 +};
295 +
296 +static int akcipher_check_key(struct socket *sock)
297 +{
298 + struct sock *psk;
299 + struct alg_sock *pask;
300 + struct akcipher_tfm *tfm;
301 + struct sock *sk = sock->sk;
302 + struct alg_sock *ask = alg_sk(sk);
303 + int err = 0;
304 +
305 + lock_sock(sk);
306 + if (ask->refcnt)
307 + goto unlock_child;
308 +
309 + psk = ask->parent;
310 + pask = alg_sk(ask->parent);
311 + tfm = pask->private;
312 +
313 + lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
314 + if (!tfm->has_key) {
315 + err = -ENOKEY;
316 + goto unlock;
317 + }
318 +
319 + if (!pask->refcnt++)
320 + sock_hold(psk);
321 +
322 + ask->refcnt = 1;
323 + sock_put(psk);
324 +
325 + err = 0;
326 +
327 +unlock:
328 + release_sock(psk);
329 +unlock_child:
330 + release_sock(sk);
331 +
332 + return err;
333 +}
334 +
335 +static int akcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
336 + size_t size)
337 +{
338 + int err;
339 +
340 + err = akcipher_check_key(sock);
341 + if (err)
342 + return err;
343 +
344 + return akcipher_sendmsg(sock, msg, size);
345 +}
346 +
347 +static ssize_t akcipher_sendpage_nokey(struct socket *sock, struct page *page,
348 + int offset, size_t size, int flags)
349 +{
350 + int err;
351 +
352 + err = akcipher_check_key(sock);
353 + if (err)
354 + return err;
355 +
356 + return af_alg_sendpage(sock, page, offset, size, flags);
357 +}
358 +
359 +static int akcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
360 + size_t ignored, int flags)
361 +{
362 + int err;
363 +
364 + err = akcipher_check_key(sock);
365 + if (err)
366 + return err;
367 +
368 + return akcipher_recvmsg(sock, msg, ignored, flags);
369 +}
370 +
371 +static struct proto_ops algif_akcipher_ops_nokey = {
372 + .family = PF_ALG,
373 +
374 + .connect = sock_no_connect,
375 + .socketpair = sock_no_socketpair,
376 + .getname = sock_no_getname,
377 + .ioctl = sock_no_ioctl,
378 + .listen = sock_no_listen,
379 + .shutdown = sock_no_shutdown,
380 + .getsockopt = sock_no_getsockopt,
381 + .mmap = sock_no_mmap,
382 + .bind = sock_no_bind,
383 + .accept = sock_no_accept,
384 + .setsockopt = sock_no_setsockopt,
385 +
386 + .release = af_alg_release,
387 + .sendmsg = akcipher_sendmsg_nokey,
388 + .sendpage = akcipher_sendpage_nokey,
389 + .recvmsg = akcipher_recvmsg_nokey,
390 + .poll = af_alg_poll,
391 +};
392 +
393 +static void *akcipher_bind(const char *name, u32 type, u32 mask)
394 +{
395 + struct akcipher_tfm *tfm;
396 + struct crypto_akcipher *akcipher;
397 +
398 + tfm = kmalloc(sizeof(*tfm), GFP_KERNEL);
399 + if (!tfm)
400 + return ERR_PTR(-ENOMEM);
401 +
402 + akcipher = crypto_alloc_akcipher(name, type, mask);
403 + if (IS_ERR(akcipher)) {
404 + kfree(tfm);
405 + return ERR_CAST(akcipher);
406 + }
407 +
408 + tfm->akcipher = akcipher;
409 + tfm->has_key = false;
410 +
411 + return tfm;
412 +}
413 +
414 +static void akcipher_release(void *private)
415 +{
416 + struct akcipher_tfm *tfm = private;
417 + struct crypto_akcipher *akcipher = tfm->akcipher;
418 +
419 + crypto_free_akcipher(akcipher);
420 + kfree(tfm);
421 +}
422 +
423 +static int akcipher_setprivkey(void *private, const u8 *key,
424 + unsigned int keylen)
425 +{
426 + struct akcipher_tfm *tfm = private;
427 + struct crypto_akcipher *akcipher = tfm->akcipher;
428 + int err;
429 +
430 + err = crypto_akcipher_set_priv_key(akcipher, key, keylen);
431 + tfm->has_key = !err;
432 +
433 + /* Return the maximum size of the akcipher operation. */
434 + if (!err)
435 + err = crypto_akcipher_maxsize(akcipher);
436 +
437 + return err;
438 +}
439 +
440 +static int akcipher_setpubkey(void *private, const u8 *key, unsigned int keylen)
441 +{
442 + struct akcipher_tfm *tfm = private;
443 + struct crypto_akcipher *akcipher = tfm->akcipher;
444 + int err;
445 +
446 + err = crypto_akcipher_set_pub_key(akcipher, key, keylen);
447 + tfm->has_key = !err;
448 +
449 + /* Return the maximum size of the akcipher operation. */
450 + if (!err)
451 + err = crypto_akcipher_maxsize(akcipher);
452 +
453 + return err;
454 +}
455 +
456 +static void akcipher_sock_destruct(struct sock *sk)
457 +{
458 + struct alg_sock *ask = alg_sk(sk);
459 + struct af_alg_ctx *ctx = ask->private;
460 +
461 + af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
462 + sock_kfree_s(sk, ctx, ctx->len);
463 + af_alg_release_parent(sk);
464 +}
465 +
466 +static int akcipher_accept_parent_nokey(void *private, struct sock *sk)
467 +{
468 + struct af_alg_ctx *ctx;
469 + struct alg_sock *ask = alg_sk(sk);
470 + unsigned int len = sizeof(*ctx);
471 +
472 + ctx = sock_kmalloc(sk, len, GFP_KERNEL);
473 + if (!ctx)
474 + return -ENOMEM;
475 +
476 + INIT_LIST_HEAD(&ctx->tsgl_list);
477 + ctx->len = len;
478 + ctx->used = 0;
479 + atomic_set(&ctx->rcvused, 0);
480 + ctx->more = 0;
481 + ctx->merge = 0;
482 + ctx->op = 0;
483 + crypto_init_wait(&ctx->wait);
484 +
485 + ask->private = ctx;
486 +
487 + sk->sk_destruct = akcipher_sock_destruct;
488 +
489 + return 0;
490 +}
491 +
492 +static int akcipher_accept_parent(void *private, struct sock *sk)
493 +{
494 + struct akcipher_tfm *tfm = private;
495 +
496 + if (!tfm->has_key)
497 + return -ENOKEY;
498 +
499 + return akcipher_accept_parent_nokey(private, sk);
500 +}
501 +
502 +static const struct af_alg_type algif_type_akcipher = {
503 + .bind = akcipher_bind,
504 + .release = akcipher_release,
505 + .setkey = akcipher_setprivkey,
506 + .setpubkey = akcipher_setpubkey,
507 + .setauthsize = NULL,
508 + .accept = akcipher_accept_parent,
509 + .accept_nokey = akcipher_accept_parent_nokey,
510 + .ops = &algif_akcipher_ops,
511 + .ops_nokey = &algif_akcipher_ops_nokey,
512 + .name = "akcipher",
513 + .owner = THIS_MODULE
514 +};
515 +
516 +static int __init algif_akcipher_init(void)
517 +{
518 + return af_alg_register_type(&algif_type_akcipher);
519 +}
520 +
521 +static void __exit algif_akcipher_exit(void)
522 +{
523 + int err = af_alg_unregister_type(&algif_type_akcipher);
524 +
525 + BUG_ON(err);
526 +}
527 +
528 +module_init(algif_akcipher_init);
529 +module_exit(algif_akcipher_exit);
530 +MODULE_LICENSE("GPL");
531 +MODULE_AUTHOR("Stephan Mueller <[email protected]>");
532 +MODULE_DESCRIPTION("Asymmetric kernel crypto API user space interface");
533 diff --git a/include/crypto/if_alg.h b/include/crypto/if_alg.h
534 index cc1d3eecd556..79c5010a713b 100644
535 --- a/include/crypto/if_alg.h
536 +++ b/include/crypto/if_alg.h
537 @@ -23,6 +23,7 @@
538
539 #include <crypto/aead.h>
540 #include <crypto/skcipher.h>
541 +#include <crypto/akcipher.h>
542
543 #define ALG_MAX_PAGES 16
544
545 @@ -115,6 +116,7 @@ struct af_alg_async_req {
546 union {
547 struct aead_request aead_req;
548 struct skcipher_request skcipher_req;
549 + struct akcipher_request akcipher_req;
550 } cra_u;
551
552 /* req ctx trails this struct */
553 --
554 2.14.3
555