port to GCC 4, reorganize implementations of SetKey

pull/2/head
weidai 2006-12-10 02:12:23 +00:00
parent 28c392e082
commit f05ea58bb3
66 changed files with 203 additions and 250 deletions

View File

@ -61,17 +61,16 @@ static inline word32 reverseBits(word32 a)
pi_gamma_pi(a0, a1, a2); \
}
void ThreeWay::Base::UncheckedSetKey(CipherDir dir, const byte *uk, unsigned int length, unsigned int r)
void ThreeWay::Base::UncheckedSetKey(const byte *uk, unsigned int length, const NameValuePairs &params)
{
AssertValidKeyLength(length);
AssertValidRounds(r);
m_rounds = r;
m_rounds = GetRoundsAndThrowIfInvalid(params, this);
for (unsigned int i=0; i<3; i++)
m_k[i] = (word32)uk[4*i+3] | ((word32)uk[4*i+2]<<8) | ((word32)uk[4*i+1]<<16) | ((word32)uk[4*i]<<24);
if (dir == DECRYPTION)
if (!IsForwardTransformation())
{
theta(m_k[0], m_k[1], m_k[2]);
mu(m_k[0], m_k[1], m_k[2]);

2
3way.h
View File

@ -21,7 +21,7 @@ class ThreeWay : public ThreeWay_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<ThreeWay_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *key, unsigned int length, unsigned int rounds);
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
protected:
unsigned int m_rounds;

View File

@ -3,9 +3,9 @@ CXXFLAGS = -g
# Uncomment the following two lines to do a release build.
# Note that you must define NDEBUG for your own application if you define it for Crypto++.
# Make sure you run the validation tests and test your own program thoroughly
# after turning on -O2. The GCC optimizer may have bugs that cause it to generate incorrect code.
# after turning on -O3. The GCC optimizer may have bugs that cause it to generate incorrect code.
# Try removing -fdata-sections if you get "undefined external reference" errors.
# CXXFLAGS = -O2 -DNDEBUG -ffunction-sections -fdata-sections
# CXXFLAGS = -O3 -DNDEBUG -ffunction-sections -fdata-sections
# LDFLAGS += -Wl,--gc-sections
ARFLAGS = -cr # ar needs the dash on OpenBSD
RANLIB = ranlib

View File

@ -21,7 +21,7 @@ ARC4_Base::~ARC4_Base()
m_x = m_y = 0;
}
void ARC4_Base::UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int keyLen, const byte *iv)
void ARC4_Base::UncheckedSetKey(const byte *key, unsigned int keyLen, const NameValuePairs &params)
{
AssertValidKeyLength(keyLen);

2
arc4.h
View File

@ -26,7 +26,7 @@ public:
typedef SymmetricCipherFinal<ARC4_Base> Decryption;
protected:
void UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv);
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
virtual unsigned int GetDefaultDiscardBytes() const {return 0;}
FixedSizeSecBlock<byte, 256> m_state;

View File

