00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CRYPTOPP_STRCIPHR_H
00029 #define CRYPTOPP_STRCIPHR_H
00030
00031 #include "seckey.h"
00032 #include "secblock.h"
00033 #include "argnames.h"
00034
00035 NAMESPACE_BEGIN(CryptoPP)
00036
00037 template <class POLICY_INTERFACE, class BASE = Empty>
00038 class CRYPTOPP_NO_VTABLE AbstractPolicyHolder : public BASE
00039 {
00040 public:
00041 typedef POLICY_INTERFACE PolicyInterface;
00042
00043 protected:
00044 virtual const POLICY_INTERFACE & GetPolicy() const =0;
00045 virtual POLICY_INTERFACE & AccessPolicy() =0;
00046 };
00047
00048 template <class POLICY, class BASE, class POLICY_INTERFACE = CPP_TYPENAME BASE::PolicyInterface>
00049 class ConcretePolicyHolder : public BASE, protected POLICY
00050 {
00051 protected:
00052 const POLICY_INTERFACE & GetPolicy() const {return *this;}
00053 POLICY_INTERFACE & AccessPolicy() {return *this;}
00054 };
00055
00056 enum KeystreamOperation {WRITE_KEYSTREAM, XOR_KEYSTREAM, XOR_KEYSTREAM_INPLACE};
00057
00058 struct CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AdditiveCipherAbstractPolicy
00059 {
00060 virtual unsigned int GetAlignment() const =0;
00061 virtual unsigned int GetBytesPerIteration() const =0;
00062 virtual unsigned int GetIterationsToBuffer() const =0;
00063 virtual void WriteKeystream(byte *keystreamBuffer, unsigned int iterationCount) =0;
00064 virtual bool CanOperateKeystream() const {return false;}
00065 virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount) {assert(false);}
00066 virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length) =0;
00067 virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv) {throw NotImplemented("StreamTransformation: this object doesn't support resynchronization");}
00068 virtual bool IsRandomAccess() const =0;
00069 virtual void SeekToIteration(lword iterationCount) {assert(!IsRandomAccess()); throw NotImplemented("StreamTransformation: this object doesn't support random access");}
00070 };
00071
00072 template <typename WT, unsigned int W, unsigned int X = 1, class BASE = AdditiveCipherAbstractPolicy>
00073 struct CRYPTOPP_NO_VTABLE AdditiveCipherConcretePolicy : public BASE
00074 {
00075 typedef WT WordType;
00076
00077 unsigned int GetAlignment() const {return sizeof(WordType);}
00078 unsigned int GetBytesPerIteration() const {return sizeof(WordType) * W;}
00079 unsigned int GetIterationsToBuffer() const {return X;}
00080 void WriteKeystream(byte *buffer, unsigned int iterationCount)
00081 {OperateKeystream(WRITE_KEYSTREAM, buffer, NULL, iterationCount);}
00082 bool CanOperateKeystream() const {return true;}
00083 virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount) =0;
00084
00085 template <class B>
00086 struct KeystreamOutput
00087 {
00088 KeystreamOutput(KeystreamOperation operation, byte *output, const byte *input)
00089 : m_operation(operation), m_output(output), m_input(input) {}
00090
00091 inline KeystreamOutput & operator()(WordType keystreamWord)
00092 {
00093 assert(IsAligned<WordType>(m_input));
00094 assert(IsAligned<WordType>(m_output));
00095
00096 if (!NativeByteOrderIs(B::ToEnum()))
00097 keystreamWord = ByteReverse(keystreamWord);
00098
00099 if (m_operation == WRITE_KEYSTREAM)
00100 *(WordType*)m_output = keystreamWord;
00101 else if (m_operation == XOR_KEYSTREAM)
00102 {
00103 *(WordType*)m_output = keystreamWord ^ *(WordType*)m_input;
00104 m_input += sizeof(WordType);
00105 }
00106 else if (m_operation == XOR_KEYSTREAM_INPLACE)
00107 *(WordType*)m_output ^= keystreamWord;
00108
00109 m_output += sizeof(WordType);
00110
00111 return *this;
00112 }
00113
00114 KeystreamOperation m_operation;
00115 byte *m_output;
00116 const byte *m_input;
00117 };
00118 };
00119
00120 template <class BASE = AbstractPolicyHolder<AdditiveCipherAbstractPolicy, TwoBases<SymmetricCipher, RandomNumberGenerator> > >
00121 class CRYPTOPP_NO_VTABLE AdditiveCipherTemplate : public BASE
00122 {
00123 public:
00124 byte GenerateByte();
00125 void ProcessData(byte *outString, const byte *inString, unsigned int length);
00126 void Resynchronize(const byte *iv);
00127 unsigned int OptimalBlockSize() const {return this->GetPolicy().GetBytesPerIteration();}
00128 unsigned int GetOptimalNextBlockSize() const {return this->m_leftOver;}
00129 unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
00130 bool IsSelfInverting() const {return true;}
00131 bool IsForwardTransformation() const {return true;}
00132 bool IsRandomAccess() const {return this->GetPolicy().IsRandomAccess();}
00133 void Seek(lword position);
00134
00135 typedef typename BASE::PolicyInterface PolicyInterface;
00136
00137 protected:
00138 void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv);
00139
00140 unsigned int GetBufferByteSize(const PolicyInterface &policy) const {return policy.GetBytesPerIteration() * policy.GetIterationsToBuffer();}
00141
00142 inline byte * KeystreamBufferBegin() {return this->m_buffer.data();}
00143 inline byte * KeystreamBufferEnd() {return (this->m_buffer.data() + this->m_buffer.size());}
00144
00145 SecByteBlock m_buffer;
00146 unsigned int m_leftOver;
00147 };
00148
00149 CRYPTOPP_DLL_TEMPLATE_CLASS TwoBases<SymmetricCipher, RandomNumberGenerator>;
00150 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractPolicyHolder<AdditiveCipherAbstractPolicy, TwoBases<SymmetricCipher, RandomNumberGenerator> >;
00151 CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<>;
00152
00153 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_CipherAbstractPolicy
00154 {
00155 public:
00156 virtual unsigned int GetAlignment() const =0;
00157 virtual unsigned int GetBytesPerIteration() const =0;
00158 virtual byte * GetRegisterBegin() =0;
00159 virtual void TransformRegister() =0;
00160 virtual bool CanIterate() const {return false;}
00161 virtual void Iterate(byte *output, const byte *input, CipherDir dir, unsigned int iterationCount) {assert(false);}
00162 virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length) =0;
00163 virtual void CipherResynchronize(const byte *iv) {throw NotImplemented("StreamTransformation: this object doesn't support resynchronization");}
00164 };
00165
00166 template <typename WT, unsigned int W, class BASE = CFB_CipherAbstractPolicy>
00167 struct CRYPTOPP_NO_VTABLE CFB_CipherConcretePolicy : public BASE
00168 {
00169 typedef WT WordType;
00170
00171 unsigned int GetAlignment() const {return sizeof(WordType);}
00172 unsigned int GetBytesPerIteration() const {return sizeof(WordType) * W;}
00173 bool CanIterate() const {return true;}
00174 void TransformRegister() {this->Iterate(NULL, NULL, ENCRYPTION, 1);}
00175
00176 template <class B>
00177 struct RegisterOutput
00178 {
00179 RegisterOutput(byte *output, const byte *input, CipherDir dir)
00180 : m_output(output), m_input(input), m_dir(dir) {}
00181
00182 inline RegisterOutput& operator()(WordType ®isterWord)
00183 {
00184 assert(IsAligned<WordType>(m_output));
00185 assert(IsAligned<WordType>(m_input));
00186
00187 if (!NativeByteOrderIs(B::ToEnum()))
00188 registerWord = ByteReverse(registerWord);
00189
00190 if (m_dir == ENCRYPTION)
00191 {
00192 if (m_input == NULL)
00193 assert(m_output == NULL);
00194 else
00195 {
00196 WordType ct = *(const WordType *)m_input ^ registerWord;
00197 registerWord = ct;
00198 *(WordType*)m_output = ct;
00199 m_input += sizeof(WordType);
00200 m_output += sizeof(WordType);
00201 }
00202 }
00203 else
00204 {
00205 WordType ct = *(const WordType *)m_input;
00206 *(WordType*)m_output = registerWord ^ ct;
00207 registerWord = ct;
00208 m_input += sizeof(WordType);
00209 m_output += sizeof(WordType);
00210 }
00211
00212
00213
00214 return *this;
00215 }
00216
00217 byte *m_output;
00218 const byte *m_input;
00219 CipherDir m_dir;
00220 };
00221 };
00222
00223 template <class BASE>
00224 class CRYPTOPP_NO_VTABLE CFB_CipherTemplate : public BASE
00225 {
00226 public:
00227 void ProcessData(byte *outString, const byte *inString, unsigned int length);
00228 void Resynchronize(const byte *iv);
00229 unsigned int OptimalBlockSize() const {return this->GetPolicy().GetBytesPerIteration();}
00230 unsigned int GetOptimalNextBlockSize() const {return m_leftOver;}
00231 unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
00232 bool IsRandomAccess() const {return false;}
00233 bool IsSelfInverting() const {return false;}
00234
00235 typedef typename BASE::PolicyInterface PolicyInterface;
00236
00237 protected:
00238 virtual void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length) =0;
00239
00240 void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv);
00241
00242 unsigned int m_leftOver;
00243 };
00244
00245 template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
00246 class CRYPTOPP_NO_VTABLE CFB_EncryptionTemplate : public CFB_CipherTemplate<BASE>
00247 {
00248 bool IsForwardTransformation() const {return true;}
00249 void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length);
00250 };
00251
00252 template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
00253 class CRYPTOPP_NO_VTABLE CFB_DecryptionTemplate : public CFB_CipherTemplate<BASE>
00254 {
00255 bool IsForwardTransformation() const {return false;}
00256 void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length);
00257 };
00258
00259 template <class BASE>
00260 class CFB_RequireFullDataBlocks : public BASE
00261 {
00262 public:
00263 unsigned int MandatoryBlockSize() const {return this->OptimalBlockSize();}
00264 };
00265
00266
00267 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
00268 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<>;
00269 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<>;
00270
00271
00272 template <class BASE, class INFO = BASE>
00273 class SymmetricCipherFinal : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
00274 {
00275 public:
00276 SymmetricCipherFinal() {}
00277 SymmetricCipherFinal(const byte *key)
00278 {SetKey(key, this->DEFAULT_KEYLENGTH);}
00279 SymmetricCipherFinal(const byte *key, unsigned int length)
00280 {SetKey(key, length);}
00281 SymmetricCipherFinal(const byte *key, unsigned int length, const byte *iv)
00282 {this->SetKeyWithIV(key, length, iv);}
00283
00284 void SetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms = g_nullNameValuePairs)
00285 {
00286 this->ThrowIfInvalidKeyLength(length);
00287 this->UncheckedSetKey(params, key, length, this->GetIVAndThrowIfInvalid(params));
00288 }
00289
00290 Clonable * Clone() const {return static_cast<SymmetricCipher *>(new SymmetricCipherFinal<BASE, INFO>(*this));}
00291 };
00292
00293 template <class S>
00294 void AdditiveCipherTemplate<S>::UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv)
00295 {
00296 PolicyInterface &policy = this->AccessPolicy();
00297 policy.CipherSetKey(params, key, length);
00298 m_leftOver = 0;
00299 m_buffer.New(GetBufferByteSize(policy));
00300
00301 if (this->IsResynchronizable())
00302 policy.CipherResynchronize(m_buffer, iv);
00303 }
00304
00305 template <class BASE>
00306 void CFB_CipherTemplate<BASE>::UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv)
00307 {
00308 PolicyInterface &policy = this->AccessPolicy();
00309 policy.CipherSetKey(params, key, length);
00310
00311 if (this->IsResynchronizable())
00312 policy.CipherResynchronize(iv);
00313
00314 m_leftOver = policy.GetBytesPerIteration();
00315 }
00316
00317 NAMESPACE_END
00318
00319 #endif