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.
pull/531/head
Jeffrey Walton 2017-10-05 09:28:56 -04:00
parent 1d0df34ae8
commit 01e46aa474
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 56 additions and 60 deletions

View File

@ -391,6 +391,7 @@ TestData/rsa2048.dat
TestData/rsa400pb.dat TestData/rsa400pb.dat
TestData/rsa400pv.dat TestData/rsa400pv.dat
TestData/rsa512a.dat TestData/rsa512a.dat
TestData/rsa2048a.dat
TestData/rw1024.dat TestData/rw1024.dat
TestData/rw2048.dat TestData/rw2048.dat
TestData/saferval.dat TestData/saferval.dat

View File

@ -124,6 +124,56 @@ const word32 s_rconLE[] = {
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 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 ANONYMOUS_NAMESPACE_END
// ************************* Portable Code ************************************ // ************************* Portable Code ************************************
@ -264,6 +314,10 @@ void Rijndael::Base::UncheckedSetKey(const byte *userKey, unsigned int keyLen, c
{ {
AssertValidKeyLength(keyLen); AssertValidKeyLength(keyLen);
#if CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86
m_aliasBlock.New(s_sizeToAllocate);
#endif
m_rounds = keyLen/4 + 6; m_rounds = keyLen/4 + 6;
m_key.New(4*(m_rounds+1)); m_key.New(4*(m_rounds+1));
word32 *rk = m_key; word32 *rk = m_key;
@ -1069,63 +1123,6 @@ void Rijndael_Enc_AdvancedProcessBlocks(void *locals, const word32 *k);
} }
#endif #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 #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 size_t Rijndael::Enc::AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const
{ {

View File

@ -57,6 +57,7 @@ class CRYPTOPP_DLL Rijndael : public Rijndael_Info, public BlockCipherDocumentat
unsigned int m_rounds; unsigned int m_rounds;
FixedSizeAlignedSecBlock<word32, 4*15> m_key; FixedSizeAlignedSecBlock<word32, 4*15> m_key;
SecByteBlock m_aliasBlock;
}; };
//! \brief Provides implementation for encryption transformation //! \brief Provides implementation for encryption transformation
@ -69,10 +70,7 @@ class CRYPTOPP_DLL Rijndael : public Rijndael_Info, public BlockCipherDocumentat
public: public:
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
#if CRYPTOPP_ENABLE_ADVANCED_PROCESS_BLOCKS #if CRYPTOPP_ENABLE_ADVANCED_PROCESS_BLOCKS
Enc();
size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const; size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
private:
SecByteBlock m_aliasBlock;
#endif #endif
}; };