Thomas Gleixner | ea5d8cf | 2019-01-18 00:14:18 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 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. |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef _CRYPTO_AEGIS_H |
| 10 | #define _CRYPTO_AEGIS_H |
| 11 | |
| 12 | #include <crypto/aes.h> |
Ard Biesheuvel | 521cdde | 2019-07-03 10:55:09 +0200 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 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)]; |
Ard Biesheuvel | 4a34e3c | 2018-10-01 10:36:38 +0200 | [diff] [blame] | 20 | __le32 words32[AEGIS_BLOCK_SIZE / sizeof(__le32)]; |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 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 | |
Arnd Bergmann | 97ac82d | 2019-07-18 15:50:04 +0200 | [diff] [blame] | 27 | static __always_inline void crypto_aegis_block_xor(union aegis_block *dst, |
| 28 | const union aegis_block *src) |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 29 | { |
| 30 | dst->words64[0] ^= src->words64[0]; |
| 31 | dst->words64[1] ^= src->words64[1]; |
| 32 | } |
| 33 | |
Arnd Bergmann | 97ac82d | 2019-07-18 15:50:04 +0200 | [diff] [blame] | 34 | static __always_inline void crypto_aegis_block_and(union aegis_block *dst, |
| 35 | const union aegis_block *src) |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 36 | { |
| 37 | dst->words64[0] &= src->words64[0]; |
| 38 | dst->words64[1] &= src->words64[1]; |
| 39 | } |
| 40 | |
Arnd Bergmann | 97ac82d | 2019-07-18 15:50:04 +0200 | [diff] [blame] | 41 | static __always_inline void crypto_aegis_aesenc(union aegis_block *dst, |
| 42 | const union aegis_block *src, |
| 43 | const union aegis_block *key) |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 44 | { |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 45 | const u8 *s = src->bytes; |
Ard Biesheuvel | 521cdde | 2019-07-03 10:55:09 +0200 | [diff] [blame] | 46 | const u32 *t = crypto_ft_tab[0]; |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 47 | u32 d0, d1, d2, d3; |
| 48 | |
Ard Biesheuvel | 521cdde | 2019-07-03 10:55:09 +0200 | [diff] [blame] | 49 | d0 = t[s[ 0]] ^ rol32(t[s[ 5]], 8) ^ rol32(t[s[10]], 16) ^ rol32(t[s[15]], 24); |
| 50 | d1 = t[s[ 4]] ^ rol32(t[s[ 9]], 8) ^ rol32(t[s[14]], 16) ^ rol32(t[s[ 3]], 24); |
| 51 | d2 = t[s[ 8]] ^ rol32(t[s[13]], 8) ^ rol32(t[s[ 2]], 16) ^ rol32(t[s[ 7]], 24); |
| 52 | d3 = t[s[12]] ^ rol32(t[s[ 1]], 8) ^ rol32(t[s[ 6]], 16) ^ rol32(t[s[11]], 24); |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 53 | |
Ard Biesheuvel | 4a34e3c | 2018-10-01 10:36:38 +0200 | [diff] [blame] | 54 | dst->words32[0] = cpu_to_le32(d0) ^ key->words32[0]; |
| 55 | dst->words32[1] = cpu_to_le32(d1) ^ key->words32[1]; |
| 56 | dst->words32[2] = cpu_to_le32(d2) ^ key->words32[2]; |
| 57 | dst->words32[3] = cpu_to_le32(d3) ^ key->words32[3]; |
Ondrej Mosnacek | f606a88 | 2018-05-11 14:12:49 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | #endif /* _CRYPTO_AEGIS_H */ |