blob: 09d5a34ab339974239c5c90d5cb62ad0b5f158c9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Martin Willi71ebc4d2015-06-01 13:44:00 +02002/*
3 * ChaCha20-Poly1305 AEAD, RFC7539
4 *
5 * Copyright (C) 2015 Martin Willi
Martin Willi71ebc4d2015-06-01 13:44:00 +02006 */
7
8#include <crypto/internal/aead.h>
9#include <crypto/internal/hash.h>
10#include <crypto/internal/skcipher.h>
11#include <crypto/scatterwalk.h>
Eric Biggers1ca1b912018-11-16 17:26:21 -080012#include <crypto/chacha.h>
Martin Willi2546f812015-07-16 19:14:05 +020013#include <crypto/poly1305.h>
Martin Willi71ebc4d2015-06-01 13:44:00 +020014#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18
19#include "internal.h"
20
Martin Willi71ebc4d2015-06-01 13:44:00 +020021struct chachapoly_instance_ctx {
22 struct crypto_skcipher_spawn chacha;
23 struct crypto_ahash_spawn poly;
24 unsigned int saltlen;
25};
26
27struct chachapoly_ctx {
Herbert Xu1e1f0062016-07-12 13:17:40 +080028 struct crypto_skcipher *chacha;
Martin Willi71ebc4d2015-06-01 13:44:00 +020029 struct crypto_ahash *poly;
30 /* key bytes we use for the ChaCha20 IV */
31 unsigned int saltlen;
32 u8 salt[];
33};
34
35struct poly_req {
36 /* zero byte padding for AD/ciphertext, as needed */
37 u8 pad[POLY1305_BLOCK_SIZE];
38 /* tail data with AD/ciphertext lengths */
39 struct {
40 __le64 assoclen;
41 __le64 cryptlen;
42 } tail;
43 struct scatterlist src[1];
44 struct ahash_request req; /* must be last member */
45};
46
47struct chacha_req {
Eric Biggers1ca1b912018-11-16 17:26:21 -080048 u8 iv[CHACHA_IV_SIZE];
Martin Willi71ebc4d2015-06-01 13:44:00 +020049 struct scatterlist src[1];
Herbert Xu1e1f0062016-07-12 13:17:40 +080050 struct skcipher_request req; /* must be last member */
Martin Willi71ebc4d2015-06-01 13:44:00 +020051};
52
53struct chachapoly_req_ctx {
Herbert Xu74790922015-07-16 12:35:08 +080054 struct scatterlist src[2];
55 struct scatterlist dst[2];
Martin Willic2b7b20a2015-06-16 11:34:16 +020056 /* the key we generate for Poly1305 using Chacha20 */
57 u8 key[POLY1305_KEY_SIZE];
Martin Willi71ebc4d2015-06-01 13:44:00 +020058 /* calculated Poly1305 tag */
59 u8 tag[POLY1305_DIGEST_SIZE];
60 /* length of data to en/decrypt, without ICV */
61 unsigned int cryptlen;
Herbert Xu74790922015-07-16 12:35:08 +080062 /* Actual AD, excluding IV */
63 unsigned int assoclen;
Eric Biggers7545b6c2019-05-31 11:12:30 -070064 /* request flags, with MAY_SLEEP cleared if needed */
65 u32 flags;
Martin Willi71ebc4d2015-06-01 13:44:00 +020066 union {
67 struct poly_req poly;
68 struct chacha_req chacha;
69 } u;
70};
71
72static inline void async_done_continue(struct aead_request *req, int err,
73 int (*cont)(struct aead_request *))
74{
Eric Biggers7545b6c2019-05-31 11:12:30 -070075 if (!err) {
76 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
77
78 rctx->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
Martin Willi71ebc4d2015-06-01 13:44:00 +020079 err = cont(req);
Eric Biggers7545b6c2019-05-31 11:12:30 -070080 }
Martin Willi71ebc4d2015-06-01 13:44:00 +020081
82 if (err != -EINPROGRESS && err != -EBUSY)
83 aead_request_complete(req, err);
84}
85
86static void chacha_iv(u8 *iv, struct aead_request *req, u32 icb)
87{
88 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
89 __le32 leicb = cpu_to_le32(icb);
90
91 memcpy(iv, &leicb, sizeof(leicb));
92 memcpy(iv + sizeof(leicb), ctx->salt, ctx->saltlen);
93 memcpy(iv + sizeof(leicb) + ctx->saltlen, req->iv,
Eric Biggers1ca1b912018-11-16 17:26:21 -080094 CHACHA_IV_SIZE - sizeof(leicb) - ctx->saltlen);
Martin Willi71ebc4d2015-06-01 13:44:00 +020095}
96
97static int poly_verify_tag(struct aead_request *req)
98{
99 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
100 u8 tag[sizeof(rctx->tag)];
101
Herbert Xu74790922015-07-16 12:35:08 +0800102 scatterwalk_map_and_copy(tag, req->src,
103 req->assoclen + rctx->cryptlen,
104 sizeof(tag), 0);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200105 if (crypto_memneq(tag, rctx->tag, sizeof(tag)))
106 return -EBADMSG;
107 return 0;
108}
109
110static int poly_copy_tag(struct aead_request *req)
111{
112 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
113
Herbert Xu74790922015-07-16 12:35:08 +0800114 scatterwalk_map_and_copy(rctx->tag, req->dst,
115 req->assoclen + rctx->cryptlen,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200116 sizeof(rctx->tag), 1);
117 return 0;
118}
119
120static void chacha_decrypt_done(struct crypto_async_request *areq, int err)
121{
122 async_done_continue(areq->data, err, poly_verify_tag);
123}
124
125static int chacha_decrypt(struct aead_request *req)
126{
127 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
128 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
129 struct chacha_req *creq = &rctx->u.chacha;
Herbert Xu74790922015-07-16 12:35:08 +0800130 struct scatterlist *src, *dst;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200131 int err;
132
Jason A. Donenfeld161151d2015-12-06 02:51:38 +0100133 if (rctx->cryptlen == 0)
134 goto skip;
135
Martin Willi71ebc4d2015-06-01 13:44:00 +0200136 chacha_iv(creq->iv, req, 1);
137
Herbert Xu74790922015-07-16 12:35:08 +0800138 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
139 dst = src;
Eric Biggers76cadf22019-06-02 22:46:34 -0700140 if (req->src != req->dst)
Herbert Xu74790922015-07-16 12:35:08 +0800141 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
Herbert Xu74790922015-07-16 12:35:08 +0800142
Eric Biggers7545b6c2019-05-31 11:12:30 -0700143 skcipher_request_set_callback(&creq->req, rctx->flags,
Herbert Xu1e1f0062016-07-12 13:17:40 +0800144 chacha_decrypt_done, req);
145 skcipher_request_set_tfm(&creq->req, ctx->chacha);
146 skcipher_request_set_crypt(&creq->req, src, dst,
147 rctx->cryptlen, creq->iv);
148 err = crypto_skcipher_decrypt(&creq->req);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200149 if (err)
150 return err;
151
Jason A. Donenfeld161151d2015-12-06 02:51:38 +0100152skip:
Martin Willi71ebc4d2015-06-01 13:44:00 +0200153 return poly_verify_tag(req);
154}
155
156static int poly_tail_continue(struct aead_request *req)
157{
158 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
159
160 if (rctx->cryptlen == req->cryptlen) /* encrypting */
161 return poly_copy_tag(req);
162
163 return chacha_decrypt(req);
164}
165
166static void poly_tail_done(struct crypto_async_request *areq, int err)
167{
168 async_done_continue(areq->data, err, poly_tail_continue);
169}
170
171static int poly_tail(struct aead_request *req)
172{
Herbert Xu74790922015-07-16 12:35:08 +0800173 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
174 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200175 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
176 struct poly_req *preq = &rctx->u.poly;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200177 int err;
178
Eric Biggers76cadf22019-06-02 22:46:34 -0700179 preq->tail.assoclen = cpu_to_le64(rctx->assoclen);
180 preq->tail.cryptlen = cpu_to_le64(rctx->cryptlen);
181 sg_init_one(preq->src, &preq->tail, sizeof(preq->tail));
Martin Willi71ebc4d2015-06-01 13:44:00 +0200182
Eric Biggers7545b6c2019-05-31 11:12:30 -0700183 ahash_request_set_callback(&preq->req, rctx->flags,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200184 poly_tail_done, req);
185 ahash_request_set_tfm(&preq->req, ctx->poly);
186 ahash_request_set_crypt(&preq->req, preq->src,
187 rctx->tag, sizeof(preq->tail));
188
189 err = crypto_ahash_finup(&preq->req);
190 if (err)
191 return err;
192
193 return poly_tail_continue(req);
194}
195
196static void poly_cipherpad_done(struct crypto_async_request *areq, int err)
197{
198 async_done_continue(areq->data, err, poly_tail);
199}
200
201static int poly_cipherpad(struct aead_request *req)
202{
203 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
204 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
205 struct poly_req *preq = &rctx->u.poly;
Eric Biggers76cadf22019-06-02 22:46:34 -0700206 unsigned int padlen;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200207 int err;
208
Eric Biggers76cadf22019-06-02 22:46:34 -0700209 padlen = -rctx->cryptlen % POLY1305_BLOCK_SIZE;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200210 memset(preq->pad, 0, sizeof(preq->pad));
Eric Biggers76cadf22019-06-02 22:46:34 -0700211 sg_init_one(preq->src, preq->pad, padlen);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200212
Eric Biggers7545b6c2019-05-31 11:12:30 -0700213 ahash_request_set_callback(&preq->req, rctx->flags,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200214 poly_cipherpad_done, req);
215 ahash_request_set_tfm(&preq->req, ctx->poly);
216 ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
217
218 err = crypto_ahash_update(&preq->req);
219 if (err)
220 return err;
221
222 return poly_tail(req);
223}
224
225static void poly_cipher_done(struct crypto_async_request *areq, int err)
226{
227 async_done_continue(areq->data, err, poly_cipherpad);
228}
229
230static int poly_cipher(struct aead_request *req)
231{
232 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
233 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
234 struct poly_req *preq = &rctx->u.poly;
235 struct scatterlist *crypt = req->src;
236 int err;
237
238 if (rctx->cryptlen == req->cryptlen) /* encrypting */
239 crypt = req->dst;
240
Herbert Xu74790922015-07-16 12:35:08 +0800241 crypt = scatterwalk_ffwd(rctx->src, crypt, req->assoclen);
242
Eric Biggers7545b6c2019-05-31 11:12:30 -0700243 ahash_request_set_callback(&preq->req, rctx->flags,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200244 poly_cipher_done, req);
245 ahash_request_set_tfm(&preq->req, ctx->poly);
246 ahash_request_set_crypt(&preq->req, crypt, NULL, rctx->cryptlen);
247
248 err = crypto_ahash_update(&preq->req);
249 if (err)
250 return err;
251
252 return poly_cipherpad(req);
253}
254
255static void poly_adpad_done(struct crypto_async_request *areq, int err)
256{
257 async_done_continue(areq->data, err, poly_cipher);
258}
259
260static int poly_adpad(struct aead_request *req)
261{
262 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
263 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
264 struct poly_req *preq = &rctx->u.poly;
Eric Biggers76cadf22019-06-02 22:46:34 -0700265 unsigned int padlen;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200266 int err;
267
Eric Biggers76cadf22019-06-02 22:46:34 -0700268 padlen = -rctx->assoclen % POLY1305_BLOCK_SIZE;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200269 memset(preq->pad, 0, sizeof(preq->pad));
Eric Biggers76cadf22019-06-02 22:46:34 -0700270 sg_init_one(preq->src, preq->pad, padlen);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200271
Eric Biggers7545b6c2019-05-31 11:12:30 -0700272 ahash_request_set_callback(&preq->req, rctx->flags,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200273 poly_adpad_done, req);
274 ahash_request_set_tfm(&preq->req, ctx->poly);
275 ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
276
277 err = crypto_ahash_update(&preq->req);
278 if (err)
279 return err;
280
281 return poly_cipher(req);
282}
283
284static void poly_ad_done(struct crypto_async_request *areq, int err)
285{
286 async_done_continue(areq->data, err, poly_adpad);
287}
288
289static int poly_ad(struct aead_request *req)
290{
291 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
292 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
293 struct poly_req *preq = &rctx->u.poly;
294 int err;
295
Eric Biggers7545b6c2019-05-31 11:12:30 -0700296 ahash_request_set_callback(&preq->req, rctx->flags,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200297 poly_ad_done, req);
298 ahash_request_set_tfm(&preq->req, ctx->poly);
Herbert Xu74790922015-07-16 12:35:08 +0800299 ahash_request_set_crypt(&preq->req, req->src, NULL, rctx->assoclen);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200300
301 err = crypto_ahash_update(&preq->req);
302 if (err)
303 return err;
304
305 return poly_adpad(req);
306}
307
Martin Willic2b7b20a2015-06-16 11:34:16 +0200308static void poly_setkey_done(struct crypto_async_request *areq, int err)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200309{
310 async_done_continue(areq->data, err, poly_ad);
311}
312
Martin Willic2b7b20a2015-06-16 11:34:16 +0200313static int poly_setkey(struct aead_request *req)
314{
315 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
316 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
317 struct poly_req *preq = &rctx->u.poly;
318 int err;
319
Eric Biggers76cadf22019-06-02 22:46:34 -0700320 sg_init_one(preq->src, rctx->key, sizeof(rctx->key));
Martin Willic2b7b20a2015-06-16 11:34:16 +0200321
Eric Biggers7545b6c2019-05-31 11:12:30 -0700322 ahash_request_set_callback(&preq->req, rctx->flags,
Martin Willic2b7b20a2015-06-16 11:34:16 +0200323 poly_setkey_done, req);
324 ahash_request_set_tfm(&preq->req, ctx->poly);
325 ahash_request_set_crypt(&preq->req, preq->src, NULL, sizeof(rctx->key));
326
327 err = crypto_ahash_update(&preq->req);
328 if (err)
329 return err;
330
331 return poly_ad(req);
332}
333
334static void poly_init_done(struct crypto_async_request *areq, int err)
335{
336 async_done_continue(areq->data, err, poly_setkey);
337}
338
Martin Willi71ebc4d2015-06-01 13:44:00 +0200339static int poly_init(struct aead_request *req)
340{
341 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
342 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
343 struct poly_req *preq = &rctx->u.poly;
344 int err;
345
Eric Biggers7545b6c2019-05-31 11:12:30 -0700346 ahash_request_set_callback(&preq->req, rctx->flags,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200347 poly_init_done, req);
348 ahash_request_set_tfm(&preq->req, ctx->poly);
349
350 err = crypto_ahash_init(&preq->req);
351 if (err)
352 return err;
353
Martin Willic2b7b20a2015-06-16 11:34:16 +0200354 return poly_setkey(req);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200355}
356
357static void poly_genkey_done(struct crypto_async_request *areq, int err)
358{
Martin Willic2b7b20a2015-06-16 11:34:16 +0200359 async_done_continue(areq->data, err, poly_init);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200360}
361
362static int poly_genkey(struct aead_request *req)
363{
Herbert Xu74790922015-07-16 12:35:08 +0800364 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
365 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200366 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
367 struct chacha_req *creq = &rctx->u.chacha;
368 int err;
369
Herbert Xu74790922015-07-16 12:35:08 +0800370 rctx->assoclen = req->assoclen;
371
372 if (crypto_aead_ivsize(tfm) == 8) {
373 if (rctx->assoclen < 8)
374 return -EINVAL;
375 rctx->assoclen -= 8;
376 }
377
Martin Willic2b7b20a2015-06-16 11:34:16 +0200378 memset(rctx->key, 0, sizeof(rctx->key));
Eric Biggers76cadf22019-06-02 22:46:34 -0700379 sg_init_one(creq->src, rctx->key, sizeof(rctx->key));
Martin Willi71ebc4d2015-06-01 13:44:00 +0200380
381 chacha_iv(creq->iv, req, 0);
382
Eric Biggers7545b6c2019-05-31 11:12:30 -0700383 skcipher_request_set_callback(&creq->req, rctx->flags,
Herbert Xu1e1f0062016-07-12 13:17:40 +0800384 poly_genkey_done, req);
385 skcipher_request_set_tfm(&creq->req, ctx->chacha);
386 skcipher_request_set_crypt(&creq->req, creq->src, creq->src,
387 POLY1305_KEY_SIZE, creq->iv);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200388
Herbert Xu1e1f0062016-07-12 13:17:40 +0800389 err = crypto_skcipher_decrypt(&creq->req);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200390 if (err)
391 return err;
392
Martin Willic2b7b20a2015-06-16 11:34:16 +0200393 return poly_init(req);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200394}
395
396static void chacha_encrypt_done(struct crypto_async_request *areq, int err)
397{
398 async_done_continue(areq->data, err, poly_genkey);
399}
400
401static int chacha_encrypt(struct aead_request *req)
402{
403 struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
404 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
405 struct chacha_req *creq = &rctx->u.chacha;
Herbert Xu74790922015-07-16 12:35:08 +0800406 struct scatterlist *src, *dst;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200407 int err;
408
Jason A. Donenfeld161151d2015-12-06 02:51:38 +0100409 if (req->cryptlen == 0)
410 goto skip;
411
Martin Willi71ebc4d2015-06-01 13:44:00 +0200412 chacha_iv(creq->iv, req, 1);
413
Herbert Xu74790922015-07-16 12:35:08 +0800414 src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
415 dst = src;
Eric Biggers76cadf22019-06-02 22:46:34 -0700416 if (req->src != req->dst)
Herbert Xu74790922015-07-16 12:35:08 +0800417 dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
Herbert Xu74790922015-07-16 12:35:08 +0800418
Eric Biggers7545b6c2019-05-31 11:12:30 -0700419 skcipher_request_set_callback(&creq->req, rctx->flags,
Herbert Xu1e1f0062016-07-12 13:17:40 +0800420 chacha_encrypt_done, req);
421 skcipher_request_set_tfm(&creq->req, ctx->chacha);
422 skcipher_request_set_crypt(&creq->req, src, dst,
423 req->cryptlen, creq->iv);
424 err = crypto_skcipher_encrypt(&creq->req);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200425 if (err)
426 return err;
427
Jason A. Donenfeld161151d2015-12-06 02:51:38 +0100428skip:
Martin Willi71ebc4d2015-06-01 13:44:00 +0200429 return poly_genkey(req);
430}
431
432static int chachapoly_encrypt(struct aead_request *req)
433{
434 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
435
436 rctx->cryptlen = req->cryptlen;
Eric Biggers7545b6c2019-05-31 11:12:30 -0700437 rctx->flags = aead_request_flags(req);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200438
439 /* encrypt call chain:
440 * - chacha_encrypt/done()
Martin Willic2b7b20a2015-06-16 11:34:16 +0200441 * - poly_genkey/done()
Martin Willi71ebc4d2015-06-01 13:44:00 +0200442 * - poly_init/done()
Martin Willic2b7b20a2015-06-16 11:34:16 +0200443 * - poly_setkey/done()
Martin Willi71ebc4d2015-06-01 13:44:00 +0200444 * - poly_ad/done()
445 * - poly_adpad/done()
446 * - poly_cipher/done()
447 * - poly_cipherpad/done()
448 * - poly_tail/done/continue()
449 * - poly_copy_tag()
450 */
451 return chacha_encrypt(req);
452}
453
454static int chachapoly_decrypt(struct aead_request *req)
455{
456 struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
457
Martin Willi71ebc4d2015-06-01 13:44:00 +0200458 rctx->cryptlen = req->cryptlen - POLY1305_DIGEST_SIZE;
Eric Biggers7545b6c2019-05-31 11:12:30 -0700459 rctx->flags = aead_request_flags(req);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200460
461 /* decrypt call chain:
Martin Willic2b7b20a2015-06-16 11:34:16 +0200462 * - poly_genkey/done()
Martin Willi71ebc4d2015-06-01 13:44:00 +0200463 * - poly_init/done()
Martin Willic2b7b20a2015-06-16 11:34:16 +0200464 * - poly_setkey/done()
Martin Willi71ebc4d2015-06-01 13:44:00 +0200465 * - poly_ad/done()
466 * - poly_adpad/done()
467 * - poly_cipher/done()
468 * - poly_cipherpad/done()
469 * - poly_tail/done/continue()
470 * - chacha_decrypt/done()
471 * - poly_verify_tag()
472 */
473 return poly_genkey(req);
474}
475
476static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
477 unsigned int keylen)
478{
479 struct chachapoly_ctx *ctx = crypto_aead_ctx(aead);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200480
Eric Biggers1ca1b912018-11-16 17:26:21 -0800481 if (keylen != ctx->saltlen + CHACHA_KEY_SIZE)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200482 return -EINVAL;
483
484 keylen -= ctx->saltlen;
485 memcpy(ctx->salt, key + keylen, ctx->saltlen);
486
Herbert Xu1e1f0062016-07-12 13:17:40 +0800487 crypto_skcipher_clear_flags(ctx->chacha, CRYPTO_TFM_REQ_MASK);
488 crypto_skcipher_set_flags(ctx->chacha, crypto_aead_get_flags(aead) &
489 CRYPTO_TFM_REQ_MASK);
Eric Biggersaf5034e2019-12-30 21:19:38 -0600490 return crypto_skcipher_setkey(ctx->chacha, key, keylen);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200491}
492
493static int chachapoly_setauthsize(struct crypto_aead *tfm,
494 unsigned int authsize)
495{
496 if (authsize != POLY1305_DIGEST_SIZE)
497 return -EINVAL;
498
499 return 0;
500}
501
Herbert Xu74790922015-07-16 12:35:08 +0800502static int chachapoly_init(struct crypto_aead *tfm)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200503{
Herbert Xu74790922015-07-16 12:35:08 +0800504 struct aead_instance *inst = aead_alg_instance(tfm);
505 struct chachapoly_instance_ctx *ictx = aead_instance_ctx(inst);
506 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
Herbert Xu1e1f0062016-07-12 13:17:40 +0800507 struct crypto_skcipher *chacha;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200508 struct crypto_ahash *poly;
509 unsigned long align;
510
511 poly = crypto_spawn_ahash(&ictx->poly);
512 if (IS_ERR(poly))
513 return PTR_ERR(poly);
514
Eric Biggers60425a82016-10-28 09:52:19 -0700515 chacha = crypto_spawn_skcipher(&ictx->chacha);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200516 if (IS_ERR(chacha)) {
517 crypto_free_ahash(poly);
518 return PTR_ERR(chacha);
519 }
520
521 ctx->chacha = chacha;
522 ctx->poly = poly;
523 ctx->saltlen = ictx->saltlen;
524
Herbert Xu74790922015-07-16 12:35:08 +0800525 align = crypto_aead_alignmask(tfm);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200526 align &= ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu74790922015-07-16 12:35:08 +0800527 crypto_aead_set_reqsize(
528 tfm,
529 align + offsetof(struct chachapoly_req_ctx, u) +
530 max(offsetof(struct chacha_req, req) +
Herbert Xu1e1f0062016-07-12 13:17:40 +0800531 sizeof(struct skcipher_request) +
532 crypto_skcipher_reqsize(chacha),
Herbert Xu74790922015-07-16 12:35:08 +0800533 offsetof(struct poly_req, req) +
534 sizeof(struct ahash_request) +
535 crypto_ahash_reqsize(poly)));
Martin Willi71ebc4d2015-06-01 13:44:00 +0200536
537 return 0;
538}
539
Herbert Xu74790922015-07-16 12:35:08 +0800540static void chachapoly_exit(struct crypto_aead *tfm)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200541{
Herbert Xu74790922015-07-16 12:35:08 +0800542 struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200543
544 crypto_free_ahash(ctx->poly);
Herbert Xu1e1f0062016-07-12 13:17:40 +0800545 crypto_free_skcipher(ctx->chacha);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200546}
547
Herbert Xu74790922015-07-16 12:35:08 +0800548static void chachapoly_free(struct aead_instance *inst)
549{
550 struct chachapoly_instance_ctx *ctx = aead_instance_ctx(inst);
551
552 crypto_drop_skcipher(&ctx->chacha);
553 crypto_drop_ahash(&ctx->poly);
554 kfree(inst);
555}
556
557static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
558 const char *name, unsigned int ivsize)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200559{
560 struct crypto_attr_type *algt;
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800561 u32 mask;
Herbert Xu74790922015-07-16 12:35:08 +0800562 struct aead_instance *inst;
Herbert Xu1e1f0062016-07-12 13:17:40 +0800563 struct skcipher_alg *chacha;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200564 struct crypto_alg *poly;
Herbert Xu74790922015-07-16 12:35:08 +0800565 struct hash_alg_common *poly_hash;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200566 struct chachapoly_instance_ctx *ctx;
567 const char *chacha_name, *poly_name;
568 int err;
569
570 if (ivsize > CHACHAPOLY_IV_SIZE)
Herbert Xu74790922015-07-16 12:35:08 +0800571 return -EINVAL;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200572
573 algt = crypto_get_attr_type(tb);
574 if (IS_ERR(algt))
Herbert Xu74790922015-07-16 12:35:08 +0800575 return PTR_ERR(algt);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200576
Herbert Xu5e4b8c12015-08-13 17:29:06 +0800577 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
Herbert Xu74790922015-07-16 12:35:08 +0800578 return -EINVAL;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200579
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800580 mask = crypto_requires_sync(algt->type, algt->mask);
581
Martin Willi71ebc4d2015-06-01 13:44:00 +0200582 chacha_name = crypto_attr_alg_name(tb[1]);
583 if (IS_ERR(chacha_name))
Herbert Xu74790922015-07-16 12:35:08 +0800584 return PTR_ERR(chacha_name);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200585 poly_name = crypto_attr_alg_name(tb[2]);
586 if (IS_ERR(poly_name))
Herbert Xu74790922015-07-16 12:35:08 +0800587 return PTR_ERR(poly_name);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200588
589 poly = crypto_find_alg(poly_name, &crypto_ahash_type,
590 CRYPTO_ALG_TYPE_HASH,
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800591 CRYPTO_ALG_TYPE_AHASH_MASK | mask);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200592 if (IS_ERR(poly))
Herbert Xu74790922015-07-16 12:35:08 +0800593 return PTR_ERR(poly);
Eric Biggerse57121d2017-12-11 12:15:17 -0800594 poly_hash = __crypto_hash_alg_common(poly);
595
596 err = -EINVAL;
597 if (poly_hash->digestsize != POLY1305_DIGEST_SIZE)
598 goto out_put_poly;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200599
600 err = -ENOMEM;
601 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
602 if (!inst)
603 goto out_put_poly;
604
Herbert Xu74790922015-07-16 12:35:08 +0800605 ctx = aead_instance_ctx(inst);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200606 ctx->saltlen = CHACHAPOLY_IV_SIZE - ivsize;
Herbert Xu74790922015-07-16 12:35:08 +0800607 err = crypto_init_ahash_spawn(&ctx->poly, poly_hash,
608 aead_crypto_instance(inst));
Martin Willi71ebc4d2015-06-01 13:44:00 +0200609 if (err)
610 goto err_free_inst;
611
Eric Biggersb9f76dd2020-01-02 19:58:45 -0800612 err = crypto_grab_skcipher(&ctx->chacha, aead_crypto_instance(inst),
613 chacha_name, 0, mask);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200614 if (err)
615 goto err_drop_poly;
616
Herbert Xu1e1f0062016-07-12 13:17:40 +0800617 chacha = crypto_spawn_skcipher_alg(&ctx->chacha);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200618
619 err = -EINVAL;
620 /* Need 16-byte IV size, including Initial Block Counter value */
Eric Biggers1ca1b912018-11-16 17:26:21 -0800621 if (crypto_skcipher_alg_ivsize(chacha) != CHACHA_IV_SIZE)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200622 goto out_drop_chacha;
623 /* Not a stream cipher? */
Herbert Xu1e1f0062016-07-12 13:17:40 +0800624 if (chacha->base.cra_blocksize != 1)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200625 goto out_drop_chacha;
626
627 err = -ENAMETOOLONG;
Herbert Xu74790922015-07-16 12:35:08 +0800628 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
Eric Biggers5e27f382019-03-31 13:04:16 -0700629 "%s(%s,%s)", name, chacha->base.cra_name,
630 poly->cra_name) >= CRYPTO_MAX_ALG_NAME)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200631 goto out_drop_chacha;
Herbert Xu74790922015-07-16 12:35:08 +0800632 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
Herbert Xu1e1f0062016-07-12 13:17:40 +0800633 "%s(%s,%s)", name, chacha->base.cra_driver_name,
Martin Willi71ebc4d2015-06-01 13:44:00 +0200634 poly->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
635 goto out_drop_chacha;
636
Herbert Xu1e1f0062016-07-12 13:17:40 +0800637 inst->alg.base.cra_flags = (chacha->base.cra_flags | poly->cra_flags) &
Herbert Xu74790922015-07-16 12:35:08 +0800638 CRYPTO_ALG_ASYNC;
Herbert Xu1e1f0062016-07-12 13:17:40 +0800639 inst->alg.base.cra_priority = (chacha->base.cra_priority +
Herbert Xu74790922015-07-16 12:35:08 +0800640 poly->cra_priority) / 2;
641 inst->alg.base.cra_blocksize = 1;
Herbert Xu1e1f0062016-07-12 13:17:40 +0800642 inst->alg.base.cra_alignmask = chacha->base.cra_alignmask |
Herbert Xu74790922015-07-16 12:35:08 +0800643 poly->cra_alignmask;
644 inst->alg.base.cra_ctxsize = sizeof(struct chachapoly_ctx) +
645 ctx->saltlen;
646 inst->alg.ivsize = ivsize;
Herbert Xu1e1f0062016-07-12 13:17:40 +0800647 inst->alg.chunksize = crypto_skcipher_alg_chunksize(chacha);
Herbert Xu74790922015-07-16 12:35:08 +0800648 inst->alg.maxauthsize = POLY1305_DIGEST_SIZE;
649 inst->alg.init = chachapoly_init;
650 inst->alg.exit = chachapoly_exit;
651 inst->alg.encrypt = chachapoly_encrypt;
652 inst->alg.decrypt = chachapoly_decrypt;
653 inst->alg.setkey = chachapoly_setkey;
654 inst->alg.setauthsize = chachapoly_setauthsize;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200655
Herbert Xu74790922015-07-16 12:35:08 +0800656 inst->free = chachapoly_free;
657
658 err = aead_register_instance(tmpl, inst);
659 if (err)
660 goto out_drop_chacha;
661
662out_put_poly:
Martin Willi71ebc4d2015-06-01 13:44:00 +0200663 crypto_mod_put(poly);
Herbert Xu74790922015-07-16 12:35:08 +0800664 return err;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200665
666out_drop_chacha:
667 crypto_drop_skcipher(&ctx->chacha);
668err_drop_poly:
669 crypto_drop_ahash(&ctx->poly);
670err_free_inst:
671 kfree(inst);
Herbert Xu74790922015-07-16 12:35:08 +0800672 goto out_put_poly;
Martin Willi71ebc4d2015-06-01 13:44:00 +0200673}
674
Herbert Xu74790922015-07-16 12:35:08 +0800675static int rfc7539_create(struct crypto_template *tmpl, struct rtattr **tb)
Martin Willi71ebc4d2015-06-01 13:44:00 +0200676{
Herbert Xu74790922015-07-16 12:35:08 +0800677 return chachapoly_create(tmpl, tb, "rfc7539", 12);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200678}
679
Herbert Xu74790922015-07-16 12:35:08 +0800680static int rfc7539esp_create(struct crypto_template *tmpl, struct rtattr **tb)
Martin Willi4db4ad22015-06-01 13:44:02 +0200681{
Herbert Xu74790922015-07-16 12:35:08 +0800682 return chachapoly_create(tmpl, tb, "rfc7539esp", 8);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200683}
684
Xiongfeng Wang1a5e02b2019-01-18 13:58:15 +0800685static struct crypto_template rfc7539_tmpls[] = {
686 {
687 .name = "rfc7539",
688 .create = rfc7539_create,
689 .module = THIS_MODULE,
690 }, {
691 .name = "rfc7539esp",
692 .create = rfc7539esp_create,
693 .module = THIS_MODULE,
694 },
Martin Willi4db4ad22015-06-01 13:44:02 +0200695};
696
Martin Willi71ebc4d2015-06-01 13:44:00 +0200697static int __init chacha20poly1305_module_init(void)
698{
Xiongfeng Wang1a5e02b2019-01-18 13:58:15 +0800699 return crypto_register_templates(rfc7539_tmpls,
700 ARRAY_SIZE(rfc7539_tmpls));
Martin Willi71ebc4d2015-06-01 13:44:00 +0200701}
702
703static void __exit chacha20poly1305_module_exit(void)
704{
Xiongfeng Wang1a5e02b2019-01-18 13:58:15 +0800705 crypto_unregister_templates(rfc7539_tmpls,
706 ARRAY_SIZE(rfc7539_tmpls));
Martin Willi71ebc4d2015-06-01 13:44:00 +0200707}
708
Eric Biggersc4741b22019-04-11 21:57:42 -0700709subsys_initcall(chacha20poly1305_module_init);
Martin Willi71ebc4d2015-06-01 13:44:00 +0200710module_exit(chacha20poly1305_module_exit);
711
712MODULE_LICENSE("GPL");
713MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
714MODULE_DESCRIPTION("ChaCha20-Poly1305 AEAD");
Martin Willi71ebc4d2015-06-01 13:44:00 +0200715MODULE_ALIAS_CRYPTO("rfc7539");
Martin Willi4db4ad22015-06-01 13:44:02 +0200716MODULE_ALIAS_CRYPTO("rfc7539esp");