xref: /linux/crypto/aegis.h (revision 09149997)
1ea5d8cfaSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2f606a88eSOndrej Mosnacek /*
3f606a88eSOndrej Mosnacek  * AEGIS common definitions
4f606a88eSOndrej Mosnacek  *
5f606a88eSOndrej Mosnacek  * Copyright (c) 2018 Ondrej Mosnacek <omosnacek@gmail.com>
6f606a88eSOndrej Mosnacek  * Copyright (c) 2018 Red Hat, Inc. All rights reserved.
7f606a88eSOndrej Mosnacek  */
8f606a88eSOndrej Mosnacek 
9f606a88eSOndrej Mosnacek #ifndef _CRYPTO_AEGIS_H
10f606a88eSOndrej Mosnacek #define _CRYPTO_AEGIS_H
11f606a88eSOndrej Mosnacek 
12f606a88eSOndrej Mosnacek #include <crypto/aes.h>
13521cdde7SArd Biesheuvel #include <linux/bitops.h>
14f606a88eSOndrej Mosnacek #include <linux/types.h>
15f606a88eSOndrej Mosnacek 
16f606a88eSOndrej Mosnacek #define AEGIS_BLOCK_SIZE 16
17f606a88eSOndrej Mosnacek 
18f606a88eSOndrej Mosnacek union aegis_block {
19f606a88eSOndrej Mosnacek 	__le64 words64[AEGIS_BLOCK_SIZE / sizeof(__le64)];
204a34e3c2SArd Biesheuvel 	__le32 words32[AEGIS_BLOCK_SIZE / sizeof(__le32)];
21f606a88eSOndrej Mosnacek 	u8 bytes[AEGIS_BLOCK_SIZE];
22f606a88eSOndrej Mosnacek };
23f606a88eSOndrej Mosnacek 
24*09149997SHerbert Xu struct aegis_state;
25*09149997SHerbert Xu 
26*09149997SHerbert Xu extern int aegis128_have_aes_insn;
27*09149997SHerbert Xu 
28f606a88eSOndrej Mosnacek #define AEGIS_BLOCK_ALIGN (__alignof__(union aegis_block))
29f606a88eSOndrej Mosnacek #define AEGIS_ALIGNED(p) IS_ALIGNED((uintptr_t)p, AEGIS_BLOCK_ALIGN)
30f606a88eSOndrej Mosnacek 
31*09149997SHerbert Xu bool crypto_aegis128_have_simd(void);
32*09149997SHerbert Xu void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
33*09149997SHerbert Xu void crypto_aegis128_init_simd(struct aegis_state *state,
34*09149997SHerbert Xu 			       const union aegis_block *key,
35*09149997SHerbert Xu 			       const u8 *iv);
36*09149997SHerbert Xu void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
37*09149997SHerbert Xu 					const u8 *src, unsigned int size);
38*09149997SHerbert Xu void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
39*09149997SHerbert Xu 					const u8 *src, unsigned int size);
40*09149997SHerbert Xu int crypto_aegis128_final_simd(struct aegis_state *state,
41*09149997SHerbert Xu 			       union aegis_block *tag_xor,
42*09149997SHerbert Xu 			       unsigned int assoclen,
43*09149997SHerbert Xu 			       unsigned int cryptlen,
44*09149997SHerbert Xu 			       unsigned int authsize);
45*09149997SHerbert Xu 
crypto_aegis_block_xor(union aegis_block * dst,const union aegis_block * src)4697ac82d9SArnd Bergmann static __always_inline void crypto_aegis_block_xor(union aegis_block *dst,
47f606a88eSOndrej Mosnacek 						   const union aegis_block *src)
48f606a88eSOndrej Mosnacek {
49f606a88eSOndrej Mosnacek 	dst->words64[0] ^= src->words64[0];
50f606a88eSOndrej Mosnacek 	dst->words64[1] ^= src->words64[1];
51f606a88eSOndrej Mosnacek }
52f606a88eSOndrej Mosnacek 
crypto_aegis_block_and(union aegis_block * dst,const union aegis_block * src)5397ac82d9SArnd Bergmann static __always_inline void crypto_aegis_block_and(union aegis_block *dst,
54f606a88eSOndrej Mosnacek 						   const union aegis_block *src)
55f606a88eSOndrej Mosnacek {
56f606a88eSOndrej Mosnacek 	dst->words64[0] &= src->words64[0];
57f606a88eSOndrej Mosnacek 	dst->words64[1] &= src->words64[1];
58f606a88eSOndrej Mosnacek }
59f606a88eSOndrej Mosnacek 
crypto_aegis_aesenc(union aegis_block * dst,const union aegis_block * src,const union aegis_block * key)6097ac82d9SArnd Bergmann static __always_inline void crypto_aegis_aesenc(union aegis_block *dst,
61f606a88eSOndrej Mosnacek 						const union aegis_block *src,
62f606a88eSOndrej Mosnacek 						const union aegis_block *key)
63f606a88eSOndrej Mosnacek {
64f606a88eSOndrej Mosnacek 	const u8  *s  = src->bytes;
65521cdde7SArd Biesheuvel 	const u32 *t = crypto_ft_tab[0];
66f606a88eSOndrej Mosnacek 	u32 d0, d1, d2, d3;
67f606a88eSOndrej Mosnacek 
68521cdde7SArd Biesheuvel 	d0 = t[s[ 0]] ^ rol32(t[s[ 5]], 8) ^ rol32(t[s[10]], 16) ^ rol32(t[s[15]], 24);
69521cdde7SArd Biesheuvel 	d1 = t[s[ 4]] ^ rol32(t[s[ 9]], 8) ^ rol32(t[s[14]], 16) ^ rol32(t[s[ 3]], 24);
70521cdde7SArd Biesheuvel 	d2 = t[s[ 8]] ^ rol32(t[s[13]], 8) ^ rol32(t[s[ 2]], 16) ^ rol32(t[s[ 7]], 24);
71521cdde7SArd Biesheuvel 	d3 = t[s[12]] ^ rol32(t[s[ 1]], 8) ^ rol32(t[s[ 6]], 16) ^ rol32(t[s[11]], 24);
72f606a88eSOndrej Mosnacek 
734a34e3c2SArd Biesheuvel 	dst->words32[0] = cpu_to_le32(d0) ^ key->words32[0];
744a34e3c2SArd Biesheuvel 	dst->words32[1] = cpu_to_le32(d1) ^ key->words32[1];
754a34e3c2SArd Biesheuvel 	dst->words32[2] = cpu_to_le32(d2) ^ key->words32[2];
764a34e3c2SArd Biesheuvel 	dst->words32[3] = cpu_to_le32(d3) ^ key->words32[3];
77f606a88eSOndrej Mosnacek }
78f606a88eSOndrej Mosnacek 
79f606a88eSOndrej Mosnacek #endif /* _CRYPTO_AEGIS_H */
80