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 GCC4FIX0 {BLOCKSIZE = N};
00025 };
00026
00027
00028
00029
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 ¶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 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 ¶m)
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
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 struct SimpledKeyed_Helper {
00127 template <class T>
00128 static inline void CheckedSetKey(T *obj, Empty empty, const byte *key, unsigned int length, const NameValuePairs ¶m)
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 ¶m)
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 ¶m = 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 ¶ms = 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
00209
00210
00211
00212
00213 struct BlockCipherDocumentation
00214 {
00215
00216 typedef BlockCipher Encryption;
00217
00218 typedef BlockCipher Decryption;
00219 };
00220
00221
00222
00223
00224
00225
00226 struct SymmetricCipherDocumentation
00227 {
00228
00229 typedef SymmetricCipher Encryption;
00230
00231 typedef SymmetricCipher Decryption;
00232 };
00233
00234 NAMESPACE_END
00235
00236 #endif