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 24 # pragma warning(push) 25 # pragma warning(disable: 4231 4275) 26 # if (CRYPTOPP_MSC_VERSION >= 1400) 27 # pragma warning(disable: 6011 6386 28193) 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);}
107 this->ThrowIfResynchronizable();
108 this->m_cipher = &cipher;
109 this->ResizeBuffers();
119 this->ThrowIfInvalidIV(iv);
120 this->m_cipher = &cipher;
121 this->ResizeBuffers();
122 this->SetFeedbackSize(feedbackSize);
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();
148 template <
class POLICY_INTERFACE>
152 void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length);
155 template <
class POLICY_INTERFACE>
158 m_cipher->
SetKey(key, length, params);
161 SetFeedbackSize(feedbackSize);
168 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CFB";}
175 unsigned int GetBytesPerIteration()
const {
return m_feedbackSize;}
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;
196 inline void CopyOrZero(
void *dest,
size_t dsize,
const void *src,
size_t ssize)
204 memset(dest, 0, dsize);
211 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "OFB";}
217 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
218 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
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();}
236 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
237 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
238 void WriteKeystream(byte *buffer,
size_t iterationCount)
240 bool CanOperateKeystream()
const {
return true;}
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);
254 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
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();}
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);
313 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms)
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);
345 template <
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();
408 template <
class BASE>
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();}
451 template <
class CIPHER>
471 template <
class CIPHER>
493 template <
class CIPHER>
515 template <
class CIPHER>
534 template <
class CIPHER>
555 template <
class CIPHER>
578 template <
class CIPHER>
601 #if CRYPTOPP_MSC_VERSION 602 # pragma warning(pop) 605 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE 606 # pragma GCC diagnostic pop Used to pass byte array input as part of a NameValuePairs object.
Standard names for retrieving values by name when working with NameValuePairs.
An invalid argument was detected.
const char * FeedbackSize()
int
CipherModeFinalTemplate_CipherHolder()
Construct a CipherModeFinalTemplate.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
Classes for working with NameValuePairs.
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Utility functions for the Crypto++ library.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
size_t GetValidKeyLength(size_t keylength) const
Returns a valid key length for the algorithm.
CFB mode, external cipher, providing FIPS validated cryptography.
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Set external block cipher and IV.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Construct a CipherModeFinalTemplate.
CipherDir
Specifies a direction for a cipher to operate.
CipherModeFinalTemplate_ExternalCipher()
Construct a default CipherModeFinalTemplate.
Abstract base classes that provide a uniform interface to this library.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Base class for feedback based stream ciphers with SymmetricCipher interface.
void CopyOrZero(void *dest, size_t dsize, const void *src, size_t ssize)
Initialize a block of memory.
Wirte the keystream to the output buffer, input is NULL.
CBC-CTS block cipher mode of operation.
bool CipherIsRandomAccess() const
Flag indicating random access.
size_t MinKeyLength() const
Returns smallest valid key length.
CTR block cipher mode of operation.
std::string AlgorithmName() const
Provides the name of this algorithm.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
Construct a CipherModeFinalTemplate.
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Interface for one direction (encryption or decryption) of a block cipher.
const char * StolenIV()
byte *
OFB block cipher mode of operation.
CBC-CTS block cipher mode of operation decryption operation.
size_t MaxKeyLength() const
Returns largest valid key length.
Classes and functions for secure memory allocations.
ECB block cipher mode of operation.
Base class for feedback based stream ciphers in the reverse direction with SymmetricCipher interface...
Uses encapsulation to hide an object in derived classes.
void SetCipher(BlockCipher &cipher)
Set external block cipher.
CBC-CTS block cipher mode of operation encryption operation.
virtual IV_Requirement IVRequirement() const =0
Minimal requirement for secure IVs.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Block cipher mode of operation aggregate.
CFB mode, external cipher.
CBC block cipher mode of operation default implementation.
bool IsSelfInverting() const
Determines whether the cipher is self-inverting.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
virtual void Resynchronize(const byte *iv, int ivLength=-1)
Resynchronize with an IV.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
Block cipher mode of operation information.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
unsigned int MandatoryBlockSize() const
Provides the mandatory block size of the cipher.
Block cipher mode of operation default implementation.
ECB mode, external cipher.
static std::string CRYPTOPP_API StaticAlgorithmName()
Provides the name of this algorithm.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
size_t DefaultKeyLength() const
Returns default key length.
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface...
OFB block cipher mode of operation.
CBC mode, external cipher.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
const char * BlockSize()
int, in bytes
CFB block cipher mode of operation.
CTR block cipher mode of operation.
OFB mode, external cipher.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
Construct a CipherModeFinalTemplate.
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Classes for implementing stream ciphers.
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
IV_Requirement
Secure IVs requirements as enumerated values.
Block cipher mode of operation information.
CBC block cipher mode of operation decryption operation.
CTR mode, external cipher.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
CBC block cipher mode of operation encryption operation.
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
Sets the key for this object without performing parameter validation.
Block cipher mode of operation common operations.
KeystreamOperation
Keystream operation flags.
Crypto++ library namespace.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
unsigned int IVSize() const
Returns length of the IV accepted by this object.
bool IsResynchronizable() const
Determines if the object can be resynchronized.
bool CipherIsRandomAccess() const
Flag indicating random access.
CFB block cipher mode of operation.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Base class for additive stream ciphers with SymmetricCipher interface.
CFB block cipher mode of operation providing FIPS validated cryptography.
ECB block cipher mode of operation default implementation.
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
Construct a CipherModeFinalTemplate.
bool IsRandomAccess() const
Determines whether the cipher supports random access.
Interface for retrieving values given their names.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
Construct a CipherModeFinalTemplate.
CBC block cipher mode of operation.
CBC mode with ciphertext stealing, external cipher.