28#ifndef CRYPTOPP_STRCIPHR_H
29#define CRYPTOPP_STRCIPHR_H
33#if CRYPTOPP_MSC_VERSION
35# pragma warning(disable: 4127 4189 4231 4275)
48template <
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;
63template <
class POLICY,
class BASE,
class POLICY_INTERFACE =
typename BASE::PolicyInterface>
69 const POLICY_INTERFACE & GetPolicy()
const {
return *
this;}
70 POLICY_INTERFACE & AccessPolicy() {
return *
this;}
150 {CRYPTOPP_UNUSED(operation); CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input);
164 {CRYPTOPP_UNUSED(keystreamBuffer); CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length);
165 throw NotImplemented(
"SimpleKeyingInterface: this object doesn't support resynchronization");}
175 {CRYPTOPP_UNUSED(iterationCount);
CRYPTOPP_ASSERT(!CipherIsRandomAccess());
176 throw NotImplemented(
"StreamTransformation: this object doesn't support random access");}
200template <
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)) \
296template <
class BASE = AbstractPolicyHolder<AdditiveCipherAbstractPolicy, SymmetricCipher> >
329 void ProcessData(
byte *outString,
const byte *inString,
size_t length);
387 typedef typename BASE::PolicyInterface PolicyInterface;
390 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
392 unsigned int GetBufferByteSize(
const PolicyInterface &policy)
const {
return policy.GetBytesPerIteration() * policy.GetIterationsToBuffer();}
394 inline byte * KeystreamBufferBegin() {
return this->m_buffer.data();}
395 inline byte * KeystreamBufferEnd() {
return (
PtrAdd(this->m_buffer.data(), this->m_buffer.size()));}
435 virtual void Iterate(
byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount)
436 {CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(dir);
450 {CRYPTOPP_UNUSED(iv); CRYPTOPP_UNUSED(length);
451 throw NotImplemented(
"SimpleKeyingInterface: this object doesn't support resynchronization");}
474template <
typename WT,
unsigned int W,
class BASE = CFB_CipherAbstractPolicy>
508 : m_output(output), m_input(input), m_dir(dir) {}
523 if (m_input == NULLPTR)
530 WordType ct = GetWord<WordType>(
false, NativeByteOrder::ToEnum(), m_input) ^ registerWord;
534 PutWord<WordType>(
false, NativeByteOrder::ToEnum(), m_output, ct);
536 m_input +=
sizeof(WordType);
537 m_output +=
sizeof(WordType);
543 WordType ct = GetWord<WordType>(
false, NativeByteOrder::ToEnum(), m_input);
546 PutWord<WordType>(
false, NativeByteOrder::ToEnum(), m_output, registerWord ^ ct);
549 m_input +=
sizeof(WordType);
550 m_output +=
sizeof(WordType);
592 void ProcessData(
byte *outString,
const byte *inString,
size_t length);
641 typedef typename BASE::PolicyInterface PolicyInterface;
644 virtual void CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length) =0;
646 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
653template <
class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
656 bool IsForwardTransformation()
const {
return true;}
657 void CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length);
662template <
class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
665 bool IsForwardTransformation()
const {
return false;}
666 void CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length);
675 unsigned int MandatoryBlockSize()
const {
return this->OptimalBlockSize();}
682template <
class BASE,
class INFO = BASE>
695 {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
701 {this->SetKey(key, length);}
708 {this->SetKeyWithIV(key, length, iv);}
718#ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
719# include "strciphr.cpp"
733#if CRYPTOPP_MSC_VERSION
Standard names for retrieving values by name when working with NameValuePairs.
Access a stream cipher policy object.
Base class for additive stream ciphers with SymmetricCipher interface.
unsigned int OptimalBlockSize() const
Provides number of ideal bytes to process.
void ProcessData(byte *outString, const byte *inString, size_t length)
Apply keystream to data.
bool IsRandomAccess() const
Flag indicating random access.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
unsigned int GetOptimalNextBlockSize() const
Provides number of ideal bytes to process.
void Seek(lword position)
Seeks to a random position in the stream.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
bool IsSelfInverting() const
Determines if the cipher is self inverting.
bool IsForwardTransformation() const
Determines if the cipher is a forward transformation.
unsigned int OptimalDataAlignment() const
Provides number of ideal data alignment.
SecBlock using AllocatorWithCleanup<byte, true> typedef.
Policy object for feedback based stream ciphers.
virtual void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
Iterate the cipher.
virtual unsigned int GetAlignment() const =0
Provides data alignment requirements.
virtual unsigned int GetBytesPerIteration() const =0
Provides number of bytes operated upon during an iteration.
virtual byte * GetRegisterBegin()=0
Access the feedback register.
virtual void TransformRegister()=0
TODO.
virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)=0
Key the cipher.
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
virtual void CipherResynchronize(const byte *iv, size_t length)
Resynchronize the cipher.
virtual bool CanIterate() const
Flag indicating iteration support.
Base class for feedback based stream ciphers with SymmetricCipher interface.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
unsigned int OptimalBlockSize() const
Provides number of ideal bytes to process.
unsigned int OptimalDataAlignment() const
Provides number of ideal data alignment.
bool IsRandomAccess() const
Flag indicating random access.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
unsigned int GetOptimalNextBlockSize() const
Provides number of ideal bytes to process.
bool IsSelfInverting() const
Determines if the cipher is self inverting.
void ProcessData(byte *outString, const byte *inString, size_t length)
Apply keystream to data.
Base class for feedback based stream ciphers in the reverse direction with SymmetricCipher interface.
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface.
Base class for feedback based stream ciphers with a mandatory block size.
Interface for cloning objects.
Stream cipher policy object.
Base class for all exceptions thrown by the library.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Interface for retrieving values given their names.
A method was called which was not implemented.
Interface for random number generators.
SymmetricCipher implementation.
Clonable * Clone() const
Clone a SymmetricCipher.
SymmetricCipherFinal()
Construct a stream cipher.
SymmetricCipherFinal(const byte *key, size_t length, const byte *iv)
Construct a stream cipher.
SymmetricCipherFinal(const byte *key, size_t length)
Construct a stream cipher.
SymmetricCipherFinal(const byte *key)
Construct a stream cipher.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
Library configuration file.
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
word64 lword
Large word type.
Abstract base classes that provide a uniform interface to this library.
CipherDir
Specifies a direction for a cipher to operate.
@ ENCRYPTION
the cipher is performing encryption
byte ByteReverse(byte value)
Reverses bytes in a 8-bit value.
bool NativeByteOrderIs(ByteOrder order)
Determines whether order follows native byte ordering.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
PTR PtrAdd(PTR pointer, OFF offset)
Create a pointer with an offset.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
Classes and functions for implementing secret key algorithms.
KeystreamOperation
Keystream operation flags.
@ XOR_KEYSTREAM
XOR the input buffer and keystream, write to the output buffer.
@ WRITE_KEYSTREAM
Write the keystream to the output buffer, input is NULL.
@ XOR_KEYSTREAM_BOTH_ALIGNED
XOR the aligned input buffer and keystream, write to the aligned output buffer.
@ XOR_KEYSTREAM_INPUT_ALIGNED
XOR the aligned input buffer and keystream, write to the output buffer.
@ XOR_KEYSTREAM_OUTPUT_ALIGNED
XOR the input buffer and keystream, write to the aligned output buffer.
@ WRITE_KEYSTREAM_ALIGNED
Write the keystream to the aligned output buffer, input is NULL.
KeystreamOperationFlags
Keystream operation flags.
@ INPUT_NULL
Input buffer is NULL.
@ INPUT_ALIGNED
Input buffer is aligned.
@ OUTPUT_ALIGNED
Output buffer is aligned.
Policy object for additive stream ciphers.
virtual bool CanOperateKeystream() const
Flag indicating.
virtual std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
virtual unsigned int GetAlignment() const
Provides data alignment requirements.
virtual unsigned int GetOptimalBlockSize() const
Provides number of ideal bytes to process.
virtual unsigned int GetBytesPerIteration() const =0
Provides number of bytes operated upon during an iteration.
virtual void SeekToIteration(lword iterationCount)
Seeks to a random position in the stream.
virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)=0
Key the cipher.
virtual unsigned int GetIterationsToBuffer() const =0
Provides buffer size based on iterations.
virtual bool CipherIsRandomAccess() const =0
Flag indicating random access.
virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
Operates the keystream.
virtual void WriteKeystream(byte *keystream, size_t iterationCount)
Generate the keystream.
Base class for additive stream ciphers.
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
bool CanOperateKeystream() const
Flag indicating.
unsigned int GetAlignment() const
Provides data alignment requirements.
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)=0
Operates the keystream.
WT WordType
Word type for the cipher.
unsigned int GetIterationsToBuffer() const
Provides buffer size based on iterations.
Provides alternate access to a feedback register.
RegisterOutput & operator()(WordType ®isterWord)
XOR feedback register with data.
Base class for feedback based stream ciphers.
unsigned int GetAlignment() const
Provides data alignment requirements.
bool CanIterate() const
Flag indicating iteration support.
void TransformRegister()
Perform one iteration in the forward direction.
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.