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 4231 4275) 48 template <
class POLICY_INTERFACE,
class BASE = Empty>
52 typedef POLICY_INTERFACE PolicyInterface;
56 virtual const POLICY_INTERFACE & GetPolicy()
const =0;
57 virtual POLICY_INTERFACE & AccessPolicy() =0;
63 template <
class POLICY,
class BASE,
class POLICY_INTERFACE =
typename BASE::PolicyInterface>
69 const POLICY_INTERFACE & GetPolicy()
const {
return *
this;}
70 POLICY_INTERFACE & AccessPolicy() {
return *
this;}
117 virtual unsigned int GetBytesPerIteration()
const =0;
127 virtual unsigned int GetIterationsToBuffer()
const =0;
150 {CRYPTOPP_UNUSED(operation); CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input);
157 virtual void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length) =0;
164 {CRYPTOPP_UNUSED(keystreamBuffer); CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length);
165 throw NotImplemented(
"SimpleKeyingInterface: this object doesn't support resynchronization");}
170 virtual bool CipherIsRandomAccess()
const =0;
175 {CRYPTOPP_UNUSED(iterationCount);
CRYPTOPP_ASSERT(!CipherIsRandomAccess());
176 throw NotImplemented(
"StreamTransformation: this object doesn't support random access");}
200 template <
typename WT,
unsigned int W,
unsigned int X = 1,
class BASE = AdditiveCipherAbstractPolicy>
211 CRYPTOPP_CONSTANT(BYTES_PER_ITERATION =
sizeof(
WordType) * W);
253 #define CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, b, i, a) \ 254 PutWord(((x & OUTPUT_ALIGNED) != 0), b, output+i*sizeof(WordType), (x & INPUT_NULL) ? (a) : (a) ^ GetWord<WordType>(((x & INPUT_ALIGNED) != 0), b, input+i*sizeof(WordType))); 260 #define CRYPTOPP_KEYSTREAM_OUTPUT_XMM(x, i, a) {\ 261 __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));\ 262 if (x & OUTPUT_ALIGNED) _mm_store_si128((__m128i *)output+i, t);\ 263 else _mm_storeu_si128((__m128i *)output+i, t);} 266 #define CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(x, y) \ 269 case WRITE_KEYSTREAM: \ 270 x(EnumToInt(WRITE_KEYSTREAM)) \ 272 case XOR_KEYSTREAM: \ 273 x(EnumToInt(XOR_KEYSTREAM)) \ 276 case XOR_KEYSTREAM_INPUT_ALIGNED: \ 277 x(EnumToInt(XOR_KEYSTREAM_INPUT_ALIGNED)) \ 280 case XOR_KEYSTREAM_OUTPUT_ALIGNED: \ 281 x(EnumToInt(XOR_KEYSTREAM_OUTPUT_ALIGNED)) \ 284 case WRITE_KEYSTREAM_ALIGNED: \ 285 x(EnumToInt(WRITE_KEYSTREAM_ALIGNED)) \ 287 case XOR_KEYSTREAM_BOTH_ALIGNED: \ 288 x(EnumToInt(XOR_KEYSTREAM_BOTH_ALIGNED)) \ 296 template <
class BASE = AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher> >
323 void ProcessData(
byte *outString,
const byte *inString,
size_t length);
328 void Resynchronize(
const byte *iv,
int length=-1);
363 void Seek(
lword position);
381 typedef typename BASE::PolicyInterface PolicyInterface;
384 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
386 unsigned int GetBufferByteSize(
const PolicyInterface &policy)
const {
return policy.GetBytesPerIteration() * policy.GetIterationsToBuffer();}
388 inline byte * KeystreamBufferBegin() {
return this->m_buffer.data();}
389 inline byte * KeystreamBufferEnd() {
return (
PtrAdd(this->m_buffer.data(), this->m_buffer.size()));}
406 virtual unsigned int GetAlignment()
const =0;
411 virtual unsigned int GetBytesPerIteration()
const =0;
415 virtual byte * GetRegisterBegin() =0;
418 virtual void TransformRegister() =0;
431 {CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(dir);
439 virtual void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length) =0;
445 {CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length);
446 throw NotImplemented(
"SimpleKeyingInterface: this object doesn't support resynchronization");}
469 template <
typename WT,
unsigned int W,
class BASE = CFB_CipherAbstractPolicy>
503 : m_output(output), m_input(input), m_dir(dir) {}
518 if (m_input == NULLPTR)
525 WordType ct = GetWord<WordType>(
false, NativeByteOrder::ToEnum(), m_input) ^ registerWord;
529 PutWord<WordType>(
false, NativeByteOrder::ToEnum(), m_output, ct);
531 m_input +=
sizeof(WordType);
532 m_output +=
sizeof(WordType);
538 WordType ct = GetWord<WordType>(
false, NativeByteOrder::ToEnum(), m_input);
541 PutWord<WordType>(
false, NativeByteOrder::ToEnum(), m_output, registerWord ^ ct);
544 m_input +=
sizeof(WordType);
545 m_output +=
sizeof(WordType);
561 template <
class BASE>
581 void ProcessData(
byte *outString,
const byte *inString,
size_t length);
586 void Resynchronize(
const byte *iv,
int length=-1);
630 typedef typename BASE::PolicyInterface PolicyInterface;
633 virtual void CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length) =0;
635 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
644 template <
class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
647 bool IsForwardTransformation()
const {
return true;}
648 void CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length);
653 template <
class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
656 bool IsForwardTransformation()
const {
return false;}
657 void CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length);
662 template <
class BASE>
666 unsigned int MandatoryBlockSize()
const {
return this->OptimalBlockSize();}
673 template <
class BASE,
class INFO = BASE>
686 {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
692 {this->SetKey(key, length);}
699 {this->SetKeyWithIV(key, length, iv);}
709 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES 710 # include "strciphr.cpp" 724 #if CRYPTOPP_MSC_VERSION 725 # pragma warning(pop) Base class for all exceptions thrown by the library.
unsigned int GetOptimalNextBlockSize() const
Provides number of ideal bytes to process.
Standard names for retrieving values by name when working with NameValuePairs.
bool CanOperateKeystream() const
Flag indicating.
bool NativeByteOrderIs(ByteOrder order)
Determines whether order follows native byte ordering.
Output buffer is aligned.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
SymmetricCipherFinal(const byte *key)
Construct a stream cipher.
unsigned int OptimalBlockSize() const
Provides number of ideal bytes to process.
bool IsRandomAccess() const
Flag indicating random access.
Base class for feedback based stream ciphers.
Base class for additive stream ciphers.
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
Operates the keystream.
unsigned int GetOptimalNextBlockSize() const
Provides number of ideal bytes to process.
XOR the input buffer and keystream, write to the aligned output buffer.
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
XOR the input buffer and keystream, write to the output buffer.
CipherDir
Specifies a direction for a cipher to operate.
Abstract base classes that provide a uniform interface to this library.
Base class for feedback based stream ciphers with SymmetricCipher interface.
unsigned int OptimalDataAlignment() const
Provides number of ideal data alignment.
Some other error occurred not belonging to other categories.
Library configuration file.
virtual unsigned int GetOptimalBlockSize() const
Provides number of ideal bytes to process.
Interface for random number generators.
unsigned int GetAlignment() const
Provides data alignment requirements.
Wirte the keystream to the output buffer, input is NULL.
Stream cipher policy object.
virtual bool CanOperateKeystream() const
Flag indicating.
Interface for cloning objects.
virtual void WriteKeystream(byte *keystream, size_t iterationCount)
Generate the keystream.
Policy object for additive stream ciphers.
the cipher is performing encryption
Classes and functions for secure memory allocations.
void TransformRegister()
Perform one iteration in the forward direction.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
Base class for feedback based stream ciphers in the reverse direction with SymmetricCipher interface...
bool IsSelfInverting() const
Determines if the cipher is self inverting.
Provides alternate access to a feedback register.
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
A method was called which was not implemented.
virtual unsigned int GetAlignment() const
Provides data alignment requirements.
bool CanIterate() const
Flag indicating iteration support.
RegisterOutput & operator()(WordType ®isterWord)
XOR feedback register with data.
Classes and functions for implementing secret key algorithms.
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
virtual void SeekToIteration(lword iterationCount)
Seeks to a random position in the stream.
unsigned int OptimalBlockSize() const
Provides number of ideal bytes to process.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
Base class for feedback based stream ciphers with a mandatory block size.
Policy object for feeback based stream ciphers.
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
SecBlock using AllocatorWithCleanup<byte, true> typedef.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface...
SymmetricCipherFinal()
Construct a stream cipher.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
bool IsSelfInverting() const
Determines if the cipher is self inverting.
bool IsRandomAccess() const
Flag indicating random access.
word64 lword
Large word type.
unsigned char byte
8-bit unsigned datatype
PTR PtrAdd(PTR pointer, OFF offset)
Create a pointer with an offset.
unsigned int OptimalDataAlignment() const
Provides number of ideal data alignment.
XOR the aligned input buffer and keystream, write to the aligned output buffer.
virtual bool CanIterate() const
Flag indicating iteration support.
SymmetricCipherFinal(const byte *key, size_t length, const byte *iv)
Construct a stream cipher.
Clonable * Clone() const
Clone a SymmetricCipher.
virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
virtual void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
Iterate the cipher.
unsigned int GetAlignment() const
Provides data alignment requirements.
KeystreamOperation
Keystream operation flags.
SymmetricCipherFinal(const byte *key, size_t length)
Construct a stream cipher.
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
virtual void CipherResynchronize(const byte *iv, size_t length)
Resynchronize the cipher.
Crypto++ library namespace.
WT WordType
Word type for the cipher.
bool IsForwardTransformation() const
Determines if the cipher is a forward transformation.
SymmetricCipher implementation.
Wirte the keystream to the aligned output buffer, input is NULL.
unsigned int GetIterationsToBuffer() const
Provides buffer size based on iterations.
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
Base class for additive stream ciphers with SymmetricCipher interface.
Access a stream cipher policy object.
XOR the aligned input buffer and keystream, write to the output buffer.
KeystreamOperationFlags
Keystream operation flags.
Interface for retrieving values given their names.