Commit | Line | Data |
---|---|---|
8ff59090 HX |
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 | * | |
e870456d SM |
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. | |
8ff59090 HX |
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 | ||
dd504589 HX |
41 | struct skcipher_tfm { |
42 | struct crypto_skcipher *skcipher; | |
43 | bool has_key; | |
44 | }; | |
45 | ||
1b784140 YX |
46 | static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg, |
47 | size_t size) | |
8ff59090 HX |
48 | { |
49 | struct sock *sk = sock->sk; | |
50 | struct alg_sock *ask = alg_sk(sk); | |
6454c2b8 HX |
51 | struct sock *psk = ask->parent; |
52 | struct alg_sock *pask = alg_sk(psk); | |
6454c2b8 HX |
53 | struct skcipher_tfm *skc = pask->private; |
54 | struct crypto_skcipher *tfm = skc->skcipher; | |
0d96e4ba | 55 | unsigned ivsize = crypto_skcipher_ivsize(tfm); |
8ff59090 | 56 | |
2d97591e | 57 | return af_alg_sendmsg(sock, msg, size, ivsize); |
a596999b TS |
58 | } |
59 | ||
e870456d SM |
60 | static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg, |
61 | size_t ignored, int flags) | |
a596999b TS |
62 | { |
63 | struct sock *sk = sock->sk; | |
64 | struct alg_sock *ask = alg_sk(sk); | |
ec69bbfb HX |
65 | struct sock *psk = ask->parent; |
66 | struct alg_sock *pask = alg_sk(psk); | |
2d97591e | 67 | struct af_alg_ctx *ctx = ask->private; |
ec69bbfb HX |
68 | struct skcipher_tfm *skc = pask->private; |
69 | struct crypto_skcipher *tfm = skc->skcipher; | |
e870456d | 70 | unsigned int bs = crypto_skcipher_blocksize(tfm); |
2d97591e | 71 | struct af_alg_async_req *areq; |
e870456d SM |
72 | int err = 0; |
73 | size_t len = 0; | |
ec69bbfb | 74 | |
c692698e SM |
75 | if (!ctx->used) { |
76 | err = af_alg_wait_for_data(sk, flags); | |
77 | if (err) | |
78 | return err; | |
79 | } | |
80 | ||
e870456d | 81 | /* Allocate cipher request for current operation. */ |
2d97591e SM |
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); | |
a596999b | 86 | |
e870456d | 87 | /* convert iovecs of output buffers into RX SGL */ |
2d97591e SM |
88 | err = af_alg_get_rsgl(sk, msg, flags, areq, -1, &len); |
89 | if (err) | |
90 | goto free; | |
a596999b | 91 | |
e870456d SM |
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 | */ | |
2d97591e | 107 | areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0); |
e870456d SM |
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); | |
2d97591e | 117 | af_alg_pull_tsgl(sk, len, areq->tsgl, 0); |
e870456d SM |
118 | |
119 | /* Initialize the crypto operation */ | |
2d97591e SM |
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); | |
e870456d SM |
123 | |
124 | if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) { | |
125 | /* AIO operation */ | |
7f21961a | 126 | sock_hold(sk); |
e870456d | 127 | areq->iocb = msg->msg_iocb; |
f09fca41 SM |
128 | |
129 | /* Remember output size that will be generated. */ | |
130 | areq->outlen = len; | |
131 | ||
2d97591e | 132 | skcipher_request_set_callback(&areq->cra_u.skcipher_req, |
e870456d | 133 | CRYPTO_TFM_REQ_MAY_SLEEP, |
2d97591e SM |
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); | |
7f21961a SM |
138 | |
139 | /* AIO operation in progress */ | |
f09fca41 | 140 | if (err == -EINPROGRESS || err == -EBUSY) |
7f21961a | 141 | return -EIOCBQUEUED; |
7f21961a SM |
142 | |
143 | sock_put(sk); | |
e870456d SM |
144 | } else { |
145 | /* Synchronous operation */ | |
2d97591e | 146 | skcipher_request_set_callback(&areq->cra_u.skcipher_req, |
e870456d SM |
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 ? | |
2d97591e SM |
152 | crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) : |
153 | crypto_skcipher_decrypt(&areq->cra_u.skcipher_req), | |
e870456d SM |
154 | &ctx->completion); |
155 | } | |
033f46b3 | 156 | |
e870456d | 157 | |
a596999b | 158 | free: |
7f21961a | 159 | af_alg_free_resources(areq); |
e870456d SM |
160 | |
161 | return err ? err : len; | |
a596999b TS |
162 | } |
163 | ||
e870456d SM |
164 | static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg, |
165 | size_t ignored, int flags) | |
8ff59090 HX |
166 | { |
167 | struct sock *sk = sock->sk; | |
e870456d | 168 | int ret = 0; |
8ff59090 HX |
169 | |
170 | lock_sock(sk); | |
01e97e65 | 171 | while (msg_data_left(msg)) { |
e870456d SM |
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. | |
5703c826 SM |
179 | * |
180 | * Also return the error if no data has been processed so far. | |
e870456d SM |
181 | */ |
182 | if (err <= 0) { | |
5703c826 | 183 | if (err == -EIOCBQUEUED || !ret) |
e870456d SM |
184 | ret = err; |
185 | goto out; | |
1d10eb2f AV |
186 | } |
187 | ||
e870456d | 188 | ret += err; |
8ff59090 HX |
189 | } |
190 | ||
e870456d | 191 | out: |
2d97591e | 192 | af_alg_wmem_wakeup(sk); |
8ff59090 | 193 | release_sock(sk); |
e870456d | 194 | return ret; |
a596999b | 195 | } |
8ff59090 | 196 | |
8ff59090 HX |
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, | |
2d97591e | 215 | .sendpage = af_alg_sendpage, |
8ff59090 | 216 | .recvmsg = skcipher_recvmsg, |
2d97591e | 217 | .poll = af_alg_poll, |
8ff59090 HX |
218 | }; |
219 | ||
a0fa2d03 HX |
220 | static int skcipher_check_key(struct socket *sock) |
221 | { | |
1822793a | 222 | int err = 0; |
a0fa2d03 HX |
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 | ||
1822793a | 229 | lock_sock(sk); |
a0fa2d03 | 230 | if (ask->refcnt) |
1822793a | 231 | goto unlock_child; |
a0fa2d03 HX |
232 | |
233 | psk = ask->parent; | |
234 | pask = alg_sk(ask->parent); | |
235 | tfm = pask->private; | |
236 | ||
237 | err = -ENOKEY; | |
1822793a | 238 | lock_sock_nested(psk, SINGLE_DEPTH_NESTING); |
a0fa2d03 HX |
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); | |
1822793a HX |
252 | unlock_child: |
253 | release_sock(sk); | |
a0fa2d03 HX |
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 | ||
2d97591e | 279 | return af_alg_sendpage(sock, page, offset, size, flags); |
a0fa2d03 HX |
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, | |
2d97591e | 313 | .poll = af_alg_poll, |
a0fa2d03 HX |
314 | }; |
315 | ||
8ff59090 HX |
316 | static void *skcipher_bind(const char *name, u32 type, u32 mask) |
317 | { | |
dd504589 HX |
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; | |
8ff59090 HX |
334 | } |
335 | ||
336 | static void skcipher_release(void *private) | |
337 | { | |
dd504589 HX |
338 | struct skcipher_tfm *tfm = private; |
339 | ||
340 | crypto_free_skcipher(tfm->skcipher); | |
341 | kfree(tfm); | |
8ff59090 HX |
342 | } |
343 | ||
344 | static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen) | |
345 | { | |
dd504589 HX |
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; | |
8ff59090 HX |
353 | } |
354 | ||
355 | static void skcipher_sock_destruct(struct sock *sk) | |
356 | { | |
357 | struct alg_sock *ask = alg_sk(sk); | |
2d97591e | 358 | struct af_alg_ctx *ctx = ask->private; |
e870456d SM |
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; | |
a596999b | 363 | |
2d97591e | 364 | af_alg_pull_tsgl(sk, ctx->used, NULL, 0); |
0d96e4ba | 365 | sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm)); |
8ff59090 HX |
366 | sock_kfree_s(sk, ctx, ctx->len); |
367 | af_alg_release_parent(sk); | |
368 | } | |
369 | ||
d7b65aee | 370 | static int skcipher_accept_parent_nokey(void *private, struct sock *sk) |
8ff59090 | 371 | { |
2d97591e | 372 | struct af_alg_ctx *ctx; |
8ff59090 | 373 | struct alg_sock *ask = alg_sk(sk); |
dd504589 HX |
374 | struct skcipher_tfm *tfm = private; |
375 | struct crypto_skcipher *skcipher = tfm->skcipher; | |
e870456d | 376 | unsigned int len = sizeof(*ctx); |
8ff59090 HX |
377 | |
378 | ctx = sock_kmalloc(sk, len, GFP_KERNEL); | |
379 | if (!ctx) | |
380 | return -ENOMEM; | |
381 | ||
dd504589 | 382 | ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(skcipher), |
8ff59090 HX |
383 | GFP_KERNEL); |
384 | if (!ctx->iv) { | |
385 | sock_kfree_s(sk, ctx, len); | |
386 | return -ENOMEM; | |
387 | } | |
388 | ||
dd504589 | 389 | memset(ctx->iv, 0, crypto_skcipher_ivsize(skcipher)); |
8ff59090 | 390 | |
e870456d | 391 | INIT_LIST_HEAD(&ctx->tsgl_list); |
8ff59090 HX |
392 | ctx->len = len; |
393 | ctx->used = 0; | |
36d0a678 | 394 | atomic_set(&ctx->rcvused, 0); |
8ff59090 HX |
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 | ||
8ff59090 HX |
402 | sk->sk_destruct = skcipher_sock_destruct; |
403 | ||
404 | return 0; | |
405 | } | |
406 | ||
a0fa2d03 HX |
407 | static int skcipher_accept_parent(void *private, struct sock *sk) |
408 | { | |
409 | struct skcipher_tfm *tfm = private; | |
410 | ||
6e8d8ecf | 411 | if (!tfm->has_key && crypto_skcipher_has_setkey(tfm->skcipher)) |
a0fa2d03 HX |
412 | return -ENOKEY; |
413 | ||
d7b65aee | 414 | return skcipher_accept_parent_nokey(private, sk); |
a0fa2d03 HX |
415 | } |
416 | ||
8ff59090 HX |
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, | |
a0fa2d03 | 422 | .accept_nokey = skcipher_accept_parent_nokey, |
8ff59090 | 423 | .ops = &algif_skcipher_ops, |
a0fa2d03 | 424 | .ops_nokey = &algif_skcipher_ops_nokey, |
8ff59090 HX |
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"); |