@ -6,7 +6,7 @@
NAMESPACE_BEGIN(CryptoPP)
void Blowfish::Base::UncheckedSetKey(CipherDir dir, const byte *key_string, unsigned int keylength)
void Blowfish::Base::UncheckedSetKey(const byte *key_string, unsigned int keylength, const NameValuePairs &)
{
AssertValidKeyLength(keylength);
@ -35,7 +35,7 @@ void Blowfish::Base::UncheckedSetKey(CipherDir dir, const byte *key_string, unsi
for (i=0; i<4*256-2; i+=2)
crypt_block(sbox+i, sbox+i+2);
if (dir==DECRYPTION)
if (!IsForwardTransformation())
for (i=0; i<(ROUNDS+2)/2; i++)
std::swap(pbox[i], pbox[ROUNDS+1-i]);
}

View File

@ -21,7 +21,7 @@ class Blowfish : public Blowfish_Info, public BlockCipherDocumentation
{
public:
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
void UncheckedSetKey(CipherDir direction, const byte *key_string, unsigned int keylength);
void UncheckedSetKey(const byte *key_string, unsigned int keylength, const NameValuePairs &params);
private:
void crypt_block(const word32 in[2], word32 out[2]) const;

View File

@ -56,7 +56,7 @@ inline void rotl128(word64 *x, unsigned int bits)
x[1] = (x[1] << bits) | temp;
}
void Camellia::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int keylen)
void Camellia::Base::UncheckedSetKey(const byte *key, unsigned int keylen, const NameValuePairs &)
{
AssertValidKeyLength(keylen);
@ -171,7 +171,7 @@ void Camellia::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned in
ks[32] = KB[0]; ks[33] = KB[1];
}
if (dir == DECRYPTION) // reverse key schedule order
if (!IsForwardTransformation()) // reverse key schedule order
{
std::swap(ks[0], ks[kslen-2]);
std::swap(ks[1], ks[kslen-1]);

View File

@ -25,7 +25,7 @@ class Camellia : public Camellia_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Camellia_Info>
{
public:
void UncheckedSetKey(CipherDir dir, const byte *key, unsigned int keylen);
void UncheckedSetKey(const byte *key, unsigned int keylen, const NameValuePairs &params);
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
unsigned int BlockAlignment() const {return 8;}

View File

@ -94,7 +94,7 @@ void CAST128::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock,
t = l = r = 0;
}
void CAST128::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int keylength)
void CAST128::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
{
AssertValidKeyLength(keylength);
@ -251,7 +251,7 @@ void CAST256::Base::Omega(int i, word32 kappa[8])
f2(kappa[7],kappa[0],t_m[7][i],t_r[7][i]);
}
void CAST256::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int keylength)
void CAST256::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
{
AssertValidKeyLength(keylength);
@ -273,7 +273,7 @@ void CAST256::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned
K[8*i+7]=kappa[1];
}
if (dir == DECRYPTION)
if (!IsForwardTransformation())
{
for(int j=0; j<6; ++j)
{

4
cast.h
View File

@ -27,7 +27,7 @@ class CAST128 : public CAST128_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl<CAST128_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
bool reduced;
@ -63,7 +63,7 @@ class CAST256 : public CAST256_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl<CAST256_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length = 8);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
protected:

View File

@ -6,7 +6,7 @@
NAMESPACE_BEGIN(CryptoPP)
void CBC_MAC_Base::CheckedSetKey(void *, Empty empty, const byte *key, size_t length, const NameValuePairs &params)
void CBC_MAC_Base::UncheckedSetKey(const byte *key, size_t length, const NameValuePairs &params)
{
AccessCipher().SetKey(key, length, params);
m_reg.CleanNew(AccessCipher().BlockSize());

View File

@ -12,7 +12,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_MAC_Base : public MessageAuthenticatio
public:
CBC_MAC_Base() {}
void CheckedSetKey(void *, Empty empty, const byte *key, size_t length, const NameValuePairs &params);
void UncheckedSetKey(const byte *key, size_t length, const NameValuePairs &params);
void Update(const byte *input, size_t length);
void TruncatedFinal(byte *mac, size_t size);
unsigned int DigestSize() const {return const_cast<CBC_MAC_Base*>(this)->AccessCipher().BlockSize();}

View File

@ -47,6 +47,12 @@ Algorithm::Algorithm(bool checkSelfTestStatus)
}
}
void SimpleKeyingInterface::SetKey(const byte *key, size_t length, const NameValuePairs &params)
{
this->ThrowIfInvalidKeyLength(length);
this->UncheckedSetKey(key, (unsigned int)length, params);
}
void SimpleKeyingInterface::SetKeyWithRounds(const byte *key, size_t length, int rounds)
{
SetKey(key, length, MakeParameters(Name::Rounds(), rounds));
@ -57,22 +63,22 @@ void SimpleKeyingInterface::SetKeyWithIV(const byte *key, size_t length, const b
SetKey(key, length, MakeParameters(Name::IV(), iv));
}
void SimpleKeyingInterface::ThrowIfInvalidKeyLength(const Algorithm &algorithm, size_t length)
void SimpleKeyingInterface::ThrowIfInvalidKeyLength(size_t length)
{
if (!IsValidKeyLength(length))
throw InvalidKeyLength(algorithm.AlgorithmName(), length);
throw InvalidKeyLength(GetAlgorithm().AlgorithmName(), length);
}
void SimpleKeyingInterface::ThrowIfResynchronizable()
{
if (IsResynchronizable())
throw InvalidArgument("SimpleKeyingInterface: this object requires an IV");
throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": this object requires an IV");
}
void SimpleKeyingInterface::ThrowIfInvalidIV(const byte *iv)
{
if (!iv && !(IVRequirement() == INTERNALLY_GENERATED_IV || IVRequirement() == STRUCTURED_IV || !IsResynchronizable()))
throw InvalidArgument("SimpleKeyingInterface: this object cannot use a null IV");
throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": this object cannot use a null IV");
}
const byte * SimpleKeyingInterface::GetIVAndThrowIfInvalid(const NameValuePairs &params)

View File

@ -27,7 +27,7 @@ CFG=cryptlib - Win32 Debug
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=xicl6.exe
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "cryptlib - Win32 DLL-Import Release"
@ -49,7 +49,7 @@ RSC=rc.exe
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=xilink6.exe -lib
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo
@ -72,7 +72,7 @@ LIB32=xilink6.exe -lib
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=xilink6.exe -lib
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo
@ -95,7 +95,7 @@ LIB32=xilink6.exe -lib
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=xilink6.exe -lib
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo
@ -118,7 +118,7 @@ LIB32=xilink6.exe -lib
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LIB32=xilink6.exe -lib
LIB32=link.exe -lib
# ADD BASE LIB32 /nologo
# ADD LIB32 /nologo
@ -528,6 +528,10 @@ SOURCE=.\safer.cpp
# End Source File
# Begin Source File
SOURCE=.\salsa.cpp
# End Source File
# Begin Source File
SOURCE=.\seal.cpp
# End Source File
# Begin Source File
@ -1012,6 +1016,10 @@ SOURCE=.\safer.h
# End Source File
# Begin Source File
SOURCE=.\salsa.h
# End Source File
# Begin Source File
SOURCE=.\seal.h
# End Source File
# Begin Source File

View File

@ -367,7 +367,7 @@ public:
//! set or reset the key of this object
/*! \param params is used to specify Rounds, BlockSize, etc */
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs) =0;
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs);
//! calls SetKey() with an NameValuePairs object that just specifies "Rounds"
void SetKeyWithRounds(const byte *key, size_t length, int rounds);
@ -400,15 +400,15 @@ public:
virtual void GetNextIV(byte *IV) {throw NotImplemented("SimpleKeyingInterface: this object doesn't support GetNextIV()");}
protected:
void ThrowIfInvalidKeyLength(const Algorithm &algorithm, size_t length);
virtual const Algorithm & GetAlgorithm() const =0;
virtual void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params) =0;
void ThrowIfInvalidKeyLength(size_t length);
void ThrowIfResynchronizable(); // to be called when no IV is passed
void ThrowIfInvalidIV(const byte *iv); // check for NULL IV if it can't be used
const byte * GetIVAndThrowIfInvalid(const NameValuePairs &params);
inline void AssertValidKeyLength(size_t length) const
{
assert(IsValidKeyLength(length));
}
{assert(IsValidKeyLength(length));}
};
//! interface for the data processing part of block ciphers
@ -451,6 +451,8 @@ public:
//! encrypt or decrypt multiple blocks, for bit-slicing implementations
virtual void ProcessAndXorMultipleBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t numberOfBlocks) const;
inline CipherDir GetCipherDirection() const {return IsForwardTransformation() ? ENCRYPTION : DECRYPTION;}
};
//! interface for the data processing part of stream ciphers
@ -590,9 +592,8 @@ typedef HashTransformation HashFunction;
template <class T>
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE SimpleKeyedTransformation : public T, public SimpleKeyingInterface
{
public:
void ThrowIfInvalidKeyLength(size_t length)
{SimpleKeyingInterface::ThrowIfInvalidKeyLength(*this, length);}
protected:
const Algorithm & GetAlgorithm() const {return *this;}
};
#ifdef CRYPTOPP_DOXYGEN_PROCESSING

