blob: 41a3090cda8ee069cb4b8cd315871e38fe7cc3be [file] [log] [blame]
Thomas Gleixnerea5d8cf2019-01-18 00:14:18 +01001/* SPDX-License-Identifier: GPL-2.0-or-later */
Ondrej Mosnacekf606a882018-05-11 14:12:49 +02002/*
3 * AEGIS common definitions
4 *
5 * Copyright (c) 2018 Ondrej Mosnacek <omosnacek@gmail.com>
6 * Copyright (c) 2018 Red Hat, Inc. All rights reserved.
Ondrej Mosnacekf606a882018-05-11 14:12:49 +02007 */
8
9#ifndef _CRYPTO_AEGIS_H
10#define _CRYPTO_AEGIS_H
11
12#include <crypto/aes.h>
13#include <linux/types.h>
14
15#define AEGIS_BLOCK_SIZE 16
16
17union aegis_block {
18 __le64 words64[AEGIS_BLOCK_SIZE / sizeof(__le64)];
Ard Biesheuvel4a34e3c2018-10-01 10:36:38 +020019 __le32 words32[AEGIS_BLOCK_SIZE / sizeof(__le32)];
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020020 u8 bytes[AEGIS_BLOCK_SIZE];
21};
22
23#define AEGIS_BLOCK_ALIGN (__alignof__(union aegis_block))
24#define AEGIS_ALIGNED(p) IS_ALIGNED((uintptr_t)p, AEGIS_BLOCK_ALIGN)
25
26static const union aegis_block crypto_aegis_const[2] = {
27 { .words64 = {
28 cpu_to_le64(U64_C(0x0d08050302010100)),
29 cpu_to_le64(U64_C(0x6279e99059372215)),
30 } },
31 { .words64 = {
32 cpu_to_le64(U64_C(0xf12fc26d55183ddb)),
33 cpu_to_le64(U64_C(0xdd28b57342311120)),
34 } },
35};
36
37static void crypto_aegis_block_xor(union aegis_block *dst,
38 const union aegis_block *src)
39{
40 dst->words64[0] ^= src->words64[0];
41 dst->words64[1] ^= src->words64[1];
42}
43
44static void crypto_aegis_block_and(union aegis_block *dst,
45 const union aegis_block *src)
46{
47 dst->words64[0] &= src->words64[0];
48 dst->words64[1] &= src->words64[1];
49}
50
51static void crypto_aegis_aesenc(union aegis_block *dst,
52 const union aegis_block *src,
53 const union aegis_block *key)
54{
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020055 const u8 *s = src->bytes;
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020056 const u32 *t0 = crypto_ft_tab[0];
57 const u32 *t1 = crypto_ft_tab[1];
58 const u32 *t2 = crypto_ft_tab[2];
59 const u32 *t3 = crypto_ft_tab[3];
60 u32 d0, d1, d2, d3;
61
Ard Biesheuvel4a34e3c2018-10-01 10:36:38 +020062 d0 = t0[s[ 0]] ^ t1[s[ 5]] ^ t2[s[10]] ^ t3[s[15]];
63 d1 = t0[s[ 4]] ^ t1[s[ 9]] ^ t2[s[14]] ^ t3[s[ 3]];
64 d2 = t0[s[ 8]] ^ t1[s[13]] ^ t2[s[ 2]] ^ t3[s[ 7]];
65 d3 = t0[s[12]] ^ t1[s[ 1]] ^ t2[s[ 6]] ^ t3[s[11]];
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020066
Ard Biesheuvel4a34e3c2018-10-01 10:36:38 +020067 dst->words32[0] = cpu_to_le32(d0) ^ key->words32[0];
68 dst->words32[1] = cpu_to_le32(d1) ^ key->words32[1];
69 dst->words32[2] = cpu_to_le32(d2) ^ key->words32[2];
70 dst->words32[3] = cpu_to_le32(d3) ^ key->words32[3];
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020071}
72
73#endif /* _CRYPTO_AEGIS_H */