aes.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Definitions likely to be helpful to multiple AES implementations.
  3. */
  4. /*
  5. * The 'extra' structure used by AES implementations is used to
  6. * include information about how to check if a given implementation is
  7. * available at run time, and whether we've already checked.
  8. */
  9. struct aes_extra_mutable;
  10. struct aes_extra {
  11. /* Function to check availability. Might be expensive, so we don't
  12. * want to call it more than once. */
  13. bool (*check_available)(void);
  14. /* Point to a writable substructure. */
  15. struct aes_extra_mutable *mut;
  16. };
  17. struct aes_extra_mutable {
  18. bool checked_availability;
  19. bool is_available;
  20. };
  21. static inline bool check_availability(const struct aes_extra *extra)
  22. {
  23. if (!extra->mut->checked_availability) {
  24. extra->mut->is_available = extra->check_available();
  25. extra->mut->checked_availability = true;
  26. }
  27. return extra->mut->is_available;
  28. }
  29. /*
  30. * Macros to define vtables for AES variants. There are a lot of
  31. * these, because of the cross product between cipher modes, key
  32. * sizes, and assorted HW/SW implementations, so it's worth spending
  33. * some effort here to reduce the boilerplate in the sub-files.
  34. */
  35. #define AES_EXTRA(impl_c) \
  36. static struct aes_extra_mutable aes ## impl_c ## _extra_mut; \
  37. static const struct aes_extra aes ## impl_c ## _extra = { \
  38. .check_available = aes ## impl_c ## _available, \
  39. .mut = &aes ## impl_c ## _extra_mut, \
  40. }
  41. #define AES_CBC_VTABLE(impl_c, impl_display, bits) \
  42. const ssh_cipheralg ssh_aes ## bits ## _cbc ## impl_c = { \
  43. .new = aes ## impl_c ## _new, \
  44. .free = aes ## impl_c ## _free, \
  45. .setiv = aes ## impl_c ## _setiv_cbc, \
  46. .setkey = aes ## impl_c ## _setkey, \
  47. .encrypt = aes ## bits ## impl_c ## _cbc_encrypt, \
  48. .decrypt = aes ## bits ## impl_c ## _cbc_decrypt, \
  49. .ssh2_id = "aes" #bits "-cbc", \
  50. .blksize = 16, \
  51. .real_keybits = bits, \
  52. .padded_keybytes = bits/8, \
  53. .flags = SSH_CIPHER_IS_CBC, \
  54. .text_name = "AES-" #bits " CBC (" impl_display ")", \
  55. .extra = &aes ## impl_c ## _extra, \
  56. }
  57. #define AES_SDCTR_VTABLE(impl_c, impl_display, bits) \
  58. const ssh_cipheralg ssh_aes ## bits ## _sdctr ## impl_c = { \
  59. .new = aes ## impl_c ## _new, \
  60. .free = aes ## impl_c ## _free, \
  61. .setiv = aes ## impl_c ## _setiv_sdctr, \
  62. .setkey = aes ## impl_c ## _setkey, \
  63. .encrypt = aes ## bits ## impl_c ## _sdctr, \
  64. .decrypt = aes ## bits ## impl_c ## _sdctr, \
  65. .ssh2_id = "aes" #bits "-ctr", \
  66. .blksize = 16, \
  67. .real_keybits = bits, \
  68. .padded_keybytes = bits/8, \
  69. .flags = 0, \
  70. .text_name = "AES-" #bits " SDCTR (" impl_display ")", \
  71. .extra = &aes ## impl_c ## _extra, \
  72. }
  73. #define AES_ALL_VTABLES(impl_c, impl_display) \
  74. AES_CBC_VTABLE(impl_c, impl_display, 128); \
  75. AES_CBC_VTABLE(impl_c, impl_display, 192); \
  76. AES_CBC_VTABLE(impl_c, impl_display, 256); \
  77. AES_SDCTR_VTABLE(impl_c, impl_display, 128); \
  78. AES_SDCTR_VTABLE(impl_c, impl_display, 192); \
  79. AES_SDCTR_VTABLE(impl_c, impl_display, 256)
  80. /*
  81. * Macros to repeat a piece of code particular numbers of times that
  82. * correspond to 1 fewer than the number of AES rounds. (Because the
  83. * last round is different.)
  84. */
  85. #define REP2(x) x x
  86. #define REP4(x) REP2(REP2(x))
  87. #define REP8(x) REP2(REP4(x))
  88. #define REP9(x) REP8(x) x
  89. #define REP11(x) REP8(x) REP2(x) x
  90. #define REP13(x) REP8(x) REP4(x) x
  91. /*
  92. * The round constants used in key schedule expansion.
  93. */
  94. extern const uint8_t aes_key_setup_round_constants[10];
  95. /*
  96. * The largest number of round keys ever needed.
  97. */
  98. #define MAXROUNDKEYS 15