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