Skip to content

Commit

Permalink
port to GCC 4, reorganize implementations of SetKey
Browse files Browse the repository at this point in the history
  • Loading branch information
weidai11 committed Dec 10, 2006
1 parent 28c392e commit f05ea58
Show file tree
Hide file tree
Showing 66 changed files with 203 additions and 250 deletions.
7 changes: 3 additions & 4 deletions 3way.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion 3way.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion arc4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion arc4.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CRYPTOPP_NO_VTABLE ARC4_Base : public VariableKeyLength<16, 1, 256>, publi
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;
Expand Down
4 changes: 2 additions & 2 deletions blowfish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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]);
}
Expand Down
2 changes: 1 addition & 1 deletion blowfish.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions camellia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion camellia.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand Down
6 changes: 3 additions & 3 deletions cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion cbcmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion cbcmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();}
Expand Down
14 changes: 10 additions & 4 deletions cryptlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions cryptlib.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions cryptlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE SimpleKeyingInterface

//! 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);
Expand Down Expand Up @@ -400,15 +400,15 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE SimpleKeyingInterface
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
Expand Down Expand Up @@ -451,6 +451,8 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockTransformation : public Algorithm

//! 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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion datatest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit f05ea58

Please sign in to comment.