aegis.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef _CRYPTO_AEGIS_H
  9. #define _CRYPTO_AEGIS_H
  10. #include <crypto/aes.h>
  11. #include <linux/bitops.h>
  12. #include <linux/types.h>
  13. #define AEGIS_BLOCK_SIZE 16
  14. union aegis_block {
  15. __le64 words64[AEGIS_BLOCK_SIZE / sizeof(__le64)];
  16. __le32 words32[AEGIS_BLOCK_SIZE / sizeof(__le32)];
  17. u8 bytes[AEGIS_BLOCK_SIZE];
  18. };
  19. #define AEGIS_BLOCK_ALIGN (__alignof__(union aegis_block))
  20. #define AEGIS_ALIGNED(p) IS_ALIGNED((uintptr_t)p, AEGIS_BLOCK_ALIGN)
  21. static __always_inline void crypto_aegis_block_xor(union aegis_block *dst,
  22. const union aegis_block *src)
  23. {
  24. dst->words64[0] ^= src->words64[0];
  25. dst->words64[1] ^= src->words64[1];
  26. }
  27. static __always_inline void crypto_aegis_block_and(union aegis_block *dst,
  28. const union aegis_block *src)
  29. {
  30. dst->words64[0] &= src->words64[0];
  31. dst->words64[1] &= src->words64[1];
  32. }
  33. static __always_inline void crypto_aegis_aesenc(union aegis_block *dst,
  34. const union aegis_block *src,
  35. const union aegis_block *key)
  36. {
  37. const u8 *s = src->bytes;
  38. const u32 *t = crypto_ft_tab[0];
  39. u32 d0, d1, d2, d3;
  40. d0 = t[s[ 0]] ^ rol32(t[s[ 5]], 8) ^ rol32(t[s[10]], 16) ^ rol32(t[s[15]], 24);
  41. d1 = t[s[ 4]] ^ rol32(t[s[ 9]], 8) ^ rol32(t[s[14]], 16) ^ rol32(t[s[ 3]], 24);
  42. d2 = t[s[ 8]] ^ rol32(t[s[13]], 8) ^ rol32(t[s[ 2]], 16) ^ rol32(t[s[ 7]], 24);
  43. d3 = t[s[12]] ^ rol32(t[s[ 1]], 8) ^ rol32(t[s[ 6]], 16) ^ rol32(t[s[11]], 24);
  44. dst->words32[0] = cpu_to_le32(d0) ^ key->words32[0];
  45. dst->words32[1] = cpu_to_le32(d1) ^ key->words32[1];
  46. dst->words32[2] = cpu_to_le32(d2) ^ key->words32[2];
  47. dst->words32[3] = cpu_to_le32(d3) ^ key->words32[3];
  48. }
  49. #endif /* _CRYPTO_AEGIS_H */