summaryrefslogtreecommitdiff
path: root/rijndael.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2017-10-05 09:28:56 -0400
committerJeffrey Walton <noloader@gmail.com>2017-10-05 09:28:56 -0400
commit01e46aa474f6310d281b29ba41b0e6bc696873aa (patch)
tree2660849f1b6e792e43e6d90260aebf7cac37eed4 /rijndael.cpp
parent1d0df34ae8304fa964cb7702e4f4476bbf6e9e7c (diff)
downloadcryptopp-git-01e46aa474f6310d281b29ba41b0e6bc696873aa.tar.gz
Move AliasedWithTable into unnamed namespace
Move m_aliasBlock into Rijndael::Base. m_aliasBlock is now an extra data member for Dec because the aliased table is only used for Enc when unaligned data access is in effect. However, the SecBlock is not allocated in the Dec class so there is no runtime penalty. Moving m_aliasBlock into Base also allowed us to remove the Enc::Enc() constructor, which always appeared as a wart in my eyes. Now m_aliasBlock is sized in UncheckedSetKey, so there's no need for the ctor initialization. Also see https://stackoverflow.com/q/46561818/608639 on Stack Overflow. The SO question had an unusual/unexpected interaction with CMake, so the removal of the Enc::Enc() ctor should help the problem.
Diffstat (limited to 'rijndael.cpp')
-rw-r--r--rijndael.cpp111
1 files changed, 54 insertions, 57 deletions
diff --git a/rijndael.cpp b/rijndael.cpp
index a2c89b71..141ba646 100644
--- a/rijndael.cpp
+++ b/rijndael.cpp
@@ -124,6 +124,56 @@ const word32 s_rconLE[] = {
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
};
+#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86
+
+// Determine whether the range between begin and end overlaps
+// with the same 4k block offsets as the Te table. Logically,
+// the code is trying to create the condition:
+//
+// Two sepearate memory pages:
+//
+// +-----+ +-----+
+// |XXXXX| |YYYYY|
+// |XXXXX| |YYYYY|
+// | | | |
+// | | | |
+// +-----+ +-----+
+// Te Table Locals
+//
+// Have a logical cache view of (X and Y may be inverted):
+//
+// +-----+
+// |XXXXX|
+// |XXXXX|
+// |YYYYY|
+// |YYYYY|
+// +-----+
+//
+static inline bool AliasedWithTable(const byte *begin, const byte *end)
+{
+ ptrdiff_t s0 = uintptr_t(begin)%4096, s1 = uintptr_t(end)%4096;
+ ptrdiff_t t0 = uintptr_t(Te)%4096, t1 = (uintptr_t(Te)+sizeof(Te))%4096;
+ if (t1 > t0)
+ return (s0 >= t0 && s0 < t1) || (s1 > t0 && s1 <= t1);
+ else
+ return (s0 < t1 || s1 <= t1) || (s0 >= t0 || s1 > t0);
+}
+
+struct Locals
+{
+ word32 subkeys[4*12], workspace[8];
+ const byte *inBlocks, *inXorBlocks, *outXorBlocks;
+ byte *outBlocks;
+ size_t inIncrement, inXorIncrement, outXorIncrement, outIncrement;
+ size_t regSpill, lengthAndCounterFlag, keysBegin;
+};
+
+const size_t s_aliasPageSize = 4096;
+const size_t s_aliasBlockSize = 256;
+const size_t s_sizeToAllocate = s_aliasPageSize + s_aliasBlockSize + sizeof(Locals);
+
+#endif // CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86
+
ANONYMOUS_NAMESPACE_END
// ************************* Portable Code ************************************
@@ -264,6 +314,10 @@ void Rijndael::Base::UncheckedSetKey(const byte *userKey, unsigned int keyLen, c
{
AssertValidKeyLength(keyLen);
+#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86
+ m_aliasBlock.New(s_sizeToAllocate);
+#endif
+
m_rounds = keyLen/4 + 6;
m_key.New(4*(m_rounds+1));
word32 *rk = m_key;
@@ -1069,63 +1123,6 @@ void Rijndael_Enc_AdvancedProcessBlocks(void *locals, const word32 *k);
}
#endif
-#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86
-
-// Determine whether the range between begin and end overlaps
-// with the same 4k block offsets as the Te table. Logically,
-// the code is trying to create the condition:
-//
-// Two sepearate memory pages:
-//
-// +-----+ +-----+
-// |XXXXX| |YYYYY|
-// |XXXXX| |YYYYY|
-// | | | |
-// | | | |
-// +-----+ +-----+
-// Te Table Locals
-//
-// Have a logical cache view of (X and Y may be inverted):
-//
-// +-----+
-// |XXXXX|
-// |XXXXX|
-// |YYYYY|
-// |YYYYY|
-// +-----+
-//
-static inline bool AliasedWithTable(const byte *begin, const byte *end)
-{
- ptrdiff_t s0 = uintptr_t(begin)%4096, s1 = uintptr_t(end)%4096;
- ptrdiff_t t0 = uintptr_t(Te)%4096, t1 = (uintptr_t(Te)+sizeof(Te))%4096;
- if (t1 > t0)
- return (s0 >= t0 && s0 < t1) || (s1 > t0 && s1 <= t1);
- else
- return (s0 < t1 || s1 <= t1) || (s0 >= t0 || s1 > t0);
-}
-
-struct Locals
-{
- word32 subkeys[4*12], workspace[8];
- const byte *inBlocks, *inXorBlocks, *outXorBlocks;
- byte *outBlocks;
- size_t inIncrement, inXorIncrement, outXorIncrement, outIncrement;
- size_t regSpill, lengthAndCounterFlag, keysBegin;
-};
-
-const size_t s_aliasPageSize = 4096;
-const size_t s_aliasBlockSize = 256;
-const size_t s_sizeToAllocate = s_aliasPageSize + s_aliasBlockSize + sizeof(Locals);
-
-Rijndael::Enc::Enc() : m_aliasBlock(s_sizeToAllocate) { }
-
-#endif // CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86
-
-#if CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64 || CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64
-// Do nothing
-Rijndael::Enc::Enc() { }
-#endif
-
#if CRYPTOPP_ENABLE_ADVANCED_PROCESS_BLOCKS
size_t Rijndael::Enc::AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const
{