seckey.h

00001 // seckey.h - written and placed in the public domain by Wei Dai
00002 
00003 // This file contains helper classes/functions for implementing secret key algorithms.
00004 
00005 #ifndef CRYPTOPP_SECKEY_H
00006 #define CRYPTOPP_SECKEY_H
00007 
00008 #include "cryptlib.h"
00009 #include "misc.h"
00010 #include "simple.h"
00011 
00012 NAMESPACE_BEGIN(CryptoPP)
00013 
00014 inline CipherDir ReverseCipherDir(CipherDir dir)
00015 {
00016         return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
00017 }
00018 
00019 //! to be inherited by block ciphers with fixed block size
00020 template <unsigned int N>
00021 class FixedBlockSize
00022 {
00023 public:
00024         enum GCC4FIX0 {BLOCKSIZE = N};
00025 };
00026 
00027 // ************** rounds ***************
00028 
00029 //! to be inherited by ciphers with fixed number of rounds
00030 template <unsigned int R>
00031 class FixedRounds
00032 {
00033 public:
00034         enum GCC4FIX1 {ROUNDS = R};
00035 
00036 protected:
00037         template <class T>
00038         static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs &param)
00039         {
00040                 obj->ThrowIfInvalidKeyLength(length);
00041                 int rounds = param.GetIntValueWithDefault("Rounds", ROUNDS);
00042                 if (rounds != ROUNDS)
00043                         throw InvalidRounds(obj->StaticAlgorithmName(), rounds);
00044                 obj->UncheckedSetKey(dir, key, length);
00045         }
00046 };
00047 
00048 //! to be inherited by ciphers with variable number of rounds
00049 template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX>             // use INT_MAX here because enums are treated as signed ints
00050 class VariableRounds
00051 {
00052 public:
00053         enum GCC4FIX3 {DEFAULT_ROUNDS = D, MIN_ROUNDS = N, MAX_ROUNDS = M};
00054         static unsigned int StaticGetDefaultRounds(unsigned int keylength) {return DEFAULT_ROUNDS;}
00055 
00056 protected:
00057         static inline void AssertValidRounds(unsigned int rounds)
00058         {
00059                 assert(rounds >= (unsigned int)MIN_ROUNDS && rounds <= (unsigned int)MAX_ROUNDS);
00060         }
00061 
00062         template <class T>
00063         static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs &param)
00064         {
00065                 obj->ThrowIfInvalidKeyLength(length);
00066                 int rounds = param.GetIntValueWithDefault("Rounds", obj->StaticGetDefaultRounds(length));
00067                 if (rounds < (int)MIN_ROUNDS || rounds > (int)MAX_ROUNDS)
00068                         throw InvalidRounds(obj->AlgorithmName(), rounds);
00069                 obj->UncheckedSetKey(dir, key, length, rounds);
00070         }
00071 };
00072 
00073 // ************** key length ***************
00074 
00075 //! to be inherited by keyed algorithms with fixed key length
00076 template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
00077 class FixedKeyLength
00078 {
00079 public:
00080         enum {KEYLENGTH=N, MIN_KEYLENGTH=N, MAX_KEYLENGTH=N, DEFAULT_KEYLENGTH=N};
00081         enum {IV_REQUIREMENT = IV_REQ};
00082         static unsigned int StaticGetValidKeyLength(unsigned int) {return KEYLENGTH;}
00083 };
00084 
00085 /// support query of variable key length, template parameters are default, min, max, multiple (default multiple 1)
00086 template <unsigned int D, unsigned int N, unsigned int M, unsigned int Q = 1, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
00087 class VariableKeyLength
00088 {
00089         // make these private to avoid Doxygen documenting them in all derived classes
00090         CRYPTOPP_COMPILE_ASSERT(Q > 0);
00091         CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
00092         CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
00093         CRYPTOPP_COMPILE_ASSERT(N < M);
00094         CRYPTOPP_COMPILE_ASSERT(D >= N && M >= D);
00095 
00096 public:
00097         enum {MIN_KEYLENGTH=N, MAX_KEYLENGTH=M, DEFAULT_KEYLENGTH=D, KEYLENGTH_MULTIPLE=Q};
00098         enum {IV_REQUIREMENT = IV_REQ};
00099         static unsigned int StaticGetValidKeyLength(unsigned int n)
00100         {
00101                 if (n < (unsigned int)MIN_KEYLENGTH)
00102                         return MIN_KEYLENGTH;
00103                 else if (n > (unsigned int)MAX_KEYLENGTH)
00104                         return (unsigned int)MAX_KEYLENGTH;
00105                 else
00106                 {
00107                         n += KEYLENGTH_MULTIPLE-1;
00108                         return n - n%KEYLENGTH_MULTIPLE;
00109                 }
00110         }
00111 };
00112 
00113 /// support query of key length that's the same as another class
00114 template <class T>
00115 class SameKeyLengthAs
00116 {
00117 public:
00118         enum {MIN_KEYLENGTH=T::MIN_KEYLENGTH, MAX_KEYLENGTH=T::MAX_KEYLENGTH, DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH};
00119         enum {IV_REQUIREMENT = T::IV_REQUIREMENT};
00120         static unsigned int StaticGetValidKeyLength(unsigned int keylength)
00121                 {return T::StaticGetValidKeyLength(keylength);}
00122 };
00123 
00124 // ************** implementation helper for SimpledKeyed ***************
00125 
00126 struct SimpledKeyed_Helper {
00127         template <class T>
00128         static inline void CheckedSetKey(T *obj, Empty empty, const byte *key, unsigned int length, const NameValuePairs &param)
00129         {
00130                 obj->ThrowIfInvalidKeyLength(length);
00131                 obj->UncheckedSetKey(key, length);
00132         }
00133 
00134         template <class T>
00135         static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs &param)
00136         {
00137                 obj->ThrowIfInvalidKeyLength(length);
00138                 obj->UncheckedSetKey(dir, key, length);
00139         }
00140 };
00141 
00142 //! _
00143 template <class BASE, class INFO = BASE>
00144 class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE
00145 {
00146 public:
00147         unsigned int MinKeyLength() const {return INFO::MIN_KEYLENGTH;}
00148         unsigned int MaxKeyLength() const {return (unsigned int)INFO::MAX_KEYLENGTH;}
00149         unsigned int DefaultKeyLength() const {return INFO::DEFAULT_KEYLENGTH;}
00150         unsigned int GetValidKeyLength(unsigned int n) const {return INFO::StaticGetValidKeyLength(n);}
00151         typename BASE::IV_Requirement IVRequirement() const {return (typename BASE::IV_Requirement)INFO::IV_REQUIREMENT;}
00152 
00153 protected:
00154         void AssertValidKeyLength(unsigned int length) {assert(GetValidKeyLength(length) == length);}
00155 };
00156 
00157 template <class INFO, class BASE = BlockCipher>
00158 class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<BASE, INFO> > >
00159 {
00160 public:
00161         unsigned int BlockSize() const {return this->BLOCKSIZE;}
00162 };
00163 
00164 //! _
00165 template <CipherDir DIR, class BASE>
00166 class BlockCipherFinal : public ClonableImpl<BlockCipherFinal<DIR, BASE>, BASE>
00167 {
00168 public:
00169         BlockCipherFinal() {}
00170         BlockCipherFinal(const byte *key)
00171                 {SetKey(key, this->DEFAULT_KEYLENGTH);}
00172         BlockCipherFinal(const byte *key, unsigned int length)
00173                 {SetKey(key, length);}
00174         BlockCipherFinal(const byte *key, unsigned int length, unsigned int rounds)
00175                 {this->SetKeyWithRounds(key, length, rounds);}
00176 
00177         bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
00178 
00179         void SetKey(const byte *key, unsigned int length, const NameValuePairs &param = g_nullNameValuePairs)
00180         {
00181                 BASE::CheckedSetKey(this, DIR, key, length, param);
00182         }
00183 };
00184 
00185 //! _
00186 template <class BASE, class INFO = BASE>
00187 class MessageAuthenticationCodeImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
00188 {
00189 public:
00190         void SetKey(const byte *key, unsigned int length, const NameValuePairs &params = g_nullNameValuePairs)
00191         {
00192                 BASE::CheckedSetKey(this, Empty(), key, length, params);
00193         }
00194 };
00195 
00196 //! _
00197 template <class BASE>
00198 class MessageAuthenticationCodeFinal : public ClonableImpl<MessageAuthenticationCodeFinal<BASE>, MessageAuthenticationCodeImpl<BASE> >
00199 {
00200 public:
00201         MessageAuthenticationCodeFinal() {}
00202         MessageAuthenticationCodeFinal(const byte *key)
00203                 {SetKey(key, this->DEFAULT_KEYLENGTH);}
00204         MessageAuthenticationCodeFinal(const byte *key, unsigned int length)
00205                 {this->SetKey(key, length);}
00206 };
00207 
00208 // ************** documentation ***************
00209 
00210 //! These objects usually should not be used directly. See CipherModeDocumentation instead.
00211 /*! Each class derived from this one defines two types, Encryption and Decryption, 
00212         both of which implement the BlockCipher interface. */
00213 struct BlockCipherDocumentation
00214 {
00215         //! implements the BlockCipher interface
00216         typedef BlockCipher Encryption;
00217         //! implements the BlockCipher interface
00218         typedef BlockCipher Decryption;
00219 };
00220 
00221 /*! \brief Each class derived from this one defines two types, Encryption and Decryption, 
00222         both of which implement the SymmetricCipher interface. Two types of classes derive
00223         from this class: stream ciphers and block cipher modes. Stream ciphers can be used
00224         alone, cipher mode classes need to be used with a block cipher. See CipherModeDocumentation
00225         for more for information about using cipher modes and block ciphers. */
00226 struct SymmetricCipherDocumentation
00227 {
00228         //! implements the SymmetricCipher interface
00229         typedef SymmetricCipher Encryption;
00230         //! implements the SymmetricCipher interface
00231         typedef SymmetricCipher Decryption;
00232 };
00233 
00234 NAMESPACE_END
00235 
00236 #endif

Generated on Thu Mar 30 22:11:48 2006 for Crypto++ by  doxygen 1.4.6