aegis.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * AEGIS common definitions
  4. *
  5. * Copyright (c) 2018 Ondrej Mosnacek <[email protected]>
  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. struct aegis_state;
  20. extern int aegis128_have_aes_insn;
  21. #define AEGIS_BLOCK_ALIGN (__alignof__(union aegis_block))
  22. #define AEGIS_ALIGNED(p) IS_ALIGNED((uintptr_t)p, AEGIS_BLOCK_ALIGN)
  23. bool crypto_aegis128_have_simd(void);
  24. void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
  25. void crypto_aegis128_init_simd(struct aegis_state *state,
  26. const union aegis_block *key,
  27. const u8 *iv);
  28. void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
  29. const u8 *src, unsigned int size);
  30. void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
  31. const u8 *src, unsigned int size);
  32. int crypto_aegis128_final_simd(struct aegis_state *state,
  33. union aegis_block *tag_xor,
  34. unsigned int assoclen,
  35. unsigned int cryptlen,
  36. unsigned int authsize);
  37. static __always_inline 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. static __always_inline void crypto_aegis_block_and(union aegis_block *dst,
  44. const union aegis_block *src)
  45. {
  46. dst->words64[0] &= src->words64[0];
  47. dst->words64[1] &= src->words64[1];
  48. }
  49. static __always_inline void crypto_aegis_aesenc(union aegis_block *dst,
  50. const union aegis_block *src,
  51. const union aegis_block *key)
  52. {
  53. const u8 *s = src->bytes;
  54. const u32 *t = crypto_ft_tab[0];
  55. u32 d0, d1, d2, d3;
  56. d0 = t[s[ 0]] ^ rol32(t[s[ 5]], 8) ^ rol32(t[s[10]], 16) ^ rol32(t[s[15]], 24);
  57. d1 = t[s[ 4]] ^ rol32(t[s[ 9]], 8) ^ rol32(t[s[14]], 16) ^ rol32(t[s[ 3]], 24);
  58. d2 = t[s[ 8]] ^ rol32(t[s[13]], 8) ^ rol32(t[s[ 2]], 16) ^ rol32(t[s[ 7]], 24);
  59. d3 = t[s[12]] ^ rol32(t[s[ 1]], 8) ^ rol32(t[s[ 6]], 16) ^ rol32(t[s[11]], 24);
  60. dst->words32[0] = cpu_to_le32(d0) ^ key->words32[0];
  61. dst->words32[1] = cpu_to_le32(d1) ^ key->words32[1];
  62. dst->words32[2] = cpu_to_le32(d2) ^ key->words32[2];
  63. dst->words32[3] = cpu_to_le32(d3) ^ key->words32[3];
  64. }
  65. #endif /* _CRYPTO_AEGIS_H */