Blame view

crypto/chacha20poly1305.c 17.7 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
71ebc4d1b   Martin Willi   crypto: chacha20p...
2
3
4
5
  /*
   * ChaCha20-Poly1305 AEAD, RFC7539
   *
   * Copyright (C) 2015 Martin Willi
71ebc4d1b   Martin Willi   crypto: chacha20p...
6
7
8
9
10
11
   */
  
  #include <crypto/internal/aead.h>
  #include <crypto/internal/hash.h>
  #include <crypto/internal/skcipher.h>
  #include <crypto/scatterwalk.h>
1ca1b9179   Eric Biggers   crypto: chacha20-...
12
  #include <crypto/chacha.h>
2546f811e   Martin Willi   crypto: poly1305 ...
13
  #include <crypto/poly1305.h>
71ebc4d1b   Martin Willi   crypto: chacha20p...
14
15
16
17
  #include <linux/err.h>
  #include <linux/init.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
71ebc4d1b   Martin Willi   crypto: chacha20p...
18
19
20
21
22
23
24
  struct chachapoly_instance_ctx {
  	struct crypto_skcipher_spawn chacha;
  	struct crypto_ahash_spawn poly;
  	unsigned int saltlen;
  };
  
  struct chachapoly_ctx {
1e1f00611   Herbert Xu   crypto: chacha20p...
25
  	struct crypto_skcipher *chacha;
71ebc4d1b   Martin Willi   crypto: chacha20p...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  	struct crypto_ahash *poly;
  	/* key bytes we use for the ChaCha20 IV */
  	unsigned int saltlen;
  	u8 salt[];
  };
  
  struct poly_req {
  	/* zero byte padding for AD/ciphertext, as needed */
  	u8 pad[POLY1305_BLOCK_SIZE];
  	/* tail data with AD/ciphertext lengths */
  	struct {
  		__le64 assoclen;
  		__le64 cryptlen;
  	} tail;
  	struct scatterlist src[1];
  	struct ahash_request req; /* must be last member */
  };
  
  struct chacha_req {
1ca1b9179   Eric Biggers   crypto: chacha20-...
45
  	u8 iv[CHACHA_IV_SIZE];
71ebc4d1b   Martin Willi   crypto: chacha20p...
46
  	struct scatterlist src[1];
1e1f00611   Herbert Xu   crypto: chacha20p...
47
  	struct skcipher_request req; /* must be last member */
71ebc4d1b   Martin Willi   crypto: chacha20p...
48
49
50
  };
  
  struct chachapoly_req_ctx {
747909223   Herbert Xu   crypto: chacha20p...
51
52
  	struct scatterlist src[2];
  	struct scatterlist dst[2];
c2b7b20ae   Martin Willi   crypto: poly1305 ...
53
54
  	/* the key we generate for Poly1305 using Chacha20 */
  	u8 key[POLY1305_KEY_SIZE];
71ebc4d1b   Martin Willi   crypto: chacha20p...
55
56
57
58
  	/* calculated Poly1305 tag */
  	u8 tag[POLY1305_DIGEST_SIZE];
  	/* length of data to en/decrypt, without ICV */
  	unsigned int cryptlen;
747909223   Herbert Xu   crypto: chacha20p...
59
60
  	/* Actual AD, excluding IV */
  	unsigned int assoclen;
7545b6c20   Eric Biggers   crypto: chacha20p...
61
62
  	/* request flags, with MAY_SLEEP cleared if needed */
  	u32 flags;
71ebc4d1b   Martin Willi   crypto: chacha20p...
63
64
65
66
67
68
69
70
71
  	union {
  		struct poly_req poly;
  		struct chacha_req chacha;
  	} u;
  };
  
  static inline void async_done_continue(struct aead_request *req, int err,
  				       int (*cont)(struct aead_request *))
  {
7545b6c20   Eric Biggers   crypto: chacha20p...
72
73
74
75
  	if (!err) {
  		struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  
  		rctx->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
71ebc4d1b   Martin Willi   crypto: chacha20p...
76
  		err = cont(req);
7545b6c20   Eric Biggers   crypto: chacha20p...
77
  	}
71ebc4d1b   Martin Willi   crypto: chacha20p...
78
79
80
81
82
83
84
85
86
87
88
89
90
  
  	if (err != -EINPROGRESS && err != -EBUSY)
  		aead_request_complete(req, err);
  }
  
  static void chacha_iv(u8 *iv, struct aead_request *req, u32 icb)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	__le32 leicb = cpu_to_le32(icb);
  
  	memcpy(iv, &leicb, sizeof(leicb));
  	memcpy(iv + sizeof(leicb), ctx->salt, ctx->saltlen);
  	memcpy(iv + sizeof(leicb) + ctx->saltlen, req->iv,
1ca1b9179   Eric Biggers   crypto: chacha20-...
91
  	       CHACHA_IV_SIZE - sizeof(leicb) - ctx->saltlen);