View File

@ -363,7 +363,10 @@ bool GetField(std::istream &is, std::string &name, std::string &value)
while (buffer[0] != 0);
is.clear();
is.ignore();
if (!value.empty() && value[value.size()-1] == '\r')
value.resize(value.size()-1);
if (!value.empty() && value[value.size()-1] == '\\')
{
value.resize(value.size()-1);

31
des.cpp
View File

@ -264,7 +264,7 @@ static const int bytebit[] = {
};
/* Set key (initialize key schedule array) */
void RawDES::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length)
void RawDES::RawSetKey(CipherDir dir, const byte *key)
{
SecByteBlock buffer(56+56+8);
byte *const pc1m=buffer; /* place to modify pc1 into */
@ -345,12 +345,19 @@ void RawDES::RawProcessBlock(word32 &l_, word32 &r_) const
l_ = l; r_ = r;
}
void DES_EDE2::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length)
void DES::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);
m_des1.UncheckedSetKey(dir, key);
m_des2.UncheckedSetKey(ReverseCipherDir(dir), key+8);
RawSetKey(GetCipherDirection(), userKey);
}
void DES_EDE2::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);
m_des1.RawSetKey(GetCipherDirection(), userKey);
m_des2.RawSetKey(ReverseCipherDir(GetCipherDirection()), userKey+8);
}
void DES_EDE2::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
@ -365,13 +372,13 @@ void DES_EDE2::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBloc
Block::Put(xorBlock, outBlock)(r)(l);
}
void DES_EDE3::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length)
void DES_EDE3::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);
m_des1.UncheckedSetKey(dir, key+(dir==ENCRYPTION?0:2*8));
m_des2.UncheckedSetKey(ReverseCipherDir(dir), key+8);
m_des3.UncheckedSetKey(dir, key+(dir==DECRYPTION?0:2*8));
m_des1.RawSetKey(GetCipherDirection(), userKey + (IsForwardTransformation() ? 0 : 16));
m_des2.RawSetKey(ReverseCipherDir(GetCipherDirection()), userKey + 8);
m_des3.RawSetKey(GetCipherDirection(), userKey + (IsForwardTransformation() ? 16 : 0));
}
void DES_EDE3::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
@ -420,16 +427,16 @@ void DES::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, by
Block::Put(xorBlock, outBlock)(r)(l);
}
void DES_XEX3::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length)
void DES_XEX3::Base::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);
if (!m_des.get())
m_des.reset(new DES::Encryption);
memcpy(m_x1, key+(dir==ENCRYPTION?0:2*8), BLOCKSIZE);
m_des->UncheckedSetKey(dir, key+8);
memcpy(m_x3, key+(dir==DECRYPTION?0:2*8), BLOCKSIZE);
memcpy(m_x1, key + (IsForwardTransformation() ? 0 : 16), BLOCKSIZE);
m_des->RawSetKey(GetCipherDirection(), key + 8);
memcpy(m_x3, key + (IsForwardTransformation() ? 16 : 0), BLOCKSIZE);
}
void DES_XEX3::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const

9
des.h
View File

