00001
00002
00003
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 FixedBlockSize
00022 {
00023 public:
00024 enum {BLOCKSIZE = N};
00025 };
00026
00027
00028
00029
00030 template <unsigned int R>
00031 class 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 ¶m)
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>
00050 class 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 ¶m)
00064 {
00065 obj->ThrowIfInvalidKeyLength(length);
00066 unsigned 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
00074
00075
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
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
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
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
00125
00126 template <class T>
00127 static inline void CheckedSetKey(T *obj, Empty empty, const byte *key, unsigned int length, const NameValuePairs ¶m)
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 ¶m)
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 (unsigned int)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 INTERFACE = BlockCipher>
00156 class BlockCipherBaseTemplate : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<INFO, INTERFACE> > >
00157 {
00158 public:
00159 unsigned int BlockSize() const {return BLOCKSIZE;}
00160 };
00161
00162
00163 template <CipherDir DIR, class BASE>
00164 class BlockCipherTemplate : public BASE
00165 {
00166 public:
00167 BlockCipherTemplate() {}
00168 BlockCipherTemplate(const byte *key)
00169 {SetKey(key, DEFAULT_KEYLENGTH);}
00170 BlockCipherTemplate(const byte *key, unsigned int length)
00171 {SetKey(key, length);}
00172 BlockCipherTemplate(const byte *key, unsigned int length, unsigned int rounds)
00173 {SetKeyWithRounds(key, length, rounds);}
00174
00175 bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
00176
00177 void SetKey(const byte *key, unsigned int length, const NameValuePairs ¶m = g_nullNameValuePairs)
00178 {
00179 CheckedSetKey(this, DIR, key, length, param);
00180 }
00181
00182 Clonable * Clone() const {return new BlockCipherTemplate<DIR, BASE>(*this);}
00183 };
00184
00185
00186 template <class BASE>
00187 class MessageAuthenticationCodeTemplate : public
00188 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
00189 MessageAuthenticationCode
00190 #else
00191 SimpleKeyingInterfaceImpl<BASE>
00192 #endif
00193 {
00194 public:
00195 MessageAuthenticationCodeTemplate() {}
00196 MessageAuthenticationCodeTemplate(const byte *key)
00197 {SetKey(key, DEFAULT_KEYLENGTH);}
00198 MessageAuthenticationCodeTemplate(const byte *key, unsigned int length)
00199 {SetKey(key, length);}
00200
00201 std::string AlgorithmName() const {return StaticAlgorithmName();}
00202
00203 void SetKey(const byte *key, unsigned int length, const NameValuePairs ¶m = g_nullNameValuePairs)
00204 {
00205 CheckedSetKey(this, Empty(), key, length, param);
00206 }
00207
00208 Clonable * Clone() const {return new MessageAuthenticationCodeTemplate<BASE>(*this);}
00209 };
00210
00211
00212
00213
00214
00215
00216 struct BlockCipherDocumentation
00217 {
00218
00219 typedef BlockCipher Encryption;
00220
00221 typedef BlockCipher Decryption;
00222 };
00223
00224
00225
00226
00227 struct SymmetricCipherDocumentation
00228 {
00229
00230 typedef SymmetricCipher Encryption;
00231
00232 typedef SymmetricCipher Decryption;
00233 };
00234
00235 NAMESPACE_END
00236
00237 #endif