aegis.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* SPDX-License-Identifier: GPL-2.0 */
  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. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. */
  13. #ifndef _CRYPTO_AEGIS_H
  14. #define _CRYPTO_AEGIS_H
  15. #include <crypto/aes.h>
  16. #include <linux/types.h>
  17. #define AEGIS_BLOCK_SIZE 16
  18. union aegis_block {
  19. __le64 words64[AEGIS_BLOCK_SIZE / sizeof(__le64)];
  20. u32 words32[AEGIS_BLOCK_SIZE / sizeof(u32)];
  21. u8 bytes[AEGIS_BLOCK_SIZE];
  22. };
  23. #define AEGIS_BLOCK_ALIGN (__alignof__(union aegis_block))
  24. #define AEGIS_ALIGNED(p) IS_ALIGNED((uintptr_t)p, AEGIS_BLOCK_ALIGN)
  25. static const union aegis_block crypto_aegis_const[2] = {
  26. { .words64 = {
  27. cpu_to_le64(U64_C(0x0d08050302010100)),
  28. cpu_to_le64(U64_C(0x6279e99059372215)),
  29. } },
  30. { .words64 = {
  31. cpu_to_le64(U64_C(0xf12fc26d55183ddb)),
  32. cpu_to_le64(U64_C(0xdd28b57342311120)),
  33. } },
  34. };
  35. static void crypto_aegis_block_xor(union aegis_block *dst,
  36. const union aegis_block *src)
  37. {
  38. dst->words64[0] ^= src->words64[0];
  39. dst->words64[1] ^= src->words64[1];
  40. }
  41. static void crypto_aegis_block_and(union aegis_block *dst,
  42. const union aegis_block *src)
  43. {
  44. dst->words64[0] &= src->words64[0];
  45. dst->words64[1] &= src->words64[1];
  46. }
  47. static void crypto_aegis_aesenc(union aegis_block *dst,
  48. const union aegis_block *src,
  49. const union aegis_block *key)
  50. {
  51. u32 *d = dst->words32;
  52. const u8 *s = src->bytes;
  53. const u32 *k = key->words32;
  54. const u32 *t0 = crypto_ft_tab[0];
  55. const u32 *t1 = crypto_ft_tab[1];
  56. const u32 *t2 = crypto_ft_tab[2];
  57. const u32 *t3 = crypto_ft_tab[3];
  58. u32 d0, d1, d2, d3;
  59. d0 = t0[s[ 0]] ^ t1[s[ 5]] ^ t2[s[10]] ^ t3[s[15]] ^ k[0];
  60. d1 = t0[s[ 4]] ^ t1[s[ 9]] ^ t2[s[14]] ^ t3[s[ 3]] ^ k[1];
  61. d2 = t0[s[ 8]] ^ t1[s[13]] ^ t2[s[ 2]] ^ t3[s[ 7]] ^ k[2];
  62. d3 = t0[s[12]] ^ t1[s[ 1]] ^ t2[s[ 6]] ^ t3[s[11]] ^ k[3];
  63. d[0] = d0;
  64. d[1] = d1;
  65. d[2] = d2;
  66. d[3] = d3;
  67. }
  68. #endif /* _CRYPTO_AEGIS_H */