@ -12,7 +12,7 @@ NAMESPACE_BEGIN(CryptoPP)
class CRYPTOPP_DLL RawDES
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length = 8);
void RawSetKey(CipherDir direction, const byte *userKey);
void RawProcessBlock(word32 &l, word32 &r) const;
protected:
@ -38,6 +38,7 @@ class DES : public DES_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES
{
public:
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
};
@ -63,7 +64,7 @@ class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE2_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
protected:
@ -87,7 +88,7 @@ class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE3_Info>
{
public:
void UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
protected:
@ -111,7 +112,7 @@ class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_XEX3_Info>
{
public:
void UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
protected:

4
dmac.h
View File

@ -16,7 +16,7 @@ public:
DMAC_Base() {}
void CheckedSetKey(void *, Empty empty, const byte *key, size_t length, const NameValuePairs &params);
void UncheckedSetKey(const byte *key, size_t length, const NameValuePairs &params);
void Update(const byte *input, size_t length);
void TruncatedFinal(byte *mac, size_t size);
unsigned int DigestSize() const {return DIGESTSIZE;}
@ -45,7 +45,7 @@ public:
};
template <class T>
void DMAC_Base<T>::CheckedSetKey(void *, Empty empty, const byte *key, size_t length, const NameValuePairs &params)
void DMAC_Base<T>::UncheckedSetKey(const byte *key, size_t length, const NameValuePairs &params)
{
m_subkeylength = T::StaticGetValidKeyLength(T::BLOCKSIZE);
m_subkeys.resize(2*UnsignedMin((unsigned int)T::BLOCKSIZE, m_subkeylength));

View File

@ -11,7 +11,7 @@
#define _WIN32_WINNT 0x0400
#include <windows.h>
#if defined(_MSC_VER) && _MSC_VER >= 14
#if defined(_MSC_VER) && _MSC_VER >= 1400
#ifdef _M_IX86
#define _CRT_DEBUGGER_HOOK _crt_debugger_hook
#else
@ -277,7 +277,7 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule
char moduleFilenameBuf[MAX_PATH] = "";
if (moduleFilename == NULL)
{
#ifdef _MSC_VER // ifstream doesn't support wide filename on gcc 3.4.4 cygwin
#if (defined(_MSC_VER) && _MSC_VER >= 1400) // ifstream doesn't support wide filename on other compilers
wchar_t wideModuleFilename[MAX_PATH];
if (GetModuleFileNameW(s_hModule, wideModuleFilename, MAX_PATH) > 0)
{
@ -363,7 +363,7 @@ bool IntegrityCheckModule(const char *moduleFilename, const byte *expectedModule
}
}
#if defined(_MSC_VER) && _MSC_VER >= 14
#if defined(_MSC_VER) && _MSC_VER >= 1400
// first byte of _CRT_DEBUGGER_HOOK gets modified in memory by the debugger invisibly, so read it from file
if (IsDebuggerPresent())
{

View File

@ -30,7 +30,7 @@ const byte GOST::Base::sBox[8][16]={
bool GOST::Base::sTableCalculated = false;
word32 GOST::Base::sTable[4][256];
void GOST::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
void GOST::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);

2
gost.h
View File

@ -21,7 +21,7 @@ class GOST : public GOST_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<GOST_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
static void PrecalculateSTable();

View File

@ -8,7 +8,7 @@
NAMESPACE_BEGIN(CryptoPP)
void HMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength)
void HMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
{
AssertValidKeyLength(keylength);

2
hmac.h
View File

@ -13,7 +13,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HMAC_Base : public VariableKeyLength<16, 0
{
public:
HMAC_Base() : m_innerHashKeyed(false) {}
void UncheckedSetKey(const byte *userKey, unsigned int keylength);
void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
void Restart();
void Update(const byte *input, size_t length);

View File

@ -78,7 +78,7 @@ inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b)
}
#endif // IDEA_LARGECACHE
void IDEA::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
void IDEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);
@ -88,7 +88,7 @@ void IDEA::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsig
EnKey(userKey);
if (direction==DECRYPTION)
if (!IsForwardTransformation())
DeKey();
#ifdef IDEA_LARGECACHE

2
idea.h
View File

@ -32,7 +32,7 @@ private:
unsigned int GetAlignment() const {return 2;}
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
private:
void EnKey(const byte *);

View File

@ -27,7 +27,7 @@ class LR : public LR_Info<T>, public BlockCipherDocumentation
{
public:
// VC60 workaround: have to define these functions within class definition
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
{
this->AssertValidKeyLength(length);

View File

@ -38,7 +38,7 @@ static word32 gen_mask(word32 x)
};
NAMESPACE_END
void MARS::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
void MARS::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);

2
mars.h
View File

@ -21,7 +21,7 @@ class MARS : public MARS_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<MARS_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
static const word32 Sbox[512];

View File

@ -12,7 +12,7 @@ const word32 MD5MAC_Base::T[12] =
0x96ce77b1,0x7c8e722e,0x0aab5a5f,0x18be4336,
0x21b4219d,0x4db987bc,0xbd279da2,0xc3d75bc7 };
void MD5MAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength)
void MD5MAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
{
const word32 zeros[4] = {0,0,0,0};

View File

@ -17,7 +17,7 @@ public:
MD5MAC_Base() {SetStateSize(DIGESTSIZE);}
void UncheckedSetKey(const byte *userKey, unsigned int keylength);
void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
void TruncatedFinal(byte *mac, size_t size);
unsigned int DigestSize() const {return DIGESTSIZE;}

3
mdc.h
View File

@ -28,9 +28,8 @@ class MDC : public MDC_Info<T>
typedef typename T::HashWordType HashWordType;
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
{
assert(direction == ENCRYPTION);
this->AssertValidKeyLength(length);
memcpy_s(m_key, m_key.size(), userKey, this->KEYLENGTH);
T::CorrectEndianess(Key(), Key(), this->KEYLENGTH);

View File

@ -24,11 +24,6 @@ void Modes_TestInstantiations()
}
#endif
void CipherModeBase::SetKey(const byte *key, size_t length, const NameValuePairs &params)
{
UncheckedSetKey(params, key, (unsigned int)length, GetIVAndThrowIfInvalid(params)); // the underlying cipher will check the key length
}
void CipherModeBase::GetNextIV(byte *IV)
{
if (!IsForwardTransformation())
@ -102,12 +97,12 @@ void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv)
CopyOrZero(m_counterArray, iv, s);
}
void BlockOrientedCipherModeBase::UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv)
void BlockOrientedCipherModeBase::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
m_cipher->SetKey(key, length, params);
ResizeBuffers();
if (IsResynchronizable())
Resynchronize(iv);
Resynchronize(GetIVAndThrowIfInvalid(params));
}
void BlockOrientedCipherModeBase::ProcessData(byte *outString, const byte *inString, size_t length)

View File

@ -37,8 +37,6 @@ public:
size_t GetValidKeyLength(size_t n) const {return m_cipher->GetValidKeyLength(n);}
bool IsValidKeyLength(size_t n) const {return m_cipher->IsValidKeyLength(n);}
void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs);
unsigned int OptimalDataAlignment() const {return BlockSize();}
unsigned int IVSize() const {return BlockSize();}
@ -56,7 +54,6 @@ protected:
{
m_register.New(m_cipher->BlockSize());
}
virtual void UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv) =0;
BlockCipher *m_cipher;
SecByteBlock m_register;
@ -171,7 +168,7 @@ private:
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase
{
public:
void UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv);
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
unsigned int MandatoryBlockSize() const {return BlockSize();}
bool IsRandomAccess() const {return false;}
bool IsSelfInverting() const {return false;}
@ -225,9 +222,9 @@ public:
static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";}
protected:
void UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv)
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
CBC_Encryption::UncheckedSetKey(params, key, length, iv);
CBC_Encryption::UncheckedSetKey(key, length, params);
m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), (byte *)NULL);
}

View File

