6 #ifndef CRYPTOPP_MODES_H 7 #define CRYPTOPP_MODES_H 52 this->ThrowIfResynchronizable();
53 this->m_cipher = &cipher;
54 this->ResizeBuffers();
57 void SetCipherWithIV(
BlockCipher &cipher,
const byte *iv,
int feedbackSize = 0)
59 this->ThrowIfInvalidIV(iv);
60 this->m_cipher = &cipher;
61 this->ResizeBuffers();
62 this->SetFeedbackSize(feedbackSize);
69 inline unsigned int BlockSize()
const {assert(m_register.size() > 0);
return (
unsigned int)m_register.size();}
70 virtual void SetFeedbackSize(
unsigned int feedbackSize)
72 if (!(feedbackSize == 0 || feedbackSize ==
BlockSize()))
73 throw InvalidArgument(
"CipherModeBase: feedback size cannot be specified for this cipher mode");
77 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 78 virtual void ResizeBuffers();
80 virtual void ResizeBuffers()
82 m_register.New(m_cipher->BlockSize());
93 template <
class POLICY_INTERFACE>
97 void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length);
100 template <
class POLICY_INTERFACE>
103 m_cipher->
SetKey(key, length, params);
106 SetFeedbackSize(feedbackSize);
115 CRYPTOPP_CONSTEXPR
static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CFB";}
118 unsigned int GetBytesPerIteration()
const {
return m_feedbackSize;}
119 byte * GetRegisterBegin() {
return m_register +
BlockSize() - m_feedbackSize;}
120 bool CanIterate()
const {
return m_feedbackSize ==
BlockSize();}
121 void Iterate(byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount);
122 void TransformRegister();
123 void CipherResynchronize(
const byte *iv,
size_t length);
124 void SetFeedbackSize(
unsigned int feedbackSize);
125 void ResizeBuffers();
128 unsigned int m_feedbackSize;
131 inline void CopyOrZero(
void *dest,
const void *src,
size_t s)
146 CRYPTOPP_CONSTEXPR
static const char * CRYPTOPP_API StaticAlgorithmName() {
return "OFB";}
149 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
150 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
151 void WriteKeystream(byte *keystreamBuffer,
size_t iterationCount);
152 void CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length);
162 CRYPTOPP_CONSTEXPR
static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CTR";}
165 virtual void IncrementCounterBy256();
167 unsigned int GetAlignment()
const {
return m_cipher->OptimalDataAlignment();}
168 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
169 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
170 void WriteKeystream(byte *buffer,
size_t iterationCount)
172 bool CanOperateKeystream()
const {
return true;}
173 void OperateKeystream(
KeystreamOperation operation, byte *output,
const byte *input,
size_t iterationCount);
174 void CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length);
175 void SeekToIteration(lword iterationCount);
185 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
190 void Resynchronize(
const byte *iv,
int length=-1) {
memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
193 bool RequireAlignedInput()
const {
return true;}
196 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 197 void ResizeBuffers();
201 CipherModeBase::ResizeBuffers();
215 {m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
218 void ProcessData(byte *outString,
const byte *inString,
size_t length);
219 CRYPTOPP_CONSTEXPR
static const char * CRYPTOPP_API StaticAlgorithmName() {
return "ECB";}
228 bool RequireAlignedInput()
const {
return false;}
230 CRYPTOPP_CONSTEXPR
static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CBC";}
238 void ProcessData(byte *outString,
const byte *inString,
size_t length);
246 void SetStolenIV(byte *iv) {m_stolenIV = iv;}
248 void ProcessLastBlock(byte *outString,
const byte *inString,
size_t length);
249 CRYPTOPP_CONSTEXPR
static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CBC/CTS";}
252 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms)
266 void ProcessData(byte *outString,
const byte *inString,
size_t length);
271 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 272 void ResizeBuffers();
276 BlockOrientedCipherModeBase::ResizeBuffers();
290 void ProcessLastBlock(byte *outString,
const byte *inString,
size_t length);
295 template <
class CIPHER,
class BASE>
301 this->m_cipher = &this->m_object;
302 this->ResizeBuffers();
306 this->m_cipher = &this->m_object;
307 this->SetKey(key, length);
311 this->m_cipher = &this->m_object;
316 this->m_cipher = &this->m_object;
320 static std::string CRYPTOPP_API StaticAlgorithmName()
321 {
return CIPHER::StaticAlgorithmName() +
"/" + BASE::StaticAlgorithmName();}
327 template <
class BASE>
333 {this->SetCipher(cipher);}
335 {this->SetCipherWithIV(cipher, iv, feedbackSize);}
337 std::string AlgorithmName()
const 338 {
return (this->m_cipher ? this->m_cipher->AlgorithmName() +
"/" : std::string(
"")) + BASE::StaticAlgorithmName();}
347 template <
class CIPHER>
365 template <
class CIPHER>
385 template <
class CIPHER>
405 template <
class CIPHER>
422 template <
class CIPHER>
440 template <
class CIPHER>
458 template <
class CIPHER>
476 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY bool IsSelfInverting() const
Determines whether the cipher is self-inverting.
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
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
Classes for working with NameValuePairs.
size_t GetValidKeyLength(size_t n) const
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.
CFB mode, external cipher, providing FIPS validated cryptography.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
bool IsRandomAccess() const
Determines whether the cipher supports random access.
CipherDir
Specifies a direction for a cipher to operate.
size_t MinKeyLength() const
Returns smallest valid key length in bytes.
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.
Wirte the keystream to the output buffer, input is NULL.
CBC mode with ciphertext stealing.
CTR block cipher mode of operation.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
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.
Classes and functions for secure memory allocations.
ECB block cipher mode of operation.
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
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.
unsigned int IVSize() const
Returns length of the IV accepted by this object.
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
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 IsResynchronizable() const
Determines if the object can be resynchronized.
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
virtual void Resynchronize(const byte *iv, int ivLength=-1)
Resynchronize with an IV.
size_t DefaultKeyLength() const
Returns default (recommended) key length in bytes.
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...
Block cipher mode of operation default implementation.
ECB mode, external cipher.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
unsigned int MinLastBlockSize() const
returns the minimum size of the last block, 0 indicating the last block is not special ...
SecBlock using AllocatorWithCleanup<byte, true> typedef.
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface...
OFB block cipher mode of operation.
size_t MaxKeyLength() const
Returns largest valid key length in bytes.
CBC mode, external cipher.
const char * BlockSize()
int, in bytes
CFB block cipher mode of operation.
CTR block cipher mode of operation.
bool IsValidKeyLength(size_t n) const
Returns whether keylength is a valid key length.
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...
IV_Requirement
Secure IVs requirements as enumerated values.
bool CipherIsRandomAccess() const
Flag indicating random access.
Block cipher mode of operation information.
CBC block cipher mode of operation decryption operation.
CTR mode, external cipher.
unsigned int MinLastBlockSize() const
returns the minimum size of the last block, 0 indicating the last block is not special ...
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.
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Block cipher mode of operation common operations.
KeystreamOperation
Keystream operation flags.
Crypto++ library namespace.
bool CipherIsRandomAccess() const
Flag indicating random access.
unsigned int MandatoryBlockSize() const
Provides the mandatory block size of the cipher.
unsigned int MinLastBlockSize() const
returns the minimum size of the last block, 0 indicating the last block is not special ...
CFB block cipher mode of operation.
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.
Interface for retrieving values given their names.
CBC mode with ciphertext stealing, external cipher.
Base class for identifying alogorithm.