Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# 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 //! .
    00020 template <unsigned int N>
    00021 class CRYPTOPP_DLL FixedBlockSize
    00022 {
    00023 public:
    00024         enum {BLOCKSIZE = N};
    00025 };
    00026
    00027 // ************** rounds ***************
    00028
    00029 //! .
    00030 template <unsigned int R>
    00031 class CRYPTOPP_DLL FixedRounds
    00032 {
    00033 public:
    00034         enum {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 //! .
    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 CRYPTOPP_DLL VariableRounds
    00051 {
    00052 public:
    00053         enum {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 >= MIN_ROUNDS && rounds <= 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 < (unsigned int)MIN_ROUNDS || rounds > (unsigned int)MAX_ROUNDS)
    00068                         throw InvalidRounds(obj->AlgorithmName(), rounds);
    00069                 obj->UncheckedSetKey(dir, key, length, rounds);
    00070         }
    00071 };
    00072
    00073 // ************** key length ***************
    00074
    00075 //! .
    00076 template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
    00077 class CRYPTOPP_DLL 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 CRYPTOPP_DLL 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 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 CRYPTOPP_DLL 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 template <class T>
    00127 static inline void CheckedSetKey(T *obj, Empty empty, const byte *key, unsigned int length, const NameValuePairs &param)
    00128 {
    00129         obj->ThrowIfInvalidKeyLength(length);
    00130         obj->UncheckedSetKey(key, length);
    00131 }
    00132
    00133 template <class T>
    00134 static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs &param)
    00135 {
    00136         obj->ThrowIfInvalidKeyLength(length);
    00137         obj->UncheckedSetKey(dir, key, length);
    00138 }
    00139
    00140 //! .
    00141 template <class BASE, class INFO = BASE>
    00142 class SimpleKeyingInterfaceImpl : public BASE
    00143 {
    00144 public:
    00145         unsigned int MinKeyLength() const {return INFO::MIN_KEYLENGTH;}
    00146         unsigned int MaxKeyLength() const {return INFO::MAX_KEYLENGTH;}
    00147         unsigned int DefaultKeyLength() const {return INFO::DEFAULT_KEYLENGTH;}
    00148         unsigned int GetValidKeyLength(unsigned int n) const {return INFO::StaticGetValidKeyLength(n);}
    00149         typename BASE::IV_Requirement IVRequirement() const {return (typename BASE::IV_Requirement)INFO::IV_REQUIREMENT;}
    00150
    00151 protected:
    00152         void AssertValidKeyLength(unsigned int length) {assert(GetValidKeyLength(length) == length);}
    00153 };
    00154
    00155 template <class INFO, class BASE = BlockCipher>
    00156 class BlockCipherBaseTemplate : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>, public INFO
    00157 {
    00158 public:
    00159         unsigned int BlockSize() const {return BLOCKSIZE;}
    00160 };
    00161
    00162 /*
    00163 #define CRYPTOPP_DLL_TEMPLATE_CLASS_BlockCipherBaseTemplate(Info)       \
    00164 CRYPTOPP_DLL_TEMPLATE_CLASS TwoBases<Info, BlockCipher>;        \
    00165 CRYPTOPP_DLL_TEMPLATE_CLASS SimpleKeyingInterfaceImpl<TwoBases<Info, BlockCipher> >;    \
    00166 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<Info, BlockCipher> > >;    \
    00167 CRYPTOPP_DLL_TEMPLATE_CLASS BlockCipherBaseTemplate<Info>;
    00168 */
    00169
    00170 //! .
    00171 template <CipherDir DIR, class BASE>
    00172 class BlockCipherTemplate : public BASE
    00173 {
    00174 public:
    00175         BlockCipherTemplate() {}
    00176         BlockCipherTemplate(const byte *key)
    00177                 {SetKey(key, DEFAULT_KEYLENGTH);}
    00178         BlockCipherTemplate(const byte *key, unsigned int length)
    00179                 {SetKey(key, length);}
    00180         BlockCipherTemplate(const byte *key, unsigned int length, unsigned int rounds)
    00181                 {SetKeyWithRounds(key, length, rounds);}
    00182
    00183         bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
    00184
    00185         void SetKey(const byte *key, unsigned int length, const NameValuePairs &param = g_nullNameValuePairs)
    00186         {
    00187                 CheckedSetKey(this, DIR, key, length, param);
    00188         }
    00189
    00190         Clonable * Clone() {return new BlockCipherTemplate<DIR, BASE>(*this);}
    00191 };
    00192
    00193 //! .
    00194 template <class BASE, class INFO = BASE>
    00195 class MessageAuthenticationCodeFinalTemplate : public
    00196 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
    00197         MessageAuthenticationCode
    00198 #else
    00199         AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
    00200 #endif
    00201 {
    00202 public:
    00203         MessageAuthenticationCodeFinalTemplate() {}
    00204         MessageAuthenticationCodeFinalTemplate(const byte *key)
    00205                 {SetKey(key, DEFAULT_KEYLENGTH);}
    00206         MessageAuthenticationCodeFinalTemplate(const byte *key, unsigned int length)
    00207                 {SetKey(key, length);}
    00208
    00209         void SetKey(const byte *key, unsigned int length, const NameValuePairs &param = g_nullNameValuePairs)
    00210         {
    00211                 CheckedSetKey(this, Empty(), key, length, param);
    00212         }
    00213
    00214         Clonable * Clone() {return new MessageAuthenticationCodeFinalTemplate<BASE, INFO>(*this);}
    00215 };
    00216
    00217 // ************** documentation ***************
    00218
    00219 //! These objects usually should not be used directly. See CipherModeDocumentation instead.
    00220 /*! Each class derived from this one defines two types, Encryption and Decryption,
    00221         both of which implement the BlockCipher interface. */
    00222 struct BlockCipherDocumentation
    00223 {
    00224         //! implements the BlockCipher interface
    00225         typedef BlockCipher Encryption;
    00226         //! implements the BlockCipher interface
    00227         typedef BlockCipher Decryption;
    00228 };
    00229
    00230 /*! \brief Each class derived from this one defines two types, Encryption and Decryption,
    00231         both of which implement the SymmetricCipher interface. See CipherModeDocumentation
    00232         for information about using block ciphers. */
    00233 struct SymmetricCipherDocumentation
    00234 {
    00235         //! implements the SymmetricCipher interface
    00236         typedef SymmetricCipher Encryption;
    00237         //! implements the SymmetricCipher interface
    00238         typedef SymmetricCipher Decryption;
    00239 };
    00240
    00241 NAMESPACE_END
    00242
    00243 #endif