@ -46,7 +46,7 @@ template <class T_Hash, class T_Info = T_Hash>
class HermeticHashFunctionMAC : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<MessageAuthenticationCode, VariableKeyLength<32, 0, UINT_MAX> > >, T_Info>
{
public:
void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs)
void UncheckedSetKey(const byte *key, size_t length, const NameValuePairs &params)
{
m_key.Assign(key, length);
Restart();

14
rc2.cpp
View File

@ -3,13 +3,18 @@
#include "pch.h"
#include "rc2.h"
#include "misc.h"
#include "argnames.h"
NAMESPACE_BEGIN(CryptoPP)
void RC2::Base::UncheckedSetKey(CipherDir direction, const byte *key, unsigned int keyLen, unsigned int effectiveLen)
void RC2::Base::UncheckedSetKey(const byte *key, unsigned int keyLen, const NameValuePairs &params)
{
AssertValidKeyLength(keyLen);
int effectiveLen = params.GetIntValueWithDefault(Name::EffectiveKeyLength(), DEFAULT_EFFECTIVE_KEYLENGTH);
if (effectiveLen > MAX_EFFECTIVE_KEYLENGTH)
throw InvalidArgument("RC2: effective key length parameter exceeds maximum");
static const unsigned char PITABLE[256] = {
217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
@ -46,13 +51,6 @@ void RC2::Base::UncheckedSetKey(CipherDir direction, const byte *key, unsigned i
K[i] = L[2*i] + (L[2*i+1] << 8);
}
void RC2::Base::SetKeyWithEffectiveKeyLength(const byte *key, size_t length, unsigned int effectiveKeyLength)
{
if (effectiveKeyLength > MAX_EFFECTIVE_KEYLENGTH)
throw InvalidArgument("RC2: effective key length parameter exceeds maximum");
UncheckedSetKey(ENCRYPTION, key, (unsigned int)length, effectiveKeyLength);
}
typedef BlockGetAndPut<word16, LittleEndian> Block;
void RC2::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const

24
rc2.h
View File

@ -6,6 +6,7 @@
#include "seckey.h"
#include "secblock.h"
#include "algparam.h"
NAMESPACE_BEGIN(CryptoPP)
@ -22,18 +23,9 @@ class RC2 : public RC2_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<RC2_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *key, unsigned int length, unsigned int effectiveKeyLength);
void SetKeyWithEffectiveKeyLength(const byte *key, size_t length, unsigned int effectiveKeyLength);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
template <class T>
static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs &param)
{
obj->ThrowIfInvalidKeyLength(length);
int effectiveKeyLength = param.GetIntValueWithDefault("EffectiveKeyLength", DEFAULT_EFFECTIVE_KEYLENGTH);
obj->SetKeyWithEffectiveKeyLength(key, length, effectiveKeyLength);
}
FixedSizeSecBlock<word16, 64> K; // expanded key table
};
@ -54,16 +46,20 @@ public:
{
public:
Encryption() {}
Encryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH, unsigned int effectiveLen=1024)
{SetKeyWithEffectiveKeyLength(key, keyLen, effectiveLen);}
Encryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH)
{SetKey(key, keyLen);}
Encryption(const byte *key, size_t keyLen, int effectiveKeyLen)
{SetKey(key, keyLen, MakeParameters("EffectiveKeyLength", effectiveKeyLen));}
};
class Decryption : public BlockCipherFinal<DECRYPTION, Dec>
{
public:
Decryption() {}
Decryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH, unsigned int effectiveLen=1024)
{SetKeyWithEffectiveKeyLength(key, keyLen, effectiveLen);}
Decryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH)
{SetKey(key, keyLen);}
Decryption(const byte *key, size_t keyLen, int effectiveKeyLen)
{SetKey(key, keyLen, MakeParameters("EffectiveKeyLength", effectiveKeyLen));}
};
};

View File

