thermal: enable thermal subsystem with step_wise governor
[platform/kernel/linux-starfive.git] / crypto / algif_akcipher.c
1 /*
2  * algif_akcipher: User-space interface for asymmetric cipher algorithms
3  *
4  * Copyright (C) 2018 - 2020, Stephan Mueller <smueller@chronox.de>
5  *
6  * This file provides the user-space API for asymmetric ciphers.
7  *
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)
11  * any later version.
12  *
13  * The following concept of the memory management is used:
14  *
15  * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
16  * filled by user space with the data submitted via sendpage/sendmsg. Filling
17  * up the TX SGL does not cause a crypto operation -- the data will only be
18  * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
19  * provide a buffer which is tracked with the RX SGL.
20  *
21  * During the processing of the recvmsg operation, the cipher request is
22  * allocated and prepared. As part of the recvmsg operation, the processed
23  * TX buffers are extracted from the TX SGL into a separate SGL.
24  *
25  * After the completion of the crypto operation, the RX SGL and the cipher
26  * request is released. The extracted TX SGL parts are released together with
27  * the RX SGL release.
28  */
29
30 #include <crypto/akcipher.h>
31 #include <crypto/if_alg.h>
32 #include <crypto/scatterwalk.h>
33 #include <linux/init.h>
34 #include <linux/list.h>
35 #include <linux/kernel.h>
36 #include <linux/mm.h>
37 #include <linux/module.h>
38 #include <linux/net.h>
39 #include <net/sock.h>
40
41 struct akcipher_tfm {
42         struct crypto_akcipher *akcipher;
43         bool has_key;
44 };
45
46 static int akcipher_sendmsg(struct socket *sock, struct msghdr *msg,
47                             size_t size)
48 {
49         return af_alg_sendmsg(sock, msg, size, 0);
50 }
51
52 static inline int akcipher_cipher_op(struct af_alg_ctx *ctx,
53                                      struct af_alg_async_req *areq)
54 {
55         switch (ctx->op) {
56         case ALG_OP_ENCRYPT:
57                 return crypto_akcipher_encrypt(&areq->cra_u.akcipher_req);
58         case ALG_OP_DECRYPT:
59                 return crypto_akcipher_decrypt(&areq->cra_u.akcipher_req);
60         case ALG_OP_SIGN:
61                 return crypto_akcipher_sign(&areq->cra_u.akcipher_req);
62         case ALG_OP_VERIFY:
63                 return crypto_akcipher_verify(&areq->cra_u.akcipher_req);
64         default:
65                 return -EOPNOTSUPP;
66         }
67 }
68
69 static int _akcipher_recvmsg(struct socket *sock, struct msghdr *msg,
70                              size_t ignored, int flags)
71 {
72         struct sock *sk = sock->sk;
73         struct alg_sock *ask = alg_sk(sk);
74         struct sock *psk = ask->parent;
75         struct alg_sock *pask = alg_sk(psk);
76         struct af_alg_ctx *ctx = ask->private;
77         struct akcipher_tfm *akc = pask->private;
78         struct crypto_akcipher *tfm = akc->akcipher;
79         struct af_alg_async_req *areq;
80         size_t len;
81         size_t used;
82         int err;
83         int maxsize;
84
85         if (!ctx->used) {
86                 err = af_alg_wait_for_data(sk, flags, 0);
87                 if (err)
88                         return err;
89         }
90
91         maxsize = crypto_akcipher_maxsize(tfm);
92         if (maxsize < 0)
93                 return maxsize;
94
95         /* Allocate cipher request for current operation. */
96         areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
97                                      crypto_akcipher_reqsize(tfm));
98         if (IS_ERR(areq))
99                 return PTR_ERR(areq);
100
101         /* convert iovecs of output buffers into RX SGL */
102         err = af_alg_get_rsgl(sk, msg, flags, areq, maxsize, &len);
103         if (err)
104                 goto free;
105
106         /* ensure output buffer is sufficiently large */
107         if (len < maxsize) {
108                 err = -EMSGSIZE;
109                 goto free;
110         }
111
112         /*
113          * Create a per request TX SGL for this request which tracks the
114          * SG entries from the global TX SGL.
115          */
116         used = ctx->used;
117         areq->tsgl_entries = af_alg_count_tsgl(sk, used, 0);
118         if (!areq->tsgl_entries)
119                 areq->tsgl_entries = 1;
120         areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * areq->tsgl_entries,
121                                   GFP_KERNEL);
122         if (!areq->tsgl) {
123                 err = -ENOMEM;
124                 goto free;
125         }
126         sg_init_table(areq->tsgl, areq->tsgl_entries);
127         af_alg_pull_tsgl(sk, used, areq->tsgl, 0);
128
129         /* Initialize the crypto operation */
130         akcipher_request_set_tfm(&areq->cra_u.akcipher_req, tfm);
131         akcipher_request_set_crypt(&areq->cra_u.akcipher_req, areq->tsgl,
132                                    areq->first_rsgl.sgl.sg, used, len);
133
134         if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
135                 /* AIO operation */
136                 sock_hold(sk);
137                 areq->iocb = msg->msg_iocb;
138
139                 /* Remember output size that will be generated. */
140                 areq->outlen = areq->cra_u.akcipher_req.dst_len ?
141                                 areq->cra_u.akcipher_req.dst_len : len;
142
143                 akcipher_request_set_callback(&areq->cra_u.akcipher_req,
144                                               CRYPTO_TFM_REQ_MAY_SLEEP,
145                                               af_alg_async_cb, areq);
146                 err = akcipher_cipher_op(ctx, areq);
147
148                 /* AIO operation in progress */
149                 if (err == -EINPROGRESS || err == -EBUSY)
150                         return -EIOCBQUEUED;
151
152                 sock_put(sk);
153         } else {
154                 /* Synchronous operation */
155                 akcipher_request_set_callback(&areq->cra_u.akcipher_req,
156                                               CRYPTO_TFM_REQ_MAY_SLEEP |
157                                               CRYPTO_TFM_REQ_MAY_BACKLOG,
158                                               crypto_req_done,
159                                               &ctx->wait);
160                 err = crypto_wait_req(akcipher_cipher_op(ctx, areq),
161                                       &ctx->wait);
162         }
163
164 free:
165         af_alg_free_resources(areq);
166
167         return err ? err : areq->cra_u.akcipher_req.dst_len;
168 }
169
170 static int akcipher_recvmsg(struct socket *sock, struct msghdr *msg,
171                             size_t ignored, int flags)
172 {
173         struct sock *sk = sock->sk;
174         struct alg_sock *ask = alg_sk(sk);
175         struct sock *psk = ask->parent;
176         struct alg_sock *pask = alg_sk(psk);
177         struct akcipher_tfm *akc = pask->private;
178         struct crypto_akcipher *tfm = akc->akcipher;
179         int ret = 0;
180         int err;
181
182         lock_sock(sk);
183
184         while (msg_data_left(msg)) {
185                 err = _akcipher_recvmsg(sock, msg, ignored, flags);
186
187                 /*
188                  * This error covers -EIOCBQUEUED which implies that we can
189                  * only handle one AIO request. If the caller wants to have
190                  * multiple AIO requests in parallel, he must make multiple
191                  * separate AIO calls.
192                  */
193                 if (err <= 0) {
194                         if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
195                                 ret = err;
196                         goto out;
197                 }
198
199                 ret += err;
200
201                 /*
202                  * The caller must provide crypto_akcipher_maxsize per request.
203                  * If he provides more, we conclude that multiple akcipher
204                  * operations are requested.
205                  */
206                 iov_iter_advance(&msg->msg_iter,
207                                  crypto_akcipher_maxsize(tfm) - err);
208         }
209
210 out:
211         af_alg_wmem_wakeup(sk);
212         release_sock(sk);
213         return ret;
214 }
215
216 static struct proto_ops algif_akcipher_ops = {
217         .family         =       PF_ALG,
218
219         .connect        =       sock_no_connect,
220         .socketpair     =       sock_no_socketpair,
221         .getname        =       sock_no_getname,
222         .ioctl          =       sock_no_ioctl,
223         .listen         =       sock_no_listen,
224         .shutdown       =       sock_no_shutdown,
225         .mmap           =       sock_no_mmap,
226         .bind           =       sock_no_bind,
227         .accept         =       sock_no_accept,
228
229         .release        =       af_alg_release,
230         .sendmsg        =       akcipher_sendmsg,
231         .sendpage       =       af_alg_sendpage,
232         .recvmsg        =       akcipher_recvmsg,
233         .poll           =       af_alg_poll,
234 };
235
236 static int akcipher_check_key(struct socket *sock)
237 {
238         struct sock *psk;
239         struct alg_sock *pask;
240         struct akcipher_tfm *tfm;
241         struct sock *sk = sock->sk;
242         struct alg_sock *ask = alg_sk(sk);
243         int err = 0;
244
245         lock_sock(sk);
246         if (!atomic_read(&ask->nokey_refcnt))           
247                 goto unlock_child;
248
249         psk = ask->parent;
250         pask = alg_sk(ask->parent);
251         tfm = pask->private;
252
253         lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
254         if (!tfm->has_key) {
255                 err = -ENOKEY;
256                 goto unlock;
257         }
258
259         atomic_dec(&pask->nokey_refcnt);
260         atomic_set(&ask->nokey_refcnt, 0);
261
262         err = 0;
263
264 unlock:
265         release_sock(psk);
266 unlock_child:
267         release_sock(sk);
268
269         return err;
270 }
271
272 static int akcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
273                                   size_t size)
274 {
275         int err;
276
277         err = akcipher_check_key(sock);
278         if (err)
279                 return err;
280
281         return akcipher_sendmsg(sock, msg, size);
282 }
283
284 static ssize_t akcipher_sendpage_nokey(struct socket *sock, struct page *page,
285                                        int offset, size_t size, int flags)
286 {
287         int err;
288
289         err = akcipher_check_key(sock);
290         if (err)
291                 return err;
292
293         return af_alg_sendpage(sock, page, offset, size, flags);
294 }
295
296 static int akcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
297                                   size_t ignored, int flags)
298 {
299         int err;
300
301         err = akcipher_check_key(sock);
302         if (err)
303                 return err;
304
305         return akcipher_recvmsg(sock, msg, ignored, flags);
306 }
307
308 static struct proto_ops algif_akcipher_ops_nokey = {
309         .family         =       PF_ALG,
310
311         .connect        =       sock_no_connect,
312         .socketpair     =       sock_no_socketpair,
313         .getname        =       sock_no_getname,
314         .ioctl          =       sock_no_ioctl,
315         .listen         =       sock_no_listen,
316         .shutdown       =       sock_no_shutdown,
317         .mmap           =       sock_no_mmap,
318         .bind           =       sock_no_bind,
319         .accept         =       sock_no_accept,
320
321         .release        =       af_alg_release,
322         .sendmsg        =       akcipher_sendmsg_nokey,
323         .sendpage       =       akcipher_sendpage_nokey,
324         .recvmsg        =       akcipher_recvmsg_nokey,
325         .poll           =       af_alg_poll,
326 };
327
328 static void *akcipher_bind(const char *name, u32 type, u32 mask)
329 {
330         struct akcipher_tfm *tfm;
331         struct crypto_akcipher *akcipher;
332
333         tfm = kmalloc(sizeof(*tfm), GFP_KERNEL);
334         if (!tfm)
335                 return ERR_PTR(-ENOMEM);
336
337         akcipher = crypto_alloc_akcipher(name, type, mask);
338         if (IS_ERR(akcipher)) {
339                 kfree(tfm);
340                 return ERR_CAST(akcipher);
341         }
342
343         tfm->akcipher = akcipher;
344         tfm->has_key = false;
345
346         return tfm;
347 }
348
349 static void akcipher_release(void *private)
350 {
351         struct akcipher_tfm *tfm = private;
352         struct crypto_akcipher *akcipher = tfm->akcipher;
353
354         crypto_free_akcipher(akcipher);
355         kfree(tfm);
356 }
357
358 static int akcipher_setprivkey(void *private, const u8 *key,
359                                unsigned int keylen)
360 {
361         struct akcipher_tfm *tfm = private;
362         struct crypto_akcipher *akcipher = tfm->akcipher;
363         int err;
364
365         err = crypto_akcipher_set_priv_key(akcipher, key, keylen);
366         tfm->has_key = !err;
367
368         /* Return the maximum size of the akcipher operation. */
369         if (!err)
370                 err = crypto_akcipher_maxsize(akcipher);
371
372         return err;
373 }
374
375 static int akcipher_setpubkey(void *private, const u8 *key, unsigned int keylen)
376 {
377         struct akcipher_tfm *tfm = private;
378         struct crypto_akcipher *akcipher = tfm->akcipher;
379         int err;
380
381         err = crypto_akcipher_set_pub_key(akcipher, key, keylen);
382         tfm->has_key = !err;
383
384         /* Return the maximum size of the akcipher operation. */
385         if (!err)
386                 err = crypto_akcipher_maxsize(akcipher);
387
388         return err;
389 }
390
391 static void akcipher_sock_destruct(struct sock *sk)
392 {
393         struct alg_sock *ask = alg_sk(sk);
394         struct af_alg_ctx *ctx = ask->private;
395
396         af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
397         sock_kfree_s(sk, ctx, ctx->len);
398         af_alg_release_parent(sk);
399 }
400
401 static int akcipher_accept_parent_nokey(void *private, struct sock *sk)
402 {
403         struct af_alg_ctx *ctx;
404         struct alg_sock *ask = alg_sk(sk);
405         unsigned int len = sizeof(*ctx);
406
407         ctx = sock_kmalloc(sk, len, GFP_KERNEL);
408         if (!ctx)
409                 return -ENOMEM;
410
411         INIT_LIST_HEAD(&ctx->tsgl_list);
412         ctx->len = len;
413         ctx->used = 0;
414         atomic_set(&ctx->rcvused, 0);
415         ctx->more = 0;
416         ctx->merge = 0;
417         ctx->op = 0;
418         crypto_init_wait(&ctx->wait);
419
420         ask->private = ctx;
421
422         sk->sk_destruct = akcipher_sock_destruct;
423
424         return 0;
425 }
426
427 static int akcipher_accept_parent(void *private, struct sock *sk)
428 {
429         struct akcipher_tfm *tfm = private;
430
431         if (!tfm->has_key)
432                 return -ENOKEY;
433
434         return akcipher_accept_parent_nokey(private, sk);
435 }
436
437 static const struct af_alg_type algif_type_akcipher = {
438         .bind           =       akcipher_bind,
439         .release        =       akcipher_release,
440         .setkey         =       akcipher_setprivkey,
441         .setpubkey      =       akcipher_setpubkey,
442         .setauthsize    =       NULL,
443         .accept         =       akcipher_accept_parent,
444         .accept_nokey   =       akcipher_accept_parent_nokey,
445         .ops            =       &algif_akcipher_ops,
446         .ops_nokey      =       &algif_akcipher_ops_nokey,
447         .name           =       "akcipher",
448         .owner          =       THIS_MODULE
449 };
450
451 static int __init algif_akcipher_init(void)
452 {
453         return af_alg_register_type(&algif_type_akcipher);
454 }
455
456 static void __exit algif_akcipher_exit(void)
457 {
458         int err = af_alg_unregister_type(&algif_type_akcipher);
459
460         BUG_ON(err);
461 }
462
463 module_init(algif_akcipher_init);
464 module_exit(algif_akcipher_exit);
465 MODULE_LICENSE("GPL");
466 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
467 MODULE_DESCRIPTION("Asymmetric kernel crypto API user space interface");
468