aes_i.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * AES (Rijndael) cipher
  3. * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #ifndef AES_I_H
  15. #define AES_I_H
  16. #include "aes.h"
  17. #define BLOCK_SIZE 16
  18. /* #define FULL_UNROLL */
  19. #define AES_SMALL_TABLES
  20. extern const u32 Te0[256];
  21. extern const u32 Te1[256];
  22. extern const u32 Te2[256];
  23. extern const u32 Te3[256];
  24. extern const u32 Te4[256];
  25. extern const u32 Td0[256];
  26. extern const u32 Td1[256];
  27. extern const u32 Td2[256];
  28. extern const u32 Td3[256];
  29. extern const u32 Td4[256];
  30. extern const u32 rcon[10];
  31. extern const u8 Td4s[256];
  32. extern const u8 rcons[10];
  33. #ifndef AES_SMALL_TABLES
  34. #define RCON(i) rcon[(i)]
  35. #define TE0(i) Te0[((i) >> 24) & 0xff]
  36. #define TE1(i) Te1[((i) >> 16) & 0xff]
  37. #define TE2(i) Te2[((i) >> 8) & 0xff]
  38. #define TE3(i) Te3[(i) & 0xff]
  39. #define TE41(i) (Te4[((i) >> 24) & 0xff] & 0xff000000)
  40. #define TE42(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000)
  41. #define TE43(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00)
  42. #define TE44(i) (Te4[(i) & 0xff] & 0x000000ff)
  43. #define TE421(i) (Te4[((i) >> 16) & 0xff] & 0xff000000)
  44. #define TE432(i) (Te4[((i) >> 8) & 0xff] & 0x00ff0000)
  45. #define TE443(i) (Te4[(i) & 0xff] & 0x0000ff00)
  46. #define TE414(i) (Te4[((i) >> 24) & 0xff] & 0x000000ff)
  47. #define TE4(i) (Te4[(i)] & 0x000000ff)
  48. #define TD0(i) Td0[((i) >> 24) & 0xff]
  49. #define TD1(i) Td1[((i) >> 16) & 0xff]
  50. #define TD2(i) Td2[((i) >> 8) & 0xff]
  51. #define TD3(i) Td3[(i) & 0xff]
  52. #define TD41(i) (Td4[((i) >> 24) & 0xff] & 0xff000000)
  53. #define TD42(i) (Td4[((i) >> 16) & 0xff] & 0x00ff0000)
  54. #define TD43(i) (Td4[((i) >> 8) & 0xff] & 0x0000ff00)
  55. #define TD44(i) (Td4[(i) & 0xff] & 0x000000ff)
  56. #define TD0_(i) Td0[(i) & 0xff]
  57. #define TD1_(i) Td1[(i) & 0xff]
  58. #define TD2_(i) Td2[(i) & 0xff]
  59. #define TD3_(i) Td3[(i) & 0xff]
  60. #else /* AES_SMALL_TABLES */
  61. #define RCON(i) (rcons[(i)] << 24)
  62. static inline u32 rotr(u32 val, int bits)
  63. {
  64. return (val >> bits) | (val << (32 - bits));
  65. }
  66. #define TE0(i) Te0[((i) >> 24) & 0xff]
  67. #define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8)
  68. #define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16)
  69. #define TE3(i) rotr(Te0[(i) & 0xff], 24)
  70. #define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
  71. #define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
  72. #define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
  73. #define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
  74. #define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000)
  75. #define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000)
  76. #define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00)
  77. #define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff)
  78. #define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff)
  79. #define TD0(i) Td0[((i) >> 24) & 0xff]
  80. #define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8)
  81. #define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16)
  82. #define TD3(i) rotr(Td0[(i) & 0xff], 24)
  83. #define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24)
  84. #define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16)
  85. #define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8)
  86. #define TD44(i) (Td4s[(i) & 0xff])
  87. #define TD0_(i) Td0[(i) & 0xff]
  88. #define TD1_(i) rotr(Td0[(i) & 0xff], 8)
  89. #define TD2_(i) rotr(Td0[(i) & 0xff], 16)
  90. #define TD3_(i) rotr(Td0[(i) & 0xff], 24)
  91. #endif /* AES_SMALL_TABLES */
  92. #ifdef _MSC_VER
  93. #define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
  94. #define GETU32(p) SWAP(*((u32 *)(p)))
  95. #define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
  96. #else
  97. #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \
  98. ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
  99. #define PUTU32(ct, st) { \
  100. (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
  101. (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }
  102. #endif
  103. #define AES_PRIV_SIZE (4 * 44)
  104. void rijndaelKeySetupEnc(u32 rk[/*44*/], const u8 cipherKey[]);
  105. #endif /* AES_I_H */