@ -6,12 +6,11 @@
NAMESPACE_BEGIN(CryptoPP)
void RC5::Base::UncheckedSetKey(CipherDir direction, const byte *k, unsigned int keylen, unsigned int rounds)
void RC5::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
{
AssertValidKeyLength(keylen);
AssertValidRounds(rounds);
r = rounds;
r = GetRoundsAndThrowIfInvalid(params, this);
sTable.New(2*(r+1));
static const RC5_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize

2
rc5.h
View File

@ -22,7 +22,7 @@ class RC5 : public RC5_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<RC5_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
unsigned int r; // number of rounds

View File

@ -7,12 +7,11 @@
NAMESPACE_BEGIN(CryptoPP)
void RC6::Base::UncheckedSetKey(CipherDir direction, const byte *k, unsigned int keylen, unsigned int rounds)
void RC6::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
{
AssertValidKeyLength(keylen);
AssertValidRounds(rounds);
r = rounds;
r = GetRoundsAndThrowIfInvalid(params, this);
sTable.New(2*(r+2));
static const RC6_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize

2
rc6.h
View File

@ -22,7 +22,7 @@ class RC6 : public RC6_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<RC6_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
unsigned int r; // number of rounds

View File

@ -54,7 +54,7 @@ being unloaded from L1 cache, until that round is finished.
NAMESPACE_BEGIN(CryptoPP)
void Rijndael::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int keylen)
void Rijndael::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &)
{
AssertValidKeyLength(keylen);
@ -103,7 +103,7 @@ void Rijndael::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigne
rk += keylen/4;
}
if (dir == DECRYPTION)
if (!IsForwardTransformation())
{
unsigned int i, j;
rk = m_key;

View File

@ -21,7 +21,7 @@ class CRYPTOPP_DLL Rijndael : public Rijndael_Info, public BlockCipherDocumentat
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Rijndael_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
// VS2005 workaround: have to put these on seperate lines, or error C2487 is triggered in DLL build

View File

@ -3,6 +3,7 @@
#include "pch.h"
#include "safer.h"
#include "misc.h"
#include "argnames.h"
NAMESPACE_BEGIN(CryptoPP)
@ -50,8 +51,11 @@ const byte SAFER::Base::log_tab[256] =
static const unsigned int BLOCKSIZE = 8;
static const unsigned int MAX_ROUNDS = 13;
void SAFER::Base::UncheckedSetKey(CipherDir dir, const byte *userkey_1, unsigned int length, unsigned nof_rounds)
void SAFER::Base::UncheckedSetKey(const byte *userkey_1, unsigned int length, const NameValuePairs &params)
{
bool strengthened = Strengthened();
unsigned int nof_rounds = params.GetIntValueWithDefault(Name::Rounds(), length == 8 ? (strengthened ? 8 : 6) : 10);
const byte *userkey_2 = length == 8 ? userkey_1 : userkey_1 + 8;
keySchedule.New(1 + BLOCKSIZE * (1 + 2 * nof_rounds));
@ -69,6 +73,7 @@ void SAFER::Base::UncheckedSetKey(CipherDir dir, const byte *userkey_1, unsigned
ka[BLOCKSIZE] ^= ka[j] = rotlFixed(userkey_1[j], 5U);
kb[BLOCKSIZE] ^= kb[j] = *key++ = userkey_2[j];
}
for (i = 1; i <= nof_rounds; i++)
{
for (j = 0; j < BLOCKSIZE + 1; j++)

47
safer.h
View File

@ -17,9 +17,11 @@ public:
{
public:
unsigned int GetAlignment() const {return 1;}
void UncheckedSetKey(CipherDir dir, const byte *userkey, unsigned int length, unsigned nof_rounds);
void UncheckedSetKey(const byte *userkey, unsigned int length, const NameValuePairs &params);
protected:
virtual bool Strengthened() const =0;
bool strengthened;
SecByteBlock keySchedule;
static const byte exp_tab[256];
static const byte log_tab[256];
@ -38,58 +40,39 @@ public:
};
};
template <class BASE, class INFO, bool STR>
class CRYPTOPP_NO_VTABLE SAFER_Impl : public BlockCipherImpl<INFO, BASE>
{
protected:
bool Strengthened() const {return STR;}
};
//! _
struct SAFER_K_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
{
static const char *StaticAlgorithmName() {return "SAFER-K";}
static unsigned int DefaultRounds(unsigned int keylength) {return keylength == 8 ? 6 : 10;}
};
/// <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-K">SAFER-K</a>
class SAFER_K : public SAFER_K_Info, public SAFER, public BlockCipherDocumentation
{
class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl<SAFER_K_Info, SAFER::Enc>
{
public:
Enc() {strengthened = false;}
};
class CRYPTOPP_NO_VTABLE Dec : public BlockCipherImpl<SAFER_K_Info, SAFER::Dec>
{
public:
Dec() {strengthened = false;}
};
public:
typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_K_Info, false> > Encryption;
typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_K_Info, false> > Decryption;
};
//! _
struct SAFER_SK_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
{
static const char *StaticAlgorithmName() {return "SAFER-SK";}
static unsigned int DefaultRounds(unsigned int keylength) {return keylength == 8 ? 8 : 10;}
};
/// <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-SK">SAFER-SK</a>
class SAFER_SK : public SAFER_SK_Info, public SAFER, public BlockCipherDocumentation
{
class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl<SAFER_SK_Info, SAFER::Enc>
{
public:
Enc() {strengthened = true;}
};
class CRYPTOPP_NO_VTABLE Dec : public BlockCipherImpl<SAFER_SK_Info, SAFER::Dec>
{
public:
Dec() {strengthened = true;}
};
public:
typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_SK_Info, true> > Encryption;
typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_SK_Info, true> > Decryption;
};
typedef SAFER_K::Encryption SAFER_K_Encryption;

View File

@ -32,17 +32,6 @@ class FixedRounds
{
public:
enum {ROUNDS = R};
protected:
template <class T>
static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs &param)
{
obj->ThrowIfInvalidKeyLength(length);
int rounds = param.GetIntValueWithDefault("Rounds", ROUNDS);
if (rounds != ROUNDS)
throw InvalidRounds(obj->StaticAlgorithmName(), rounds);
obj->UncheckedSetKey(dir, key, (unsigned int)length);
}
};
//! to be inherited by ciphers with variable number of rounds
@ -59,14 +48,17 @@ protected:
assert(rounds >= (unsigned int)MIN_ROUNDS && rounds <= (unsigned int)MAX_ROUNDS);
}
template <class T>
static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs &param)
inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
{
obj->ThrowIfInvalidKeyLength(length);
int rounds = param.GetIntValueWithDefault("Rounds", obj->StaticGetDefaultRounds(length));
if (rounds < (int)MIN_ROUNDS || rounds > (int)MAX_ROUNDS)
throw InvalidRounds(obj->AlgorithmName(), rounds);
obj->UncheckedSetKey(dir, key, (unsigned int)length, rounds);
throw InvalidRounds(alg->AlgorithmName(), rounds);
}
inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
{
int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS);
ThrowIfInvalidRounds(rounds, alg);
return (unsigned int)rounds;
}
};
@ -123,20 +115,6 @@ public:
// ************** implementation helper for SimpledKeyed ***************
template <class T>
static inline void CheckedSetKey(T *obj, Empty empty, const byte *key, size_t length, const NameValuePairs &param)
{
obj->ThrowIfInvalidKeyLength(length);
obj->UncheckedSetKey(key, (unsigned int)length);
}
template <class T>
static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs &param)
{
obj->ThrowIfInvalidKeyLength(length);
obj->UncheckedSetKey(dir, key, (unsigned int)length);
}
//! _
template <class BASE, class INFO = BASE>
class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE
@ -147,9 +125,6 @@ public:
size_t DefaultKeyLength() const {return INFO::DEFAULT_KEYLENGTH;}
size_t GetValidKeyLength(size_t n) const {return INFO::StaticGetValidKeyLength(n);}
typename BASE::IV_Requirement IVRequirement() const {return (typename BASE::IV_Requirement)INFO::IV_REQUIREMENT;}
protected:
void AssertValidKeyLength(size_t length) {assert(GetValidKeyLength(length) == length);}
};
template <class INFO, class BASE = BlockCipher>
@ -166,29 +141,19 @@ class BlockCipherFinal : public ClonableImpl<BlockCipherFinal<DIR, BASE>, BASE>
public:
BlockCipherFinal() {}
BlockCipherFinal(const byte *key)
{SetKey(key, this->DEFAULT_KEYLENGTH);}
{this->SetKey(key, this->DEFAULT_KEYLENGTH);}
BlockCipherFinal(const byte *key, size_t length)
{SetKey(key, length);}
{this->SetKey(key, length);}
BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
{this->SetKeyWithRounds(key, length, rounds);}
bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
void SetKey(const byte *key, size_t length, const NameValuePairs &param = g_nullNameValuePairs)
{
CheckedSetKey(this, DIR, key, length, param);
}
};
//! _
template <class BASE, class INFO = BASE>
class MessageAuthenticationCodeImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
{
public:
void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs)
{
CheckedSetKey(this, Empty(), key, length, params);
}
};
//! _
@ -198,7 +163,7 @@ class MessageAuthenticationCodeFinal : public ClonableImpl<MessageAuthentication
public:
MessageAuthenticationCodeFinal() {}
MessageAuthenticationCodeFinal(const byte *key)
{SetKey(key, this->DEFAULT_KEYLENGTH);}
{this->SetKey(key, this->DEFAULT_KEYLENGTH);}
MessageAuthenticationCodeFinal(const byte *key, size_t length)
{this->SetKey(key, length);}
};