71ebc4d1b   Martin Willi   crypto: chacha20p...
92
93
94
95
96
97
  }
  
  static int poly_verify_tag(struct aead_request *req)
  {
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	u8 tag[sizeof(rctx->tag)];
747909223   Herbert Xu   crypto: chacha20p...
98
99
100
  	scatterwalk_map_and_copy(tag, req->src,
  				 req->assoclen + rctx->cryptlen,
  				 sizeof(tag), 0);
71ebc4d1b   Martin Willi   crypto: chacha20p...
101
102
103
104
105
106
107
108
  	if (crypto_memneq(tag, rctx->tag, sizeof(tag)))
  		return -EBADMSG;
  	return 0;
  }
  
  static int poly_copy_tag(struct aead_request *req)
  {
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
747909223   Herbert Xu   crypto: chacha20p...
109
110
  	scatterwalk_map_and_copy(rctx->tag, req->dst,
  				 req->assoclen + rctx->cryptlen,
71ebc4d1b   Martin Willi   crypto: chacha20p...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  				 sizeof(rctx->tag), 1);
  	return 0;
  }
  
  static void chacha_decrypt_done(struct crypto_async_request *areq, int err)
  {
  	async_done_continue(areq->data, err, poly_verify_tag);
  }
  
  static int chacha_decrypt(struct aead_request *req)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct chacha_req *creq = &rctx->u.chacha;
747909223   Herbert Xu   crypto: chacha20p...
125
  	struct scatterlist *src, *dst;
71ebc4d1b   Martin Willi   crypto: chacha20p...
126
  	int err;
161151d79   Jason A. Donenfeld   crypto: chacha20p...
127
128
  	if (rctx->cryptlen == 0)
  		goto skip;
71ebc4d1b   Martin Willi   crypto: chacha20p...
129
  	chacha_iv(creq->iv, req, 1);
747909223   Herbert Xu   crypto: chacha20p...
130
131
  	src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
  	dst = src;
76cadf224   Eric Biggers   crypto: chacha20p...
132
  	if (req->src != req->dst)
747909223   Herbert Xu   crypto: chacha20p...
133
  		dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
747909223   Herbert Xu   crypto: chacha20p...
134

7545b6c20   Eric Biggers   crypto: chacha20p...
135
  	skcipher_request_set_callback(&creq->req, rctx->flags,
1e1f00611   Herbert Xu   crypto: chacha20p...
136
137
138
139
140
  				      chacha_decrypt_done, req);
  	skcipher_request_set_tfm(&creq->req, ctx->chacha);
  	skcipher_request_set_crypt(&creq->req, src, dst,
  				   rctx->cryptlen, creq->iv);
  	err = crypto_skcipher_decrypt(&creq->req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
141
142
  	if (err)
  		return err;
161151d79   Jason A. Donenfeld   crypto: chacha20p...
143
  skip:
71ebc4d1b   Martin Willi   crypto: chacha20p...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  	return poly_verify_tag(req);
  }
  
  static int poly_tail_continue(struct aead_request *req)
  {
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  
  	if (rctx->cryptlen == req->cryptlen) /* encrypting */
  		return poly_copy_tag(req);
  
  	return chacha_decrypt(req);
  }
  
  static void poly_tail_done(struct crypto_async_request *areq, int err)
  {
  	async_done_continue(areq->data, err, poly_tail_continue);
  }
  
  static int poly_tail(struct aead_request *req)
  {
747909223   Herbert Xu   crypto: chacha20p...
164
165
  	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
71ebc4d1b   Martin Willi   crypto: chacha20p...
166
167
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct poly_req *preq = &rctx->u.poly;
71ebc4d1b   Martin Willi   crypto: chacha20p...
168
  	int err;
76cadf224   Eric Biggers   crypto: chacha20p...
169
170
171
  	preq->tail.assoclen = cpu_to_le64(rctx->assoclen);
  	preq->tail.cryptlen = cpu_to_le64(rctx->cryptlen);
  	sg_init_one(preq->src, &preq->tail, sizeof(preq->tail));
71ebc4d1b   Martin Willi   crypto: chacha20p...
172

7545b6c20   Eric Biggers   crypto: chacha20p...
173
  	ahash_request_set_callback(&preq->req, rctx->flags,
71ebc4d1b   Martin Willi   crypto: chacha20p...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  				   poly_tail_done, req);
  	ahash_request_set_tfm(&preq->req, ctx->poly);
  	ahash_request_set_crypt(&preq->req, preq->src,
  				rctx->tag, sizeof(preq->tail));
  
  	err = crypto_ahash_finup(&preq->req);
  	if (err)
  		return err;
  
  	return poly_tail_continue(req);
  }
  
  static void poly_cipherpad_done(struct crypto_async_request *areq, int err)
  {
  	async_done_continue(areq->data, err, poly_tail);
  }
  
  static int poly_cipherpad(struct aead_request *req)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct poly_req *preq = &rctx->u.poly;
