blob: 6ef9c174c973d1eb50f8e47dd016bce5722f6b59 [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>
Ard Biesheuvel521cdde2019-07-03 10:55:09 +020013#include <linux/bitops.h>
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020014#include <linux/types.h>
15
16#define AEGIS_BLOCK_SIZE 16
17
18union aegis_block {
19 __le64 words64[AEGIS_BLOCK_SIZE / sizeof(__le64)];
Ard Biesheuvel4a34e3c2018-10-01 10:36:38 +020020 __le32 words32[AEGIS_BLOCK_SIZE / sizeof(__le32)];
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020021 u8 bytes[AEGIS_BLOCK_SIZE];
22};
23
Herbert Xu09149992021-03-08 16:41:32 +110024struct aegis_state;
25
26extern int aegis128_have_aes_insn;
27
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020028#define AEGIS_BLOCK_ALIGN (__alignof__(union aegis_block))
29#define AEGIS_ALIGNED(p) IS_ALIGNED((uintptr_t)p, AEGIS_BLOCK_ALIGN)
30
Herbert Xu09149992021-03-08 16:41:32 +110031bool crypto_aegis128_have_simd(void);
32void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
33void crypto_aegis128_init_simd(struct aegis_state *state,
34 const union aegis_block *key,
35 const u8 *iv);
36void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
37 const u8 *src, unsigned int size);
38void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
39 const u8 *src, unsigned int size);
40int crypto_aegis128_final_simd(struct aegis_state *state,
41 union aegis_block *tag_xor,
42 unsigned int assoclen,
43 unsigned int cryptlen,
44 unsigned int authsize);
45
Arnd Bergmann97ac82d2019-07-18 15:50:04 +020046static __always_inline void crypto_aegis_block_xor(union aegis_block *dst,
47 const union aegis_block *src)
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020048{
49 dst->words64[0] ^= src->words64[0];
50 dst->words64[1] ^= src->words64[1];
51}
52
Arnd Bergmann97ac82d2019-07-18 15:50:04 +020053static __always_inline void crypto_aegis_block_and(union aegis_block *dst,
54 const union aegis_block *src)
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020055{
56 dst->words64[0] &= src->words64[0];
57 dst->words64[1] &= src->words64[1];
58}
59
Arnd Bergmann97ac82d2019-07-18 15:50:04 +020060static __always_inline void crypto_aegis_aesenc(union aegis_block *dst,
61 const union aegis_block *src,
62 const union aegis_block *key)
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020063{
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020064 const u8 *s = src->bytes;
Ard Biesheuvel521cdde2019-07-03 10:55:09 +020065 const u32 *t = crypto_ft_tab[0];
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020066 u32 d0, d1, d2, d3;
67
Ard Biesheuvel521cdde2019-07-03 10:55:09 +020068 d0 = t[s[ 0]] ^ rol32(t[s[ 5]], 8) ^ rol32(t[s[10]], 16) ^ rol32(t[s[15]], 24);
69 d1 = t[s[ 4]] ^ rol32(t[s[ 9]], 8) ^ rol32(t[s[14]], 16) ^ rol32(t[s[ 3]], 24);
70 d2 = t[s[ 8]] ^ rol32(t[s[13]], 8) ^ rol32(t[s[ 2]], 16) ^ rol32(t[s[ 7]], 24);
71 d3 = t[s[12]] ^ rol32(t[s[ 1]], 8) ^ rol32(t[s[ 6]], 16) ^ rol32(t[s[11]], 24);
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020072
Ard Biesheuvel4a34e3c2018-10-01 10:36:38 +020073 dst->words32[0] = cpu_to_le32(d0) ^ key->words32[0];
74 dst->words32[1] = cpu_to_le32(d1) ^ key->words32[1];
75 dst->words32[2] = cpu_to_le32(d2) ^ key->words32[2];
76 dst->words32[3] = cpu_to_le32(d3) ^ key->words32[3];
Ondrej Mosnacekf606a882018-05-11 14:12:49 +020077}
78
79#endif /* _CRYPTO_AEGIS_H */