From c3bad1afc1564f3bfac8434d45d6694811139333 Mon Sep 17 00:00:00 2001 From: weidai Date: Sun, 10 Dec 2006 02:12:23 +0000 Subject: port to GCC 4, reorganize implementations of SetKey git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@248 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- seckey.h | 59 ++++++++++++----------------------------------------------- 1 file changed, 12 insertions(+), 47 deletions(-) (limited to 'seckey.h') diff --git a/seckey.h b/seckey.h index d7f90d2..b5e8824 100644 --- a/seckey.h +++ b/seckey.h @@ -32,17 +32,6 @@ class FixedRounds { public: enum {ROUNDS = R}; - -protected: - template - static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs ¶m) - { - 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 - static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs ¶m) + 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 ¶m, 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 -static inline void CheckedSetKey(T *obj, Empty empty, const byte *key, size_t length, const NameValuePairs ¶m) -{ - obj->ThrowIfInvalidKeyLength(length); - obj->UncheckedSetKey(key, (unsigned int)length); -} - -template -static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, size_t length, const NameValuePairs ¶m) -{ - obj->ThrowIfInvalidKeyLength(length); - obj->UncheckedSetKey(dir, key, (unsigned int)length); -} - //! _ template 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 @@ -166,29 +141,19 @@ class BlockCipherFinal : public ClonableImpl, 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 ¶m = g_nullNameValuePairs) - { - CheckedSetKey(this, DIR, key, length, param); - } }; //! _ template class MessageAuthenticationCodeImpl : public AlgorithmImpl, INFO> { -public: - void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms = g_nullNameValuePairs) - { - CheckedSetKey(this, Empty(), key, length, params); - } }; //! _ @@ -198,7 +163,7 @@ class MessageAuthenticationCodeFinal : public ClonableImplDEFAULT_KEYLENGTH);} + {this->SetKey(key, this->DEFAULT_KEYLENGTH);} MessageAuthenticationCodeFinal(const byte *key, size_t length) {this->SetKey(key, length);} }; -- cgit v1.2.1