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) 66 this->ThrowIfResynchronizable();
67 this->m_cipher = &cipher;
68 this->ResizeBuffers();
71 void SetCipherWithIV(
BlockCipher &cipher,
const byte *iv,
int feedbackSize = 0)
73 this->ThrowIfInvalidIV(iv);
74 this->m_cipher = &cipher;
75 this->ResizeBuffers();
76 this->SetFeedbackSize(feedbackSize);
83 inline unsigned int BlockSize()
const {
CRYPTOPP_ASSERT(m_register.size() > 0);
return (
unsigned int)m_register.size();}
84 virtual void SetFeedbackSize(
unsigned int feedbackSize)
86 if (!(feedbackSize == 0 || feedbackSize ==
BlockSize()))
87 throw InvalidArgument(
"CipherModeBase: feedback size cannot be specified for this cipher mode");
90 virtual void ResizeBuffers();
98 template <
class POLICY_INTERFACE>
102 void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length);
105 template <
class POLICY_INTERFACE>
108 m_cipher->
SetKey(key, length, params);
111 SetFeedbackSize(feedbackSize);
118 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CFB";}
124 unsigned int GetBytesPerIteration()
const {
return m_feedbackSize;}
125 byte * GetRegisterBegin() {
return m_register +
BlockSize() - m_feedbackSize;}
126 bool CanIterate()
const {
return m_feedbackSize ==
BlockSize();}
127 void Iterate(byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount);
128 void TransformRegister();
129 void CipherResynchronize(
const byte *iv,
size_t length);
130 void SetFeedbackSize(
unsigned int feedbackSize);
131 void ResizeBuffers();
134 unsigned int m_feedbackSize;
137 inline void CopyOrZero(
void *dest,
size_t d,
const void *src,
size_t s)
152 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "OFB";}
158 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
159 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
160 void WriteKeystream(byte *keystreamBuffer,
size_t iterationCount);
161 void CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length);
168 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CTR";}
175 virtual void IncrementCounterBy256();
176 unsigned int GetAlignment()
const {
return m_cipher->OptimalDataAlignment();}
177 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
178 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
179 void WriteKeystream(byte *buffer,
size_t iterationCount)
181 bool CanOperateKeystream()
const {
return true;}
182 void OperateKeystream(
KeystreamOperation operation, byte *output,
const byte *input,
size_t iterationCount);
183 void CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length);
184 void SeekToIteration(lword iterationCount);
194 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
199 void Resynchronize(
const byte *iv,
int length=-1) {
memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
202 bool RequireAlignedInput()
const {
return true;}
203 virtual void ResizeBuffers();
212 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "ECB";}
215 {m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
218 void ProcessData(byte *outString,
const byte *inString,
size_t length);
225 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CBC";}
228 bool RequireAlignedInput()
const {
return false;}
236 void ProcessData(byte *outString,
const byte *inString,
size_t length);
244 CRYPTOPP_STATIC_CONSTEXPR
const char* CRYPTOPP_API StaticAlgorithmName() {
return "CBC/CTS";}
246 void SetStolenIV(byte *iv) {m_stolenIV = iv;}
248 size_t ProcessLastBlock(byte *outString,
size_t outLength,
const byte *inString,
size_t inLength);
265 void ProcessData(byte *outString,
const byte *inString,
size_t length);
268 virtual void ResizeBuffers();
279 size_t ProcessLastBlock(byte *outString,
size_t outLength,
const byte *inString,
size_t inLength);
283 template <
class CIPHER,
class BASE>
287 static std::string CRYPTOPP_API StaticAlgorithmName()
288 {
return CIPHER::StaticAlgorithmName() +
"/" + BASE::StaticAlgorithmName();}
292 this->m_cipher = &this->m_object;
293 this->ResizeBuffers();
297 this->m_cipher = &this->m_object;
298 this->SetKey(key, length);
302 this->m_cipher = &this->m_object;
307 this->m_cipher = &this->m_object;
314 template <
class BASE>
320 {this->SetCipher(cipher);}
322 {this->SetCipherWithIV(cipher, iv, feedbackSize);}
324 std::string AlgorithmName()
const 325 {
return (this->m_cipher ? this->m_cipher->AlgorithmName() +
"/" : std::string(
"")) + BASE::StaticAlgorithmName();}
335 template <
class CIPHER>
355 template <
class CIPHER>
377 template <
class CIPHER>
399 template <
class CIPHER>
418 template <
class CIPHER>
439 template <
class CIPHER>
462 template <
class CIPHER>
485 #if CRYPTOPP_MSC_VERSION 486 # pragma warning(pop) 489 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE 490 # pragma GCC diagnostic pop Used to pass byte array input as part of a NameValuePairs object.
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Standard names for retrieving values by name when working with NameValuePairs.
An invalid argument was detected.
const char * FeedbackSize()
int
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
Classes for working with NameValuePairs.
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 n) 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.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
CipherDir
Specifies a direction for a cipher to operate.
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.
The object does not use an IV.
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.
bool IsValidKeyLength(size_t n) const
Returns whether keylength is a valid key length.
CTR block cipher mode of operation.
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.
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.
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.
unsigned int MinLastBlockSize() const
Provides the size of the last block.
size_t DefaultKeyLength() const
Returns default key length.
SecBlock using AllocatorWithCleanup<byte, true> typedef.
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.
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.
const NameValuePairs g_nullNameValuePairs
An empty set of name-value pairs.
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.
The IV must be random and unpredictable.
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.
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.
bool IsRandomAccess() const
Determines whether the cipher supports random access.
Interface for retrieving values given their names.
The IV must be random and possibly predictable.
CBC block cipher mode of operation.
CBC mode with ciphertext stealing, external cipher.