6#ifndef CRYPTOPP_MODES_H
7#define CRYPTOPP_MODES_H
17#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
18# pragma GCC diagnostic push
19# pragma GCC diagnostic ignored "-Wconversion"
20# pragma GCC diagnostic ignored "-Wsign-conversion"
23#if CRYPTOPP_MSC_VERSION
25# pragma warning(disable: 4231 4275)
26# if (CRYPTOPP_MSC_VERSION >= 1400)
27# pragma warning(disable: 6011 6386 28193)
31NAMESPACE_BEGIN(CryptoPP)
56 return m_cipher != NULLPTR ? m_cipher->AlgorithmProvider() :
"C++";
79 size_t GetValidKeyLength(
size_t keylength)
const {
return m_cipher->GetValidKeyLength(keylength);}
85 bool IsValidKeyLength(
size_t keylength)
const {
return m_cipher->IsValidKeyLength(keylength);}
96 unsigned int IVSize()
const {
return BlockSize();}
108 this->m_cipher = &cipher;
109 this->ResizeBuffers();
120 this->m_cipher = &cipher;
121 this->ResizeBuffers();
122 this->SetFeedbackSize(feedbackSize);
129 inline unsigned int BlockSize()
const
131 CRYPTOPP_ASSERT(m_register.size() > 0);
132 return static_cast<unsigned int>(m_register.size());
134 virtual void SetFeedbackSize(
unsigned int feedbackSize)
136 if (!(feedbackSize == 0 || feedbackSize == BlockSize()))
137 throw InvalidArgument(
"CipherModeBase: feedback size cannot be specified for this cipher mode");
140 virtual void ResizeBuffers();
148template <
class POLICY_INTERFACE>
152 void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length);
155template <
class POLICY_INTERFACE>
158 m_cipher->
SetKey(key, length, params);
161 SetFeedbackSize(feedbackSize);
168 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CFB";}
176 bool CanIterate()
const {
return m_feedbackSize == BlockSize();}
177 void Iterate(
byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount);
178 void TransformRegister();
179 void CipherResynchronize(
const byte *iv,
size_t length);
180 void SetFeedbackSize(
unsigned int feedbackSize);
181 void ResizeBuffers();
182 byte * GetRegisterBegin();
185 unsigned int m_feedbackSize;
196inline void CopyOrZero(
void *dest,
size_t dsize,
const void *src,
size_t ssize)
198 CRYPTOPP_ASSERT(dest);
199 CRYPTOPP_ASSERT(dsize >= ssize);
204 memset(dest, 0, dsize);
211 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "OFB";}
219 void WriteKeystream(
byte *keystreamBuffer,
size_t iterationCount);
220 void CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length);
227 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CTR";}
234 virtual void IncrementCounterBy256();
235 unsigned int GetAlignment()
const {
return m_cipher->OptimalDataAlignment();}
241 void OperateKeystream(
KeystreamOperation operation,
byte *output,
const byte *input,
size_t iterationCount);
242 void CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length);
243 void SeekToIteration(
lword iterationCount);
259 {
return m_cipher->IsForwardTransformation();}
261 {
memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
264 bool RequireAlignedInput()
const {
return true;}
265 virtual void ResizeBuffers();
274 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "ECB";}
277 {m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
279 unsigned int OptimalBlockSize()
const {
return static_cast<unsigned int>(BlockSize() * m_cipher->OptimalNumberOfParallelBlocks());}
280 void ProcessData(
byte *outString,
const byte *inString,
size_t length);
287 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CBC";}
290 bool RequireAlignedInput()
const {
return false;}
298 void ProcessData(
byte *outString,
const byte *inString,
size_t length);
306 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CBC/CTS";}
308 void SetStolenIV(
byte *iv) {m_stolenIV = iv;}
310 size_t ProcessLastBlock(
byte *outString,
size_t outLength,
const byte *inString,
size_t inLength);
327 void ProcessData(
byte *outString,
const byte *inString,
size_t length);
330 virtual void ResizeBuffers();
341 size_t ProcessLastBlock(
byte *outString,
size_t outLength,
const byte *inString,
size_t inLength);
345template <
class CIPHER,
class BASE>
355 {
return CIPHER::StaticAlgorithmName() +
"/" + BASE::StaticAlgorithmName();}
360 this->m_cipher = &this->m_object;
361 this->ResizeBuffers();
371 this->m_cipher = &this->m_object;
372 this->SetKey(key, length);
383 this->m_cipher = &this->m_object;
396 this->m_cipher = &this->m_object;
401 std::string AlgorithmProvider()
const {
402 return this->m_cipher->AlgorithmProvider();
420 {this->SetCipher(cipher);}
428 {this->SetCipherWithIV(cipher, iv, feedbackSize);}
437 {
return (this->m_cipher ? this->m_cipher->AlgorithmName() +
"/" : std::string(
"")) + BASE::StaticAlgorithmName();}
440 std::string AlgorithmProvider()
const
441 {
return this->m_cipher->AlgorithmProvider();}
451template <
class CIPHER>
471template <
class CIPHER>
493template <
class CIPHER>
515template <
class CIPHER>
534template <
class CIPHER>
555template <
class CIPHER>
578template <
class CIPHER>
601#if CRYPTOPP_MSC_VERSION
605#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
606# pragma GCC diagnostic pop
Classes for working with NameValuePairs.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Standard names for retrieving values by name when working with NameValuePairs.
Base class for additive stream ciphers with SymmetricCipher interface.
Interface for one direction (encryption or decryption) of a block cipher.
Block cipher mode of operation default implementation.
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
bool IsSelfInverting() const
Determines whether the cipher is self-inverting.
bool IsRandomAccess() const
Determines whether the cipher supports random access.
unsigned int MandatoryBlockSize() const
Provides the mandatory block size of the cipher.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
CBC-CTS block cipher mode of operation decryption operation.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
CBC-CTS block cipher mode of operation encryption operation.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
Sets the key for this object without performing parameter validation.
CBC block cipher mode of operation decryption operation.
CBC block cipher mode of operation encryption operation.
CBC block cipher mode of operation default implementation.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
Base class for feedback based stream ciphers with SymmetricCipher interface.
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.
CFB block cipher mode of operation.
bool CanIterate() const
Flag indicating iteration support.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
CTR block cipher mode of operation.
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
unsigned int GetAlignment() const
Provides data alignment requirements.
unsigned int GetIterationsToBuffer() const
Provides buffer size based on iterations.
bool CipherIsRandomAccess() const
Flag indicating random access.
void WriteKeystream(byte *buffer, size_t iterationCount)
Generate the keystream.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
bool CanOperateKeystream() const
Flag indicating.
Block cipher mode of operation information.
unsigned int IVSize() const
Returns length of the IV accepted by this object.
size_t GetValidKeyLength(size_t keylength) const
Returns a valid key length for the algorithm.
void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Set external block cipher and IV.
size_t MaxKeyLength() const
Returns largest valid key length.
size_t DefaultKeyLength() const
Returns default key length.
void SetCipher(BlockCipher &cipher)
Set external block cipher.
virtual IV_Requirement IVRequirement() const =0
Minimal requirement for secure IVs.
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
size_t MinKeyLength() const
Returns smallest valid key length.
bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
Block cipher mode of operation aggregate.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
Construct a CipherModeFinalTemplate.
CipherModeFinalTemplate_CipherHolder()
Construct a CipherModeFinalTemplate.
static std::string CRYPTOPP_API StaticAlgorithmName()
Provides the name of this algorithm.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
Construct a CipherModeFinalTemplate.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
Construct a CipherModeFinalTemplate.
CipherModeFinalTemplate_ExternalCipher()
Construct a default CipherModeFinalTemplate.
std::string AlgorithmName() const
Provides the name of this algorithm.
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
Construct a CipherModeFinalTemplate.
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Construct a CipherModeFinalTemplate.
Used to pass byte array input as part of a NameValuePairs object.
ECB block cipher mode of operation default implementation.
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
An invalid argument was detected.
Block cipher mode of operation common operations.
Interface for retrieving values given their names.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
OFB block cipher mode of operation.
unsigned int GetIterationsToBuffer() const
Provides buffer size based on iterations.
unsigned int GetBytesPerIteration() const
Provides number of bytes operated upon during an iteration.
bool CipherIsRandomAccess() const
Flag indicating random access.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Uses encapsulation to hide an object in derived classes.
bool IsResynchronizable() const
Determines if the object can be resynchronized.
IV_Requirement
Secure IVs requirements as enumerated values.
@ UNIQUE_IV
The IV must be unique.
@ RANDOM_IV
The IV must be random and possibly predictable.
@ NOT_RESYNCHRONIZABLE
The object does not use an IV.
@ UNPREDICTABLE_RANDOM_IV
The IV must be random and unpredictable.
virtual void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)=0
Sets the key for this object without performing parameter validation.
void ThrowIfInvalidIV(const byte *iv)
Validates the IV.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
virtual void Resynchronize(const byte *iv, int ivLength=-1)
Resynchronize with an IV.
void ThrowIfResynchronizable()
Validates the object.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
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.
Utility functions for the Crypto++ library.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
void CopyOrZero(void *dest, size_t dsize, const void *src, size_t ssize)
Initialize a block of memory.
Classes and functions for secure memory allocations.
Classes for implementing stream ciphers.
KeystreamOperation
Keystream operation flags.
@ WRITE_KEYSTREAM
Write the keystream to the output buffer, input is NULL.
CBC mode with ciphertext stealing, external cipher.
CBC-CTS block cipher mode of operation.
CBC mode, external cipher.
CBC block cipher mode of operation.
CFB mode, external cipher, providing FIPS validated cryptography.
CFB block cipher mode of operation providing FIPS validated cryptography.
CFB mode, external cipher.
CFB block cipher mode of operation.
CTR mode, external cipher.
CTR block cipher mode of operation.
Block cipher mode of operation information.
ECB mode, external cipher.
ECB block cipher mode of operation.
OFB mode, external cipher.
OFB block cipher mode of operation.
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.