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