Crypto++
8.6
Free C++ class library of cryptographic schemes
|
Go to the documentation of this file.
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);}
96 unsigned int IVSize()
const {
return BlockSize();}
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);
129 inline unsigned int BlockSize()
const
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();}
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);
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
unsigned int IVSize() const
Returns length of the IV accepted by this object.
OFB block cipher mode of operation.
size_t GetValidKeyLength(size_t keylength) const
Returns a valid key length for the algorithm.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
@ UNPREDICTABLE_RANDOM_IV
The IV must be random and unpredictable.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
CBC block cipher mode of operation decryption operation.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
CBC block cipher mode of operation encryption operation.
CTR mode, external cipher.
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.
CFB block cipher mode of operation.
@ NOT_RESYNCHRONIZABLE
The object does not use an IV.
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
size_t DefaultKeyLength() const
Returns default key length.
Uses encapsulation to hide an object in derived classes.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
CBC-CTS block cipher mode of operation.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Block cipher mode of operation information.
virtual IV_Requirement IVRequirement() const =0
Minimal requirement for secure IVs.
Classes and functions for secure memory allocations.
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
CTR block cipher mode of operation.
Block cipher mode of operation common operations.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Set external block cipher and IV.
CFB mode, external cipher, providing FIPS validated cryptography.
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
Sets the key for this object without performing parameter validation.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
CipherModeFinalTemplate_CipherHolder()
Construct a CipherModeFinalTemplate.
ECB block cipher mode of operation.
CipherModeFinalTemplate_ExternalCipher()
Construct a default CipherModeFinalTemplate.
bool IsSelfInverting() const
Determines whether the cipher is self-inverting.
@ UNIQUE_IV
The IV must be unique.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
CipherDir
Specifies a direction for a cipher to operate.
IV_Requirement
Secure IVs requirements as enumerated values.
CBC-CTS block cipher mode of operation decryption operation.
Standard names for retrieving values by name when working with NameValuePairs.
virtual void Resynchronize(const byte *iv, int ivLength=-1)
Resynchronize with an IV.
Utility functions for the Crypto++ library.
CBC block cipher mode of operation.
CBC-CTS block cipher mode of operation encryption operation.
Block cipher mode of operation information.
Base class for additive stream ciphers with SymmetricCipher interface.
bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
std::string AlgorithmName() const
Provides the name of this algorithm.
unsigned int MandatoryBlockSize() const
Provides the mandatory block size of the cipher.
bool IsResynchronizable() const
Determines if the object can be resynchronized.
@ WRITE_KEYSTREAM
Write the keystream to the output buffer, input is NULL.
KeystreamOperation
Keystream operation flags.
void CopyOrZero(void *dest, size_t dsize, const void *src, size_t ssize)
Initialize a block of memory.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
Construct a CipherModeFinalTemplate.
CFB block cipher mode of operation.
bool IsRandomAccess() const
Determines whether the cipher supports random access.
CBC mode, external cipher.
size_t MaxKeyLength() const
Returns largest valid key length.
Block cipher mode of operation aggregate.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
Construct a CipherModeFinalTemplate.
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Construct a CipherModeFinalTemplate.
void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
bool CipherIsRandomAccess() const
Flag indicating random access.
bool CipherIsRandomAccess() const
Flag indicating random access.
Base class for feedback based stream ciphers with SymmetricCipher interface.
word64 lword
Large word type.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Base class for feedback based stream ciphers in the reverse direction with SymmetricCipher interface.
An invalid argument was detected.
OFB block cipher mode of operation.
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
Construct a CipherModeFinalTemplate.
OFB mode, external cipher.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
void SetCipher(BlockCipher &cipher)
Set external block cipher.
Crypto++ library namespace.
#define CRYPTOPP_API
Win32 calling convention.
CFB mode, external cipher.
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
CFB block cipher mode of operation providing FIPS validated cryptography.
CBC mode with ciphertext stealing, external cipher.
Interface for one direction (encryption or decryption) of a block cipher.
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
ECB mode, external cipher.
Block cipher mode of operation default implementation.
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
CBC block cipher mode of operation default implementation.
CTR block cipher mode of operation.
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
Construct a CipherModeFinalTemplate.
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface.
Interface for retrieving values given their names.
Abstract base classes that provide a uniform interface to this library.
size_t MinKeyLength() const
Returns smallest valid key length.
ECB block cipher mode of operation default implementation.
Used to pass byte array input as part of a NameValuePairs object.
Classes for working with NameValuePairs.
Classes for implementing stream ciphers.
@ RANDOM_IV
The IV must be random and possibly predictable.
static std::string StaticAlgorithmName()
Provides the name of this algorithm.