View File

@ -421,7 +421,7 @@ NAMESPACE_BEGIN(CryptoPP)
c ^= k[4 * r + 2]; \
d ^= k[4 * r + 3];}
void Serpent::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int keylen)
void Serpent::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &)
{
AssertValidKeyLength(keylen);

View File

@ -21,7 +21,7 @@ class Serpent : public Serpent_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Serpent_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
FixedSizeSecBlock<word32, 140> m_key;

View File

@ -31,7 +31,7 @@ NAMESPACE_BEGIN(CryptoPP)
#define P(a,b,c,d,e,f,g,h,k) \
h-=S0(a)+Maj(a,b,c);d-=h;h-=S1(e)+Ch(e,f,g)+*--k;
void SHACAL2::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int keylen)
void SHACAL2::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &)
{
AssertValidKeyLength(keylen);

View File

@ -21,7 +21,7 @@ class SHACAL2 : public SHACAL2_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<SHACAL2_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
FixedSizeSecBlock<word32, 64> m_key;

View File

@ -32,12 +32,11 @@ static word64 SHARKTransform(word64 a)
return result;
}
void SHARK::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int keyLen, unsigned int rounds)
void SHARK::Base::UncheckedSetKey(const byte *key, unsigned int keyLen, const NameValuePairs &params)
{
AssertValidKeyLength(keyLen);
AssertValidRounds(rounds);
m_rounds = rounds;
m_rounds = GetRoundsAndThrowIfInvalid(params, this);
m_roundKeys.New(m_rounds+1);
// concatenate key enought times to fill a
@ -55,7 +54,7 @@ void SHARK::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int k
m_roundKeys[m_rounds] = SHARKTransform(m_roundKeys[m_rounds]);
if (dir == DECRYPTION)
if (!IsForwardTransformation())
{
unsigned int i;

View File

@ -25,7 +25,7 @@ class SHARK : public SHARK_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<SHARK_Info>
{
public:
void UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length, unsigned int rounds);
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &param);
protected:
unsigned int m_rounds;

View File

@ -78,7 +78,7 @@ const byte SKIPJACK::Base::fTable[256] = {
/**
* Preprocess a user key into a table to save an XOR at each F-table access.
*/
void SKIPJACK::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length)
void SKIPJACK::Base::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);

View File

@ -21,7 +21,7 @@ class SKIPJACK : public SKIPJACK_Info, public BlockCipherDocumentation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<SKIPJACK_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
static const byte fTable[256];

View File

@ -31,7 +31,7 @@ static void SquareTransform (word32 in[4], word32 out[4])
}
}
void Square::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int length)
void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
AssertValidKeyLength(length);
@ -52,7 +52,7 @@ void Square::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned
}
/* produce the round keys */
if (dir == ENCRYPTION)
if (IsForwardTransformation())
{
for (int i = 0; i < ROUNDS; i++)
SquareTransform (roundkeys[i], roundkeys[i]);

View File

@ -21,7 +21,7 @@ class Square : public Square_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Square_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
FixedSizeSecBlock<word32[4], ROUNDS+1> roundkeys;

View File

@ -135,7 +135,7 @@ public:
typedef typename BASE::PolicyInterface PolicyInterface;
protected:
void UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv);
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
unsigned int GetBufferByteSize(const PolicyInterface &policy) const {return policy.GetBytesPerIteration() * policy.GetIterationsToBuffer();}
@ -233,7 +233,7 @@ public:
protected:
virtual void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length) =0;
void UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv);
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
size_t m_leftOver;
};
@ -266,23 +266,17 @@ class SymmetricCipherFinal : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE
public:
SymmetricCipherFinal() {}
SymmetricCipherFinal(const byte *key)
{SetKey(key, this->DEFAULT_KEYLENGTH);}
{this->SetKey(key, this->DEFAULT_KEYLENGTH);}
SymmetricCipherFinal(const byte *key, size_t length)
{SetKey(key, length);}
{this->SetKey(key, length);}
SymmetricCipherFinal(const byte *key, size_t length, const byte *iv)
{this->SetKeyWithIV(key, length, iv);}
void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs)
{
this->ThrowIfInvalidKeyLength(length);
this->UncheckedSetKey(params, key, (unsigned int)length, this->GetIVAndThrowIfInvalid(params));
}
Clonable * Clone() const {return static_cast<SymmetricCipher *>(new SymmetricCipherFinal<BASE, INFO>(*this));}
};
template <class S>
void AdditiveCipherTemplate<S>::UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv)
void AdditiveCipherTemplate<S>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
PolicyInterface &policy = this->AccessPolicy();
policy.CipherSetKey(params, key, length);
@ -290,17 +284,17 @@ void AdditiveCipherTemplate<S>::UncheckedSetKey(const NameValuePairs &params, co
m_buffer.New(GetBufferByteSize(policy));
if (this->IsResynchronizable())
policy.CipherResynchronize(m_buffer, iv);
policy.CipherResynchronize(m_buffer, this->GetIVAndThrowIfInvalid(params));
}
template <class BASE>
void CFB_CipherTemplate<BASE>::UncheckedSetKey(const NameValuePairs &params, const byte *key, unsigned int length, const byte *iv)
void CFB_CipherTemplate<BASE>::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
PolicyInterface &policy = this->AccessPolicy();
policy.CipherSetKey(params, key, length);
if (this->IsResynchronizable())
policy.CipherResynchronize(iv);
policy.CipherResynchronize(this->GetIVAndThrowIfInvalid(params));
m_leftOver = policy.GetBytesPerIteration();
}

