Skip to content

Commit

Permalink
Move AliasedWithTable into unnamed namespace
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
noloader committed Oct 5, 2017
1 parent 1d0df34 commit 01e46aa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 60 deletions.
1 change: 1 addition & 0 deletions Filelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ TestData/rsa2048.dat
TestData/rsa400pb.dat
TestData/rsa400pv.dat
TestData/rsa512a.dat
TestData/rsa2048a.dat
TestData/rw1024.dat
TestData/rw2048.dat
TestData/saferval.dat
Expand Down
111 changes: 54 additions & 57 deletions rijndael.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ************************************
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 1 addition & 3 deletions rijndael.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class CRYPTOPP_DLL Rijndael : public Rijndael_Info, public BlockCipherDocumentat

unsigned int m_rounds;
FixedSizeAlignedSecBlock<word32, 4*15> m_key;
SecByteBlock m_aliasBlock;
};

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

Expand Down

0 comments on commit 01e46aa

Please sign in to comment.