2017-01-27 12:05:45 +00:00
|
|
|
// modes.h - originally written and placed in the public domain by Wei Dai
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file modes.h
|
|
|
|
/// \brief Classes for block cipher modes of operation
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_MODES_H
|
|
|
|
#define CRYPTOPP_MODES_H
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "secblock.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "strciphr.h"
|
|
|
|
#include "argnames.h"
|
|
|
|
#include "algparam.h"
|
|
|
|
|
2016-12-02 19:47:31 +00:00
|
|
|
// Issue 340
|
|
|
|
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wconversion"
|
|
|
|
# pragma GCC diagnostic ignored "-Wsign-conversion"
|
|
|
|
#endif
|
|
|
|
|
2017-06-02 09:18:52 +00:00
|
|
|
#if CRYPTOPP_MSC_VERSION
|
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable: 4231 4275)
|
|
|
|
# if (CRYPTOPP_MSC_VERSION >= 1400)
|
|
|
|
# pragma warning(disable: 6011 6386 28193)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Block cipher mode of operation information
|
|
|
|
/// \details Each class derived from this one defines two types, Encryption and Decryption,
|
|
|
|
/// both of which implement the SymmetricCipher interface.
|
|
|
|
/// For each mode there are two classes, one of which is a template class,
|
|
|
|
/// and the other one has a name that ends in "_ExternalCipher".
|
|
|
|
/// The "external cipher" mode objects hold a reference to the underlying block cipher,
|
|
|
|
/// instead of holding an instance of it. The reference must be passed in to the constructor.
|
|
|
|
/// For the "cipher holder" classes, the CIPHER template parameter should be a class
|
|
|
|
/// derived from BlockCipherDocumentation, for example DES or AES.
|
|
|
|
/// \details See NIST SP 800-38A for definitions of these modes. See
|
|
|
|
/// AuthenticatedSymmetricCipherDocumentation for authenticated encryption modes.
|
2015-11-05 06:59:46 +00:00
|
|
|
struct CipherModeDocumentation : public SymmetricCipherDocumentation
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Block cipher mode of operation information
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
virtual ~CipherModeBase() {}
|
2015-11-05 06:59:46 +00:00
|
|
|
size_t MinKeyLength() const {return m_cipher->MinKeyLength();}
|
|
|
|
size_t MaxKeyLength() const {return m_cipher->MaxKeyLength();}
|
|
|
|
size_t DefaultKeyLength() const {return m_cipher->DefaultKeyLength();}
|
|
|
|
size_t GetValidKeyLength(size_t n) const {return m_cipher->GetValidKeyLength(n);}
|
|
|
|
bool IsValidKeyLength(size_t n) const {return m_cipher->IsValidKeyLength(n);}
|
|
|
|
|
|
|
|
unsigned int OptimalDataAlignment() const {return m_cipher->OptimalDataAlignment();}
|
|
|
|
|
|
|
|
unsigned int IVSize() const {return BlockSize();}
|
|
|
|
virtual IV_Requirement IVRequirement() const =0;
|
|
|
|
|
|
|
|
void SetCipher(BlockCipher &cipher)
|
|
|
|
{
|
|
|
|
this->ThrowIfResynchronizable();
|
|
|
|
this->m_cipher = &cipher;
|
|
|
|
this->ResizeBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
|
|
|
|
{
|
|
|
|
this->ThrowIfInvalidIV(iv);
|
|
|
|
this->m_cipher = &cipher;
|
|
|
|
this->ResizeBuffers();
|
|
|
|
this->SetFeedbackSize(feedbackSize);
|
|
|
|
if (this->IsResynchronizable())
|
|
|
|
this->Resynchronize(iv);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-09-05 20:28:00 +00:00
|
|
|
CipherModeBase() : m_cipher(NULLPTR) {}
|
2016-09-16 15:27:15 +00:00
|
|
|
inline unsigned int BlockSize() const {CRYPTOPP_ASSERT(m_register.size() > 0); return (unsigned int)m_register.size();}
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void SetFeedbackSize(unsigned int feedbackSize)
|
|
|
|
{
|
|
|
|
if (!(feedbackSize == 0 || feedbackSize == BlockSize()))
|
|
|
|
throw InvalidArgument("CipherModeBase: feedback size cannot be specified for this cipher mode");
|
|
|
|
}
|
2016-09-06 08:04:03 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
virtual void ResizeBuffers();
|
|
|
|
|
|
|
|
BlockCipher *m_cipher;
|
|
|
|
AlignedSecByteBlock m_register;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Block cipher mode of operation common operations
|
|
|
|
/// \tparam POLICY_INTERFACE common operations
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class POLICY_INTERFACE>
|
|
|
|
class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE
|
|
|
|
{
|
|
|
|
unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
|
|
|
|
void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class POLICY_INTERFACE>
|
|
|
|
void ModePolicyCommonTemplate<POLICY_INTERFACE>::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)
|
|
|
|
{
|
|
|
|
m_cipher->SetKey(key, length, params);
|
|
|
|
ResizeBuffers();
|
|
|
|
int feedbackSize = params.GetIntValueWithDefault(Name::FeedbackSize(), 0);
|
|
|
|
SetFeedbackSize(feedbackSize);
|
|
|
|
}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CFB block cipher mode of operation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate<CFB_CipherAbstractPolicy>
|
|
|
|
{
|
|
|
|
public:
|
2016-12-01 14:37:04 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CFB";}
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2016-12-03 05:32:07 +00:00
|
|
|
virtual ~CFB_ModePolicy() {}
|
|
|
|
IV_Requirement IVRequirement() const {return RANDOM_IV;}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
protected:
|
|
|
|
unsigned int GetBytesPerIteration() const {return m_feedbackSize;}
|
|
|
|
byte * GetRegisterBegin() {return m_register + BlockSize() - m_feedbackSize;}
|
|
|
|
bool CanIterate() const {return m_feedbackSize == BlockSize();}
|
|
|
|
void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount);
|
|
|
|
void TransformRegister();
|
|
|
|
void CipherResynchronize(const byte *iv, size_t length);
|
|
|
|
void SetFeedbackSize(unsigned int feedbackSize);
|
|
|
|
void ResizeBuffers();
|
|
|
|
|
2017-09-05 20:28:00 +00:00
|
|
|
SecByteBlock m_temp;
|
2015-11-05 06:59:46 +00:00
|
|
|
unsigned int m_feedbackSize;
|
|
|
|
};
|
|
|
|
|
Add variable block size support for block ciphers
This should lead the way for more modern block ciphers like Threefish and Kalyna. It tested well with both regular cipher modes (the mode has an instance of the cipher) and external cipher modes (the cipher and mode are distinct objects, and the mode holds a reference to the cipher).
We still have to work out the details of naming a cipher. For example, Kalyna with a 128-bit key can use a 128-bit or 256-bit block size. Kalyna-128 is not enough to describe the algorithm and locate it in the object registry. Kalyna-128-128 looks kind of weird; maybe Kalyna-128(128) or Kalyna-128(256) would be better.
Here are the initial test cases to verify functionality:
byte key[64] = {}, iv[32] = {};
ECB_Mode<Kalyna>::Encryption enc1;
enc1.SetKey(key, 16);
CBC_Mode<Kalyna>::Encryption enc2;
enc2.SetKeyWithIV(key, 16, iv);
AlgorithmParameters params = MakeParameters
(Name::BlockSize(), 32)
(Name::IV(), ConstByteArrayParameter(iv, 32));
CTR_Mode<Kalyna>::Encryption enc3;
enc3.SetKey(key, 16, params);
CBC_Mode<Kalyna>::Encryption enc4;
enc4.SetKey(key, 32, params);
Kalyna::Encryption enc5;
ECB_Mode_ExternalCipher::Encryption ecb(enc5);
ecb.SetKey(key, 16, params);
Kalyna::Encryption enc6;
ECB_Mode_ExternalCipher::Encryption cbc(enc6);
cbc.SetKey(key, 32, params);
2017-05-01 20:23:57 +00:00
|
|
|
inline void CopyOrZero(void *dest, size_t d, const void *src, size_t s)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
Add variable block size support for block ciphers
This should lead the way for more modern block ciphers like Threefish and Kalyna. It tested well with both regular cipher modes (the mode has an instance of the cipher) and external cipher modes (the cipher and mode are distinct objects, and the mode holds a reference to the cipher).
We still have to work out the details of naming a cipher. For example, Kalyna with a 128-bit key can use a 128-bit or 256-bit block size. Kalyna-128 is not enough to describe the algorithm and locate it in the object registry. Kalyna-128-128 looks kind of weird; maybe Kalyna-128(128) or Kalyna-128(256) would be better.
Here are the initial test cases to verify functionality:
byte key[64] = {}, iv[32] = {};
ECB_Mode<Kalyna>::Encryption enc1;
enc1.SetKey(key, 16);
CBC_Mode<Kalyna>::Encryption enc2;
enc2.SetKeyWithIV(key, 16, iv);
AlgorithmParameters params = MakeParameters
(Name::BlockSize(), 32)
(Name::IV(), ConstByteArrayParameter(iv, 32));
CTR_Mode<Kalyna>::Encryption enc3;
enc3.SetKey(key, 16, params);
CBC_Mode<Kalyna>::Encryption enc4;
enc4.SetKey(key, 32, params);
Kalyna::Encryption enc5;
ECB_Mode_ExternalCipher::Encryption ecb(enc5);
ecb.SetKey(key, 16, params);
Kalyna::Encryption enc6;
ECB_Mode_ExternalCipher::Encryption cbc(enc6);
cbc.SetKey(key, 32, params);
2017-05-01 20:23:57 +00:00
|
|
|
CRYPTOPP_ASSERT(dest);
|
|
|
|
CRYPTOPP_ASSERT(d >= s);
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
if (src)
|
Add variable block size support for block ciphers
This should lead the way for more modern block ciphers like Threefish and Kalyna. It tested well with both regular cipher modes (the mode has an instance of the cipher) and external cipher modes (the cipher and mode are distinct objects, and the mode holds a reference to the cipher).
We still have to work out the details of naming a cipher. For example, Kalyna with a 128-bit key can use a 128-bit or 256-bit block size. Kalyna-128 is not enough to describe the algorithm and locate it in the object registry. Kalyna-128-128 looks kind of weird; maybe Kalyna-128(128) or Kalyna-128(256) would be better.
Here are the initial test cases to verify functionality:
byte key[64] = {}, iv[32] = {};
ECB_Mode<Kalyna>::Encryption enc1;
enc1.SetKey(key, 16);
CBC_Mode<Kalyna>::Encryption enc2;
enc2.SetKeyWithIV(key, 16, iv);
AlgorithmParameters params = MakeParameters
(Name::BlockSize(), 32)
(Name::IV(), ConstByteArrayParameter(iv, 32));
CTR_Mode<Kalyna>::Encryption enc3;
enc3.SetKey(key, 16, params);
CBC_Mode<Kalyna>::Encryption enc4;
enc4.SetKey(key, 32, params);
Kalyna::Encryption enc5;
ECB_Mode_ExternalCipher::Encryption ecb(enc5);
ecb.SetKey(key, 16, params);
Kalyna::Encryption enc6;
ECB_Mode_ExternalCipher::Encryption cbc(enc6);
cbc.SetKey(key, 32, params);
2017-05-01 20:23:57 +00:00
|
|
|
memcpy_s(dest, d, src, s);
|
2015-11-05 06:59:46 +00:00
|
|
|
else
|
Add variable block size support for block ciphers
This should lead the way for more modern block ciphers like Threefish and Kalyna. It tested well with both regular cipher modes (the mode has an instance of the cipher) and external cipher modes (the cipher and mode are distinct objects, and the mode holds a reference to the cipher).
We still have to work out the details of naming a cipher. For example, Kalyna with a 128-bit key can use a 128-bit or 256-bit block size. Kalyna-128 is not enough to describe the algorithm and locate it in the object registry. Kalyna-128-128 looks kind of weird; maybe Kalyna-128(128) or Kalyna-128(256) would be better.
Here are the initial test cases to verify functionality:
byte key[64] = {}, iv[32] = {};
ECB_Mode<Kalyna>::Encryption enc1;
enc1.SetKey(key, 16);
CBC_Mode<Kalyna>::Encryption enc2;
enc2.SetKeyWithIV(key, 16, iv);
AlgorithmParameters params = MakeParameters
(Name::BlockSize(), 32)
(Name::IV(), ConstByteArrayParameter(iv, 32));
CTR_Mode<Kalyna>::Encryption enc3;
enc3.SetKey(key, 16, params);
CBC_Mode<Kalyna>::Encryption enc4;
enc4.SetKey(key, 32, params);
Kalyna::Encryption enc5;
ECB_Mode_ExternalCipher::Encryption ecb(enc5);
ecb.SetKey(key, 16, params);
Kalyna::Encryption enc6;
ECB_Mode_ExternalCipher::Encryption cbc(enc6);
cbc.SetKey(key, 32, params);
2017-05-01 20:23:57 +00:00
|
|
|
memset(dest, 0, d);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief OFB block cipher mode of operation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "OFB";}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
bool CipherIsRandomAccess() const {return false;}
|
|
|
|
IV_Requirement IVRequirement() const {return UNIQUE_IV;}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int GetBytesPerIteration() const {return BlockSize();}
|
|
|
|
unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
|
|
|
|
void WriteKeystream(byte *keystreamBuffer, size_t iterationCount);
|
|
|
|
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CTR block cipher mode of operation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CTR";}
|
|
|
|
|
|
|
|
virtual ~CTR_ModePolicy() {}
|
2015-11-05 06:59:46 +00:00
|
|
|
bool CipherIsRandomAccess() const {return true;}
|
|
|
|
IV_Requirement IVRequirement() const {return RANDOM_IV;}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void IncrementCounterBy256();
|
|
|
|
unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
|
|
|
|
unsigned int GetBytesPerIteration() const {return BlockSize();}
|
|
|
|
unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
|
|
|
|
void WriteKeystream(byte *buffer, size_t iterationCount)
|
2017-03-01 11:10:06 +00:00
|
|
|
{OperateKeystream(WRITE_KEYSTREAM, buffer, NULLPTR, iterationCount);}
|
2015-11-05 06:59:46 +00:00
|
|
|
bool CanOperateKeystream() const {return true;}
|
|
|
|
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
|
|
|
|
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
|
|
|
|
void SeekToIteration(lword iterationCount);
|
|
|
|
|
|
|
|
AlignedSecByteBlock m_counterArray;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Block cipher mode of operation default implementation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
virtual ~BlockOrientedCipherModeBase() {}
|
2015-11-05 06:59:46 +00:00
|
|
|
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms);
|
|
|
|
unsigned int MandatoryBlockSize() const {return BlockSize();}
|
|
|
|
bool IsRandomAccess() const {return false;}
|
|
|
|
bool IsSelfInverting() const {return false;}
|
|
|
|
bool IsForwardTransformation() const {return m_cipher->IsForwardTransformation();}
|
|
|
|
void Resynchronize(const byte *iv, int length=-1) {memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool RequireAlignedInput() const {return true;}
|
2016-12-03 05:32:07 +00:00
|
|
|
virtual void ResizeBuffers();
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-09-05 20:28:00 +00:00
|
|
|
SecByteBlock m_buffer;
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief ECB block cipher mode of operation default implementation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherModeBase
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECB";}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms = g_nullNameValuePairs)
|
|
|
|
{m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
|
|
|
|
IV_Requirement IVRequirement() const {return NOT_RESYNCHRONIZABLE;}
|
|
|
|
unsigned int OptimalBlockSize() const {return BlockSize() * m_cipher->OptimalNumberOfParallelBlocks();}
|
|
|
|
void ProcessData(byte *outString, const byte *inString, size_t length);
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC block cipher mode of operation default implementation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CBC";}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
IV_Requirement IVRequirement() const {return UNPREDICTABLE_RANDOM_IV;}
|
|
|
|
bool RequireAlignedInput() const {return false;}
|
|
|
|
unsigned int MinLastBlockSize() const {return 0;}
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC block cipher mode of operation encryption operation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void ProcessData(byte *outString, const byte *inString, size_t length);
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC-CTS block cipher mode of operation encryption operation
|
2018-01-23 01:13:18 +00:00
|
|
|
/// \since Crypto++ 3.0
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
void SetStolenIV(byte *iv) {m_stolenIV = iv;}
|
|
|
|
unsigned int MinLastBlockSize() const {return BlockSize()+1;}
|
2017-09-30 02:34:33 +00:00
|
|
|
size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
|
|
|
|
{
|
|
|
|
CBC_Encryption::UncheckedSetKey(key, length, params);
|
2017-03-01 11:10:06 +00:00
|
|
|
m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), (byte *)NULLPTR);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
byte *m_stolenIV;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC block cipher mode of operation decryption operation
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
virtual ~CBC_Decryption() {}
|
2015-11-05 06:59:46 +00:00
|
|
|
void ProcessData(byte *outString, const byte *inString, size_t length);
|
2016-09-06 08:04:03 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
protected:
|
2016-12-03 05:32:07 +00:00
|
|
|
virtual void ResizeBuffers();
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
AlignedSecByteBlock m_temp;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC-CTS block cipher mode of operation decryption operation
|
2018-01-23 01:13:18 +00:00
|
|
|
/// \since Crypto++ 3.0
|
2015-11-05 06:59:46 +00:00
|
|
|
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
unsigned int MinLastBlockSize() const {return BlockSize()+1;}
|
2017-09-30 02:34:33 +00:00
|
|
|
size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength);
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Block cipher mode of operation aggregate
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER, class BASE>
|
|
|
|
class CipherModeFinalTemplate_CipherHolder : protected ObjectHolder<CIPHER>, public AlgorithmImpl<BASE, CipherModeFinalTemplate_CipherHolder<CIPHER, BASE> >
|
|
|
|
{
|
|
|
|
public:
|
2016-12-03 05:32:07 +00:00
|
|
|
static std::string CRYPTOPP_API StaticAlgorithmName()
|
|
|
|
{return CIPHER::StaticAlgorithmName() + "/" + BASE::StaticAlgorithmName();}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
CipherModeFinalTemplate_CipherHolder()
|
|
|
|
{
|
|
|
|
this->m_cipher = &this->m_object;
|
|
|
|
this->ResizeBuffers();
|
|
|
|
}
|
|
|
|
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
|
|
|
|
{
|
|
|
|
this->m_cipher = &this->m_object;
|
|
|
|
this->SetKey(key, length);
|
|
|
|
}
|
|
|
|
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
|
|
|
|
{
|
|
|
|
this->m_cipher = &this->m_object;
|
|
|
|
this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize())));
|
|
|
|
}
|
|
|
|
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
|
|
|
|
{
|
|
|
|
this->m_cipher = &this->m_object;
|
|
|
|
this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize()))(Name::FeedbackSize(), feedbackSize));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \tparam BASE CipherModeFinalTemplate_CipherHolder base class
|
|
|
|
/// \details Base class for external mode cipher combinations
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class BASE>
|
|
|
|
class CipherModeFinalTemplate_ExternalCipher : public BASE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CipherModeFinalTemplate_ExternalCipher() {}
|
|
|
|
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
|
|
|
|
{this->SetCipher(cipher);}
|
|
|
|
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
|
|
|
|
{this->SetCipherWithIV(cipher, iv, feedbackSize);}
|
|
|
|
|
|
|
|
std::string AlgorithmName() const
|
|
|
|
{return (this->m_cipher ? this->m_cipher->AlgorithmName() + "/" : std::string("")) + BASE::StaticAlgorithmName();}
|
|
|
|
};
|
|
|
|
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CFB block cipher mode of operation
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER>
|
|
|
|
struct CFB_Mode : public CipherModeDocumentation
|
|
|
|
{
|
2016-12-03 10:05:56 +00:00
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CFB mode, external cipher.
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
struct CFB_Mode_ExternalCipher : public CipherModeDocumentation
|
|
|
|
{
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CFB block cipher mode of operation providing FIPS validated cryptography.
|
|
|
|
/// \details Requires full block plaintext according to FIPS 800-38A
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER>
|
|
|
|
struct CFB_FIPS_Mode : public CipherModeDocumentation
|
|
|
|
{
|
2016-12-03 10:05:56 +00:00
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CFB mode, external cipher, providing FIPS validated cryptography.
|
|
|
|
/// \details Requires full block plaintext according to FIPS 800-38A
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
struct CFB_FIPS_Mode_ExternalCipher : public CipherModeDocumentation
|
|
|
|
{
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
|
|
|
|
};
|
|
|
|
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> >;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief OFB block cipher mode of operation
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER>
|
|
|
|
struct OFB_Mode : public CipherModeDocumentation
|
|
|
|
{
|
2016-12-03 10:05:56 +00:00
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
|
2015-11-05 06:59:46 +00:00
|
|
|
typedef Encryption Decryption;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief OFB mode, external cipher.
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
struct OFB_Mode_ExternalCipher : public CipherModeDocumentation
|
|
|
|
{
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
|
|
|
|
typedef Encryption Decryption;
|
|
|
|
};
|
|
|
|
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> >;
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > >;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CTR block cipher mode of operation
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER>
|
|
|
|
struct CTR_Mode : public CipherModeDocumentation
|
|
|
|
{
|
2016-12-03 10:05:56 +00:00
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
|
2015-11-05 06:59:46 +00:00
|
|
|
typedef Encryption Decryption;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CTR mode, external cipher.
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
struct CTR_Mode_ExternalCipher : public CipherModeDocumentation
|
|
|
|
{
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
|
|
|
|
typedef Encryption Decryption;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief ECB block cipher mode of operation
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER>
|
|
|
|
struct ECB_Mode : public CipherModeDocumentation
|
|
|
|
{
|
2016-12-03 10:05:56 +00:00
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ECB_OneWay> Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Decryption, ECB_OneWay> Decryption;
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<ECB_OneWay>;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief ECB mode, external cipher.
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
struct ECB_Mode_ExternalCipher : public CipherModeDocumentation
|
|
|
|
{
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<ECB_OneWay> Encryption;
|
|
|
|
typedef Encryption Decryption;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC block cipher mode of operation
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER>
|
|
|
|
struct CBC_Mode : public CipherModeDocumentation
|
|
|
|
{
|
2016-12-03 10:05:56 +00:00
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, CBC_Encryption> Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Decryption, CBC_Decryption> Decryption;
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Encryption>;
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Decryption>;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC mode, external cipher
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2015-11-05 06:59:46 +00:00
|
|
|
struct CBC_Mode_ExternalCipher : public CipherModeDocumentation
|
|
|
|
{
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<CBC_Encryption> Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<CBC_Decryption> Decryption;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC-CTS block cipher mode of operation
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2018-01-23 01:13:18 +00:00
|
|
|
/// \since Crypto++ 3.0
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class CIPHER>
|
|
|
|
struct CBC_CTS_Mode : public CipherModeDocumentation
|
|
|
|
{
|
2016-12-03 10:05:56 +00:00
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, CBC_CTS_Encryption> Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Decryption, CBC_CTS_Decryption> Decryption;
|
2015-11-05 06:59:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption>;
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption>;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief CBC mode with ciphertext stealing, external cipher
|
|
|
|
/// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
|
|
|
|
/// on the Crypto++ wiki.
|
2018-01-23 01:13:18 +00:00
|
|
|
/// \since Crypto++ 3.0
|
2015-11-05 06:59:46 +00:00
|
|
|
struct CBC_CTS_Mode_ExternalCipher : public CipherModeDocumentation
|
|
|
|
{
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption> Encryption;
|
|
|
|
typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption> Decryption;
|
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
2016-12-02 19:47:31 +00:00
|
|
|
// Issue 340
|
2017-06-02 09:18:52 +00:00
|
|
|
#if CRYPTOPP_MSC_VERSION
|
|
|
|
# pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2016-12-02 19:47:31 +00:00
|
|
|
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|