6 #ifndef CRYPTOPP_MODES_H 7 #define CRYPTOPP_MODES_H 50 this->ThrowIfResynchronizable();
51 this->m_cipher = &cipher;
52 this->ResizeBuffers();
55 void SetCipherWithIV(
BlockCipher &cipher,
const byte *iv,
int feedbackSize = 0)
57 this->ThrowIfInvalidIV(iv);
58 this->m_cipher = &cipher;
59 this->ResizeBuffers();
60 this->SetFeedbackSize(feedbackSize);
67 inline unsigned int BlockSize()
const {assert(m_register.size() > 0);
return (
unsigned int)m_register.size();}
68 virtual void SetFeedbackSize(
unsigned int feedbackSize)
70 if (!(feedbackSize == 0 || feedbackSize ==
BlockSize()))
71 throw InvalidArgument(
"CipherModeBase: feedback size cannot be specified for this cipher mode");
75 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 76 virtual void ResizeBuffers();
78 virtual void ResizeBuffers()
80 m_register.New(m_cipher->BlockSize());
88 template <
class POLICY_INTERFACE>
92 void CipherSetKey(
const NameValuePairs ¶ms,
const byte *key,
size_t length);
95 template <
class POLICY_INTERFACE>
98 m_cipher->
SetKey(key, length, params);
101 SetFeedbackSize(feedbackSize);
108 static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CFB";}
111 unsigned int GetBytesPerIteration()
const {
return m_feedbackSize;}
112 byte * GetRegisterBegin() {
return m_register +
BlockSize() - m_feedbackSize;}
113 bool CanIterate()
const {
return m_feedbackSize ==
BlockSize();}
114 void Iterate(byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount);
115 void TransformRegister();
116 void CipherResynchronize(
const byte *iv,
size_t length);
117 void SetFeedbackSize(
unsigned int feedbackSize);
118 void ResizeBuffers();
121 unsigned int m_feedbackSize;
124 inline void CopyOrZero(
void *dest,
const void *src,
size_t s)
135 bool CipherIsRandomAccess()
const {
return false;}
137 static const char * CRYPTOPP_API StaticAlgorithmName() {
return "OFB";}
140 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
141 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
142 void WriteKeystream(byte *keystreamBuffer,
size_t iterationCount);
143 void CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length);
149 bool CipherIsRandomAccess()
const {
return true;}
151 static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CTR";}
154 virtual void IncrementCounterBy256();
156 unsigned int GetAlignment()
const {
return m_cipher->OptimalDataAlignment();}
157 unsigned int GetBytesPerIteration()
const {
return BlockSize();}
158 unsigned int GetIterationsToBuffer()
const {
return m_cipher->OptimalNumberOfParallelBlocks();}
159 void WriteKeystream(byte *buffer,
size_t iterationCount)
160 {OperateKeystream(WRITE_KEYSTREAM, buffer, NULL, iterationCount);}
161 bool CanOperateKeystream()
const {
return true;}
162 void OperateKeystream(KeystreamOperation operation, byte *output,
const byte *input,
size_t iterationCount);
163 void CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length);
164 void SeekToIteration(lword iterationCount);
172 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms);
177 void Resynchronize(
const byte *iv,
int length=-1) {
memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
180 bool RequireAlignedInput()
const {
return true;}
183 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 184 void ResizeBuffers();
188 CipherModeBase::ResizeBuffers();
200 {m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
203 void ProcessData(byte *outString,
const byte *inString,
size_t length);
204 static const char * CRYPTOPP_API StaticAlgorithmName() {
return "ECB";}
211 bool RequireAlignedInput()
const {
return false;}
213 static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CBC";}
219 void ProcessData(byte *outString,
const byte *inString,
size_t length);
225 void SetStolenIV(byte *iv) {m_stolenIV = iv;}
227 void ProcessLastBlock(byte *outString,
const byte *inString,
size_t length);
228 static const char * CRYPTOPP_API StaticAlgorithmName() {
return "CBC/CTS";}
231 void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms)
243 void ProcessData(byte *outString,
const byte *inString,
size_t length);
248 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 249 void ResizeBuffers();
253 BlockOrientedCipherModeBase::ResizeBuffers();
265 void ProcessLastBlock(byte *outString,
const byte *inString,
size_t length);
269 template <
class CIPHER,
class BASE>
275 this->m_cipher = &this->m_object;
276 this->ResizeBuffers();
280 this->m_cipher = &this->m_object;
281 this->SetKey(key, length);
285 this->m_cipher = &this->m_object;
290 this->m_cipher = &this->m_object;
294 static std::string CRYPTOPP_API StaticAlgorithmName()
295 {
return CIPHER::StaticAlgorithmName() +
"/" + BASE::StaticAlgorithmName();}
301 template <
class BASE>
307 {this->SetCipher(cipher);}
309 {this->SetCipherWithIV(cipher, iv, feedbackSize);}
311 std::string AlgorithmName()
const 312 {
return (this->m_cipher ? this->m_cipher->AlgorithmName() +
"/" : std::string(
"")) + BASE::StaticAlgorithmName();}
321 template <
class CIPHER>
339 template <
class CIPHER>
359 template <
class CIPHER>
379 template <
class CIPHER>
396 template <
class CIPHER>
414 template <
class CIPHER>
432 template <
class CIPHER>
450 #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
OFB block cipher mode of operation.
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. ivLength=-1 means use IVSize()
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
returns the 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()
SecByteBlock is a SecBlock<byte> typedef.
CBC mode with ciphertext stealing.
CTR block cipher mode of operation.
IV_Requirement IVRequirement() const
returns the minimal requirement for secure IVs
Interface for one direction (encryption or decryption) of a block cipher.
const char * StolenIV()
byte *
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
returns the minimal requirement for secure IVs
Uses encapsulation to hide an object in derived classes.
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.
CFB mode, external cipher.
bool IsResynchronizable() const
returns whether the object can be resynchronized (i.e. supports initialization vectors) ...
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. ivLength=-1 means use IVSize()
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.
Classes for operating block cipher modes of operation.
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode...
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 ...
AlignedSecByteBlock is a SecBlock<byte, AllocatorWithCleanup<byte, true> > typedef.
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
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.
bool IsValidKeyLength(size_t n) const
Returns whether keylength is a valid key length.
OFB mode, external cipher.
virtual IV_Requirement IVRequirement() const =0
returns the minimal requirement for secure IVs
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher...
IV_Requirement
Provides IV requirements as an enumerated value.
CTR mode, external cipher.
unsigned int MinLastBlockSize() const
returns the minimum size of the last block, 0 indicating the last block is not special ...
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
returns the minimal requirement for secure IVs
Crypto++ library namespace.
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 ...
IV_Requirement IVRequirement() const
returns the minimal requirement for secure IVs
CFB block cipher mode of operation providing FIPS validated cryptography.
Interface for retrieving values given their names.
CBC mode with ciphertext stealing, external cipher.
Base class for identifying alogorithm.