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 "config.h"
00032
00033 #if CRYPTOPP_MSC_VERSION
00034 # pragma warning(push)
00035 # pragma warning(disable: 4127 4189)
00036 #endif
00037
00038 #include "cryptlib.h"
00039 #include "seckey.h"
00040 #include "secblock.h"
00041 #include "argnames.h"
00042
00043 NAMESPACE_BEGIN(CryptoPP)
00044
00045 template <class POLICY_INTERFACE, class BASE = Empty>
00046 class CRYPTOPP_NO_VTABLE AbstractPolicyHolder : public BASE
00047 {
00048 public:
00049 typedef POLICY_INTERFACE PolicyInterface;
00050 virtual ~AbstractPolicyHolder() {}
00051
00052 protected:
00053 virtual const POLICY_INTERFACE & GetPolicy() const =0;
00054 virtual POLICY_INTERFACE & AccessPolicy() =0;
00055 };
00056
00057 template <class POLICY, class BASE, class POLICY_INTERFACE = CPP_TYPENAME BASE::PolicyInterface>
00058 class ConcretePolicyHolder : public BASE, protected POLICY
00059 {
00060 protected:
00061 const POLICY_INTERFACE & GetPolicy() const {return *this;}
00062 POLICY_INTERFACE & AccessPolicy() {return *this;}
00063 };
00064
00065 enum KeystreamOperationFlags {OUTPUT_ALIGNED=1, INPUT_ALIGNED=2, INPUT_NULL = 4};
00066 enum KeystreamOperation {
00067 WRITE_KEYSTREAM = INPUT_NULL,
00068 WRITE_KEYSTREAM_ALIGNED = INPUT_NULL | OUTPUT_ALIGNED,
00069 XOR_KEYSTREAM = 0,
00070 XOR_KEYSTREAM_INPUT_ALIGNED = INPUT_ALIGNED,
00071 XOR_KEYSTREAM_OUTPUT_ALIGNED= OUTPUT_ALIGNED,
00072 XOR_KEYSTREAM_BOTH_ALIGNED = OUTPUT_ALIGNED | INPUT_ALIGNED};
00073
00074 struct CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AdditiveCipherAbstractPolicy
00075 {
00076 virtual ~AdditiveCipherAbstractPolicy() {}
00077 virtual unsigned int GetAlignment() const {return 1;}
00078 virtual unsigned int GetBytesPerIteration() const =0;
00079 virtual unsigned int GetOptimalBlockSize() const {return GetBytesPerIteration();}
00080 virtual unsigned int GetIterationsToBuffer() const =0;
00081 virtual void WriteKeystream(byte *keystream, size_t iterationCount)
00082 {OperateKeystream(KeystreamOperation(INPUT_NULL | (KeystreamOperationFlags)IsAlignedOn(keystream, GetAlignment())), keystream, NULL, iterationCount);}
00083 virtual bool CanOperateKeystream() const {return false;}
00084 virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
00085 {CRYPTOPP_UNUSED(operation); CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(iterationCount); assert(false);}
00086 virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length) =0;
00087 virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
00088 {CRYPTOPP_UNUSED(keystreamBuffer); CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length); throw NotImplemented("SimpleKeyingInterface: this object doesn't support resynchronization");}
00089 virtual bool CipherIsRandomAccess() const =0;
00090 virtual void SeekToIteration(lword iterationCount)
00091 {CRYPTOPP_UNUSED(iterationCount); assert(!CipherIsRandomAccess()); throw NotImplemented("StreamTransformation: this object doesn't support random access");}
00092 };
00093
00094 template <typename WT, unsigned int W, unsigned int X = 1, class BASE = AdditiveCipherAbstractPolicy>
00095 struct CRYPTOPP_NO_VTABLE AdditiveCipherConcretePolicy : public BASE
00096 {
00097 typedef WT WordType;
00098 CRYPTOPP_CONSTANT(BYTES_PER_ITERATION = sizeof(WordType) * W)
00099
00100 #if !(CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64)
00101 unsigned int GetAlignment() const {return GetAlignmentOf<WordType>();}
00102 #endif
00103 unsigned int GetBytesPerIteration() const {return BYTES_PER_ITERATION;}
00104 unsigned int GetIterationsToBuffer() const {return X;}
00105 bool CanOperateKeystream() const {return true;}
00106 virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) =0;
00107 };
00108
00109
00110 #define CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, b, i, a) \
00111 PutWord(bool(x & OUTPUT_ALIGNED), b, output+i*sizeof(WordType), (x & INPUT_NULL) ? (a) : (a) ^ GetWord<WordType>(bool(x & INPUT_ALIGNED), b, input+i*sizeof(WordType)));
00112 #define CRYPTOPP_KEYSTREAM_OUTPUT_XMM(x, i, a) {\
00113 __m128i t = (x & INPUT_NULL) ? a : _mm_xor_si128(a, (x & INPUT_ALIGNED) ? _mm_load_si128((__m128i *)input+i) : _mm_loadu_si128((__m128i *)input+i));\
00114 if (x & OUTPUT_ALIGNED) _mm_store_si128((__m128i *)output+i, t);\
00115 else _mm_storeu_si128((__m128i *)output+i, t);}
00116 #define CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(x, y) \
00117 switch (operation) \
00118 { \
00119 case WRITE_KEYSTREAM: \
00120 x(WRITE_KEYSTREAM) \
00121 break; \
00122 case XOR_KEYSTREAM: \
00123 x(XOR_KEYSTREAM) \
00124 input += y; \
00125 break; \
00126 case XOR_KEYSTREAM_INPUT_ALIGNED: \
00127 x(XOR_KEYSTREAM_INPUT_ALIGNED) \
00128 input += y; \
00129 break; \
00130 case XOR_KEYSTREAM_OUTPUT_ALIGNED: \
00131 x(XOR_KEYSTREAM_OUTPUT_ALIGNED) \
00132 input += y; \
00133 break; \
00134 case WRITE_KEYSTREAM_ALIGNED: \
00135 x(WRITE_KEYSTREAM_ALIGNED) \
00136 break; \
00137 case XOR_KEYSTREAM_BOTH_ALIGNED: \
00138 x(XOR_KEYSTREAM_BOTH_ALIGNED) \
00139 input += y; \
00140 break; \
00141 } \
00142 output += y;
00143
00144 template <class BASE = AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher> >
00145 class CRYPTOPP_NO_VTABLE AdditiveCipherTemplate : public BASE, public RandomNumberGenerator
00146 {
00147 public:
00148 void GenerateBlock(byte *output, size_t size);
00149 void ProcessData(byte *outString, const byte *inString, size_t length);
00150 void Resynchronize(const byte *iv, int length=-1);
00151 unsigned int OptimalBlockSize() const {return this->GetPolicy().GetOptimalBlockSize();}
00152 unsigned int GetOptimalNextBlockSize() const {return (unsigned int)this->m_leftOver;}
00153 unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
00154 bool IsSelfInverting() const {return true;}
00155 bool IsForwardTransformation() const {return true;}
00156 bool IsRandomAccess() const {return this->GetPolicy().CipherIsRandomAccess();}
00157 void Seek(lword position);
00158
00159 typedef typename BASE::PolicyInterface PolicyInterface;
00160
00161 protected:
00162 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms);
00163
00164 unsigned int GetBufferByteSize(const PolicyInterface &policy) const {return policy.GetBytesPerIteration() * policy.GetIterationsToBuffer();}
00165
00166 inline byte * KeystreamBufferBegin() {return this->m_buffer.data();}
00167 inline byte * KeystreamBufferEnd() {return (this->m_buffer.data() + this->m_buffer.size());}
00168
00169 SecByteBlock m_buffer;
00170 size_t m_leftOver;
00171 };
00172
00173 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_CipherAbstractPolicy
00174 {
00175 public:
00176 virtual ~CFB_CipherAbstractPolicy() {}
00177 virtual unsigned int GetAlignment() const =0;
00178 virtual unsigned int GetBytesPerIteration() const =0;
00179 virtual byte * GetRegisterBegin() =0;
00180 virtual void TransformRegister() =0;
00181 virtual bool CanIterate() const {return false;}
00182 virtual void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
00183 {CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(dir); CRYPTOPP_UNUSED(iterationCount);
00184 assert(false); throw Exception(Exception::OTHER_ERROR, "SimpleKeyingInterface: unexpected error");}
00185 virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length) =0;
00186 virtual void CipherResynchronize(const byte *iv, size_t length)
00187 {CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length); throw NotImplemented("SimpleKeyingInterface: this object doesn't support resynchronization");}
00188 };
00189
00190 template <typename WT, unsigned int W, class BASE = CFB_CipherAbstractPolicy>
00191 struct CRYPTOPP_NO_VTABLE CFB_CipherConcretePolicy : public BASE
00192 {
00193 typedef WT WordType;
00194
00195 unsigned int GetAlignment() const {return sizeof(WordType);}
00196 unsigned int GetBytesPerIteration() const {return sizeof(WordType) * W;}
00197 bool CanIterate() const {return true;}
00198 void TransformRegister() {this->Iterate(NULL, NULL, ENCRYPTION, 1);}
00199
00200 template <class B>
00201 struct RegisterOutput
00202 {
00203 RegisterOutput(byte *output, const byte *input, CipherDir dir)
00204 : m_output(output), m_input(input), m_dir(dir) {}
00205
00206 inline RegisterOutput& operator()(WordType ®isterWord)
00207 {
00208 assert(IsAligned<WordType>(m_output));
00209 assert(IsAligned<WordType>(m_input));
00210
00211 if (!NativeByteOrderIs(B::ToEnum()))
00212 registerWord = ByteReverse(registerWord);
00213
00214 if (m_dir == ENCRYPTION)
00215 {
00216 if (m_input == NULL)
00217 assert(m_output == NULL);
00218 else
00219 {
00220 WordType ct = *(const WordType *)m_input ^ registerWord;
00221 registerWord = ct;
00222 *(WordType*)m_output = ct;
00223 m_input += sizeof(WordType);
00224 m_output += sizeof(WordType);
00225 }
00226 }
00227 else
00228 {
00229 WordType ct = *(const WordType *)m_input;
00230 *(WordType*)m_output = registerWord ^ ct;
00231 registerWord = ct;
00232 m_input += sizeof(WordType);
00233 m_output += sizeof(WordType);
00234 }
00235
00236
00237
00238 return *this;
00239 }
00240
00241 byte *m_output;
00242 const byte *m_input;
00243 CipherDir m_dir;
00244 };
00245 };
00246
00247 template <class BASE>
00248 class CRYPTOPP_NO_VTABLE CFB_CipherTemplate : public BASE
00249 {
00250 public:
00251 void ProcessData(byte *outString, const byte *inString, size_t length);
00252 void Resynchronize(const byte *iv, int length=-1);
00253 unsigned int OptimalBlockSize() const {return this->GetPolicy().GetBytesPerIteration();}
00254 unsigned int GetOptimalNextBlockSize() const {return (unsigned int)m_leftOver;}
00255 unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
00256 bool IsRandomAccess() const {return false;}
00257 bool IsSelfInverting() const {return false;}
00258
00259 typedef typename BASE::PolicyInterface PolicyInterface;
00260
00261 protected:
00262 virtual void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length) =0;
00263
00264 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms);
00265
00266 size_t m_leftOver;
00267 };
00268
00269 template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
00270 class CRYPTOPP_NO_VTABLE CFB_EncryptionTemplate : public CFB_CipherTemplate<BASE>
00271 {
00272 bool IsForwardTransformation() const {return true;}
00273 void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length);
00274 };
00275
00276 template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
00277 class CRYPTOPP_NO_VTABLE CFB_DecryptionTemplate : public CFB_CipherTemplate<BASE>
00278 {
00279 bool IsForwardTransformation() const {return false;}
00280 void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length);
00281 };
00282
00283 template <class BASE>
00284 class CFB_RequireFullDataBlocks : public BASE
00285 {
00286 public:
00287 unsigned int MandatoryBlockSize() const {return this->OptimalBlockSize();}
00288 };
00289
00290
00291 template <class BASE, class INFO = BASE>
00292 class SymmetricCipherFinal : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
00293 {
00294 public:
00295 SymmetricCipherFinal() {}
00296 SymmetricCipherFinal(const byte *key)
00297 {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
00298 SymmetricCipherFinal(const byte *key, size_t length)
00299 {this->SetKey(key, length);}
00300 SymmetricCipherFinal(const byte *key, size_t length, const byte *iv)
00301 {this->SetKeyWithIV(key, length, iv);}
00302
00303 Clonable * Clone() const {return static_cast<SymmetricCipher *>(new SymmetricCipherFinal<BASE, INFO>(*this));}
00304 };
00305
00306 NAMESPACE_END
00307
00308 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
00309 #include "strciphr.cpp"
00310 #endif
00311
00312 NAMESPACE_BEGIN(CryptoPP)
00313 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher>;
00314 CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher> >;
00315 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
00316 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
00317 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
00318
00319 NAMESPACE_END
00320
00321 #if CRYPTOPP_MSC_VERSION
00322 # pragma warning(pop)
00323 #endif
00324
00325 #endif