blob: 54f2fb2c6535dac1657d4267e4f09270c58040c6 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stephan Mueller400c40c2015-02-28 20:50:00 +01002/*
3 * algif_aead: User-space interface for AEAD algorithms
4 *
5 * Copyright (C) 2014, Stephan Mueller <smueller@chronox.de>
6 *
7 * This file provides the user-space API for AEAD ciphers.
8 *
Stephan Muellerd887c522017-06-25 17:12:59 +02009 * The following concept of the memory management is used:
10 *
11 * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
12 * filled by user space with the data submitted via sendpage/sendmsg. Filling
13 * up the TX SGL does not cause a crypto operation -- the data will only be
14 * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
15 * provide a buffer which is tracked with the RX SGL.
16 *
17 * During the processing of the recvmsg operation, the cipher request is
18 * allocated and prepared. As part of the recvmsg operation, the processed
19 * TX buffers are extracted from the TX SGL into a separate SGL.
20 *
21 * After the completion of the crypto operation, the RX SGL and the cipher
22 * request is released. The extracted TX SGL parts are released together with
23 * the RX SGL release.
Stephan Mueller400c40c2015-02-28 20:50:00 +010024 */
25
Tadeusz Struk83094e5e2016-03-11 11:50:33 -080026#include <crypto/internal/aead.h>
Stephan Mueller400c40c2015-02-28 20:50:00 +010027#include <crypto/scatterwalk.h>
28#include <crypto/if_alg.h>
Stephan Mueller72548b02017-07-30 14:32:58 +020029#include <crypto/skcipher.h>
30#include <crypto/null.h>
Stephan Mueller400c40c2015-02-28 20:50:00 +010031#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/kernel.h>
34#include <linux/mm.h>
35#include <linux/module.h>
36#include <linux/net.h>
37#include <net/sock.h>
38
Stephan Mueller2a2a2512017-04-24 11:15:23 +020039struct aead_tfm {
40 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -070041 struct crypto_sync_skcipher *null_tfm;
Stephan Mueller2a2a2512017-04-24 11:15:23 +020042};
43
Stephan Muellerd887c522017-06-25 17:12:59 +020044static inline bool aead_sufficient_data(struct sock *sk)
45{
46 struct alg_sock *ask = alg_sk(sk);
47 struct sock *psk = ask->parent;
48 struct alg_sock *pask = alg_sk(psk);
Stephan Mueller2d975912017-08-02 07:56:19 +020049 struct af_alg_ctx *ctx = ask->private;
Stephan Muellerd887c522017-06-25 17:12:59 +020050 struct aead_tfm *aeadc = pask->private;
51 struct crypto_aead *tfm = aeadc->aead;
52 unsigned int as = crypto_aead_authsize(tfm);
Stephan Mueller400c40c2015-02-28 20:50:00 +010053
Stephan Mueller0c1e16c2016-12-05 15:26:19 +010054 /*
55 * The minimum amount of memory needed for an AEAD cipher is
56 * the AAD and in case of decryption the tag.
57 */
58 return ctx->used >= ctx->aead_assoclen + (ctx->enc ? 0 : as);
Stephan Mueller400c40c2015-02-28 20:50:00 +010059}
60
Linus Torvaldseccd02f2015-04-15 14:09:46 -070061static int aead_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
Stephan Mueller400c40c2015-02-28 20:50:00 +010062{
63 struct sock *sk = sock->sk;
64 struct alg_sock *ask = alg_sk(sk);
Stephan Muellerd887c522017-06-25 17:12:59 +020065 struct sock *psk = ask->parent;
66 struct alg_sock *pask = alg_sk(psk);
Stephan Muellerd887c522017-06-25 17:12:59 +020067 struct aead_tfm *aeadc = pask->private;
68 struct crypto_aead *tfm = aeadc->aead;
69 unsigned int ivsize = crypto_aead_ivsize(tfm);
Stephan Mueller400c40c2015-02-28 20:50:00 +010070
Stephan Mueller2d975912017-08-02 07:56:19 +020071 return af_alg_sendmsg(sock, msg, size, ivsize);
Tadeusz Struk83094e5e2016-03-11 11:50:33 -080072}
73
Kees Cook8d605392018-09-18 19:10:51 -070074static int crypto_aead_copy_sgl(struct crypto_sync_skcipher *null_tfm,
Stephan Mueller72548b02017-07-30 14:32:58 +020075 struct scatterlist *src,
76 struct scatterlist *dst, unsigned int len)
77{
Kees Cook8d605392018-09-18 19:10:51 -070078 SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, null_tfm);
Stephan Mueller72548b02017-07-30 14:32:58 +020079
Kees Cook8d605392018-09-18 19:10:51 -070080 skcipher_request_set_sync_tfm(skreq, null_tfm);
Stephan Mueller72548b02017-07-30 14:32:58 +020081 skcipher_request_set_callback(skreq, CRYPTO_TFM_REQ_MAY_BACKLOG,
82 NULL, NULL);
83 skcipher_request_set_crypt(skreq, src, dst, len, NULL);
84
85 return crypto_skcipher_encrypt(skreq);
86}
87
Stephan Muellerd887c522017-06-25 17:12:59 +020088static int _aead_recvmsg(struct socket *sock, struct msghdr *msg,
89 size_t ignored, int flags)
Stephan Mueller400c40c2015-02-28 20:50:00 +010090{
91 struct sock *sk = sock->sk;
92 struct alg_sock *ask = alg_sk(sk);
Stephan Muellerd887c522017-06-25 17:12:59 +020093 struct sock *psk = ask->parent;
94 struct alg_sock *pask = alg_sk(psk);
Stephan Mueller2d975912017-08-02 07:56:19 +020095 struct af_alg_ctx *ctx = ask->private;
Stephan Muellerd887c522017-06-25 17:12:59 +020096 struct aead_tfm *aeadc = pask->private;
97 struct crypto_aead *tfm = aeadc->aead;
Kees Cook8d605392018-09-18 19:10:51 -070098 struct crypto_sync_skcipher *null_tfm = aeadc->null_tfm;
Stephan Mueller8e1fa892017-11-10 11:04:52 +010099 unsigned int i, as = crypto_aead_authsize(tfm);
Stephan Mueller2d975912017-08-02 07:56:19 +0200100 struct af_alg_async_req *areq;
Stephan Mueller8e1fa892017-11-10 11:04:52 +0100101 struct af_alg_tsgl *tsgl, *tmp;
102 struct scatterlist *rsgl_src, *tsgl_src = NULL;
Stephan Muellerd887c522017-06-25 17:12:59 +0200103 int err = 0;
104 size_t used = 0; /* [in] TX bufs to be en/decrypted */
105 size_t outlen = 0; /* [out] RX bufs produced by kernel */
106 size_t usedpages = 0; /* [in] RX bufs to be used from user */
107 size_t processed = 0; /* [in] TX bufs to be consumed */
Stephan Mueller400c40c2015-02-28 20:50:00 +0100108
Herbert Xuf3c802a2020-05-30 00:23:49 +1000109 if (!ctx->init || ctx->more) {
110 err = af_alg_wait_for_data(sk, flags, 0);
Stephan Mueller11edb552017-11-29 12:02:23 +0100111 if (err)
112 return err;
113 }
114
Stephan Mueller400c40c2015-02-28 20:50:00 +0100115 /*
Stephan Muellerd887c522017-06-25 17:12:59 +0200116 * Data length provided by caller via sendmsg/sendpage that has not
117 * yet been processed.
Stephan Mueller400c40c2015-02-28 20:50:00 +0100118 */
Stephan Mueller400c40c2015-02-28 20:50:00 +0100119 used = ctx->used;
120
121 /*
122 * Make sure sufficient data is present -- note, the same check is
Randy Dunlap4eb57bc2020-07-30 19:39:19 -0700123 * also present in sendmsg/sendpage. The checks in sendpage/sendmsg
Stephan Mueller400c40c2015-02-28 20:50:00 +0100124 * shall provide an information to the data sender that something is
125 * wrong, but they are irrelevant to maintain the kernel integrity.
126 * We need this check here too in case user space decides to not honor
127 * the error message in sendmsg/sendpage and still call recvmsg. This
128 * check here protects the kernel integrity.
129 */
Stephan Muellerd887c522017-06-25 17:12:59 +0200130 if (!aead_sufficient_data(sk))
131 return -EINVAL;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100132
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100133 /*
134 * Calculate the minimum output buffer size holding the result of the
135 * cipher operation. When encrypting data, the receiving buffer is
136 * larger by the tag length compared to the input buffer as the
137 * encryption operation generates the tag. For decryption, the input
138 * buffer provides the tag which is consumed resulting in only the
139 * plaintext without a buffer for the tag returned to the caller.
140 */
141 if (ctx->enc)
142 outlen = used + as;
143 else
144 outlen = used - as;
Herbert Xu19fa7752015-05-27 17:24:41 +0800145
Stephan Mueller400c40c2015-02-28 20:50:00 +0100146 /*
147 * The cipher operation input data is reduced by the associated data
148 * length as this data is processed separately later on.
149 */
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100150 used -= ctx->aead_assoclen;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100151
Stephan Muellerd887c522017-06-25 17:12:59 +0200152 /* Allocate cipher request for current operation. */
Stephan Mueller2d975912017-08-02 07:56:19 +0200153 areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
154 crypto_aead_reqsize(tfm));
155 if (IS_ERR(areq))
156 return PTR_ERR(areq);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100157
Stephan Muellerd887c522017-06-25 17:12:59 +0200158 /* convert iovecs of output buffers into RX SGL */
Stephan Mueller2d975912017-08-02 07:56:19 +0200159 err = af_alg_get_rsgl(sk, msg, flags, areq, outlen, &usedpages);
160 if (err)
161 goto free;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100162
Stephan Muellerd887c522017-06-25 17:12:59 +0200163 /*
164 * Ensure output buffer is sufficiently large. If the caller provides
165 * less buffer space, only use the relative required input size. This
166 * allows AIO operation where the caller sent all data to be processed
167 * and the AIO operation performs the operation on the different chunks
168 * of the input data.
169 */
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100170 if (usedpages < outlen) {
Stephan Muellerd887c522017-06-25 17:12:59 +0200171 size_t less = outlen - usedpages;
172
173 if (used < less) {
174 err = -EINVAL;
175 goto free;
176 }
177 used -= less;
178 outlen -= less;
Stephan Mueller0c1e16c2016-12-05 15:26:19 +0100179 }
Stephan Mueller400c40c2015-02-28 20:50:00 +0100180
Stephan Muellerd887c522017-06-25 17:12:59 +0200181 processed = used + ctx->aead_assoclen;
Stephan Mueller8e1fa892017-11-10 11:04:52 +0100182 list_for_each_entry_safe(tsgl, tmp, &ctx->tsgl_list, list) {
183 for (i = 0; i < tsgl->cur; i++) {
184 struct scatterlist *process_sg = tsgl->sg + i;
185
186 if (!(process_sg->length) || !sg_page(process_sg))
187 continue;
188 tsgl_src = process_sg;
189 break;
190 }
191 if (tsgl_src)
192 break;
193 }
194 if (processed && !tsgl_src) {
195 err = -EFAULT;
196 goto free;
197 }
Stephan Mueller72548b02017-07-30 14:32:58 +0200198
199 /*
200 * Copy of AAD from source to destination
201 *
202 * The AAD is copied to the destination buffer without change. Even
203 * when user space uses an in-place cipher operation, the kernel
204 * will copy the data as it does not see whether such in-place operation
205 * is initiated.
206 *
207 * To ensure efficiency, the following implementation ensure that the
208 * ciphers are invoked to perform a crypto operation in-place. This
209 * is achieved by memory management specified as follows.
210 */
211
212 /* Use the RX SGL as source (and destination) for crypto op. */
Stephan Mueller8e1fa892017-11-10 11:04:52 +0100213 rsgl_src = areq->first_rsgl.sgl.sg;
Stephan Mueller72548b02017-07-30 14:32:58 +0200214
215 if (ctx->enc) {
216 /*
217 * Encryption operation - The in-place cipher operation is
218 * achieved by the following operation:
219 *
Stephan Mueller75d11e72017-08-09 16:20:00 +0200220 * TX SGL: AAD || PT
Stephan Mueller72548b02017-07-30 14:32:58 +0200221 * | |
222 * | copy |
223 * v v
Stephan Mueller75d11e72017-08-09 16:20:00 +0200224 * RX SGL: AAD || PT || Tag
Stephan Mueller72548b02017-07-30 14:32:58 +0200225 */
Stephan Mueller8e1fa892017-11-10 11:04:52 +0100226 err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
Stephan Mueller72548b02017-07-30 14:32:58 +0200227 areq->first_rsgl.sgl.sg, processed);
228 if (err)
229 goto free;
Stephan Mueller2d975912017-08-02 07:56:19 +0200230 af_alg_pull_tsgl(sk, processed, NULL, 0);
Stephan Mueller72548b02017-07-30 14:32:58 +0200231 } else {
232 /*
233 * Decryption operation - To achieve an in-place cipher
234 * operation, the following SGL structure is used:
235 *
236 * TX SGL: AAD || CT || Tag
237 * | | ^
238 * | copy | | Create SGL link.
239 * v v |
240 * RX SGL: AAD || CT ----+
241 */
242
243 /* Copy AAD || CT to RX SGL buffer for in-place operation. */
Stephan Mueller8e1fa892017-11-10 11:04:52 +0100244 err = crypto_aead_copy_sgl(null_tfm, tsgl_src,
Stephan Mueller72548b02017-07-30 14:32:58 +0200245 areq->first_rsgl.sgl.sg, outlen);
246 if (err)
247 goto free;
248
249 /* Create TX SGL for tag and chain it to RX SGL. */
Stephan Mueller2d975912017-08-02 07:56:19 +0200250 areq->tsgl_entries = af_alg_count_tsgl(sk, processed,
251 processed - as);
Stephan Mueller72548b02017-07-30 14:32:58 +0200252 if (!areq->tsgl_entries)
253 areq->tsgl_entries = 1;
Kees Cook76e43e32018-06-12 14:28:11 -0700254 areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
255 areq->tsgl_entries),
Stephan Mueller72548b02017-07-30 14:32:58 +0200256 GFP_KERNEL);
257 if (!areq->tsgl) {
258 err = -ENOMEM;
259 goto free;
260 }
261 sg_init_table(areq->tsgl, areq->tsgl_entries);
262
263 /* Release TX SGL, except for tag data and reassign tag data. */
Stephan Mueller2d975912017-08-02 07:56:19 +0200264 af_alg_pull_tsgl(sk, processed, areq->tsgl, processed - as);
Stephan Mueller72548b02017-07-30 14:32:58 +0200265
266 /* chain the areq TX SGL holding the tag with RX SGL */
Stephan Mueller2d975912017-08-02 07:56:19 +0200267 if (usedpages) {
Stephan Mueller72548b02017-07-30 14:32:58 +0200268 /* RX SGL present */
Stephan Mueller2d975912017-08-02 07:56:19 +0200269 struct af_alg_sgl *sgl_prev = &areq->last_rsgl->sgl;
Stephan Mueller72548b02017-07-30 14:32:58 +0200270
271 sg_unmark_end(sgl_prev->sg + sgl_prev->npages - 1);
272 sg_chain(sgl_prev->sg, sgl_prev->npages + 1,
273 areq->tsgl);
274 } else
275 /* no RX SGL present (e.g. authentication only) */
Stephan Mueller8e1fa892017-11-10 11:04:52 +0100276 rsgl_src = areq->tsgl;
Stephan Muellerd887c522017-06-25 17:12:59 +0200277 }
Stephan Mueller400c40c2015-02-28 20:50:00 +0100278
Stephan Muellerd887c522017-06-25 17:12:59 +0200279 /* Initialize the crypto operation */
Stephan Mueller8e1fa892017-11-10 11:04:52 +0100280 aead_request_set_crypt(&areq->cra_u.aead_req, rsgl_src,
Stephan Muellerd887c522017-06-25 17:12:59 +0200281 areq->first_rsgl.sgl.sg, used, ctx->iv);
Stephan Mueller2d975912017-08-02 07:56:19 +0200282 aead_request_set_ad(&areq->cra_u.aead_req, ctx->aead_assoclen);
283 aead_request_set_tfm(&areq->cra_u.aead_req, tfm);
Stephan Muellerd887c522017-06-25 17:12:59 +0200284
285 if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
286 /* AIO operation */
Stephan Mueller7d2c3f52017-11-10 13:20:55 +0100287 sock_hold(sk);
Stephan Muellerd887c522017-06-25 17:12:59 +0200288 areq->iocb = msg->msg_iocb;
Stephan Muellerd53c5132017-12-08 11:50:37 +0100289
290 /* Remember output size that will be generated. */
291 areq->outlen = outlen;
292
Stephan Mueller2d975912017-08-02 07:56:19 +0200293 aead_request_set_callback(&areq->cra_u.aead_req,
Stephan Muellerd887c522017-06-25 17:12:59 +0200294 CRYPTO_TFM_REQ_MAY_BACKLOG,
Stephan Mueller2d975912017-08-02 07:56:19 +0200295 af_alg_async_cb, areq);
296 err = ctx->enc ? crypto_aead_encrypt(&areq->cra_u.aead_req) :
297 crypto_aead_decrypt(&areq->cra_u.aead_req);
Stephan Mueller7d2c3f52017-11-10 13:20:55 +0100298
299 /* AIO operation in progress */
Stephan Muellerd53c5132017-12-08 11:50:37 +0100300 if (err == -EINPROGRESS || err == -EBUSY)
Stephan Mueller7d2c3f52017-11-10 13:20:55 +0100301 return -EIOCBQUEUED;
Stephan Mueller7d2c3f52017-11-10 13:20:55 +0100302
303 sock_put(sk);
Stephan Muellerd887c522017-06-25 17:12:59 +0200304 } else {
305 /* Synchronous operation */
Stephan Mueller2d975912017-08-02 07:56:19 +0200306 aead_request_set_callback(&areq->cra_u.aead_req,
Stephan Muellerd887c522017-06-25 17:12:59 +0200307 CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossef2c3f8b12017-10-18 08:00:39 +0100308 crypto_req_done, &ctx->wait);
309 err = crypto_wait_req(ctx->enc ?
Stephan Mueller2d975912017-08-02 07:56:19 +0200310 crypto_aead_encrypt(&areq->cra_u.aead_req) :
311 crypto_aead_decrypt(&areq->cra_u.aead_req),
Gilad Ben-Yossef2c3f8b12017-10-18 08:00:39 +0100312 &ctx->wait);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100313 }
314
Stephan Muellerd887c522017-06-25 17:12:59 +0200315
316free:
Stephan Mueller7d2c3f52017-11-10 13:20:55 +0100317 af_alg_free_resources(areq);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100318
319 return err ? err : outlen;
320}
321
Stephan Muellerd887c522017-06-25 17:12:59 +0200322static int aead_recvmsg(struct socket *sock, struct msghdr *msg,
323 size_t ignored, int flags)
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800324{
Stephan Muellerd887c522017-06-25 17:12:59 +0200325 struct sock *sk = sock->sk;
326 int ret = 0;
327
328 lock_sock(sk);
329 while (msg_data_left(msg)) {
330 int err = _aead_recvmsg(sock, msg, ignored, flags);
331
332 /*
333 * This error covers -EIOCBQUEUED which implies that we can
334 * only handle one AIO request. If the caller wants to have
335 * multiple AIO requests in parallel, he must make multiple
336 * separate AIO calls.
Stephan Mueller5703c822017-07-30 14:31:18 +0200337 *
338 * Also return the error if no data has been processed so far.
Stephan Muellerd887c522017-06-25 17:12:59 +0200339 */
340 if (err <= 0) {
Stephan Mueller5703c822017-07-30 14:31:18 +0200341 if (err == -EIOCBQUEUED || err == -EBADMSG || !ret)
Stephan Muellerd887c522017-06-25 17:12:59 +0200342 ret = err;
343 goto out;
344 }
345
346 ret += err;
347 }
348
349out:
Stephan Mueller2d975912017-08-02 07:56:19 +0200350 af_alg_wmem_wakeup(sk);
Stephan Muellerd887c522017-06-25 17:12:59 +0200351 release_sock(sk);
352 return ret;
Tadeusz Struk83094e5e2016-03-11 11:50:33 -0800353}
354
Stephan Mueller400c40c2015-02-28 20:50:00 +0100355static struct proto_ops algif_aead_ops = {
356 .family = PF_ALG,
357
358 .connect = sock_no_connect,
359 .socketpair = sock_no_socketpair,
360 .getname = sock_no_getname,
361 .ioctl = sock_no_ioctl,
362 .listen = sock_no_listen,
363 .shutdown = sock_no_shutdown,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100364 .mmap = sock_no_mmap,
365 .bind = sock_no_bind,
366 .accept = sock_no_accept,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100367
368 .release = af_alg_release,
369 .sendmsg = aead_sendmsg,
Stephan Mueller2d975912017-08-02 07:56:19 +0200370 .sendpage = af_alg_sendpage,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100371 .recvmsg = aead_recvmsg,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700372 .poll = af_alg_poll,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100373};
374
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200375static int aead_check_key(struct socket *sock)
376{
377 int err = 0;
378 struct sock *psk;
379 struct alg_sock *pask;
380 struct aead_tfm *tfm;
381 struct sock *sk = sock->sk;
382 struct alg_sock *ask = alg_sk(sk);
383
384 lock_sock(sk);
Herbert Xu34c86f42020-06-08 16:48:43 +1000385 if (!atomic_read(&ask->nokey_refcnt))
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200386 goto unlock_child;
387
388 psk = ask->parent;
389 pask = alg_sk(ask->parent);
390 tfm = pask->private;
391
392 err = -ENOKEY;
393 lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
Eric Biggersdc26c172018-01-03 11:16:30 -0800394 if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200395 goto unlock;
396
Herbert Xu34c86f42020-06-08 16:48:43 +1000397 atomic_dec(&pask->nokey_refcnt);
398 atomic_set(&ask->nokey_refcnt, 0);
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200399
400 err = 0;
401
402unlock:
403 release_sock(psk);
404unlock_child:
405 release_sock(sk);
406
407 return err;
408}
409
410static int aead_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
411 size_t size)
412{
413 int err;
414
415 err = aead_check_key(sock);
416 if (err)
417 return err;
418
419 return aead_sendmsg(sock, msg, size);
420}
421
422static ssize_t aead_sendpage_nokey(struct socket *sock, struct page *page,
423 int offset, size_t size, int flags)
424{
425 int err;
426
427 err = aead_check_key(sock);
428 if (err)
429 return err;
430
Stephan Mueller2d975912017-08-02 07:56:19 +0200431 return af_alg_sendpage(sock, page, offset, size, flags);
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200432}
433
434static int aead_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
435 size_t ignored, int flags)
436{
437 int err;
438
439 err = aead_check_key(sock);
440 if (err)
441 return err;
442
443 return aead_recvmsg(sock, msg, ignored, flags);
444}
445
446static struct proto_ops algif_aead_ops_nokey = {
447 .family = PF_ALG,
448
449 .connect = sock_no_connect,
450 .socketpair = sock_no_socketpair,
451 .getname = sock_no_getname,
452 .ioctl = sock_no_ioctl,
453 .listen = sock_no_listen,
454 .shutdown = sock_no_shutdown,
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200455 .mmap = sock_no_mmap,
456 .bind = sock_no_bind,
457 .accept = sock_no_accept,
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200458
459 .release = af_alg_release,
460 .sendmsg = aead_sendmsg_nokey,
461 .sendpage = aead_sendpage_nokey,
462 .recvmsg = aead_recvmsg_nokey,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700463 .poll = af_alg_poll,
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200464};
465
Stephan Mueller400c40c2015-02-28 20:50:00 +0100466static void *aead_bind(const char *name, u32 type, u32 mask)
467{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200468 struct aead_tfm *tfm;
469 struct crypto_aead *aead;
Kees Cook8d605392018-09-18 19:10:51 -0700470 struct crypto_sync_skcipher *null_tfm;
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200471
472 tfm = kzalloc(sizeof(*tfm), GFP_KERNEL);
473 if (!tfm)
474 return ERR_PTR(-ENOMEM);
475
476 aead = crypto_alloc_aead(name, type, mask);
477 if (IS_ERR(aead)) {
478 kfree(tfm);
479 return ERR_CAST(aead);
480 }
481
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800482 null_tfm = crypto_get_default_null_skcipher();
Stephan Mueller72548b02017-07-30 14:32:58 +0200483 if (IS_ERR(null_tfm)) {
484 crypto_free_aead(aead);
485 kfree(tfm);
486 return ERR_CAST(null_tfm);
487 }
488
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200489 tfm->aead = aead;
Stephan Mueller72548b02017-07-30 14:32:58 +0200490 tfm->null_tfm = null_tfm;
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200491
492 return tfm;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100493}
494
495static void aead_release(void *private)
496{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200497 struct aead_tfm *tfm = private;
498
499 crypto_free_aead(tfm->aead);
Eric Biggers3a2d4fb2017-12-07 10:56:34 -0800500 crypto_put_default_null_skcipher();
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200501 kfree(tfm);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100502}
503
504static int aead_setauthsize(void *private, unsigned int authsize)
505{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200506 struct aead_tfm *tfm = private;
507
508 return crypto_aead_setauthsize(tfm->aead, authsize);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100509}
510
511static int aead_setkey(void *private, const u8 *key, unsigned int keylen)
512{
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200513 struct aead_tfm *tfm = private;
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200514
Eric Biggersdc26c172018-01-03 11:16:30 -0800515 return crypto_aead_setkey(tfm->aead, key, keylen);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100516}
517
518static void aead_sock_destruct(struct sock *sk)
519{
520 struct alg_sock *ask = alg_sk(sk);
Stephan Mueller2d975912017-08-02 07:56:19 +0200521 struct af_alg_ctx *ctx = ask->private;
Stephan Muellerd887c522017-06-25 17:12:59 +0200522 struct sock *psk = ask->parent;
523 struct alg_sock *pask = alg_sk(psk);
524 struct aead_tfm *aeadc = pask->private;
525 struct crypto_aead *tfm = aeadc->aead;
526 unsigned int ivlen = crypto_aead_ivsize(tfm);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100527
Stephan Mueller2d975912017-08-02 07:56:19 +0200528 af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100529 sock_kzfree_s(sk, ctx->iv, ivlen);
530 sock_kfree_s(sk, ctx, ctx->len);
531 af_alg_release_parent(sk);
532}
533
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200534static int aead_accept_parent_nokey(void *private, struct sock *sk)
Stephan Mueller400c40c2015-02-28 20:50:00 +0100535{
Stephan Mueller2d975912017-08-02 07:56:19 +0200536 struct af_alg_ctx *ctx;
Stephan Mueller400c40c2015-02-28 20:50:00 +0100537 struct alg_sock *ask = alg_sk(sk);
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200538 struct aead_tfm *tfm = private;
539 struct crypto_aead *aead = tfm->aead;
Stephan Muellerd887c522017-06-25 17:12:59 +0200540 unsigned int len = sizeof(*ctx);
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200541 unsigned int ivlen = crypto_aead_ivsize(aead);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100542
543 ctx = sock_kmalloc(sk, len, GFP_KERNEL);
544 if (!ctx)
545 return -ENOMEM;
546 memset(ctx, 0, len);
547
548 ctx->iv = sock_kmalloc(sk, ivlen, GFP_KERNEL);
549 if (!ctx->iv) {
550 sock_kfree_s(sk, ctx, len);
551 return -ENOMEM;
552 }
553 memset(ctx->iv, 0, ivlen);
554
Stephan Muellerd887c522017-06-25 17:12:59 +0200555 INIT_LIST_HEAD(&ctx->tsgl_list);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100556 ctx->len = len;
Gilad Ben-Yossef2c3f8b12017-10-18 08:00:39 +0100557 crypto_init_wait(&ctx->wait);
Stephan Mueller400c40c2015-02-28 20:50:00 +0100558
559 ask->private = ctx;
560
Stephan Mueller400c40c2015-02-28 20:50:00 +0100561 sk->sk_destruct = aead_sock_destruct;
562
563 return 0;
564}
565
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200566static int aead_accept_parent(void *private, struct sock *sk)
567{
568 struct aead_tfm *tfm = private;
569
Eric Biggersdc26c172018-01-03 11:16:30 -0800570 if (crypto_aead_get_flags(tfm->aead) & CRYPTO_TFM_NEED_KEY)
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200571 return -ENOKEY;
572
573 return aead_accept_parent_nokey(private, sk);
574}
575
Stephan Mueller400c40c2015-02-28 20:50:00 +0100576static const struct af_alg_type algif_type_aead = {
577 .bind = aead_bind,
578 .release = aead_release,
579 .setkey = aead_setkey,
580 .setauthsize = aead_setauthsize,
581 .accept = aead_accept_parent,
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200582 .accept_nokey = aead_accept_parent_nokey,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100583 .ops = &algif_aead_ops,
Stephan Mueller2a2a2512017-04-24 11:15:23 +0200584 .ops_nokey = &algif_aead_ops_nokey,
Stephan Mueller400c40c2015-02-28 20:50:00 +0100585 .name = "aead",
586 .owner = THIS_MODULE
587};
588
589static int __init algif_aead_init(void)
590{
591 return af_alg_register_type(&algif_type_aead);
592}
593
594static void __exit algif_aead_exit(void)
595{
596 int err = af_alg_unregister_type(&algif_type_aead);
597 BUG_ON(err);
598}
599
600module_init(algif_aead_init);
601module_exit(algif_aead_exit);
602MODULE_LICENSE("GPL");
603MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
604MODULE_DESCRIPTION("AEAD kernel crypto API user space interface");