arm64: dts: exynos: Update Exynos5433 CHIPID node
[platform/kernel/linux-exynos.git] / crypto / algif_skcipher.c
1 /*
2  * algif_skcipher: User-space interface for skcipher algorithms
3  *
4  * This file provides the user-space API for symmetric key ciphers.
5  *
6  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
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/scatterwalk.h>
31 #include <crypto/skcipher.h>
32 #include <crypto/if_alg.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 skcipher_tfm {
42         struct crypto_skcipher *skcipher;
43         bool has_key;
44 };
45
46 static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
47                             size_t size)
48 {
49         struct sock *sk = sock->sk;
50         struct alg_sock *ask = alg_sk(sk);
51         struct sock *psk = ask->parent;
52         struct alg_sock *pask = alg_sk(psk);
53         struct skcipher_tfm *skc = pask->private;
54         struct crypto_skcipher *tfm = skc->skcipher;
55         unsigned ivsize = crypto_skcipher_ivsize(tfm);
56
57         return af_alg_sendmsg(sock, msg, size, ivsize);
58 }
59
60 static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
61                              size_t ignored, int flags)
62 {
63         struct sock *sk = sock->sk;
64         struct alg_sock *ask = alg_sk(sk);
65         struct sock *psk = ask->parent;
66         struct alg_sock *pask = alg_sk(psk);
67         struct af_alg_ctx *ctx = ask->private;
68         struct skcipher_tfm *skc = pask->private;
69         struct crypto_skcipher *tfm = skc->skcipher;
70         unsigned int bs = crypto_skcipher_blocksize(tfm);
71         struct af_alg_async_req *areq;
72         int err = 0;
73         size_t len = 0;
74
75         if (!ctx->used) {
76                 err = af_alg_wait_for_data(sk, flags);
77                 if (err)
78                         return err;
79         }
80
81         /* Allocate cipher request for current operation. */
82         areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
83                                      crypto_skcipher_reqsize(tfm));
84         if (IS_ERR(areq))
85                 return PTR_ERR(areq);
86
87         /* convert iovecs of output buffers into RX SGL */
88         err = af_alg_get_rsgl(sk, msg, flags, areq, -1, &len);
89         if (err)
90                 goto free;
91
92         /* Process only as much RX buffers for which we have TX data */
93         if (len > ctx->used)
94                 len = ctx->used;
95
96         /*
97          * If more buffers are to be expected to be processed, process only
98          * full block size buffers.
99          */
100         if (ctx->more || len < ctx->used)
101                 len -= len % bs;
102
103         /*
104          * Create a per request TX SGL for this request which tracks the
105          * SG entries from the global TX SGL.
106          */
107         areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0);
108         if (!areq->tsgl_entries)
109                 areq->tsgl_entries = 1;
110         areq->tsgl = sock_kmalloc(sk, sizeof(*areq->tsgl) * areq->tsgl_entries,
111                                   GFP_KERNEL);
112         if (!areq->tsgl) {
113                 err = -ENOMEM;
114                 goto free;
115         }
116         sg_init_table(areq->tsgl, areq->tsgl_entries);
117         af_alg_pull_tsgl(sk, len, areq->tsgl, 0);
118
119         /* Initialize the crypto operation */
120         skcipher_request_set_tfm(&areq->cra_u.skcipher_req, tfm);
121         skcipher_request_set_crypt(&areq->cra_u.skcipher_req, areq->tsgl,
122                                    areq->first_rsgl.sgl.sg, len, ctx->iv);
123
124         if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
125                 /* AIO operation */
126                 sock_hold(sk);
127                 areq->iocb = msg->msg_iocb;
128
129                 /* Remember output size that will be generated. */
130                 areq->outlen = len;
131
132                 skcipher_request_set_callback(&areq->cra_u.skcipher_req,
133                                               CRYPTO_TFM_REQ_MAY_SLEEP,
134                                               af_alg_async_cb, areq);
135                 err = ctx->enc ?
136                         crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
137                         crypto_skcipher_decrypt(&areq->cra_u.skcipher_req);
138
139                 /* AIO operation in progress */
140                 if (err == -EINPROGRESS || err == -EBUSY)
141                         return -EIOCBQUEUED;
142
143                 sock_put(sk);
144         } else {
145                 /* Synchronous operation */
146                 skcipher_request_set_callback(&areq->cra_u.skcipher_req,
147                                               CRYPTO_TFM_REQ_MAY_SLEEP |
148                                               CRYPTO_TFM_REQ_MAY_BACKLOG,
149                                               af_alg_complete,
150                                               &ctx->completion);
151                 err = af_alg_wait_for_completion(ctx->enc ?
152                         crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
153                         crypto_skcipher_decrypt(&areq->cra_u.skcipher_req),
154                                                  &ctx->completion);
155         }
156
157
158 free:
159         af_alg_free_resources(areq);
160
161         return err ? err : len;
162 }
163
164 static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
165                             size_t ignored, int flags)
166 {
167         struct sock *sk = sock->sk;
168         int ret = 0;
169
170         lock_sock(sk);
171         while (msg_data_left(msg)) {
172                 int err = _skcipher_recvmsg(sock, msg, ignored, flags);
173
174                 /*
175                  * This error covers -EIOCBQUEUED which implies that we can
176                  * only handle one AIO request. If the caller wants to have
177                  * multiple AIO requests in parallel, he must make multiple
178                  * separate AIO calls.
179                  *
180                  * Also return the error if no data has been processed so far.
181                  */
182                 if (err <= 0) {
183                         if (err == -EIOCBQUEUED || !ret)
184                                 ret = err;
185                         goto out;
186                 }
187
188                 ret += err;
189         }
190
191 out:
192         af_alg_wmem_wakeup(sk);
193         release_sock(sk);
194         return ret;
195 }
196
197
198 static struct proto_ops algif_skcipher_ops = {
199         .family         =       PF_ALG,
200
201         .connect        =       sock_no_connect,
202         .socketpair     =       sock_no_socketpair,
203         .getname        =       sock_no_getname,
204         .ioctl          =       sock_no_ioctl,
205         .listen         =       sock_no_listen,
206         .shutdown       =       sock_no_shutdown,
207         .getsockopt     =       sock_no_getsockopt,
208         .mmap           =       sock_no_mmap,
209         .bind           =       sock_no_bind,
210         .accept         =       sock_no_accept,
211         .setsockopt     =       sock_no_setsockopt,
212
213         .release        =       af_alg_release,
214         .sendmsg        =       skcipher_sendmsg,
215         .sendpage       =       af_alg_sendpage,
216         .recvmsg        =       skcipher_recvmsg,
217         .poll           =       af_alg_poll,
218 };
219
220 static int skcipher_check_key(struct socket *sock)
221 {
222         int err = 0;
223         struct sock *psk;
224         struct alg_sock *pask;
225         struct skcipher_tfm *tfm;
226         struct sock *sk = sock->sk;
227         struct alg_sock *ask = alg_sk(sk);
228
229         lock_sock(sk);
230         if (ask->refcnt)
231                 goto unlock_child;
232
233         psk = ask->parent;
234         pask = alg_sk(ask->parent);
235         tfm = pask->private;
236
237         err = -ENOKEY;
238         lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
239         if (!tfm->has_key)
240                 goto unlock;
241
242         if (!pask->refcnt++)
243                 sock_hold(psk);
244
245         ask->refcnt = 1;
246         sock_put(psk);
247
248         err = 0;
249
250 unlock:
251         release_sock(psk);
252 unlock_child:
253         release_sock(sk);
254
255         return err;
256 }
257
258 static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
259                                   size_t size)
260 {
261         int err;
262
263         err = skcipher_check_key(sock);
264         if (err)
265                 return err;
266
267         return skcipher_sendmsg(sock, msg, size);
268 }
269
270 static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
271                                        int offset, size_t size, int flags)
272 {
273         int err;
274
275         err = skcipher_check_key(sock);
276         if (err)
277                 return err;
278
279         return af_alg_sendpage(sock, page, offset, size, flags);
280 }
281
282 static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
283                                   size_t ignored, int flags)
284 {
285         int err;
286
287         err = skcipher_check_key(sock);
288         if (err)
289                 return err;
290
291         return skcipher_recvmsg(sock, msg, ignored, flags);
292 }
293
294 static struct proto_ops algif_skcipher_ops_nokey = {
295         .family         =       PF_ALG,
296
297         .connect        =       sock_no_connect,
298         .socketpair     =       sock_no_socketpair,
299         .getname        =       sock_no_getname,
300         .ioctl          =       sock_no_ioctl,
301         .listen         =       sock_no_listen,
302         .shutdown       =       sock_no_shutdown,
303         .getsockopt     =       sock_no_getsockopt,
304         .mmap           =       sock_no_mmap,
305         .bind           =       sock_no_bind,
306         .accept         =       sock_no_accept,
307         .setsockopt     =       sock_no_setsockopt,
308
309         .release        =       af_alg_release,
310         .sendmsg        =       skcipher_sendmsg_nokey,
311         .sendpage       =       skcipher_sendpage_nokey,
312         .recvmsg        =       skcipher_recvmsg_nokey,
313         .poll           =       af_alg_poll,
314 };
315
316 static void *skcipher_bind(const char *name, u32 type, u32 mask)
317 {
318         struct skcipher_tfm *tfm;
319         struct crypto_skcipher *skcipher;
320
321         tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
322         if (!tfm)
323                 return ERR_PTR(-ENOMEM);
324
325         skcipher = crypto_alloc_skcipher(name, type, mask);
326         if (IS_ERR(skcipher)) {
327                 kfree(tfm);
328                 return ERR_CAST(skcipher);
329         }
330
331         tfm->skcipher = skcipher;
332
333         return tfm;
334 }
335
336 static void skcipher_release(void *private)
337 {
338         struct skcipher_tfm *tfm = private;
339
340         crypto_free_skcipher(tfm->skcipher);
341         kfree(tfm);
342 }
343
344 static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
345 {
346         struct skcipher_tfm *tfm = private;
347         int err;
348
349         err = crypto_skcipher_setkey(tfm->skcipher, key, keylen);
350         tfm->has_key = !err;
351
352         return err;
353 }
354
355 static void skcipher_sock_destruct(struct sock *sk)
356 {
357         struct alg_sock *ask = alg_sk(sk);
358         struct af_alg_ctx *ctx = ask->private;
359         struct sock *psk = ask->parent;
360         struct alg_sock *pask = alg_sk(psk);
361         struct skcipher_tfm *skc = pask->private;
362         struct crypto_skcipher *tfm = skc->skcipher;
363
364         af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
365         sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
366         sock_kfree_s(sk, ctx, ctx->len);
367         af_alg_release_parent(sk);
368 }
369
370 static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
371 {
372         struct af_alg_ctx *ctx;
373         struct alg_sock *ask = alg_sk(sk);
374         struct skcipher_tfm *tfm = private;
375         struct crypto_skcipher *skcipher = tfm->skcipher;
376         unsigned int len = sizeof(*ctx);
377
378         ctx = sock_kmalloc(sk, len, GFP_KERNEL);
379         if (!ctx)
380                 return -ENOMEM;
381
382         ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher),
383                                GFP_KERNEL);
384         if (!ctx->iv) {
385                 sock_kfree_s(sk, ctx, len);
386                 return -ENOMEM;
387         }
388
389         memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher));
390
391         INIT_LIST_HEAD(&ctx->tsgl_list);
392         ctx->len = len;
393         ctx->used = 0;
394         atomic_set(&ctx->rcvused, 0);
395         ctx->more = 0;
396         ctx->merge = 0;
397         ctx->enc = 0;
398         af_alg_init_completion(&ctx->completion);
399
400         ask->private = ctx;
401
402         sk->sk_destruct = skcipher_sock_destruct;
403
404         return 0;
405 }
406
407 static int skcipher_accept_parent(void *private, struct sock *sk)
408 {
409         struct skcipher_tfm *tfm = private;
410
411         if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher))
412                 return -ENOKEY;
413
414         return skcipher_accept_parent_nokey(private, sk);
415 }
416
417 static const struct af_alg_type algif_type_skcipher = {
418         .bind           =       skcipher_bind,
419         .release        =       skcipher_release,
420         .setkey         =       skcipher_setkey,
421         .accept         =       skcipher_accept_parent,
422         .accept_nokey   =       skcipher_accept_parent_nokey,
423         .ops            =       &algif_skcipher_ops,
424         .ops_nokey      =       &algif_skcipher_ops_nokey,
425         .name           =       "skcipher",
426         .owner          =       THIS_MODULE
427 };
428
429 static int __init algif_skcipher_init(void)
430 {
431         return af_alg_register_type(&algif_type_skcipher);
432 }
433
434 static void __exit algif_skcipher_exit(void)
435 {
436         int err = af_alg_unregister_type(&algif_type_skcipher);
437         BUG_ON(err);
438 }
439
440 module_init(algif_skcipher_init);
441 module_exit(algif_skcipher_exit);
442 MODULE_LICENSE("GPL");