blob: fa69e99968e24d9ad5e0345b3f7e0a22f51fb5ea [file] [log] [blame]
Thomas Gleixnerbb4ce822019-01-18 00:14:20 +01001// SPDX-License-Identifier: GPL-2.0-or-later
Ondrej Mosnacekf606a882018-05-11 14:12:49 +02002/*
3 * The AEGIS-128 Authenticated-Encryption Algorithm
4 *
5 * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com>
6 * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
Ondrej Mosnacekf606a882018-05-11 14:12:49 +02007 */
8
9#include <crypto/algapi.h>
10#include <crypto/internal/aead.h>
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +030011#include <crypto/internal/simd.h>
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020012#include <crypto/internal/skcipher.h>
13#include <crypto/scatterwalk.h>
14#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/scatterlist.h>
19
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +030020#include <asm/simd.h>
21
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020022#include "aegis.h"
23
24#define AEGIS128_NONCE_SIZE 16
25#define AEGIS128_STATE_BLOCKS 5
26#define AEGIS128_KEY_SIZE 16
27#define AEGIS128_MIN_AUTH_SIZE 8
28#define AEGIS128_MAX_AUTH_SIZE 16
29
30struct aegis_state {
31 union aegis_block blocks[AEGIS128_STATE_BLOCKS];
32};
33
34struct aegis_ctx {
35 union aegis_block key;
36};
37
38struct aegis128_ops {
39 int (*skcipher_walk_init)(struct skcipher_walk *walk,
40 struct aead_request *req, bool atomic);
41
42 void (*crypt_chunk)(struct aegis_state *state, u8 *dst,
43 const u8 *src, unsigned int size);
44};
45
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +030046static bool have_simd;
47
48static bool aegis128_do_simd(void)
49{
50#ifdef CONFIG_CRYPTO_AEGIS128_SIMD
51 if (have_simd)
52 return crypto_simd_usable();
53#endif
54 return false;
55}
56
57bool crypto_aegis128_have_simd(void);
58void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
59void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
60 const u8 *src, unsigned int size);
61void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
62 const u8 *src, unsigned int size);
63
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020064static void crypto_aegis128_update(struct aegis_state *state)
65{
66 union aegis_block tmp;
67 unsigned int i;
68
69 tmp = state->blocks[AEGIS128_STATE_BLOCKS - 1];
70 for (i = AEGIS128_STATE_BLOCKS - 1; i > 0; i--)
71 crypto_aegis_aesenc(&state->blocks[i], &state->blocks[i - 1],
72 &state->blocks[i]);
73 crypto_aegis_aesenc(&state->blocks[0], &tmp, &state->blocks[0]);
74}
75
76static void crypto_aegis128_update_a(struct aegis_state *state,
77 const union aegis_block *msg)
78{
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +030079 if (aegis128_do_simd()) {
80 crypto_aegis128_update_simd(state, msg);
81 return;
82 }
83
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020084 crypto_aegis128_update(state);
85 crypto_aegis_block_xor(&state->blocks[0], msg);
86}
87
88static void crypto_aegis128_update_u(struct aegis_state *state, const void *msg)
89{
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +030090 if (aegis128_do_simd()) {
91 crypto_aegis128_update_simd(state, msg);
92 return;
93 }
94
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020095 crypto_aegis128_update(state);
96 crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
97}
98
99static void crypto_aegis128_init(struct aegis_state *state,
100 const union aegis_block *key,
101 const u8 *iv)
102{
103 union aegis_block key_iv;
104 unsigned int i;
105
106 key_iv = *key;
107 crypto_xor(key_iv.bytes, iv, AEGIS_BLOCK_SIZE);
108
109 state->blocks[0] = key_iv;
110 state->blocks[1] = crypto_aegis_const[1];
111 state->blocks[2] = crypto_aegis_const[0];
112 state->blocks[3] = *key;
113 state->blocks[4] = *key;
114
115 crypto_aegis_block_xor(&state->blocks[3], &crypto_aegis_const[0]);
116 crypto_aegis_block_xor(&state->blocks[4], &crypto_aegis_const[1]);
117
118 for (i = 0; i < 5; i++) {
119 crypto_aegis128_update_a(state, key);
120 crypto_aegis128_update_a(state, &key_iv);
121 }
122}
123
124static void crypto_aegis128_ad(struct aegis_state *state,
125 const u8 *src, unsigned int size)
126{
127 if (AEGIS_ALIGNED(src)) {
128 const union aegis_block *src_blk =
129 (const union aegis_block *)src;
130
131 while (size >= AEGIS_BLOCK_SIZE) {
132 crypto_aegis128_update_a(state, src_blk);
133
134 size -= AEGIS_BLOCK_SIZE;
135 src_blk++;
136 }
137 } else {
138 while (size >= AEGIS_BLOCK_SIZE) {
139 crypto_aegis128_update_u(state, src);
140
141 size -= AEGIS_BLOCK_SIZE;
142 src += AEGIS_BLOCK_SIZE;
143 }
144 }
145}
146
147static void crypto_aegis128_encrypt_chunk(struct aegis_state *state, u8 *dst,
148 const u8 *src, unsigned int size)
149{
150 union aegis_block tmp;
151
152 if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
153 while (size >= AEGIS_BLOCK_SIZE) {
154 union aegis_block *dst_blk =
155 (union aegis_block *)dst;
156 const union aegis_block *src_blk =
157 (const union aegis_block *)src;
158
159 tmp = state->blocks[2];
160 crypto_aegis_block_and(&tmp, &state->blocks[3]);
161 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
162 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
163 crypto_aegis_block_xor(&tmp, src_blk);
164
165 crypto_aegis128_update_a(state, src_blk);
166
167 *dst_blk = tmp;
168
169 size -= AEGIS_BLOCK_SIZE;
170 src += AEGIS_BLOCK_SIZE;
171 dst += AEGIS_BLOCK_SIZE;
172 }
173 } else {
174 while (size >= AEGIS_BLOCK_SIZE) {
175 tmp = state->blocks[2];
176 crypto_aegis_block_and(&tmp, &state->blocks[3]);
177 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
178 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
179 crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
180
181 crypto_aegis128_update_u(state, src);
182
183 memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
184
185 size -= AEGIS_BLOCK_SIZE;
186 src += AEGIS_BLOCK_SIZE;
187 dst += AEGIS_BLOCK_SIZE;
188 }
189 }
190
191 if (size > 0) {
192 union aegis_block msg = {};
193 memcpy(msg.bytes, src, size);
194
195 tmp = state->blocks[2];
196 crypto_aegis_block_and(&tmp, &state->blocks[3]);
197 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
198 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
199
200 crypto_aegis128_update_a(state, &msg);
201
202 crypto_aegis_block_xor(&msg, &tmp);
203
204 memcpy(dst, msg.bytes, size);
205 }
206}
207
208static void crypto_aegis128_decrypt_chunk(struct aegis_state *state, u8 *dst,
209 const u8 *src, unsigned int size)
210{
211 union aegis_block tmp;
212
213 if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
214 while (size >= AEGIS_BLOCK_SIZE) {
215 union aegis_block *dst_blk =
216 (union aegis_block *)dst;
217 const union aegis_block *src_blk =
218 (const union aegis_block *)src;
219
220 tmp = state->blocks[2];
221 crypto_aegis_block_and(&tmp, &state->blocks[3]);
222 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
223 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
224 crypto_aegis_block_xor(&tmp, src_blk);
225
226 crypto_aegis128_update_a(state, &tmp);
227
228 *dst_blk = tmp;
229
230 size -= AEGIS_BLOCK_SIZE;
231 src += AEGIS_BLOCK_SIZE;
232 dst += AEGIS_BLOCK_SIZE;
233 }
234 } else {
235 while (size >= AEGIS_BLOCK_SIZE) {
236 tmp = state->blocks[2];
237 crypto_aegis_block_and(&tmp, &state->blocks[3]);
238 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
239 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
240 crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
241
242 crypto_aegis128_update_a(state, &tmp);
243
244 memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
245
246 size -= AEGIS_BLOCK_SIZE;
247 src += AEGIS_BLOCK_SIZE;
248 dst += AEGIS_BLOCK_SIZE;
249 }
250 }
251
252 if (size > 0) {
253 union aegis_block msg = {};
254 memcpy(msg.bytes, src, size);
255
256 tmp = state->blocks[2];
257 crypto_aegis_block_and(&tmp, &state->blocks[3]);
258 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
259 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
260 crypto_aegis_block_xor(&msg, &tmp);
261
262 memset(msg.bytes + size, 0, AEGIS_BLOCK_SIZE - size);
263
264 crypto_aegis128_update_a(state, &msg);
265
266 memcpy(dst, msg.bytes, size);
267 }
268}
269
270static void crypto_aegis128_process_ad(struct aegis_state *state,
271 struct scatterlist *sg_src,
272 unsigned int assoclen)
273{
274 struct scatter_walk walk;
275 union aegis_block buf;
276 unsigned int pos = 0;
277
278 scatterwalk_start(&walk, sg_src);
279 while (assoclen != 0) {
280 unsigned int size = scatterwalk_clamp(&walk, assoclen);
281 unsigned int left = size;
282 void *mapped = scatterwalk_map(&walk);
283 const u8 *src = (const u8 *)mapped;
284
285 if (pos + size >= AEGIS_BLOCK_SIZE) {
286 if (pos > 0) {
287 unsigned int fill = AEGIS_BLOCK_SIZE - pos;
288 memcpy(buf.bytes + pos, src, fill);
289 crypto_aegis128_update_a(state, &buf);
290 pos = 0;
291 left -= fill;
292 src += fill;
293 }
294
295 crypto_aegis128_ad(state, src, left);
296 src += left & ~(AEGIS_BLOCK_SIZE - 1);
297 left &= AEGIS_BLOCK_SIZE - 1;
298 }
299
300 memcpy(buf.bytes + pos, src, left);
301
302 pos += left;
303 assoclen -= size;
304 scatterwalk_unmap(mapped);
305 scatterwalk_advance(&walk, size);
306 scatterwalk_done(&walk, 0, assoclen);
307 }
308
309 if (pos > 0) {
310 memset(buf.bytes + pos, 0, AEGIS_BLOCK_SIZE - pos);
311 crypto_aegis128_update_a(state, &buf);
312 }
313}
314
315static void crypto_aegis128_process_crypt(struct aegis_state *state,
316 struct aead_request *req,
317 const struct aegis128_ops *ops)
318{
319 struct skcipher_walk walk;
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200320
321 ops->skcipher_walk_init(&walk, req, false);
322
323 while (walk.nbytes) {
Eric Biggers0f533e62019-01-31 23:51:36 -0800324 unsigned int nbytes = walk.nbytes;
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200325
Eric Biggers0f533e62019-01-31 23:51:36 -0800326 if (nbytes < walk.total)
327 nbytes = round_down(nbytes, walk.stride);
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200328
Eric Biggers0f533e62019-01-31 23:51:36 -0800329 ops->crypt_chunk(state, walk.dst.virt.addr, walk.src.virt.addr,
330 nbytes);
331
332 skcipher_walk_done(&walk, walk.nbytes - nbytes);
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200333 }
334}
335
336static void crypto_aegis128_final(struct aegis_state *state,
337 union aegis_block *tag_xor,
338 u64 assoclen, u64 cryptlen)
339{
340 u64 assocbits = assoclen * 8;
341 u64 cryptbits = cryptlen * 8;
342
343 union aegis_block tmp;
344 unsigned int i;
345
346 tmp.words64[0] = cpu_to_le64(assocbits);
347 tmp.words64[1] = cpu_to_le64(cryptbits);
348
349 crypto_aegis_block_xor(&tmp, &state->blocks[3]);
350
351 for (i = 0; i < 7; i++)
352 crypto_aegis128_update_a(state, &tmp);
353
354 for (i = 0; i < AEGIS128_STATE_BLOCKS; i++)
355 crypto_aegis_block_xor(tag_xor, &state->blocks[i]);
356}
357
358static int crypto_aegis128_setkey(struct crypto_aead *aead, const u8 *key,
359 unsigned int keylen)
360{
361 struct aegis_ctx *ctx = crypto_aead_ctx(aead);
362
363 if (keylen != AEGIS128_KEY_SIZE) {
364 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
365 return -EINVAL;
366 }
367
368 memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
369 return 0;
370}
371
372static int crypto_aegis128_setauthsize(struct crypto_aead *tfm,
373 unsigned int authsize)
374{
375 if (authsize > AEGIS128_MAX_AUTH_SIZE)
376 return -EINVAL;
377 if (authsize < AEGIS128_MIN_AUTH_SIZE)
378 return -EINVAL;
379 return 0;
380}
381
382static void crypto_aegis128_crypt(struct aead_request *req,
383 union aegis_block *tag_xor,
384 unsigned int cryptlen,
385 const struct aegis128_ops *ops)
386{
387 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
388 struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
389 struct aegis_state state;
390
391 crypto_aegis128_init(&state, &ctx->key, req->iv);
392 crypto_aegis128_process_ad(&state, req->src, req->assoclen);
393 crypto_aegis128_process_crypt(&state, req, ops);
394 crypto_aegis128_final(&state, tag_xor, req->assoclen, cryptlen);
395}
396
397static int crypto_aegis128_encrypt(struct aead_request *req)
398{
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +0300399 const struct aegis128_ops *ops = &(struct aegis128_ops){
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200400 .skcipher_walk_init = skcipher_walk_aead_encrypt,
401 .crypt_chunk = crypto_aegis128_encrypt_chunk,
402 };
403
404 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
405 union aegis_block tag = {};
406 unsigned int authsize = crypto_aead_authsize(tfm);
407 unsigned int cryptlen = req->cryptlen;
408
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +0300409 if (aegis128_do_simd())
410 ops = &(struct aegis128_ops){
411 .skcipher_walk_init = skcipher_walk_aead_encrypt,
412 .crypt_chunk = crypto_aegis128_encrypt_chunk_simd };
413
414 crypto_aegis128_crypt(req, &tag, cryptlen, ops);
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200415
416 scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
417 authsize, 1);
418 return 0;
419}
420
421static int crypto_aegis128_decrypt(struct aead_request *req)
422{
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +0300423 const struct aegis128_ops *ops = &(struct aegis128_ops){
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200424 .skcipher_walk_init = skcipher_walk_aead_decrypt,
425 .crypt_chunk = crypto_aegis128_decrypt_chunk,
426 };
427 static const u8 zeros[AEGIS128_MAX_AUTH_SIZE] = {};
428
429 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
430 union aegis_block tag;
431 unsigned int authsize = crypto_aead_authsize(tfm);
432 unsigned int cryptlen = req->cryptlen - authsize;
433
434 scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
435 authsize, 0);
436
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +0300437 if (aegis128_do_simd())
438 ops = &(struct aegis128_ops){
439 .skcipher_walk_init = skcipher_walk_aead_decrypt,
440 .crypt_chunk = crypto_aegis128_decrypt_chunk_simd };
441
442 crypto_aegis128_crypt(req, &tag, cryptlen, ops);
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200443
444 return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
445}
446
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200447static struct aead_alg crypto_aegis128_alg = {
448 .setkey = crypto_aegis128_setkey,
449 .setauthsize = crypto_aegis128_setauthsize,
450 .encrypt = crypto_aegis128_encrypt,
451 .decrypt = crypto_aegis128_decrypt,
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200452
453 .ivsize = AEGIS128_NONCE_SIZE,
454 .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
455 .chunksize = AEGIS_BLOCK_SIZE,
456
457 .base = {
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200458 .cra_blocksize = 1,
459 .cra_ctxsize = sizeof(struct aegis_ctx),
460 .cra_alignmask = 0,
461
462 .cra_priority = 100,
463
464 .cra_name = "aegis128",
465 .cra_driver_name = "aegis128-generic",
466
467 .cra_module = THIS_MODULE,
468 }
469};
470
471static int __init crypto_aegis128_module_init(void)
472{
Ard Biesheuvelcf3d41a2019-08-12 01:59:10 +0300473 if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD))
474 have_simd = crypto_aegis128_have_simd();
475
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200476 return crypto_register_aead(&crypto_aegis128_alg);
477}
478
479static void __exit crypto_aegis128_module_exit(void)
480{
481 crypto_unregister_aead(&crypto_aegis128_alg);
482}
483
Eric Biggersc4741b22019-04-11 21:57:42 -0700484subsys_initcall(crypto_aegis128_module_init);
Ondrej Mosnacekf606a882018-05-11 14:12:49 +0200485module_exit(crypto_aegis128_module_exit);
486
487MODULE_LICENSE("GPL");
488MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
489MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm");
490MODULE_ALIAS_CRYPTO("aegis128");
491MODULE_ALIAS_CRYPTO("aegis128-generic");