28 #ifndef CRYPTOPP_STRCIPHR_H 29 #define CRYPTOPP_STRCIPHR_H 33 #if CRYPTOPP_MSC_VERSION 34 # pragma warning(push) 35 # pragma warning(disable: 4127 4189) 45 template <class POLICY_INTERFACE, class BASE = Empty>
49 typedef POLICY_INTERFACE PolicyInterface;
53 virtual const POLICY_INTERFACE & GetPolicy()
const =0;
54 virtual POLICY_INTERFACE & AccessPolicy() =0;
57 template <
class POLICY,
class BASE,
class POLICY_INTERFACE = CPP_TYPENAME BASE::PolicyInterface>
61 const POLICY_INTERFACE & GetPolicy()
const {
return *
this;}
62 POLICY_INTERFACE & AccessPolicy() {
return *
this;}
65 enum KeystreamOperationFlags {OUTPUT_ALIGNED=1, INPUT_ALIGNED=2, INPUT_NULL = 4};
66 enum KeystreamOperation {
67 WRITE_KEYSTREAM = INPUT_NULL,
68 WRITE_KEYSTREAM_ALIGNED = INPUT_NULL | OUTPUT_ALIGNED,
70 XOR_KEYSTREAM_INPUT_ALIGNED = INPUT_ALIGNED,
71 XOR_KEYSTREAM_OUTPUT_ALIGNED= OUTPUT_ALIGNED,
72 XOR_KEYSTREAM_BOTH_ALIGNED = OUTPUT_ALIGNED | INPUT_ALIGNED};
77 virtual unsigned int GetAlignment()
const {
return 1;}
78 virtual unsigned int GetBytesPerIteration()
const =0;
79 virtual unsigned int GetOptimalBlockSize()
const {
return GetBytesPerIteration();}
80 virtual unsigned int GetIterationsToBuffer()
const =0;
81 virtual void WriteKeystream(byte *keystream,
size_t iterationCount)
82 {OperateKeystream(KeystreamOperation(INPUT_NULL | (KeystreamOperationFlags)
IsAlignedOn(keystream, GetAlignment())), keystream, NULL, iterationCount);}
83 virtual bool CanOperateKeystream()
const {
return false;}
84 virtual void OperateKeystream(KeystreamOperation operation, byte *output,
const byte *input,
size_t iterationCount)
85 {CRYPTOPP_UNUSED(operation); CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(iterationCount); assert(
false);}
86 virtual void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length) =0;
87 virtual void CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length)
88 {CRYPTOPP_UNUSED(keystreamBuffer); CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length);
throw NotImplemented(
"SimpleKeyingInterface: this object doesn't support resynchronization");}
89 virtual bool CipherIsRandomAccess()
const =0;
90 virtual void SeekToIteration(lword iterationCount)
91 {CRYPTOPP_UNUSED(iterationCount); assert(!CipherIsRandomAccess());
throw NotImplemented(
"StreamTransformation: this object doesn't support random access");}
94 template <
typename WT,
unsigned int W,
unsigned int X = 1,
class BASE = AdditiveCipherAbstractPolicy>
98 CRYPTOPP_CONSTANT(BYTES_PER_ITERATION =
sizeof(WordType) * W)
100 #if !(CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64) 101 unsigned int GetAlignment()
const {
return GetAlignmentOf<WordType>();}
103 unsigned int GetBytesPerIteration()
const {
return BYTES_PER_ITERATION;}
104 unsigned int GetIterationsToBuffer()
const {
return X;}
105 bool CanOperateKeystream()
const {
return true;}
106 virtual void OperateKeystream(KeystreamOperation operation, byte *output,
const byte *input,
size_t iterationCount) =0;
110 #define CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, b, i, a) \ 111 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))); 112 #define CRYPTOPP_KEYSTREAM_OUTPUT_XMM(x, i, a) {\ 113 __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));\ 114 if (x & OUTPUT_ALIGNED) _mm_store_si128((__m128i *)output+i, t);\ 115 else _mm_storeu_si128((__m128i *)output+i, t);} 116 #define CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(x, y) \ 119 case WRITE_KEYSTREAM: \ 122 case XOR_KEYSTREAM: \ 126 case XOR_KEYSTREAM_INPUT_ALIGNED: \ 127 x(XOR_KEYSTREAM_INPUT_ALIGNED) \ 130 case XOR_KEYSTREAM_OUTPUT_ALIGNED: \ 131 x(XOR_KEYSTREAM_OUTPUT_ALIGNED) \ 134 case WRITE_KEYSTREAM_ALIGNED: \ 135 x(WRITE_KEYSTREAM_ALIGNED) \ 137 case XOR_KEYSTREAM_BOTH_ALIGNED: \ 138 x(XOR_KEYSTREAM_BOTH_ALIGNED) \ 144 template <
class BASE = AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher> >
148 void GenerateBlock(byte *output,
size_t size);
149 void ProcessData(byte *outString,
const byte *inString,
size_t length);
150 void Resynchronize(
const byte *iv,
int length=-1);
151 unsigned int OptimalBlockSize()
const {
return this->GetPolicy().GetOptimalBlockSize();}
152 unsigned int GetOptimalNextBlockSize()
const {
return (
unsigned int)this->m_leftOver;}
153 unsigned int OptimalDataAlignment()
const {
return this->GetPolicy().GetAlignment();}
154 bool IsSelfInverting()
const {
return true;}
155 bool IsForwardTransformation()
const {
return true;}
156 bool IsRandomAccess()
const {
return this->GetPolicy().CipherIsRandomAccess();}
157 void Seek(lword position);
159 typedef typename BASE::PolicyInterface PolicyInterface;
162 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
164 unsigned int GetBufferByteSize(
const PolicyInterface &policy)
const {
return policy.GetBytesPerIteration() * policy.GetIterationsToBuffer();}
166 inline byte * KeystreamBufferBegin() {
return this->m_buffer.data();}
167 inline byte * KeystreamBufferEnd() {
return (this->m_buffer.data() + this->m_buffer.size());}
177 virtual unsigned int GetAlignment()
const =0;
178 virtual unsigned int GetBytesPerIteration()
const =0;
179 virtual byte * GetRegisterBegin() =0;
180 virtual void TransformRegister() =0;
181 virtual bool CanIterate()
const {
return false;}
182 virtual void Iterate(byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount)
183 {CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(dir); CRYPTOPP_UNUSED(iterationCount);
185 virtual void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length) =0;
186 virtual void CipherResynchronize(
const byte *iv,
size_t length)
187 {CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length);
throw NotImplemented(
"SimpleKeyingInterface: this object doesn't support resynchronization");}
190 template <
typename WT,
unsigned int W,
class BASE = CFB_CipherAbstractPolicy>
195 unsigned int GetAlignment()
const {
return sizeof(WordType);}
196 unsigned int GetBytesPerIteration()
const {
return sizeof(WordType) * W;}
197 bool CanIterate()
const {
return true;}
198 void TransformRegister() {this->Iterate(NULL, NULL, ENCRYPTION, 1);}
204 : m_output(output), m_input(input), m_dir(dir) {}
208 assert(IsAligned<WordType>(m_output));
209 assert(IsAligned<WordType>(m_input));
214 if (m_dir == ENCRYPTION)
217 assert(m_output == NULL);
220 WordType ct = *(
const WordType *)m_input ^ registerWord;
222 *(WordType*)m_output = ct;
223 m_input +=
sizeof(WordType);
224 m_output +=
sizeof(WordType);
229 WordType ct = *(
const WordType *)m_input;
230 *(WordType*)m_output = registerWord ^ ct;
232 m_input +=
sizeof(WordType);
233 m_output +=
sizeof(WordType);
247 template <
class BASE>
251 void ProcessData(byte *outString,
const byte *inString,
size_t length);
252 void Resynchronize(
const byte *iv,
int length=-1);
253 unsigned int OptimalBlockSize()
const {
return this->GetPolicy().GetBytesPerIteration();}
254 unsigned int GetOptimalNextBlockSize()
const {
return (
unsigned int)m_leftOver;}
255 unsigned int OptimalDataAlignment()
const {
return this->GetPolicy().GetAlignment();}
256 bool IsRandomAccess()
const {
return false;}
257 bool IsSelfInverting()
const {
return false;}
259 typedef typename BASE::PolicyInterface PolicyInterface;
262 virtual void CombineMessageAndShiftRegister(byte *output, byte *reg,
const byte *message,
size_t length) =0;
264 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
269 template <
class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
272 bool IsForwardTransformation()
const {
return true;}
273 void CombineMessageAndShiftRegister(byte *output, byte *reg,
const byte *message,
size_t length);
276 template <
class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
279 bool IsForwardTransformation()
const {
return false;}
280 void CombineMessageAndShiftRegister(byte *output, byte *reg,
const byte *message,
size_t length);
283 template <
class BASE>
287 unsigned int MandatoryBlockSize()
const {
return this->OptimalBlockSize();}
291 template <
class BASE,
class INFO = BASE>
297 {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
299 {this->SetKey(key, length);}
301 {this->SetKeyWithIV(key, length, iv);}
308 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES 309 #include "strciphr.cpp" 321 #if CRYPTOPP_MSC_VERSION 322 # pragma warning(pop) Base class for all exceptions thrown by Crypto++.
Standard names for retrieving values by name when working with NameValuePairs.
bool NativeByteOrderIs(ByteOrder order)
Determines whether order follows native byte ordering.
CipherDir
Specifies a direction for a cipher to operate.
Abstract base classes that provide a uniform interface to this library.
Some other error occurred not belong to any of the above categories.
Library configuration file.
Interface for random number generators.
SecByteBlock is a SecBlock<byte> typedef.
Interface for cloning objects.
Classes and functions for secure memory allocations.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
A method was called which was not implemented.
Classes and functions for implementing secret key algorithms.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Crypto++ library namespace.
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
Interface for retrieving values given their names.
Base class for identifying alogorithm.