1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * The AEGIS-128 Authenticated-Encryption Algorithm
5 * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com>
6 * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
9 #include <crypto/algapi.h>
10 #include <crypto/internal/aead.h>
11 #include <crypto/internal/simd.h>
12 #include <crypto/internal/skcipher.h>
13 #include <crypto/scatterwalk.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/jump_label.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/scatterlist.h>
25 #define AEGIS128_NONCE_SIZE 16
26 #define AEGIS128_STATE_BLOCKS 5
27 #define AEGIS128_KEY_SIZE 16
28 #define AEGIS128_MIN_AUTH_SIZE 8
29 #define AEGIS128_MAX_AUTH_SIZE 16
32 union aegis_block blocks[AEGIS128_STATE_BLOCKS];
36 union aegis_block key;
39 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_simd);
41 static const union aegis_block crypto_aegis_const[2] = {
43 cpu_to_le64(U64_C(0x0d08050302010100)),
44 cpu_to_le64(U64_C(0x6279e99059372215)),
47 cpu_to_le64(U64_C(0xf12fc26d55183ddb)),
48 cpu_to_le64(U64_C(0xdd28b57342311120)),
52 static bool aegis128_do_simd(void)
54 #ifdef CONFIG_CRYPTO_AEGIS128_SIMD
55 if (static_branch_likely(&have_simd))
56 return crypto_simd_usable();
61 bool crypto_aegis128_have_simd(void);
62 void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
63 void crypto_aegis128_init_simd(struct aegis_state *state,
64 const union aegis_block *key,
66 void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
67 const u8 *src, unsigned int size);
68 void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
69 const u8 *src, unsigned int size);
70 int crypto_aegis128_final_simd(struct aegis_state *state,
71 union aegis_block *tag_xor,
72 unsigned int assoclen,
73 unsigned int cryptlen,
74 unsigned int authsize);
76 static void crypto_aegis128_update(struct aegis_state *state)
78 union aegis_block tmp;
81 tmp = state->blocks[AEGIS128_STATE_BLOCKS - 1];
82 for (i = AEGIS128_STATE_BLOCKS - 1; i > 0; i--)
83 crypto_aegis_aesenc(&state->blocks[i], &state->blocks[i - 1],
85 crypto_aegis_aesenc(&state->blocks[0], &tmp, &state->blocks[0]);
88 static void crypto_aegis128_update_a(struct aegis_state *state,
89 const union aegis_block *msg)
91 if (aegis128_do_simd()) {
92 crypto_aegis128_update_simd(state, msg);
96 crypto_aegis128_update(state);
97 crypto_aegis_block_xor(&state->blocks[0], msg);
100 static void crypto_aegis128_update_u(struct aegis_state *state, const void *msg)
102 if (aegis128_do_simd()) {
103 crypto_aegis128_update_simd(state, msg);
107 crypto_aegis128_update(state);
108 crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
111 static void crypto_aegis128_init(struct aegis_state *state,
112 const union aegis_block *key,
115 union aegis_block key_iv;
119 crypto_xor(key_iv.bytes, iv, AEGIS_BLOCK_SIZE);
121 state->blocks[0] = key_iv;
122 state->blocks[1] = crypto_aegis_const[1];
123 state->blocks[2] = crypto_aegis_const[0];
124 state->blocks[3] = *key;
125 state->blocks[4] = *key;
127 crypto_aegis_block_xor(&state->blocks[3], &crypto_aegis_const[0]);
128 crypto_aegis_block_xor(&state->blocks[4], &crypto_aegis_const[1]);
130 for (i = 0; i < 5; i++) {
131 crypto_aegis128_update_a(state, key);
132 crypto_aegis128_update_a(state, &key_iv);
136 static void crypto_aegis128_ad(struct aegis_state *state,
137 const u8 *src, unsigned int size)
139 if (AEGIS_ALIGNED(src)) {
140 const union aegis_block *src_blk =
141 (const union aegis_block *)src;
143 while (size >= AEGIS_BLOCK_SIZE) {
144 crypto_aegis128_update_a(state, src_blk);
146 size -= AEGIS_BLOCK_SIZE;
150 while (size >= AEGIS_BLOCK_SIZE) {
151 crypto_aegis128_update_u(state, src);
153 size -= AEGIS_BLOCK_SIZE;
154 src += AEGIS_BLOCK_SIZE;
159 static void crypto_aegis128_wipe_chunk(struct aegis_state *state, u8 *dst,
160 const u8 *src, unsigned int size)
162 memzero_explicit(dst, size);
165 static void crypto_aegis128_encrypt_chunk(struct aegis_state *state, u8 *dst,
166 const u8 *src, unsigned int size)
168 union aegis_block tmp;
170 if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
171 while (size >= AEGIS_BLOCK_SIZE) {
172 union aegis_block *dst_blk =
173 (union aegis_block *)dst;
174 const union aegis_block *src_blk =
175 (const union aegis_block *)src;
177 tmp = state->blocks[2];
178 crypto_aegis_block_and(&tmp, &state->blocks[3]);
179 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
180 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
181 crypto_aegis_block_xor(&tmp, src_blk);
183 crypto_aegis128_update_a(state, src_blk);
187 size -= AEGIS_BLOCK_SIZE;
188 src += AEGIS_BLOCK_SIZE;
189 dst += AEGIS_BLOCK_SIZE;
192 while (size >= AEGIS_BLOCK_SIZE) {
193 tmp = state->blocks[2];
194 crypto_aegis_block_and(&tmp, &state->blocks[3]);
195 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
196 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
197 crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
199 crypto_aegis128_update_u(state, src);
201 memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
203 size -= AEGIS_BLOCK_SIZE;
204 src += AEGIS_BLOCK_SIZE;
205 dst += AEGIS_BLOCK_SIZE;
210 union aegis_block msg = {};
211 memcpy(msg.bytes, src, size);
213 tmp = state->blocks[2];
214 crypto_aegis_block_and(&tmp, &state->blocks[3]);
215 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
216 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
218 crypto_aegis128_update_a(state, &msg);
220 crypto_aegis_block_xor(&msg, &tmp);
222 memcpy(dst, msg.bytes, size);
226 static void crypto_aegis128_decrypt_chunk(struct aegis_state *state, u8 *dst,
227 const u8 *src, unsigned int size)
229 union aegis_block tmp;
231 if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
232 while (size >= AEGIS_BLOCK_SIZE) {
233 union aegis_block *dst_blk =
234 (union aegis_block *)dst;
235 const union aegis_block *src_blk =
236 (const union aegis_block *)src;
238 tmp = state->blocks[2];
239 crypto_aegis_block_and(&tmp, &state->blocks[3]);
240 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
241 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
242 crypto_aegis_block_xor(&tmp, src_blk);
244 crypto_aegis128_update_a(state, &tmp);
248 size -= AEGIS_BLOCK_SIZE;
249 src += AEGIS_BLOCK_SIZE;
250 dst += AEGIS_BLOCK_SIZE;
253 while (size >= AEGIS_BLOCK_SIZE) {
254 tmp = state->blocks[2];
255 crypto_aegis_block_and(&tmp, &state->blocks[3]);
256 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
257 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
258 crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
260 crypto_aegis128_update_a(state, &tmp);
262 memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
264 size -= AEGIS_BLOCK_SIZE;
265 src += AEGIS_BLOCK_SIZE;
266 dst += AEGIS_BLOCK_SIZE;
271 union aegis_block msg = {};
272 memcpy(msg.bytes, src, size);
274 tmp = state->blocks[2];
275 crypto_aegis_block_and(&tmp, &state->blocks[3]);
276 crypto_aegis_block_xor(&tmp, &state->blocks[4]);
277 crypto_aegis_block_xor(&tmp, &state->blocks[1]);
278 crypto_aegis_block_xor(&msg, &tmp);
280 memset(msg.bytes + size, 0, AEGIS_BLOCK_SIZE - size);
282 crypto_aegis128_update_a(state, &msg);
284 memcpy(dst, msg.bytes, size);
288 static void crypto_aegis128_process_ad(struct aegis_state *state,
289 struct scatterlist *sg_src,
290 unsigned int assoclen)
292 struct scatter_walk walk;
293 union aegis_block buf;
294 unsigned int pos = 0;
296 scatterwalk_start(&walk, sg_src);
297 while (assoclen != 0) {
298 unsigned int size = scatterwalk_clamp(&walk, assoclen);
299 unsigned int left = size;
300 void *mapped = scatterwalk_map(&walk);
301 const u8 *src = (const u8 *)mapped;
303 if (pos + size >= AEGIS_BLOCK_SIZE) {
305 unsigned int fill = AEGIS_BLOCK_SIZE - pos;
306 memcpy(buf.bytes + pos, src, fill);
307 crypto_aegis128_update_a(state, &buf);
313 crypto_aegis128_ad(state, src, left);
314 src += left & ~(AEGIS_BLOCK_SIZE - 1);
315 left &= AEGIS_BLOCK_SIZE - 1;
318 memcpy(buf.bytes + pos, src, left);
322 scatterwalk_unmap(mapped);
323 scatterwalk_advance(&walk, size);
324 scatterwalk_done(&walk, 0, assoclen);
328 memset(buf.bytes + pos, 0, AEGIS_BLOCK_SIZE - pos);
329 crypto_aegis128_update_a(state, &buf);
333 static __always_inline
334 int crypto_aegis128_process_crypt(struct aegis_state *state,
335 struct skcipher_walk *walk,
336 void (*crypt)(struct aegis_state *state,
337 u8 *dst, const u8 *src,
342 while (walk->nbytes) {
343 unsigned int nbytes = walk->nbytes;
345 if (nbytes < walk->total)
346 nbytes = round_down(nbytes, walk->stride);
348 crypt(state, walk->dst.virt.addr, walk->src.virt.addr, nbytes);
350 err = skcipher_walk_done(walk, walk->nbytes - nbytes);
355 static void crypto_aegis128_final(struct aegis_state *state,
356 union aegis_block *tag_xor,
357 u64 assoclen, u64 cryptlen)
359 u64 assocbits = assoclen * 8;
360 u64 cryptbits = cryptlen * 8;
362 union aegis_block tmp;
365 tmp.words64[0] = cpu_to_le64(assocbits);
366 tmp.words64[1] = cpu_to_le64(cryptbits);
368 crypto_aegis_block_xor(&tmp, &state->blocks[3]);
370 for (i = 0; i < 7; i++)
371 crypto_aegis128_update_a(state, &tmp);
373 for (i = 0; i < AEGIS128_STATE_BLOCKS; i++)
374 crypto_aegis_block_xor(tag_xor, &state->blocks[i]);
377 static int crypto_aegis128_setkey(struct crypto_aead *aead, const u8 *key,
380 struct aegis_ctx *ctx = crypto_aead_ctx(aead);
382 if (keylen != AEGIS128_KEY_SIZE)
385 memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
389 static int crypto_aegis128_setauthsize(struct crypto_aead *tfm,
390 unsigned int authsize)
392 if (authsize > AEGIS128_MAX_AUTH_SIZE)
394 if (authsize < AEGIS128_MIN_AUTH_SIZE)
399 static int crypto_aegis128_encrypt(struct aead_request *req)
401 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
402 union aegis_block tag = {};
403 unsigned int authsize = crypto_aead_authsize(tfm);
404 struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
405 unsigned int cryptlen = req->cryptlen;
406 struct skcipher_walk walk;
407 struct aegis_state state;
409 skcipher_walk_aead_encrypt(&walk, req, false);
410 if (aegis128_do_simd()) {
411 crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
412 crypto_aegis128_process_ad(&state, req->src, req->assoclen);
413 crypto_aegis128_process_crypt(&state, &walk,
414 crypto_aegis128_encrypt_chunk_simd);
415 crypto_aegis128_final_simd(&state, &tag, req->assoclen,
418 crypto_aegis128_init(&state, &ctx->key, req->iv);
419 crypto_aegis128_process_ad(&state, req->src, req->assoclen);
420 crypto_aegis128_process_crypt(&state, &walk,
421 crypto_aegis128_encrypt_chunk);
422 crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
425 scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
430 static int crypto_aegis128_decrypt(struct aead_request *req)
432 static const u8 zeros[AEGIS128_MAX_AUTH_SIZE] = {};
433 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
434 union aegis_block tag;
435 unsigned int authsize = crypto_aead_authsize(tfm);
436 unsigned int cryptlen = req->cryptlen - authsize;
437 struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
438 struct skcipher_walk walk;
439 struct aegis_state state;
441 scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
444 skcipher_walk_aead_decrypt(&walk, req, false);
445 if (aegis128_do_simd()) {
446 crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
447 crypto_aegis128_process_ad(&state, req->src, req->assoclen);
448 crypto_aegis128_process_crypt(&state, &walk,
449 crypto_aegis128_decrypt_chunk_simd);
450 if (unlikely(crypto_aegis128_final_simd(&state, &tag,
452 cryptlen, authsize))) {
453 skcipher_walk_aead_decrypt(&walk, req, false);
454 crypto_aegis128_process_crypt(NULL, req, &walk,
455 crypto_aegis128_wipe_chunk);
460 crypto_aegis128_init(&state, &ctx->key, req->iv);
461 crypto_aegis128_process_ad(&state, req->src, req->assoclen);
462 crypto_aegis128_process_crypt(&state, &walk,
463 crypto_aegis128_decrypt_chunk);
464 crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
467 if (unlikely(crypto_memneq(tag.bytes, zeros, authsize))) {
469 * From Chapter 4. 'Security Analysis' of the AEGIS spec [0]
471 * "3. If verification fails, the decrypted plaintext and the
472 * wrong authentication tag should not be given as output."
474 * [0] https://competitions.cr.yp.to/round3/aegisv11.pdf
476 skcipher_walk_aead_decrypt(&walk, req, false);
477 crypto_aegis128_process_crypt(NULL, &walk,
478 crypto_aegis128_wipe_chunk);
479 memzero_explicit(&tag, sizeof(tag));
485 static struct aead_alg crypto_aegis128_alg = {
486 .setkey = crypto_aegis128_setkey,
487 .setauthsize = crypto_aegis128_setauthsize,
488 .encrypt = crypto_aegis128_encrypt,
489 .decrypt = crypto_aegis128_decrypt,
491 .ivsize = AEGIS128_NONCE_SIZE,
492 .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
493 .chunksize = AEGIS_BLOCK_SIZE,
497 .cra_ctxsize = sizeof(struct aegis_ctx),
502 .cra_name = "aegis128",
503 .cra_driver_name = "aegis128-generic",
505 .cra_module = THIS_MODULE,
509 static int __init crypto_aegis128_module_init(void)
511 if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) &&
512 crypto_aegis128_have_simd())
513 static_branch_enable(&have_simd);
515 return crypto_register_aead(&crypto_aegis128_alg);
518 static void __exit crypto_aegis128_module_exit(void)
520 crypto_unregister_aead(&crypto_aegis128_alg);
523 subsys_initcall(crypto_aegis128_module_init);
524 module_exit(crypto_aegis128_module_exit);
526 MODULE_LICENSE("GPL");
527 MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
528 MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm");
529 MODULE_ALIAS_CRYPTO("aegis128");
530 MODULE_ALIAS_CRYPTO("aegis128-generic");