rijndael.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // rijndael.h - originally written and placed in the public domain by Wei Dai
  2. /// \file rijndael.h
  3. /// \brief Classes for Rijndael encryption algorithm
  4. /// \details All key sizes are supported. The library only provides Rijndael with 128-bit blocks,
  5. /// and not 192-bit or 256-bit blocks
  6. /// \since Rijndael since Crypto++ 3.1, Intel AES-NI since Crypto++ 5.6.1, ARMv8 AES since Crypto++ 6.0,
  7. /// Power8 AES since Crypto++ 6.0, ARMv7 AES since Crypto++ 8.0
  8. #ifndef CRYPTOPP_RIJNDAEL_H
  9. #define CRYPTOPP_RIJNDAEL_H
  10. #include "seckey.h"
  11. #include "secblock.h"
  12. // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler
  13. // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232
  14. #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
  15. # define CRYPTOPP_DISABLE_RIJNDAEL_ASM 1
  16. #endif
  17. #if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_ARM32 || \
  18. CRYPTOPP_BOOL_ARMV8 || CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
  19. # define CRYPTOPP_RIJNDAEL_ADVANCED_PROCESS_BLOCKS 1
  20. #endif
  21. NAMESPACE_BEGIN(CryptoPP)
  22. /// \brief Rijndael block cipher information
  23. /// \details All key sizes are supported. The library only provides Rijndael with 128-bit blocks,
  24. /// and not 192-bit or 256-bit blocks
  25. /// \since Rijndael since Crypto++ 3.1, Intel AES-NI since Crypto++ 5.6.1, ARMv8 AES since Crypto++ 6.0,
  26. /// Power8 AES since Crypto++ 6.0, ARMv7 AES since Crypto++ 8.0
  27. struct Rijndael_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>
  28. {
  29. CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "AES";}
  30. };
  31. /// \brief Rijndael block cipher
  32. /// \details All key sizes are supported. The library only provides Rijndael with 128-bit blocks,
  33. /// and not 192-bit or 256-bit blocks
  34. /// \since Rijndael since Crypto++ 3.1, Intel AES-NI since Crypto++ 5.6.1, ARMv8 AES since Crypto++ 6.0,
  35. /// Power8 AES since Crypto++ 6.0, ARMv7 AES since Crypto++ 8.0
  36. /// \sa <a href="http://www.cryptopp.com/wiki/Rijndael">Rijndael</a>
  37. class CRYPTOPP_DLL Rijndael : public Rijndael_Info, public BlockCipherDocumentation
  38. {
  39. /// \brief Rijndael block cipher transformation functions
  40. /// \details Provides implementation common to encryption and decryption
  41. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Rijndael_Info>
  42. {
  43. public:
  44. void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
  45. std::string AlgorithmProvider() const;
  46. unsigned int OptimalDataAlignment() const;
  47. protected:
  48. static void FillEncTable();
  49. static void FillDecTable();
  50. // VS2005 workaround: have to put these on separate lines, or error C2487 is triggered in DLL build
  51. static const byte Se[256];
  52. static const byte Sd[256];
  53. static const word32 rcon[];
  54. unsigned int m_rounds;
  55. SecBlock<word32, AllocatorWithCleanup<word32, true> > m_key;
  56. mutable SecByteBlock m_aliasBlock;
  57. };
  58. /// \brief Encryption transformation
  59. /// \details Enc provides implementation for encryption transformation. All key sizes are supported.
  60. /// The library only provides Rijndael with 128-bit blocks, and not 192-bit or 256-bit blocks
  61. /// \since Rijndael since Crypto++ 3.1, Intel AES-NI since Crypto++ 5.6.1, ARMv8 AES since Crypto++ 6.0,
  62. /// Power8 AES since Crypto++ 6.0, ARMv7 AES since Crypto++ 8.0
  63. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Enc : public Base
  64. {
  65. public:
  66. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  67. #if CRYPTOPP_RIJNDAEL_ADVANCED_PROCESS_BLOCKS
  68. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  69. #endif
  70. };
  71. /// \brief Decryption transformation
  72. /// \details Dec provides implementation for decryption transformation. All key sizes are supported.
  73. /// The library only provides Rijndael with 128-bit blocks, and not 192-bit or 256-bit blocks
  74. /// \since Rijndael since Crypto++ 3.1, Intel AES-NI since Crypto++ 5.6.1, ARMv8 AES since Crypto++ 6.0,
  75. /// Power8 AES since Crypto++ 6.0, ARMv7 AES since Crypto++ 8.0
  76. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Dec : public Base
  77. {
  78. public:
  79. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  80. #if CRYPTOPP_RIJNDAEL_ADVANCED_PROCESS_BLOCKS
  81. size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
  82. #endif
  83. };
  84. public:
  85. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  86. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  87. };
  88. typedef Rijndael::Encryption RijndaelEncryption;
  89. typedef Rijndael::Decryption RijndaelDecryption;
  90. NAMESPACE_END
  91. #endif