xref: /OK3568_Linux_fs/kernel/crypto/aegis.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * AEGIS common definitions
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2018 Ondrej Mosnacek <omosnacek@gmail.com>
6*4882a593Smuzhiyun  * Copyright (c) 2018 Red Hat, Inc. All rights reserved.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef _CRYPTO_AEGIS_H
10*4882a593Smuzhiyun #define _CRYPTO_AEGIS_H
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <crypto/aes.h>
13*4882a593Smuzhiyun #include <linux/bitops.h>
14*4882a593Smuzhiyun #include <linux/types.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define AEGIS_BLOCK_SIZE 16
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun union aegis_block {
19*4882a593Smuzhiyun 	__le64 words64[AEGIS_BLOCK_SIZE / sizeof(__le64)];
20*4882a593Smuzhiyun 	__le32 words32[AEGIS_BLOCK_SIZE / sizeof(__le32)];
21*4882a593Smuzhiyun 	u8 bytes[AEGIS_BLOCK_SIZE];
22*4882a593Smuzhiyun };
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define AEGIS_BLOCK_ALIGN (__alignof__(union aegis_block))
25*4882a593Smuzhiyun #define AEGIS_ALIGNED(p) IS_ALIGNED((uintptr_t)p, AEGIS_BLOCK_ALIGN)
26*4882a593Smuzhiyun 
crypto_aegis_block_xor(union aegis_block * dst,const union aegis_block * src)27*4882a593Smuzhiyun static __always_inline void crypto_aegis_block_xor(union aegis_block *dst,
28*4882a593Smuzhiyun 						   const union aegis_block *src)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	dst->words64[0] ^= src->words64[0];
31*4882a593Smuzhiyun 	dst->words64[1] ^= src->words64[1];
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
crypto_aegis_block_and(union aegis_block * dst,const union aegis_block * src)34*4882a593Smuzhiyun static __always_inline void crypto_aegis_block_and(union aegis_block *dst,
35*4882a593Smuzhiyun 						   const union aegis_block *src)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	dst->words64[0] &= src->words64[0];
38*4882a593Smuzhiyun 	dst->words64[1] &= src->words64[1];
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
crypto_aegis_aesenc(union aegis_block * dst,const union aegis_block * src,const union aegis_block * key)41*4882a593Smuzhiyun static __always_inline void crypto_aegis_aesenc(union aegis_block *dst,
42*4882a593Smuzhiyun 						const union aegis_block *src,
43*4882a593Smuzhiyun 						const union aegis_block *key)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	const u8  *s  = src->bytes;
46*4882a593Smuzhiyun 	const u32 *t = crypto_ft_tab[0];
47*4882a593Smuzhiyun 	u32 d0, d1, d2, d3;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	d0 = t[s[ 0]] ^ rol32(t[s[ 5]], 8) ^ rol32(t[s[10]], 16) ^ rol32(t[s[15]], 24);
50*4882a593Smuzhiyun 	d1 = t[s[ 4]] ^ rol32(t[s[ 9]], 8) ^ rol32(t[s[14]], 16) ^ rol32(t[s[ 3]], 24);
51*4882a593Smuzhiyun 	d2 = t[s[ 8]] ^ rol32(t[s[13]], 8) ^ rol32(t[s[ 2]], 16) ^ rol32(t[s[ 7]], 24);
52*4882a593Smuzhiyun 	d3 = t[s[12]] ^ rol32(t[s[ 1]], 8) ^ rol32(t[s[ 6]], 16) ^ rol32(t[s[11]], 24);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	dst->words32[0] = cpu_to_le32(d0) ^ key->words32[0];
55*4882a593Smuzhiyun 	dst->words32[1] = cpu_to_le32(d1) ^ key->words32[1];
56*4882a593Smuzhiyun 	dst->words32[2] = cpu_to_le32(d2) ^ key->words32[2];
57*4882a593Smuzhiyun 	dst->words32[3] = cpu_to_le32(d3) ^ key->words32[3];
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #endif /* _CRYPTO_AEGIS_H */
61