Debian Codegraph @

kernel-patches/4.14-rc1/asym/v9-0003-crypto-AF_ALG-add-asymmetric-cipher.patch

raw
1 From 7c604d30f77ff4634497c027922e55a8e9752142 Mon Sep 17 00:00:00 2001
2 From: Stephan Mueller <[email protected]>
3 Date: Thu, 28 Sep 2017 16:27:58 +0200
4 Subject: [PATCH v9 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 | 470 ++++++++++++++++++++++++++++++++++++++++++++++++
17 include/crypto/if_alg.h | 2 +
18 4 files changed, 482 insertions(+)
19 create mode 100644 crypto/algif_akcipher.c
20
21 diff --git a/crypto/Kconfig b/crypto/Kconfig
22 index 97fb7360207b..4975da52ce23 100644
23 --- a/crypto/Kconfig
24 +++ b/crypto/Kconfig
25 @@ -1771,6 +1771,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 723b0b41dab9..b9b86934fd3e 100644
43 --- a/crypto/Makefile
44 +++ b/crypto/Makefile
45 @@ -134,6 +134,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..9fb56117e5ed
56 --- /dev/null
57 +++ b/crypto/algif_akcipher.c
58 @@ -0,0 +1,470 @@
59 +/*
60 + * algif_akcipher: User-space interface for asymmetric cipher algorithms
61 + *
62 + * Copyright (C) 2017 - 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 int _akcipher_recvmsg(struct socket *sock, struct msghdr *msg,
111 + size_t ignored, int flags)
112 +{
113 + struct sock *sk = sock->sk;
114 + struct alg_sock *ask = alg_sk(sk);
115 + struct sock *psk = ask->parent;
116 + struct alg_sock *pask = alg_sk(psk);
117 + struct af_alg_ctx *ctx = ask->private;
118 + struct akcipher_tfm *akc = pask->private;
119 + struct crypto_akcipher *tfm = akc->akcipher;
120 + struct af_alg_async_req *areq;
121 + size_t len;
122 + size_t used;
123 + int err;
124 + int maxsize;
125 +
126 + maxsize = crypto_akcipher_maxsize(tfm);
127 + if (maxsize < 0)
128 + return maxsize;
129 +
130 + /* Allocate cipher request for current operation. */
131 + areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
132 + crypto_akcipher_reqsize(tfm));
133 + if (IS_ERR(areq))
134 + return PTR_ERR(areq);
135 +
136 + /* convert iovecs of output buffers into RX SGL */
137 + err = af_alg_get_rsgl(sk, msg, flags, areq, maxsize, &len);
138 + if (err)
139 + goto free;
140 +
141 + /* ensure output buffer is sufficiently large */
142 + if (len < maxsize) {
143 + err = -EMSGSIZE;
144 + goto free;
145 + }
146 +
147 + /*
148 + * Create a per request TX SGL for this request which tracks the
149 + * SG entries from the global TX SGL.
150 + */
151 + used = ctx->used;
152 + areq->tsgl_entries = af_alg_count_tsgl(sk, used, 0);
153 + if (!areq->tsgl_entries)
154 + areq->tsgl_entries = 1;
155 + areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * areq->tsgl_entries,
156 + GFP_KERNEL);
157 + if (!areq->tsgl) {
158 + err = -ENOMEM;
159 + goto free;
160 + }
161 + sg_init_table(areq->tsgl, areq->tsgl_entries);
162 + af_alg_pull_tsgl(sk, used, areq->tsgl, 0);
163 +
164 + /* Initialize the crypto operation */
165 + akcipher_request_set_tfm(&areq->cra_u.akcipher_req, tfm);
166 + akcipher_request_set_crypt(&areq->cra_u.akcipher_req, areq->tsgl,
167 + areq->first_rsgl.sgl.sg, used, len);
168 +
169 + if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
170 + /* AIO operation */
171 + areq->iocb = msg->msg_iocb;
172 + akcipher_request_set_callback(&areq->cra_u.akcipher_req,
173 + CRYPTO_TFM_REQ_MAY_SLEEP,
174 + af_alg_async_cb, areq);
175 + } else {
176 + /* Synchronous operation */
177 + akcipher_request_set_callback(&areq->cra_u.akcipher_req,
178 + CRYPTO_TFM_REQ_MAY_SLEEP |
179 + CRYPTO_TFM_REQ_MAY_BACKLOG,
180 + af_alg_complete,
181 + &ctx->completion);
182 + }
183 +
184 + switch (ctx->op) {
185 + case ALG_OP_ENCRYPT:
186 + err = crypto_akcipher_encrypt(&areq->cra_u.akcipher_req);
187 + break;
188 + case ALG_OP_DECRYPT:
189 + err = crypto_akcipher_decrypt(&areq->cra_u.akcipher_req);
190 + break;
191 + case ALG_OP_SIGN:
192 + err = crypto_akcipher_sign(&areq->cra_u.akcipher_req);
193 + break;
194 + case ALG_OP_VERIFY:
195 + err = crypto_akcipher_verify(&areq->cra_u.akcipher_req);
196 + break;
197 + default:
198 + err = -EOPNOTSUPP;
199 + goto free;
200 + }
201 +
202 + /* Wait for synchronous operation completion */
203 + if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb))
204 + err = af_alg_wait_for_completion(err, &ctx->completion);
205 +
206 + /* AIO operation in progress */
207 + if (err == -EINPROGRESS) {
208 + sock_hold(sk);
209 +
210 + /* Remember output size that will be generated. */
211 + areq->outlen = areq->cra_u.akcipher_req.dst_len ?
212 + areq->cra_u.akcipher_req.dst_len : len;
213 +
214 + return -EIOCBQUEUED;
215 + }
216 +
217 +free:
218 + af_alg_free_areq_sgls(areq);
219 + sock_kfree_s(sk, areq, areq->areqlen);
220 +
221 + return err ? err : areq->cra_u.akcipher_req.dst_len;
222 +}
223 +
224 +static int akcipher_recvmsg(struct socket *sock, struct msghdr *msg,
225 + size_t ignored, int flags)
226 +{
227 + struct sock *sk = sock->sk;
228 + struct alg_sock *ask = alg_sk(sk);
229 + struct sock *psk = ask->parent;
230 + struct alg_sock *pask = alg_sk(psk);
231 + struct akcipher_tfm *akc = pask->private;
232 + struct crypto_akcipher *tfm = akc->akcipher;
233 + int ret = 0;
234 + int err;
235 +
236 + lock_sock(sk);
237 +
238 + while (msg_data_left(msg)) {
239 + err = _akcipher_recvmsg(sock, msg, ignored, flags);
240 +
241 + /*
242 + * This error covers -EIOCBQUEUED which implies that we can
243 + * only handle one AIO request. If the caller wants to have
244 + * multiple AIO requests in parallel, he must make multiple
245 + * separate AIO calls.
246 + */
247 + if (err <= 0) {
248 + if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
249 + ret = err;
250 + goto out;
251 + }
252 +
253 + ret += err;
254 +
255 + /*
256 + * The caller must provide crypto_akcipher_maxsize per request.
257 + * If he provides more, we conclude that multiple akcipher
258 + * operations are requested.
259 + */
260 + iov_iter_advance(&msg->msg_iter,
261 + crypto_akcipher_maxsize(tfm) - err);
262 + }
263 +
264 +out:
265 + af_alg_wmem_wakeup(sk);
266 + release_sock(sk);
267 + return ret;
268 +}
269 +
270 +static struct proto_ops algif_akcipher_ops = {
271 + .family = PF_ALG,
272 +
273 + .connect = sock_no_connect,
274 + .socketpair = sock_no_socketpair,
275 + .getname = sock_no_getname,
276 + .ioctl = sock_no_ioctl,
277 + .listen = sock_no_listen,
278 + .shutdown = sock_no_shutdown,
279 + .getsockopt = sock_no_getsockopt,
280 + .mmap = sock_no_mmap,
281 + .bind = sock_no_bind,
282 + .accept = sock_no_accept,
283 + .setsockopt = sock_no_setsockopt,
284 +
285 + .release = af_alg_release,
286 + .sendmsg = akcipher_sendmsg,
287 + .sendpage = af_alg_sendpage,
288 + .recvmsg = akcipher_recvmsg,
289 + .poll = af_alg_poll,
290 +};
291 +
292 +static int akcipher_check_key(struct socket *sock)
293 +{
294 + struct sock *psk;
295 + struct alg_sock *pask;
296 + struct akcipher_tfm *tfm;
297 + struct sock *sk = sock->sk;
298 + struct alg_sock *ask = alg_sk(sk);
299 + int err = 0;
300 +
301 + lock_sock(sk);
302 + if (ask->refcnt)
303 + goto unlock_child;
304 +
305 + psk = ask->parent;
306 + pask = alg_sk(ask->parent);
307 + tfm = pask->private;
308 +
309 + lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
310 + if (!tfm->has_key) {
311 + err = -ENOKEY;
312 + goto unlock;
313 + }
314 +
315 + if (!pask->refcnt++)
316 + sock_hold(psk);
317 +
318 + ask->refcnt = 1;
319 + sock_put(psk);
320 +
321 + err = 0;
322 +
323 +unlock:
324 + release_sock(psk);
325 +unlock_child:
326 + release_sock(sk);
327 +
328 + return err;
329 +}
330 +
331 +static int akcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
332 + size_t size)
333 +{
334 + int err;
335 +
336 + err = akcipher_check_key(sock);
337 + if (err)
338 + return err;
339 +
340 + return akcipher_sendmsg(sock, msg, size);
341 +}
342 +
343 +static ssize_t akcipher_sendpage_nokey(struct socket *sock, struct page *page,
344 + int offset, size_t size, int flags)
345 +{
346 + int err;
347 +
348 + err = akcipher_check_key(sock);
349 + if (err)
350 + return err;
351 +
352 + return af_alg_sendpage(sock, page, offset, size, flags);
353 +}
354 +
355 +static int akcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
356 + size_t ignored, int flags)
357 +{
358 + int err;
359 +
360 + err = akcipher_check_key(sock);
361 + if (err)
362 + return err;
363 +
364 + return akcipher_recvmsg(sock, msg, ignored, flags);
365 +}
366 +
367 +static struct proto_ops algif_akcipher_ops_nokey = {
368 + .family = PF_ALG,
369 +
370 + .connect = sock_no_connect,
371 + .socketpair = sock_no_socketpair,
372 + .getname = sock_no_getname,
373 + .ioctl = sock_no_ioctl,
374 + .listen = sock_no_listen,
375 + .shutdown = sock_no_shutdown,
376 + .getsockopt = sock_no_getsockopt,
377 + .mmap = sock_no_mmap,
378 + .bind = sock_no_bind,
379 + .accept = sock_no_accept,
380 + .setsockopt = sock_no_setsockopt,
381 +
382 + .release = af_alg_release,
383 + .sendmsg = akcipher_sendmsg_nokey,
384 + .sendpage = akcipher_sendpage_nokey,
385 + .recvmsg = akcipher_recvmsg_nokey,
386 + .poll = af_alg_poll,
387 +};
388 +
389 +static void *akcipher_bind(const char *name, u32 type, u32 mask)
390 +{
391 + struct akcipher_tfm *tfm;
392 + struct crypto_akcipher *akcipher;
393 +
394 + tfm = kmalloc(sizeof(*tfm), GFP_KERNEL);
395 + if (!tfm)
396 + return ERR_PTR(-ENOMEM);
397 +
398 + akcipher = crypto_alloc_akcipher(name, type, mask);
399 + if (IS_ERR(akcipher)) {
400 + kfree(tfm);
401 + return ERR_CAST(akcipher);
402 + }
403 +
404 + tfm->akcipher = akcipher;
405 + tfm->has_key = false;
406 +
407 + return tfm;
408 +}
409 +
410 +static void akcipher_release(void *private)
411 +{
412 + struct akcipher_tfm *tfm = private;
413 + struct crypto_akcipher *akcipher = tfm->akcipher;
414 +
415 + crypto_free_akcipher(akcipher);
416 + kfree(tfm);
417 +}
418 +
419 +static int akcipher_setprivkey(void *private, const u8 *key,
420 + unsigned int keylen)
421 +{
422 + struct akcipher_tfm *tfm = private;
423 + struct crypto_akcipher *akcipher = tfm->akcipher;
424 + int err;
425 +
426 + err = crypto_akcipher_set_priv_key(akcipher, key, keylen);
427 + tfm->has_key = !err;
428 +
429 + /* Return the maximum size of the akcipher operation. */
430 + if (!err)
431 + err = crypto_akcipher_maxsize(akcipher);
432 +
433 + return err;
434 +}
435 +
436 +static int akcipher_setpubkey(void *private, const u8 *key, unsigned int keylen)
437 +{
438 + struct akcipher_tfm *tfm = private;
439 + struct crypto_akcipher *akcipher = tfm->akcipher;
440 + int err;
441 +
442 + err = crypto_akcipher_set_pub_key(akcipher, key, keylen);
443 + tfm->has_key = !err;
444 +
445 + /* Return the maximum size of the akcipher operation. */
446 + if (!err)
447 + err = crypto_akcipher_maxsize(akcipher);
448 +
449 + return err;
450 +}
451 +
452 +static void akcipher_sock_destruct(struct sock *sk)
453 +{
454 + struct alg_sock *ask = alg_sk(sk);
455 + struct af_alg_ctx *ctx = ask->private;
456 +
457 + af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
458 + sock_kfree_s(sk, ctx, ctx->len);
459 + af_alg_release_parent(sk);
460 +}
461 +
462 +static int akcipher_accept_parent_nokey(void *private, struct sock *sk)
463 +{
464 + struct af_alg_ctx *ctx;
465 + struct alg_sock *ask = alg_sk(sk);
466 + unsigned int len = sizeof(*ctx);
467 +
468 + ctx = sock_kmalloc(sk, len, GFP_KERNEL);
469 + if (!ctx)
470 + return -ENOMEM;
471 +
472 + INIT_LIST_HEAD(&ctx->tsgl_list);
473 + ctx->len = len;
474 + ctx->used = 0;
475 + ctx->rcvused = 0;
476 + ctx->more = 0;
477 + ctx->merge = 0;
478 + ctx->op = 0;
479 + af_alg_init_completion(&ctx->completion);
480 +
481 + ask->private = ctx;
482 +
483 + sk->sk_destruct = akcipher_sock_destruct;
484 +
485 + return 0;
486 +}
487 +
488 +static int akcipher_accept_parent(void *private, struct sock *sk)
489 +{
490 + struct akcipher_tfm *tfm = private;
491 +
492 + if (!tfm->has_key)
493 + return -ENOKEY;
494 +
495 + return akcipher_accept_parent_nokey(private, sk);
496 +}
497 +
498 +static const struct af_alg_type algif_type_akcipher = {
499 + .bind = akcipher_bind,
500 + .release = akcipher_release,
501 + .setkey = akcipher_setprivkey,
502 + .setpubkey = akcipher_setpubkey,
503 + .setauthsize = NULL,
504 + .accept = akcipher_accept_parent,
505 + .accept_nokey = akcipher_accept_parent_nokey,
506 + .ops = &algif_akcipher_ops,
507 + .ops_nokey = &algif_akcipher_ops_nokey,
508 + .name = "akcipher",
509 + .owner = THIS_MODULE
510 +};
511 +
512 +static int __init algif_akcipher_init(void)
513 +{
514 + return af_alg_register_type(&algif_type_akcipher);
515 +}
516 +
517 +static void __exit algif_akcipher_exit(void)
518 +{
519 + int err = af_alg_unregister_type(&algif_type_akcipher);
520 +
521 + BUG_ON(err);
522 +}
523 +
524 +module_init(algif_akcipher_init);
525 +module_exit(algif_akcipher_exit);
526 +MODULE_LICENSE("GPL");
527 +MODULE_AUTHOR("Stephan Mueller <[email protected]>");
528 +MODULE_DESCRIPTION("Asymmetric kernel crypto API user space interface");
529 diff --git a/include/crypto/if_alg.h b/include/crypto/if_alg.h
530 index d1de8ed3e77b..a2b396756e24 100644
531 --- a/include/crypto/if_alg.h
532 +++ b/include/crypto/if_alg.h
533 @@ -22,6 +22,7 @@
534
535 #include <crypto/aead.h>
536 #include <crypto/skcipher.h>
537 +#include <crypto/akcipher.h>
538
539 #define ALG_MAX_PAGES 16
540
541 @@ -119,6 +120,7 @@ struct af_alg_async_req {
542 union {
543 struct aead_request aead_req;
544 struct skcipher_request skcipher_req;
545 + struct akcipher_request akcipher_req;
546 } cra_u;
547
548 /* req ctx trails this struct */
549 --
550 2.13.5
551