76cadf224   Eric Biggers   crypto: chacha20p...
196
  	unsigned int padlen;
71ebc4d1b   Martin Willi   crypto: chacha20p...
197
  	int err;
76cadf224   Eric Biggers   crypto: chacha20p...
198
  	padlen = -rctx->cryptlen % POLY1305_BLOCK_SIZE;
71ebc4d1b   Martin Willi   crypto: chacha20p...
199
  	memset(preq->pad, 0, sizeof(preq->pad));
76cadf224   Eric Biggers   crypto: chacha20p...
200
  	sg_init_one(preq->src, preq->pad, padlen);
71ebc4d1b   Martin Willi   crypto: chacha20p...
201

7545b6c20   Eric Biggers   crypto: chacha20p...
202
  	ahash_request_set_callback(&preq->req, rctx->flags,
71ebc4d1b   Martin Willi   crypto: chacha20p...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  				   poly_cipherpad_done, req);
  	ahash_request_set_tfm(&preq->req, ctx->poly);
  	ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
  
  	err = crypto_ahash_update(&preq->req);
  	if (err)
  		return err;
  
  	return poly_tail(req);
  }
  
  static void poly_cipher_done(struct crypto_async_request *areq, int err)
  {
  	async_done_continue(areq->data, err, poly_cipherpad);
  }
  
  static int poly_cipher(struct aead_request *req)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct poly_req *preq = &rctx->u.poly;
  	struct scatterlist *crypt = req->src;
  	int err;
  
  	if (rctx->cryptlen == req->cryptlen) /* encrypting */
  		crypt = req->dst;
747909223   Herbert Xu   crypto: chacha20p...
229
  	crypt = scatterwalk_ffwd(rctx->src, crypt, req->assoclen);
7545b6c20   Eric Biggers   crypto: chacha20p...
230
  	ahash_request_set_callback(&preq->req, rctx->flags,
71ebc4d1b   Martin Willi   crypto: chacha20p...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  				   poly_cipher_done, req);
  	ahash_request_set_tfm(&preq->req, ctx->poly);
  	ahash_request_set_crypt(&preq->req, crypt, NULL, rctx->cryptlen);
  
  	err = crypto_ahash_update(&preq->req);
  	if (err)
  		return err;
  
  	return poly_cipherpad(req);
  }
  
  static void poly_adpad_done(struct crypto_async_request *areq, int err)
  {
  	async_done_continue(areq->data, err, poly_cipher);
  }
  
  static int poly_adpad(struct aead_request *req)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct poly_req *preq = &rctx->u.poly;
76cadf224   Eric Biggers   crypto: chacha20p...
252
  	unsigned int padlen;
71ebc4d1b   Martin Willi   crypto: chacha20p...
253
  	int err;
76cadf224   Eric Biggers   crypto: chacha20p...
254
  	padlen = -rctx->assoclen % POLY1305_BLOCK_SIZE;
71ebc4d1b   Martin Willi   crypto: chacha20p...
255
  	memset(preq->pad, 0, sizeof(preq->pad));
76cadf224   Eric Biggers   crypto: chacha20p...
256
  	sg_init_one(preq->src, preq->pad, padlen);
71ebc4d1b   Martin Willi   crypto: chacha20p...
257

7545b6c20   Eric Biggers   crypto: chacha20p...
258
  	ahash_request_set_callback(&preq->req, rctx->flags,
71ebc4d1b   Martin Willi   crypto: chacha20p...
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  				   poly_adpad_done, req);
  	ahash_request_set_tfm(&preq->req, ctx->poly);
  	ahash_request_set_crypt(&preq->req, preq->src, NULL, padlen);
  
  	err = crypto_ahash_update(&preq->req);
  	if (err)
  		return err;
  
  	return poly_cipher(req);
  }
  
  static void poly_ad_done(struct crypto_async_request *areq, int err)
  {
  	async_done_continue(areq->data, err, poly_adpad);
  }
  
  static int poly_ad(struct aead_request *req)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct poly_req *preq = &rctx->u.poly;
  	int err;
7545b6c20   Eric Biggers   crypto: chacha20p...
281
  	ahash_request_set_callback(&preq->req, rctx->flags,
71ebc4d1b   Martin Willi   crypto: chacha20p...
282
283
  				   poly_ad_done, req);
  	ahash_request_set_tfm(&preq->req, ctx->poly);
747909223   Herbert Xu   crypto: chacha20p...
284
  	ahash_request_set_crypt(&preq->req, req->src, NULL, rctx->assoclen);