View File

@ -9,12 +9,12 @@ NAMESPACE_BEGIN(CryptoPP)
static const word32 DELTA = 0x9e3779b9;
typedef BlockGetAndPut<word32, BigEndian> Block;
void TEA::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds)
void TEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
{
AssertValidKeyLength(length);
GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, userKey, KEYLENGTH);
m_limit = rounds * DELTA;
m_limit = GetRoundsAndThrowIfInvalid(params, this) * DELTA;
}
void TEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
@ -49,12 +49,12 @@ void TEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byt
Block::Put(xorBlock, outBlock)(y)(z);
}
void XTEA::Base::UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds)
void XTEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
{
AssertValidKeyLength(length);
GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, userKey, KEYLENGTH);
m_limit = rounds * DELTA;
m_limit = GetRoundsAndThrowIfInvalid(params, this) * DELTA;
}
void XTEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const

12
tea.h
View File

@ -21,7 +21,7 @@ class TEA : public TEA_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<TEA_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
FixedSizeSecBlock<word32, 4> m_k;
@ -60,7 +60,7 @@ class XTEA : public XTEA_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<XTEA_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length, unsigned int rounds);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
FixedSizeSecBlock<word32, 4> m_k;
@ -97,12 +97,10 @@ class BTEA : public BTEA_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BlockCipher, BTEA_Info>, BTEA_Info>, public BTEA_Info
{
public:
template <class T>
static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs &param)
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
obj->ThrowIfInvalidKeyLength(length);
obj->m_blockSize = param.GetIntValueWithDefault("BlockSize", 60*4);
GetUserKey(BIG_ENDIAN_ORDER, obj->m_k.begin(), 4, key, KEYLENGTH);
m_blockSize = params.GetIntValueWithDefault("BlockSize", 60*4);
GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, key, KEYLENGTH);
}
unsigned int BlockSize() const {return m_blockSize;}

View File

@ -749,6 +749,7 @@ bool Validate(int alg, bool thorough, const char *seed)
switch (alg)
{
case 0: result = ValidateAll(thorough); break;
case 1: result = TestSettings(); break;
case 2: result = TestOS_RNG(); break;
case 3: result = ValidateMD5(); break;
@ -812,7 +813,7 @@ bool Validate(int alg, bool thorough, const char *seed)
case 62: result = ValidateWhirlpool(); break;
case 63: result = ValidateTTMAC(); break;
case 64: result = ValidateSalsa(); break;
default: result = ValidateAll(thorough); break;
default: return false;
}
time_t endTime = time(NULL);

View File

@ -6,7 +6,7 @@
NAMESPACE_BEGIN(CryptoPP)
void TTMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength)
void TTMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
{
AssertValidKeyLength(keylength);

View File

@ -18,7 +18,7 @@ public:
TTMAC_Base() {SetStateSize(DIGESTSIZE*2);}
unsigned int DigestSize() const {return DIGESTSIZE;};
void UncheckedSetKey(const byte *userKey, unsigned int keylength);
void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
void TruncatedFinal(byte *mac, size_t size);
protected:

View File

@ -49,7 +49,7 @@ inline word32 Twofish::Base::h(word32 x, const word32 *key, unsigned int kLen)
return mds[0][GETBYTE(x,0)] ^ mds[1][GETBYTE(x,1)] ^ mds[2][GETBYTE(x,2)] ^ mds[3][GETBYTE(x,3)];
}
void Twofish::Base::UncheckedSetKey(CipherDir dir, const byte *userKey, unsigned int keylength)
void Twofish::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
{
AssertValidKeyLength(keylength);

View File

@ -21,7 +21,7 @@ class Twofish : public Twofish_Info, public BlockCipherDocumentation
class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Twofish_Info>
{
public:
void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length);
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
protected:
static word32 h0(word32 x, const word32 *key, unsigned int kLen);

View File

@ -23,7 +23,7 @@ public:
XMACC_Base() {SetStateSize(T::DIGESTSIZE);}
void CheckedSetKey(void *, Empty empty, const byte *key, size_t length, const NameValuePairs &params);
void UncheckedSetKey(const byte *key, size_t length, const NameValuePairs &params);
void Resynchronize(const byte *IV)
{
GetWord(false, BIG_ENDIAN_ORDER, m_counter, IV);
@ -70,9 +70,9 @@ public:
{this->SetKey(key, this->KEYLENGTH, MakeParameters(Name::XMACC_Counter(), counter));}
};
template <class T> void XMACC_Base<T>::CheckedSetKey(void *, Empty empty, const byte *key, size_t length, const NameValuePairs &params)
template <class T> void XMACC_Base<T>::UncheckedSetKey(const byte *key, size_t length, const NameValuePairs &params)
{
this->ThrowIfInvalidKeyLength(length);
this->AssertValidKeyLength(length);
m_counter = 0xffffffff;
const byte *iv = NULL;
if (params.GetValue(Name::IV(), iv))