71ebc4d1b   Martin Willi   crypto: chacha20p...
285
286
287
288
289
290
291
  
  	err = crypto_ahash_update(&preq->req);
  	if (err)
  		return err;
  
  	return poly_adpad(req);
  }
c2b7b20ae   Martin Willi   crypto: poly1305 ...
292
  static void poly_setkey_done(struct crypto_async_request *areq, int err)
71ebc4d1b   Martin Willi   crypto: chacha20p...
293
294
295
  {
  	async_done_continue(areq->data, err, poly_ad);
  }
c2b7b20ae   Martin Willi   crypto: poly1305 ...
296
  static int poly_setkey(struct aead_request *req)
71ebc4d1b   Martin Willi   crypto: chacha20p...
297
298
299
300
301
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct poly_req *preq = &rctx->u.poly;
  	int err;
76cadf224   Eric Biggers   crypto: chacha20p...
302
  	sg_init_one(preq->src, rctx->key, sizeof(rctx->key));
c2b7b20ae   Martin Willi   crypto: poly1305 ...
303

7545b6c20   Eric Biggers   crypto: chacha20p...
304
  	ahash_request_set_callback(&preq->req, rctx->flags,
c2b7b20ae   Martin Willi   crypto: poly1305 ...
305
  				   poly_setkey_done, req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
306
  	ahash_request_set_tfm(&preq->req, ctx->poly);
c2b7b20ae   Martin Willi   crypto: poly1305 ...
307
  	ahash_request_set_crypt(&preq->req, preq->src, NULL, sizeof(rctx->key));
71ebc4d1b   Martin Willi   crypto: chacha20p...
308

c2b7b20ae   Martin Willi   crypto: poly1305 ...
309
  	err = crypto_ahash_update(&preq->req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
310
311
312
313
314
  	if (err)
  		return err;
  
  	return poly_ad(req);
  }
c2b7b20ae   Martin Willi   crypto: poly1305 ...
315
  static void poly_init_done(struct crypto_async_request *areq, int err)
71ebc4d1b   Martin Willi   crypto: chacha20p...
316
  {
c2b7b20ae   Martin Willi   crypto: poly1305 ...
317
318
319
320
321
322
  	async_done_continue(areq->data, err, poly_setkey);
  }
  
  static int poly_init(struct aead_request *req)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
71ebc4d1b   Martin Willi   crypto: chacha20p...
323
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
c2b7b20ae   Martin Willi   crypto: poly1305 ...
324
  	struct poly_req *preq = &rctx->u.poly;
71ebc4d1b   Martin Willi   crypto: chacha20p...
325
  	int err;
7545b6c20   Eric Biggers   crypto: chacha20p...
326
  	ahash_request_set_callback(&preq->req, rctx->flags,
c2b7b20ae   Martin Willi   crypto: poly1305 ...
327
328
  				   poly_init_done, req);
  	ahash_request_set_tfm(&preq->req, ctx->poly);
71ebc4d1b   Martin Willi   crypto: chacha20p...
329

c2b7b20ae   Martin Willi   crypto: poly1305 ...
330
  	err = crypto_ahash_init(&preq->req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
331
332
  	if (err)
  		return err;
c2b7b20ae   Martin Willi   crypto: poly1305 ...
333
  	return poly_setkey(req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
334
335
336
337
  }
  
  static void poly_genkey_done(struct crypto_async_request *areq, int err)
  {
c2b7b20ae   Martin Willi   crypto: poly1305 ...
338
  	async_done_continue(areq->data, err, poly_init);
71ebc4d1b   Martin Willi   crypto: chacha20p...
339
340
341
342
  }
  
  static int poly_genkey(struct aead_request *req)
  {
747909223   Herbert Xu   crypto: chacha20p...
343
344
  	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
71ebc4d1b   Martin Willi   crypto: chacha20p...
345
346
347
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct chacha_req *creq = &rctx->u.chacha;
  	int err;
747909223   Herbert Xu   crypto: chacha20p...
348
349
350
351
352
353
354
  	rctx->assoclen = req->assoclen;
  
  	if (crypto_aead_ivsize(tfm) == 8) {
  		if (rctx->assoclen < 8)
  			return -EINVAL;
  		rctx->assoclen -= 8;
  	}
c2b7b20ae   Martin Willi   crypto: poly1305 ...
355
  	memset(rctx->key, 0, sizeof(rctx->key));
76cadf224   Eric Biggers   crypto: chacha20p...
356
  	sg_init_one(creq->src, rctx->key, sizeof(rctx->key));
71ebc4d1b   Martin Willi   crypto: chacha20p...
357
358
  
  	chacha_iv(creq->iv, req, 0);
7545b6c20   Eric Biggers   crypto: chacha20p...
359
  	skcipher_request_set_callback(&creq->req, rctx->flags,
1e1f00611   Herbert Xu   crypto: chacha20p...
360
361
362
363
  				      poly_genkey_done, req);
  	skcipher_request_set_tfm(&creq->req, ctx->chacha);
  	skcipher_request_set_crypt(&creq->req, creq->src, creq->src,
  				   POLY1305_KEY_SIZE, creq->iv);
71ebc4d1b   Martin Willi   crypto: chacha20p...
364

1e1f00611   Herbert Xu   crypto: chacha20p...
365
  	err = crypto_skcipher_decrypt(&creq->req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
366
367
  	if (err)
  		return err;
c2b7b20ae   Martin Willi   crypto: poly1305 ...
368
  	return poly_init(req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
369
370
371
372
373
374
375
376
377
378
379
380
  }
  
  static void chacha_encrypt_done(struct crypto_async_request *areq, int err)
  {
  	async_done_continue(areq->data, err, poly_genkey);
  }
  
  static int chacha_encrypt(struct aead_request *req)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  	struct chacha_req *creq = &rctx->u.chacha;
747909223   Herbert Xu   crypto: chacha20p...
381
  	struct scatterlist *src, *dst;
71ebc4d1b   Martin Willi   crypto: chacha20p...
382
  	int err;
161151d79   Jason A. Donenfeld   crypto: chacha20p...
383
384
  	if (req->cryptlen == 0)
  		goto skip;
71ebc4d1b   Martin Willi   crypto: chacha20p...
385
  	chacha_iv(creq->iv, req, 1);
747909223   Herbert Xu   crypto: chacha20p...
386
387
  	src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
  	dst = src;
76cadf224   Eric Biggers   crypto: chacha20p...
388
  	if (req->src != req->dst)
747909223   Herbert Xu   crypto: chacha20p...
389
  		dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
747909223   Herbert Xu   crypto: chacha20p...
390

7545b6c20   Eric Biggers   crypto: chacha20p...
391
  	skcipher_request_set_callback(&creq->req, rctx->flags,
1e1f00611   Herbert Xu   crypto: chacha20p...
392
393
394
395
396
  				      chacha_encrypt_done, req);
  	skcipher_request_set_tfm(&creq->req, ctx->chacha);
  	skcipher_request_set_crypt(&creq->req, src, dst,
  				   req->cryptlen, creq->iv);
  	err = crypto_skcipher_encrypt(&creq->req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
397
398
  	if (err)
  		return err;
161151d79   Jason A. Donenfeld   crypto: chacha20p...
399
  skip:
71ebc4d1b   Martin Willi   crypto: chacha20p...
400
401
402
403
404
405
406
407
  	return poly_genkey(req);
  }
  
  static int chachapoly_encrypt(struct aead_request *req)
  {
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
  
  	rctx->cryptlen = req->cryptlen;
7545b6c20   Eric Biggers   crypto: chacha20p...
408
  	rctx->flags = aead_request_flags(req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
409
410
411
  
  	/* encrypt call chain:
  	 * - chacha_encrypt/done()
c2b7b20ae   Martin Willi   crypto: poly1305 ...
412
  	 * - poly_genkey/done()
71ebc4d1b   Martin Willi   crypto: chacha20p...
413
  	 * - poly_init/done()
c2b7b20ae   Martin Willi   crypto: poly1305 ...
414
  	 * - poly_setkey/done()
71ebc4d1b   Martin Willi   crypto: chacha20p...
415
416
417
418
419
420
421
422
423
424
425
426
427
  	 * - poly_ad/done()
  	 * - poly_adpad/done()
  	 * - poly_cipher/done()
  	 * - poly_cipherpad/done()
  	 * - poly_tail/done/continue()
  	 * - poly_copy_tag()
  	 */
  	return chacha_encrypt(req);
  }
  
  static int chachapoly_decrypt(struct aead_request *req)
  {
  	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
428
  	rctx->cryptlen = req->cryptlen - POLY1305_DIGEST_SIZE;
7545b6c20   Eric Biggers   crypto: chacha20p...
429
  	rctx->flags = aead_request_flags(req);
71ebc4d1b   Martin Willi   crypto: chacha20p...
430
431
  
  	/* decrypt call chain:
c2b7b20ae   Martin Willi   crypto: poly1305 ...
432
  	 * - poly_genkey/done()
71ebc4d1b   Martin Willi   crypto: chacha20p...
433
  	 * - poly_init/done()
c2b7b20ae   Martin Willi   crypto: poly1305 ...
434
  	 * - poly_setkey/done()
71ebc4d1b   Martin Willi   crypto: chacha20p...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
  	 * - poly_ad/done()
  	 * - poly_adpad/done()
  	 * - poly_cipher/done()
  	 * - poly_cipherpad/done()
  	 * - poly_tail/done/continue()
  	 * - chacha_decrypt/done()
  	 * - poly_verify_tag()
  	 */
  	return poly_genkey(req);
  }
  
  static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
  			     unsigned int keylen)
  {
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(aead);
71ebc4d1b   Martin Willi   crypto: chacha20p...
450

1ca1b9179   Eric Biggers   crypto: chacha20-...
451
  	if (keylen != ctx->saltlen + CHACHA_KEY_SIZE)
71ebc4d1b   Martin Willi   crypto: chacha20p...
452
453
454
455
  		return -EINVAL;
  
  	keylen -= ctx->saltlen;
  	memcpy(ctx->salt, key + keylen, ctx->saltlen);
1e1f00611   Herbert Xu   crypto: chacha20p...
456
457
458
  	crypto_skcipher_clear_flags(ctx->chacha, CRYPTO_TFM_REQ_MASK);
  	crypto_skcipher_set_flags(ctx->chacha, crypto_aead_get_flags(aead) &
  					       CRYPTO_TFM_REQ_MASK);
af5034e8e   Eric Biggers   crypto: remove pr...
459
  	return crypto_skcipher_setkey(ctx->chacha, key, keylen);
71ebc4d1b   Martin Willi   crypto: chacha20p...
460
461
462
463
464
465
466
467
468
469
  }
  
  static int chachapoly_setauthsize(struct crypto_aead *tfm,
  				  unsigned int authsize)
  {
  	if (authsize != POLY1305_DIGEST_SIZE)
  		return -EINVAL;
  
  	return 0;
  }
747909223   Herbert Xu   crypto: chacha20p...
470
  static int chachapoly_init(struct crypto_aead *tfm)
71ebc4d1b   Martin Willi   crypto: chacha20p...
471
  {
747909223   Herbert Xu   crypto: chacha20p...
472
473
474
  	struct aead_instance *inst = aead_alg_instance(tfm);
  	struct chachapoly_instance_ctx *ictx = aead_instance_ctx(inst);
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
1e1f00611   Herbert Xu   crypto: chacha20p...
475
  	struct crypto_skcipher *chacha;
71ebc4d1b   Martin Willi   crypto: chacha20p...
476
477
478
479
480
481
  	struct crypto_ahash *poly;
  	unsigned long align;
  
  	poly = crypto_spawn_ahash(&ictx->poly);
  	if (IS_ERR(poly))
  		return PTR_ERR(poly);
60425a8ba   Eric Biggers   crypto: skcipher ...
482
  	chacha = crypto_spawn_skcipher(&ictx->chacha);
71ebc4d1b   Martin Willi   crypto: chacha20p...
483
484
485
486
487
488
489
490
  	if (IS_ERR(chacha)) {
  		crypto_free_ahash(poly);
  		return PTR_ERR(chacha);
  	}
  
  	ctx->chacha = chacha;
  	ctx->poly = poly;
  	ctx->saltlen = ictx->saltlen;
747909223   Herbert Xu   crypto: chacha20p...
491
  	align = crypto_aead_alignmask(tfm);
71ebc4d1b   Martin Willi   crypto: chacha20p...
492
  	align &= ~(crypto_tfm_ctx_alignment() - 1);
747909223   Herbert Xu   crypto: chacha20p...
493
494
495
496
  	crypto_aead_set_reqsize(
  		tfm,
  		align + offsetof(struct chachapoly_req_ctx, u) +
  		max(offsetof(struct chacha_req, req) +
1e1f00611   Herbert Xu   crypto: chacha20p...
497
498
  		    sizeof(struct skcipher_request) +
  		    crypto_skcipher_reqsize(chacha),
747909223   Herbert Xu   crypto: chacha20p...
499
500
501
  		    offsetof(struct poly_req, req) +
  		    sizeof(struct ahash_request) +
  		    crypto_ahash_reqsize(poly)));
71ebc4d1b   Martin Willi   crypto: chacha20p...
502
503
504
  
  	return 0;
  }
747909223   Herbert Xu   crypto: chacha20p...
505
  static void chachapoly_exit(struct crypto_aead *tfm)
71ebc4d1b   Martin Willi   crypto: chacha20p...
506
  {
747909223   Herbert Xu   crypto: chacha20p...
507
  	struct chachapoly_ctx *ctx = crypto_aead_ctx(tfm);
71ebc4d1b   Martin Willi   crypto: chacha20p...
508
509
  
  	crypto_free_ahash(ctx->poly);
1e1f00611   Herbert Xu   crypto: chacha20p...
510
  	crypto_free_skcipher(ctx->chacha);
71ebc4d1b   Martin Willi   crypto: chacha20p...
511
  }
747909223   Herbert Xu   crypto: chacha20p...
512
513
514
515
516
517
518
519
520
521
522
  static void chachapoly_free(struct aead_instance *inst)
  {
  	struct chachapoly_instance_ctx *ctx = aead_instance_ctx(inst);
  
  	crypto_drop_skcipher(&ctx->chacha);
  	crypto_drop_ahash(&ctx->poly);
  	kfree(inst);
  }
  
  static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
  			     const char *name, unsigned int ivsize)
71ebc4d1b   Martin Willi   crypto: chacha20p...
523
  {
b9f76dddb   Eric Biggers   crypto: skcipher ...
524
  	u32 mask;
747909223   Herbert Xu   crypto: chacha20p...
525
  	struct aead_instance *inst;
71ebc4d1b   Martin Willi   crypto: chacha20p...
526
  	struct chachapoly_instance_ctx *ctx;
c282586fc   Eric Biggers   crypto: chacha20p...
527
528
  	struct skcipher_alg *chacha;
  	struct hash_alg_common *poly;
71ebc4d1b   Martin Willi   crypto: chacha20p...
529
530
531
  	int err;
  
  	if (ivsize > CHACHAPOLY_IV_SIZE)
747909223   Herbert Xu   crypto: chacha20p...
532
  		return -EINVAL;
71ebc4d1b   Martin Willi   crypto: chacha20p...
533

7bcb2c99f   Eric Biggers   crypto: algapi - ...
534
535
536
  	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AEAD, &mask);
  	if (err)
  		return err;
b9f76dddb   Eric Biggers   crypto: skcipher ...
537

71ebc4d1b   Martin Willi   crypto: chacha20p...
538
539
  	inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  	if (!inst)
c282586fc   Eric Biggers   crypto: chacha20p...
540
  		return -ENOMEM;
747909223   Herbert Xu   crypto: chacha20p...
541
  	ctx = aead_instance_ctx(inst);
71ebc4d1b   Martin Willi   crypto: chacha20p...
542
  	ctx->saltlen = CHACHAPOLY_IV_SIZE - ivsize;
71ebc4d1b   Martin Willi   crypto: chacha20p...
543

b9f76dddb   Eric Biggers   crypto: skcipher ...
544
  	err = crypto_grab_skcipher(&ctx->chacha, aead_crypto_instance(inst),
c282586fc   Eric Biggers   crypto: chacha20p...
545
  				   crypto_attr_alg_name(tb[1]), 0, mask);
71ebc4d1b   Martin Willi   crypto: chacha20p...
546
  	if (err)
c282586fc   Eric Biggers   crypto: chacha20p...
547
  		goto err_free_inst;
1e1f00611   Herbert Xu   crypto: chacha20p...
548
  	chacha = crypto_spawn_skcipher_alg(&ctx->chacha);
71ebc4d1b   Martin Willi   crypto: chacha20p...
549

c282586fc   Eric Biggers   crypto: chacha20p...
550
551
552
553
554
  	err = crypto_grab_ahash(&ctx->poly, aead_crypto_instance(inst),
  				crypto_attr_alg_name(tb[2]), 0, mask);
  	if (err)
  		goto err_free_inst;
  	poly = crypto_spawn_ahash_alg(&ctx->poly);
71ebc4d1b   Martin Willi   crypto: chacha20p...
555
  	err = -EINVAL;
c282586fc   Eric Biggers   crypto: chacha20p...
556
557
  	if (poly->digestsize != POLY1305_DIGEST_SIZE)
  		goto err_free_inst;
71ebc4d1b   Martin Willi   crypto: chacha20p...
558
  	/* Need 16-byte IV size, including Initial Block Counter value */
1ca1b9179   Eric Biggers   crypto: chacha20-...
559
  	if (crypto_skcipher_alg_ivsize(chacha) != CHACHA_IV_SIZE)
c282586fc   Eric Biggers   crypto: chacha20p...
560
  		goto err_free_inst;
71ebc4d1b   Martin Willi   crypto: chacha20p...
561
  	/* Not a stream cipher? */
1e1f00611   Herbert Xu   crypto: chacha20p...
562
  	if (chacha->base.cra_blocksize != 1)
c282586fc   Eric Biggers   crypto: chacha20p...
563
  		goto err_free_inst;
71ebc4d1b   Martin Willi   crypto: chacha20p...
564
565
  
  	err = -ENAMETOOLONG;
747909223   Herbert Xu   crypto: chacha20p...
566
  	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
5e27f38f1   Eric Biggers   crypto: chacha20p...
567
  		     "%s(%s,%s)", name, chacha->base.cra_name,
c282586fc   Eric Biggers   crypto: chacha20p...
568
569
  		     poly->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
  		goto err_free_inst;
747909223   Herbert Xu   crypto: chacha20p...
570
  	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1e1f00611   Herbert Xu   crypto: chacha20p...
571
  		     "%s(%s,%s)", name, chacha->base.cra_driver_name,
c282586fc   Eric Biggers   crypto: chacha20p...
572
573
  		     poly->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  		goto err_free_inst;
71ebc4d1b   Martin Willi   crypto: chacha20p...
574

1e1f00611   Herbert Xu   crypto: chacha20p...
575
  	inst->alg.base.cra_priority = (chacha->base.cra_priority +
c282586fc   Eric Biggers   crypto: chacha20p...
576
  				       poly->base.cra_priority) / 2;
747909223   Herbert Xu   crypto: chacha20p...
577
  	inst->alg.base.cra_blocksize = 1;
1e1f00611   Herbert Xu   crypto: chacha20p...
578
  	inst->alg.base.cra_alignmask = chacha->base.cra_alignmask |
c282586fc   Eric Biggers   crypto: chacha20p...
579
  				       poly->base.cra_alignmask;
747909223   Herbert Xu   crypto: chacha20p...
580
581
582
  	inst->alg.base.cra_ctxsize = sizeof(struct chachapoly_ctx) +
  				     ctx->saltlen;
  	inst->alg.ivsize = ivsize;
1e1f00611   Herbert Xu   crypto: chacha20p...
583
  	inst->alg.chunksize = crypto_skcipher_alg_chunksize(chacha);
747909223   Herbert Xu   crypto: chacha20p...
584
585
586
587
588
589
590
591
592
593
594
  	inst->alg.maxauthsize = POLY1305_DIGEST_SIZE;
  	inst->alg.init = chachapoly_init;
  	inst->alg.exit = chachapoly_exit;
  	inst->alg.encrypt = chachapoly_encrypt;
  	inst->alg.decrypt = chachapoly_decrypt;
  	inst->alg.setkey = chachapoly_setkey;
  	inst->alg.setauthsize = chachapoly_setauthsize;
  
  	inst->free = chachapoly_free;
  
  	err = aead_register_instance(tmpl, inst);
c282586fc   Eric Biggers   crypto: chacha20p...
595
  	if (err) {
71ebc4d1b   Martin Willi   crypto: chacha20p...
596
  err_free_inst:
c282586fc   Eric Biggers   crypto: chacha20p...
597
598
599
  		chachapoly_free(inst);
  	}
  	return err;
71ebc4d1b   Martin Willi   crypto: chacha20p...
600
  }
747909223   Herbert Xu   crypto: chacha20p...
601
  static int rfc7539_create(struct crypto_template *tmpl, struct rtattr **tb)
4db4ad260   Martin Willi   crypto: chacha20p...
602
  {
747909223   Herbert Xu   crypto: chacha20p...
603
  	return chachapoly_create(tmpl, tb, "rfc7539", 12);
4db4ad260   Martin Willi   crypto: chacha20p...
604
  }
747909223   Herbert Xu   crypto: chacha20p...
605
  static int rfc7539esp_create(struct crypto_template *tmpl, struct rtattr **tb)
71ebc4d1b   Martin Willi   crypto: chacha20p...
606
  {
747909223   Herbert Xu   crypto: chacha20p...
607
  	return chachapoly_create(tmpl, tb, "rfc7539esp", 8);
71ebc4d1b   Martin Willi   crypto: chacha20p...
608
  }
1a5e02b68   Xiongfeng Wang   crypto: chacha20p...
609
610
611
612
613
614
615
616
617
618
  static struct crypto_template rfc7539_tmpls[] = {
  	{
  		.name = "rfc7539",
  		.create = rfc7539_create,
  		.module = THIS_MODULE,
  	}, {
  		.name = "rfc7539esp",
  		.create = rfc7539esp_create,
  		.module = THIS_MODULE,
  	},
4db4ad260   Martin Willi   crypto: chacha20p...
619
  };
71ebc4d1b   Martin Willi   crypto: chacha20p...
620
621
  static int __init chacha20poly1305_module_init(void)
  {
1a5e02b68   Xiongfeng Wang   crypto: chacha20p...
622
623
  	return crypto_register_templates(rfc7539_tmpls,
  					 ARRAY_SIZE(rfc7539_tmpls));
71ebc4d1b   Martin Willi   crypto: chacha20p...
624
625
626
627
  }
  
  static void __exit chacha20poly1305_module_exit(void)
  {
1a5e02b68   Xiongfeng Wang   crypto: chacha20p...
628
629
  	crypto_unregister_templates(rfc7539_tmpls,
  				    ARRAY_SIZE(rfc7539_tmpls));
71ebc4d1b   Martin Willi   crypto: chacha20p...
630
  }
c4741b230   Eric Biggers   crypto: run initc...
631
  subsys_initcall(chacha20poly1305_module_init);
71ebc4d1b   Martin Willi   crypto: chacha20p...
632
633
634
635
636
  module_exit(chacha20poly1305_module_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  MODULE_DESCRIPTION("ChaCha20-Poly1305 AEAD");
71ebc4d1b   Martin Willi   crypto: chacha20p...
637
  MODULE_ALIAS_CRYPTO("rfc7539");
4db4ad260   Martin Willi   crypto: chacha20p...
638
  MODULE_ALIAS_CRYPTO("rfc7539esp");