more unused
This commit is contained in:
@@ -596,14 +596,6 @@ unsigned int PK_FixedLengthCryptoSystem::CiphertextLength(unsigned int plainText
|
||||
return 0;
|
||||
}
|
||||
|
||||
DecodingResult PK_FixedLengthDecryptor::Decrypt(RandomNumberGenerator &rng, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const
|
||||
{
|
||||
if (cipherTextLength != FixedCiphertextLength())
|
||||
return DecodingResult();
|
||||
|
||||
return FixedLengthDecrypt(rng, cipherText, plainText);
|
||||
}
|
||||
|
||||
unsigned int PK_Signer::Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const
|
||||
{
|
||||
std::auto_ptr<PK_MessageAccumulator> m(messageAccumulator);
|
||||
|
||||
@@ -1151,27 +1151,6 @@ public:
|
||||
#endif
|
||||
};
|
||||
|
||||
//! interface for encryptors with fixed length ciphertext
|
||||
|
||||
class PK_FixedLengthEncryptor : public PK_Encryptor, virtual public PK_FixedLengthCryptoSystem
|
||||
{
|
||||
};
|
||||
|
||||
//! interface for decryptors with fixed length ciphertext
|
||||
|
||||
class PK_FixedLengthDecryptor : public PK_Decryptor, virtual public PK_FixedLengthCryptoSystem
|
||||
{
|
||||
public:
|
||||
//! decrypt a byte string, and return the length of plaintext
|
||||
/*! \pre length of cipherText == CipherTextLength()
|
||||
\pre size of plainText == MaxPlainTextLength()
|
||||
\return the actual length of the plaintext, or 0 if decryption fails.
|
||||
*/
|
||||
virtual DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const =0;
|
||||
|
||||
DecodingResult Decrypt(RandomNumberGenerator &rng, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const;
|
||||
};
|
||||
|
||||
//! interface for public-key signers and verifiers
|
||||
|
||||
/*! This class provides an interface common to signers and verifiers
|
||||
|
||||
@@ -605,38 +605,4 @@ unsigned int StringStore::CopyRangeTo2(BufferedTransformation &target, unsigned
|
||||
return blockedBytes;
|
||||
}
|
||||
|
||||
unsigned int RandomNumberStore::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking)
|
||||
{
|
||||
if (!blocking)
|
||||
throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object");
|
||||
|
||||
unsigned long transferMax = transferBytes;
|
||||
for (transferBytes = 0; transferBytes<transferMax && m_count < m_length; ++transferBytes, ++m_count)
|
||||
target.ChannelPut(channel, m_rng.GenerateByte());
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int NullStore::CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end, const std::string &channel, bool blocking) const
|
||||
{
|
||||
static const byte nullBytes[128] = {0};
|
||||
while (begin < end)
|
||||
{
|
||||
unsigned int len = STDMIN(end-begin, 128UL);
|
||||
unsigned int blockedBytes = target.ChannelPut2(channel, nullBytes, len, 0, blocking);
|
||||
if (blockedBytes)
|
||||
return blockedBytes;
|
||||
begin += len;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int NullStore::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking)
|
||||
{
|
||||
unsigned long begin = 0;
|
||||
unsigned int blockedBytes = NullStore::CopyRangeTo2(target, begin, transferBytes, channel, blocking);
|
||||
transferBytes = begin;
|
||||
m_size -= begin;
|
||||
return blockedBytes;
|
||||
}
|
||||
|
||||
NAMESPACE_END
|
||||
|
||||
@@ -458,44 +458,6 @@ private:
|
||||
unsigned int m_length, m_count;
|
||||
};
|
||||
|
||||
//! .
|
||||
class RandomNumberStore : public Store
|
||||
{
|
||||
public:
|
||||
RandomNumberStore(RandomNumberGenerator &rng, unsigned long length)
|
||||
: m_rng(rng), m_length(length), m_count(0) {}
|
||||
|
||||
bool AnyRetrievable() const {return MaxRetrievable() != 0;}
|
||||
unsigned long MaxRetrievable() const {return m_length-m_count;}
|
||||
|
||||
unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
|
||||
unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const
|
||||
{
|
||||
throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store");
|
||||
}
|
||||
|
||||
private:
|
||||
void StoreInitialize(const NameValuePairs ¶meters) {m_count = 0;}
|
||||
|
||||
RandomNumberGenerator &m_rng;
|
||||
const unsigned long m_length;
|
||||
unsigned long m_count;
|
||||
};
|
||||
|
||||
//! .
|
||||
class NullStore : public Store
|
||||
{
|
||||
public:
|
||||
NullStore(unsigned long size = ULONG_MAX) : m_size(size) {}
|
||||
void StoreInitialize(const NameValuePairs ¶meters) {}
|
||||
unsigned long MaxRetrievable() const {return m_size;}
|
||||
unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
|
||||
unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
|
||||
|
||||
private:
|
||||
unsigned long m_size;
|
||||
};
|
||||
|
||||
//! A Filter that pumps data into its attachment as input
|
||||
class Source : public InputRejecting<Filter>
|
||||
{
|
||||
@@ -570,14 +532,6 @@ public:
|
||||
: SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
|
||||
};
|
||||
|
||||
//! .
|
||||
class RandomNumberSource : public SourceTemplate<RandomNumberStore>
|
||||
{
|
||||
public:
|
||||
RandomNumberSource(RandomNumberGenerator &rng, unsigned int length, bool pumpAll, BufferedTransformation *attachment = NULL)
|
||||
: SourceTemplate<RandomNumberStore>(attachment, RandomNumberStore(rng, length)) {if (pumpAll) PumpAll();}
|
||||
};
|
||||
|
||||
NAMESPACE_END
|
||||
|
||||
#endif
|
||||
|
||||
@@ -369,64 +369,6 @@ inline word64 ByteReverse(word64 value)
|
||||
}
|
||||
#endif
|
||||
|
||||
inline byte BitReverse(byte value)
|
||||
{
|
||||
value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1);
|
||||
value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2);
|
||||
return rotlFixed(value, 4);
|
||||
}
|
||||
|
||||
inline word16 BitReverse(word16 value)
|
||||
{
|
||||
value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1);
|
||||
value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2);
|
||||
value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4);
|
||||
return ByteReverse(value);
|
||||
}
|
||||
|
||||
inline word32 BitReverse(word32 value)
|
||||
{
|
||||
value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1);
|
||||
value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2);
|
||||
value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4);
|
||||
return ByteReverse(value);
|
||||
}
|
||||
|
||||
#ifdef WORD64_AVAILABLE
|
||||
inline word64 BitReverse(word64 value)
|
||||
{
|
||||
#ifdef SLOW_WORD64
|
||||
return (word64(BitReverse(word32(value))) << 32) | BitReverse(word32(value>>32));
|
||||
#else
|
||||
value = ((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | ((value & W64LIT(0x5555555555555555)) << 1);
|
||||
value = ((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | ((value & W64LIT(0x3333333333333333)) << 2);
|
||||
value = ((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | ((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4);
|
||||
return ByteReverse(value);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
inline T BitReverse(T value)
|
||||
{
|
||||
if (sizeof(T) == 1)
|
||||
return (T)BitReverse((byte)value);
|
||||
else if (sizeof(T) == 2)
|
||||
return (T)BitReverse((word16)value);
|
||||
else if (sizeof(T) == 4)
|
||||
return (T)BitReverse((word32)value);
|
||||
else
|
||||
{
|
||||
#ifdef WORD64_AVAILABLE
|
||||
assert(sizeof(T) == 8);
|
||||
return (T)BitReverse((word64)value);
|
||||
#else
|
||||
assert(false);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T ConditionalByteReverse(ByteOrder order, T value)
|
||||
{
|
||||
@@ -461,31 +403,6 @@ inline void GetUserKey(ByteOrder order, T *out, unsigned int outlen, const byte
|
||||
ConditionalByteReverse(order, out, out, RoundUpToMultipleOf(inlen, U));
|
||||
}
|
||||
|
||||
inline byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, byte*)
|
||||
{
|
||||
return block[0];
|
||||
}
|
||||
|
||||
inline word16 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, word16*)
|
||||
{
|
||||
return (order == BIG_ENDIAN_ORDER)
|
||||
? block[1] | (block[0] << 8)
|
||||
: block[0] | (block[1] << 8);
|
||||
}
|
||||
|
||||
inline word32 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, word32*)
|
||||
{
|
||||
return (order == BIG_ENDIAN_ORDER)
|
||||
? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24)
|
||||
: word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T UnalignedGetWord(ByteOrder order, const byte *block, T*dummy=NULL)
|
||||
{
|
||||
return UnalignedGetWordNonTemplate(order, block, dummy);
|
||||
}
|
||||
|
||||
inline void UnalignedPutWord(ByteOrder order, byte *block, byte value, const byte *xorBlock = NULL)
|
||||
{
|
||||
block[0] = xorBlock ? (value ^ xorBlock[0]) : value;
|
||||
@@ -537,24 +454,6 @@ inline void UnalignedPutWord(ByteOrder order, byte *block, word32 value, const b
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block)
|
||||
{
|
||||
if (assumeAligned)
|
||||
{
|
||||
assert(IsAligned<T>(block));
|
||||
return ConditionalByteReverse(order, *reinterpret_cast<const T *>(block));
|
||||
}
|
||||
else
|
||||
return UnalignedGetWord<T>(order, block);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void GetWord(bool assumeAligned, ByteOrder order, T &result, const byte *block)
|
||||
{
|
||||
result = GetWord<T>(assumeAligned, order, block);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock = NULL)
|
||||
{
|
||||
@@ -570,73 +469,6 @@ inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, c
|
||||
UnalignedPutWord(order, block, value, xorBlock);
|
||||
}
|
||||
|
||||
template <class T, class B, bool A=true>
|
||||
class GetBlock
|
||||
{
|
||||
public:
|
||||
GetBlock(const void *block)
|
||||
: m_block((const byte *)block) {}
|
||||
|
||||
template <class U>
|
||||
inline GetBlock<T, B, A> & operator()(U &x)
|
||||
{
|
||||
CRYPTOPP_COMPILE_ASSERT(sizeof(U) >= sizeof(T));
|
||||
x = GetWord<T>(A, B::ToEnum(), m_block);
|
||||
m_block += sizeof(T);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
const byte *m_block;
|
||||
};
|
||||
|
||||
template <class T, class B, bool A=true>
|
||||
class PutBlock
|
||||
{
|
||||
public:
|
||||
PutBlock(const void *xorBlock, void *block)
|
||||
: m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {}
|
||||
|
||||
template <class U>
|
||||
inline PutBlock<T, B, A> & operator()(U x)
|
||||
{
|
||||
PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock);
|
||||
m_block += sizeof(T);
|
||||
if (m_xorBlock)
|
||||
m_xorBlock += sizeof(T);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
const byte *m_xorBlock;
|
||||
byte *m_block;
|
||||
};
|
||||
|
||||
template <class T, class B, bool A=true>
|
||||
struct BlockGetAndPut
|
||||
{
|
||||
// function needed because of C++ grammatical ambiguity between expression-statements and declarations
|
||||
static inline GetBlock<T, B, A> Get(const void *block) {return GetBlock<T, B, A>(block);}
|
||||
typedef PutBlock<T, B, A> Put;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
std::string WordToString(T value, ByteOrder order = BIG_ENDIAN_ORDER)
|
||||
{
|
||||
if (!NativeByteOrderIs(order))
|
||||
value = ByteReverse(value);
|
||||
|
||||
return std::string((char *)&value, sizeof(value));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T StringToWord(const std::string &str, ByteOrder order = BIG_ENDIAN_ORDER)
|
||||
{
|
||||
T value = 0;
|
||||
memcpy(&value, str.data(), STDMIN(sizeof(value), str.size()));
|
||||
return NativeByteOrderIs(order) ? value : ByteReverse(value);
|
||||
}
|
||||
|
||||
// ************** help remove warning on g++ ***************
|
||||
|
||||
template <bool overflow> struct SafeShifter;
|
||||
|
||||
@@ -150,23 +150,6 @@ public:
|
||||
void ProcessBlocks(byte *outString, const byte *inString, unsigned int numberOfBlocks);
|
||||
};
|
||||
|
||||
class CBC_CTS_Encryption : public CBC_Encryption
|
||||
{
|
||||
public:
|
||||
void SetStolenIV(byte *iv) {m_stolenIV = iv;}
|
||||
unsigned int MinLastBlockSize() const {return BlockSize()+1;}
|
||||
void ProcessLastBlock(byte *outString, const byte *inString, unsigned int length);
|
||||
|
||||
protected:
|
||||
void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length)
|
||||
{
|
||||
CBC_Encryption::UncheckedSetKey(params, key, length);
|
||||
m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), (byte *)NULL);
|
||||
}
|
||||
|
||||
byte *m_stolenIV;
|
||||
};
|
||||
|
||||
class CBC_Decryption : public CBC_ModeBase
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -9,67 +9,6 @@ NAMESPACE_BEGIN(CryptoPP)
|
||||
template<> const byte PKCS_DigestDecoration<SHA>::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14};
|
||||
template<> const unsigned int PKCS_DigestDecoration<SHA>::length = sizeof(PKCS_DigestDecoration<SHA>::decoration);
|
||||
|
||||
unsigned int PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(unsigned int paddedLength) const
|
||||
{
|
||||
return SaturatingSubtract(paddedLength/8, 10U);
|
||||
}
|
||||
|
||||
void PKCS_EncryptionPaddingScheme::Pad(RandomNumberGenerator &rng, const byte *input, unsigned int inputLen, byte *pkcsBlock, unsigned int pkcsBlockLen) const
|
||||
{
|
||||
assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen)); // this should be checked by caller
|
||||
|
||||
// convert from bit length to byte length
|
||||
if (pkcsBlockLen % 8 != 0)
|
||||
{
|
||||
pkcsBlock[0] = 0;
|
||||
pkcsBlock++;
|
||||
}
|
||||
pkcsBlockLen /= 8;
|
||||
|
||||
pkcsBlock[0] = 2; // block type 2
|
||||
|
||||
// pad with non-zero random bytes
|
||||
for (unsigned i = 1; i < pkcsBlockLen-inputLen-1; i++)
|
||||
pkcsBlock[i] = (byte)rng.GenerateWord32(1, 0xff);
|
||||
|
||||
pkcsBlock[pkcsBlockLen-inputLen-1] = 0; // separator
|
||||
memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
|
||||
}
|
||||
|
||||
DecodingResult PKCS_EncryptionPaddingScheme::Unpad(const byte *pkcsBlock, unsigned int pkcsBlockLen, byte *output) const
|
||||
{
|
||||
bool invalid = false;
|
||||
unsigned int maxOutputLen = MaxUnpaddedLength(pkcsBlockLen);
|
||||
|
||||
// convert from bit length to byte length
|
||||
if (pkcsBlockLen % 8 != 0)
|
||||
{
|
||||
invalid = (pkcsBlock[0] != 0) || invalid;
|
||||
pkcsBlock++;
|
||||
}
|
||||
pkcsBlockLen /= 8;
|
||||
|
||||
// Require block type 2.
|
||||
invalid = (pkcsBlock[0] != 2) || invalid;
|
||||
|
||||
// skip past the padding until we find the separator
|
||||
unsigned i=1;
|
||||
while (i<pkcsBlockLen && pkcsBlock[i++]) { // null body
|
||||
}
|
||||
assert(i==pkcsBlockLen || pkcsBlock[i-1]==0);
|
||||
|
||||
unsigned int outputLen = pkcsBlockLen - i;
|
||||
invalid = (outputLen > maxOutputLen) || invalid;
|
||||
|
||||
if (invalid)
|
||||
return DecodingResult();
|
||||
|
||||
memcpy (output, pkcsBlock+i, outputLen);
|
||||
return DecodingResult(outputLen);
|
||||
}
|
||||
|
||||
// ********************************************************
|
||||
|
||||
void PKCS1v15_SignatureMessageEncodingMethod::ComputeMessageRepresentative(RandomNumberGenerator &rng,
|
||||
const byte *recoverableMessage, unsigned int recoverableMessageLength,
|
||||
HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
|
||||
|
||||
@@ -7,15 +7,6 @@
|
||||
NAMESPACE_BEGIN(CryptoPP)
|
||||
|
||||
//! <a href="http://www.weidai.com/scan-mirror/ca.html#cem_PKCS1-1.5">EME-PKCS1-v1_5</a>
|
||||
class PKCS_EncryptionPaddingScheme : public PK_EncryptionMessageEncodingMethod
|
||||
{
|
||||
public:
|
||||
static const char * StaticAlgorithmName() {return "EME-PKCS1-v1_5";}
|
||||
|
||||
unsigned int MaxUnpaddedLength(unsigned int paddedLength) const;
|
||||
void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedLength) const;
|
||||
DecodingResult Unpad(const byte *padded, unsigned int paddedLength, byte *raw) const;
|
||||
};
|
||||
|
||||
template <class H> struct PKCS_DigestDecoration
|
||||
{
|
||||
@@ -48,14 +39,11 @@ public:
|
||||
|
||||
//! PKCS #1 version 1.5, for use with RSAES and RSASS
|
||||
/*! The following hash functions are supported for signature: SHA, MD2, MD5, RIPEMD160, SHA256, SHA384, SHA512. */
|
||||
struct PKCS1v15 : public SignatureStandard, public EncryptionStandard
|
||||
struct PKCS1v15 : public SignatureStandard
|
||||
{
|
||||
typedef PKCS_EncryptionPaddingScheme EncryptionMessageEncodingMethod;
|
||||
typedef PKCS1v15_SignatureMessageEncodingMethod SignatureMessageEncodingMethod;
|
||||
};
|
||||
|
||||
// PKCS_DecoratedHashModule can be instantiated with the following
|
||||
// classes as specified in PKCS#1 v2.0 and P1363a
|
||||
class SHA;
|
||||
|
||||
NAMESPACE_END
|
||||
|
||||
@@ -99,24 +99,4 @@ DecodingResult TF_VerifierBase::RecoverAndRestart(byte *recoveredMessage, PK_Mes
|
||||
return result;
|
||||
}
|
||||
|
||||
DecodingResult TF_DecryptorBase::FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const
|
||||
{
|
||||
SecByteBlock paddedBlock(PaddedBlockByteLength());
|
||||
Integer x = GetTrapdoorFunctionInterface().CalculateInverse(rng, Integer(cipherText, FixedCiphertextLength()));
|
||||
if (x.ByteCount() > paddedBlock.size())
|
||||
x = Integer::Zero(); // don't return false here to prevent timing attack
|
||||
x.Encode(paddedBlock, paddedBlock.size());
|
||||
return GetMessageEncodingInterface().Unpad(paddedBlock, PaddedBlockBitLength(), plainText);
|
||||
}
|
||||
|
||||
void TF_EncryptorBase::Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const
|
||||
{
|
||||
if (plainTextLength > FixedMaxPlaintextLength())
|
||||
throw InvalidArgument(AlgorithmName() + ": message too long for this public key");
|
||||
|
||||
SecByteBlock paddedBlock(PaddedBlockByteLength());
|
||||
GetMessageEncodingInterface().Pad(rng, plainText, plainTextLength, paddedBlock, PaddedBlockBitLength());
|
||||
GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng, Integer(paddedBlock, paddedBlock.size())).Encode(cipherText, FixedCiphertextLength());
|
||||
}
|
||||
|
||||
NAMESPACE_END
|
||||
|
||||
@@ -105,22 +105,6 @@ public:
|
||||
|
||||
// ********************************************************
|
||||
|
||||
//! .
|
||||
class PK_EncryptionMessageEncodingMethod
|
||||
{
|
||||
public:
|
||||
virtual ~PK_EncryptionMessageEncodingMethod() {}
|
||||
|
||||
//! max size of unpadded message in bytes, given max size of padded message in bits (1 less than size of modulus)
|
||||
virtual unsigned int MaxUnpaddedLength(unsigned int paddedLength) const =0;
|
||||
|
||||
virtual void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedBitLength) const =0;
|
||||
|
||||
virtual DecodingResult Unpad(const byte *padded, unsigned int paddedBitLength, byte *raw) const =0;
|
||||
};
|
||||
|
||||
// ********************************************************
|
||||
|
||||
//! .
|
||||
template <class TFI, class MEI>
|
||||
class TF_Base
|
||||
@@ -137,35 +121,6 @@ protected:
|
||||
|
||||
// ********************************************************
|
||||
|
||||
//! .
|
||||
template <class INTERFACE, class BASE>
|
||||
class TF_CryptoSystemBase : public INTERFACE, protected BASE
|
||||
{
|
||||
public:
|
||||
unsigned int FixedMaxPlaintextLength() const {return this->GetMessageEncodingInterface().MaxUnpaddedLength(PaddedBlockBitLength());}
|
||||
unsigned int FixedCiphertextLength() const {return this->GetTrapdoorFunctionBounds().MaxImage().ByteCount();}
|
||||
|
||||
protected:
|
||||
unsigned int PaddedBlockByteLength() const {return BitsToBytes(PaddedBlockBitLength());}
|
||||
unsigned int PaddedBlockBitLength() const {return this->GetTrapdoorFunctionBounds().PreimageBound().BitCount()-1;}
|
||||
};
|
||||
|
||||
//! .
|
||||
class TF_DecryptorBase : public TF_CryptoSystemBase<PK_FixedLengthDecryptor, TF_Base<TrapdoorFunctionInverse, PK_EncryptionMessageEncodingMethod> >
|
||||
{
|
||||
public:
|
||||
DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const;
|
||||
};
|
||||
|
||||
//! .
|
||||
class TF_EncryptorBase : public TF_CryptoSystemBase<PK_FixedLengthEncryptor, TF_Base<RandomizedTrapdoorFunction, PK_EncryptionMessageEncodingMethod> >
|
||||
{
|
||||
public:
|
||||
void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const;
|
||||
};
|
||||
|
||||
// ********************************************************
|
||||
|
||||
typedef std::pair<const byte *, unsigned int> HashIdentifier;
|
||||
|
||||
//! .
|
||||
@@ -462,18 +417,6 @@ public:
|
||||
void CopyKeyInto(typename SCHEME_OPTIONS::PublicKey &key) const {key = this->GetKey();}
|
||||
};
|
||||
|
||||
//! .
|
||||
template <class SCHEME_OPTIONS>
|
||||
class TF_DecryptorImpl : public TF_PrivateObjectImpl<TF_DecryptorBase, SCHEME_OPTIONS>
|
||||
{
|
||||
};
|
||||
|
||||
//! .
|
||||
template <class SCHEME_OPTIONS>
|
||||
class TF_EncryptorImpl : public TF_PublicObjectImpl<TF_EncryptorBase, SCHEME_OPTIONS>
|
||||
{
|
||||
};
|
||||
|
||||
//! .
|
||||
template <class SCHEME_OPTIONS>
|
||||
class TF_SignerImpl : public TF_PrivateObjectImpl<TF_SignerBase, SCHEME_OPTIONS>
|
||||
@@ -496,35 +439,8 @@ class TF_VerifierImpl : public TF_PublicObjectImpl<TF_VerifierBase, SCHEME_OPTIO
|
||||
|
||||
// ********************************************************
|
||||
|
||||
class MaskGeneratingFunction
|
||||
{
|
||||
public:
|
||||
virtual ~MaskGeneratingFunction() {}
|
||||
virtual void GenerateAndMask(HashTransformation &hash, byte *output, unsigned int outputLength, const byte *input, unsigned int inputLength, bool mask = true) const =0;
|
||||
};
|
||||
|
||||
void P1363_MGF1KDF2_Common(HashTransformation &hash, byte *output, unsigned int outputLength, const byte *input, unsigned int inputLength, bool mask, unsigned int counterStart);
|
||||
|
||||
//! .
|
||||
class P1363_MGF1 : public MaskGeneratingFunction
|
||||
{
|
||||
public:
|
||||
static const char * StaticAlgorithmName() {return "MGF1";}
|
||||
#if 0
|
||||
// VC60 workaround: this function causes internal compiler error
|
||||
template <class H>
|
||||
static void GenerateAndMaskTemplate(byte *output, unsigned int outputLength, const byte *input, unsigned int inputLength, H* dummy=NULL)
|
||||
{
|
||||
H h;
|
||||
P1363_MGF1KDF2_Common(h, output, outputLength, input, inputLength, mask, 0);
|
||||
}
|
||||
#endif
|
||||
void GenerateAndMask(HashTransformation &hash, byte *output, unsigned int outputLength, const byte *input, unsigned int inputLength, bool mask = true) const
|
||||
{
|
||||
P1363_MGF1KDF2_Common(hash, output, outputLength, input, inputLength, mask, 0);
|
||||
}
|
||||
};
|
||||
|
||||
// ********************************************************
|
||||
|
||||
//! .
|
||||
@@ -653,34 +569,9 @@ public:
|
||||
#endif
|
||||
};
|
||||
|
||||
//! Base class for public key encryption standard classes. These classes are used to select from variants of algorithms. Note that not all standards apply to all algorithms.
|
||||
struct EncryptionStandard {};
|
||||
|
||||
//! Base class for public key signature standard classes. These classes are used to select from variants of algorithms. Note that not all standards apply to all algorithms.
|
||||
struct SignatureStandard {};
|
||||
|
||||
template <class STANDARD, class KEYS, class ALG_INFO>
|
||||
class TF_ES;
|
||||
|
||||
//! Trapdoor Function Based Encryption Scheme
|
||||
template <class STANDARD, class KEYS, class ALG_INFO = TF_ES<STANDARD, KEYS, int> >
|
||||
class TF_ES : public KEYS
|
||||
{
|
||||
typedef typename STANDARD::EncryptionMessageEncodingMethod MessageEncodingMethod;
|
||||
|
||||
public:
|
||||
//! see EncryptionStandard for a list of standards
|
||||
typedef STANDARD Standard;
|
||||
typedef TF_CryptoSchemeOptions<ALG_INFO, KEYS, MessageEncodingMethod> SchemeOptions;
|
||||
|
||||
static std::string StaticAlgorithmName() {return KEYS::StaticAlgorithmName() + "/" + MessageEncodingMethod::StaticAlgorithmName();}
|
||||
|
||||
//! implements PK_Decryptor interface
|
||||
typedef PK_FinalTemplate<TF_DecryptorImpl<SchemeOptions> > Decryptor;
|
||||
//! implements PK_Encryptor interface
|
||||
typedef PK_FinalTemplate<TF_EncryptorImpl<SchemeOptions> > Encryptor;
|
||||
};
|
||||
|
||||
template <class STANDARD, class H, class KEYS, class ALG_INFO> // VC60 workaround: doesn't work if KEYS is first parameter
|
||||
class TF_SS;
|
||||
|
||||
|
||||
@@ -10,13 +10,9 @@
|
||||
#include "algparam.h"
|
||||
|
||||
|
||||
#include "oaep.cpp"
|
||||
|
||||
NAMESPACE_BEGIN(CryptoPP)
|
||||
|
||||
|
||||
template class OAEP<SHA>;
|
||||
|
||||
OID RSAFunction::GetAlgorithmID() const
|
||||
{
|
||||
return ASN1::rsaEncryption();
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
*/
|
||||
|
||||
#include "pkcspad.h"
|
||||
#include "oaep.h"
|
||||
#include "integer.h"
|
||||
#include "asn.h"
|
||||
|
||||
@@ -108,12 +107,6 @@ struct RSA
|
||||
typedef InvertibleRSAFunction PrivateKey;
|
||||
};
|
||||
|
||||
//! <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>
|
||||
template <class STANDARD>
|
||||
struct RSAES : public TF_ES<STANDARD, RSA>
|
||||
{
|
||||
};
|
||||
|
||||
//! <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>
|
||||
/*! See documentation of PKCS1v15 for a list of hash functions that can be used with it. */
|
||||
template <class STANDARD, class H>
|
||||
@@ -121,10 +114,6 @@ struct RSASS : public TF_SS<STANDARD, H, RSA>
|
||||
{
|
||||
};
|
||||
|
||||
// The two RSA encryption schemes defined in PKCS #1 v2.0
|
||||
typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
|
||||
typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
|
||||
|
||||
// The three RSA signature schemes defined in PKCS #1 v2.0
|
||||
typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
|
||||
typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
|
||||
|
||||
@@ -11,11 +11,6 @@
|
||||
|
||||
NAMESPACE_BEGIN(CryptoPP)
|
||||
|
||||
inline CipherDir ReverseCipherDir(CipherDir dir)
|
||||
{
|
||||
return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
|
||||
}
|
||||
|
||||
//! .
|
||||
template <unsigned int N>
|
||||
class FixedBlockSize
|
||||
@@ -24,52 +19,6 @@ public:
|
||||
enum {BLOCKSIZE = N};
|
||||
};
|
||||
|
||||
// ************** rounds ***************
|
||||
|
||||
//! .
|
||||
template <unsigned int R>
|
||||
class FixedRounds
|
||||
{
|
||||
public:
|
||||
enum {ROUNDS = R};
|
||||
|
||||
protected:
|
||||
template <class T>
|
||||
static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs ¶m)
|
||||
{
|
||||
obj->ThrowIfInvalidKeyLength(length);
|
||||
int rounds = param.GetIntValueWithDefault("Rounds", ROUNDS);
|
||||
if (rounds != ROUNDS)
|
||||
throw InvalidRounds(obj->StaticAlgorithmName(), rounds);
|
||||
obj->UncheckedSetKey(dir, key, length);
|
||||
}
|
||||
};
|
||||
|
||||
//! .
|
||||
template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX> // use INT_MAX here because enums are treated as signed ints
|
||||
class VariableRounds
|
||||
{
|
||||
public:
|
||||
enum {DEFAULT_ROUNDS = D, MIN_ROUNDS = N, MAX_ROUNDS = M};
|
||||
static unsigned int StaticGetDefaultRounds(unsigned int keylength) {return DEFAULT_ROUNDS;}
|
||||
|
||||
protected:
|
||||
static inline void AssertValidRounds(unsigned int rounds)
|
||||
{
|
||||
assert(rounds >= MIN_ROUNDS && rounds <= MAX_ROUNDS);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs ¶m)
|
||||
{
|
||||
obj->ThrowIfInvalidKeyLength(length);
|
||||
int rounds = param.GetIntValueWithDefault("Rounds", obj->StaticGetDefaultRounds(length));
|
||||
if (rounds < (unsigned int)MIN_ROUNDS || rounds > (unsigned int)MAX_ROUNDS)
|
||||
throw InvalidRounds(obj->AlgorithmName(), rounds);
|
||||
obj->UncheckedSetKey(dir, key, length, rounds);
|
||||
}
|
||||
};
|
||||
|
||||
// ************** key length ***************
|
||||
|
||||
//! .
|
||||
@@ -82,45 +31,6 @@ public:
|
||||
static unsigned int StaticGetValidKeyLength(unsigned int) {return KEYLENGTH;}
|
||||
};
|
||||
|
||||
/// support query of variable key length, template parameters are default, min, max, multiple (default multiple 1)
|
||||
template <unsigned int D, unsigned int N, unsigned int M, unsigned int Q = 1, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE>
|
||||
class VariableKeyLength
|
||||
{
|
||||
// make these private to avoid Doxygen documenting them in all derived classes
|
||||
CRYPTOPP_COMPILE_ASSERT(Q > 0);
|
||||
CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
|
||||
CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
|
||||
CRYPTOPP_COMPILE_ASSERT(N < M);
|
||||
CRYPTOPP_COMPILE_ASSERT(D >= N && M >= D);
|
||||
|
||||
public:
|
||||
enum {MIN_KEYLENGTH=N, MAX_KEYLENGTH=M, DEFAULT_KEYLENGTH=D, KEYLENGTH_MULTIPLE=Q};
|
||||
enum {IV_REQUIREMENT = IV_REQ};
|
||||
static unsigned int StaticGetValidKeyLength(unsigned int n)
|
||||
{
|
||||
if (n < (unsigned int)MIN_KEYLENGTH)
|
||||
return MIN_KEYLENGTH;
|
||||
else if (n > (unsigned int)MAX_KEYLENGTH)
|
||||
return (unsigned int)MAX_KEYLENGTH;
|
||||
else
|
||||
{
|
||||
n += KEYLENGTH_MULTIPLE-1;
|
||||
return n - n%KEYLENGTH_MULTIPLE;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// support query of key length that's the same as another class
|
||||
template <class T>
|
||||
class SameKeyLengthAs
|
||||
{
|
||||
public:
|
||||
enum {MIN_KEYLENGTH=T::MIN_KEYLENGTH, MAX_KEYLENGTH=T::MAX_KEYLENGTH, DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH};
|
||||
enum {IV_REQUIREMENT = T::IV_REQUIREMENT};
|
||||
static unsigned int StaticGetValidKeyLength(unsigned int keylength)
|
||||
{return T::StaticGetValidKeyLength(keylength);}
|
||||
};
|
||||
|
||||
// ************** implementation helper for SimpledKeyed ***************
|
||||
|
||||
template <class T>
|
||||
@@ -182,45 +92,8 @@ public:
|
||||
Clonable * Clone() const {return new BlockCipherTemplate<DIR, BASE>(*this);}
|
||||
};
|
||||
|
||||
//! .
|
||||
template <class BASE>
|
||||
class MessageAuthenticationCodeTemplate : public
|
||||
#ifdef CRYPTOPP_DOXYGEN_PROCESSING
|
||||
MessageAuthenticationCode
|
||||
#else
|
||||
SimpleKeyingInterfaceImpl<BASE>
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
MessageAuthenticationCodeTemplate() {}
|
||||
MessageAuthenticationCodeTemplate(const byte *key)
|
||||
{SetKey(key, this->DEFAULT_KEYLENGTH);}
|
||||
MessageAuthenticationCodeTemplate(const byte *key, unsigned int length)
|
||||
{SetKey(key, length);}
|
||||
|
||||
std::string AlgorithmName() const {return this->StaticAlgorithmName();}
|
||||
|
||||
void SetKey(const byte *key, unsigned int length, const NameValuePairs ¶m = g_nullNameValuePairs)
|
||||
{
|
||||
CheckedSetKey(this, Empty(), key, length, param);
|
||||
}
|
||||
|
||||
Clonable * Clone() const {return new MessageAuthenticationCodeTemplate<BASE>(*this);}
|
||||
};
|
||||
|
||||
// ************** documentation ***************
|
||||
|
||||
//! These objects usually should not be used directly. See CipherModeDocumentation instead.
|
||||
/*! Each class derived from this one defines two types, Encryption and Decryption,
|
||||
both of which implement the BlockCipher interface. */
|
||||
struct BlockCipherDocumentation
|
||||
{
|
||||
//! implements the BlockCipher interface
|
||||
typedef BlockCipher Encryption;
|
||||
//! implements the BlockCipher interface
|
||||
typedef BlockCipher Decryption;
|
||||
};
|
||||
|
||||
/*! \brief Each class derived from this one defines two types, Encryption and Decryption,
|
||||
both of which implement the SymmetricCipher interface. See CipherModeDocumentation
|
||||
for information about using block ciphers. */
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
// test.cpp - written and placed in the public domain by Wei Dai
|
||||
|
||||
#include "sha.h"
|
||||
#include "files.h"
|
||||
#include "rng.h"
|
||||
#include "rsa.h"
|
||||
#include "randpool.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef CRYPTOPP_WIN32_AVAILABLE
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#if (_MSC_VER >= 1000)
|
||||
#include <crtdbg.h> // for the debug heap
|
||||
#endif
|
||||
|
||||
#if defined(__MWERKS__) && defined(macintosh)
|
||||
#include <console.h>
|
||||
#endif
|
||||
|
||||
USING_NAMESPACE(CryptoPP)
|
||||
USING_NAMESPACE(std)
|
||||
|
||||
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);
|
||||
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);
|
||||
string RSADecryptString(const char *privFilename, const char *ciphertext);
|
||||
void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename);
|
||||
bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename);
|
||||
|
||||
int (*AdhocTest)(int argc, char *argv[]) = NULL;
|
||||
|
||||
#ifdef __BCPLUSPLUS__
|
||||
int cmain(int argc, char *argv[])
|
||||
#elif defined(_MSC_VER)
|
||||
int __cdecl main(int argc, char *argv[])
|
||||
#else
|
||||
int main(int argc, char *argv[])
|
||||
#endif
|
||||
{
|
||||
#ifdef _CRTDBG_LEAK_CHECK_DF
|
||||
// Turn on leak-checking
|
||||
int tempflag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
|
||||
tempflag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag( tempflag );
|
||||
#endif
|
||||
|
||||
#if defined(__MWERKS__) && defined(macintosh)
|
||||
argc = ccommand(&argv);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
std::string command, executableName, edcFilename;
|
||||
|
||||
if (argc < 2)
|
||||
command = 'h';
|
||||
else
|
||||
command = argv[1];
|
||||
|
||||
switch (command[0])
|
||||
{
|
||||
case 'g':
|
||||
{
|
||||
char seed[1024], privFilename[128], pubFilename[128];
|
||||
unsigned int keyLength;
|
||||
|
||||
cout << "Key length in bits: ";
|
||||
cin >> keyLength;
|
||||
|
||||
cout << "\nSave private key to file: ";
|
||||
cin >> privFilename;
|
||||
|
||||
cout << "\nSave public key to file: ";
|
||||
cin >> pubFilename;
|
||||
|
||||
cout << "\nRandom Seed: ";
|
||||
ws(cin);
|
||||
cin.getline(seed, 1024);
|
||||
|
||||
GenerateRSAKey(keyLength, privFilename, pubFilename, seed);
|
||||
return 0;
|
||||
}
|
||||
case 'r':
|
||||
{
|
||||
switch (argv[1][1])
|
||||
{
|
||||
case 's':
|
||||
RSASignFile(argv[2], argv[3], argv[4]);
|
||||
return 0;
|
||||
case 'v':
|
||||
{
|
||||
bool verified = RSAVerifyFile(argv[2], argv[3], argv[4]);
|
||||
cout << (verified ? "valid signature" : "invalid signature") << endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
FileSource usage("usage.dat", true, new FileSink(cout));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
catch(CryptoPP::Exception &e)
|
||||
{
|
||||
cout << "\nCryptoPP::Exception caught: " << e.what() << endl;
|
||||
return -1;
|
||||
}
|
||||
catch(std::exception &e)
|
||||
{
|
||||
cout << "\nstd::exception caught: " << e.what() << endl;
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
RandomPool & GlobalRNG()
|
||||
{
|
||||
static RandomPool randomPool;
|
||||
return randomPool;
|
||||
}
|
||||
|
||||
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed)
|
||||
{
|
||||
RandomPool randPool;
|
||||
randPool.Put((byte *)seed, strlen(seed));
|
||||
|
||||
RSAES_PKCS1v15_Decryptor priv(randPool, keyLength);
|
||||
FileSink privFile(privFilename);
|
||||
priv.DEREncode(privFile);
|
||||
privFile.MessageEnd();
|
||||
|
||||
RSAES_PKCS1v15_Encryptor pub(priv);
|
||||
FileSink pubFile(pubFilename);
|
||||
pub.DEREncode(pubFile);
|
||||
pubFile.MessageEnd();
|
||||
}
|
||||
|
||||
void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename)
|
||||
{
|
||||
FileSource privFile(privFilename, true);
|
||||
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
|
||||
// RSASSA_PKCS1v15_SHA_Signer ignores the rng. Use a real RNG for other signature schemes!
|
||||
FileSource f(messageFilename, true, new SignerFilter(GlobalRNG(), priv, new FileSink(signatureFilename)));
|
||||
}
|
||||
|
||||
bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename)
|
||||
{
|
||||
FileSource pubFile(pubFilename, true);
|
||||
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
|
||||
|
||||
FileSource signatureFile(signatureFilename, true);
|
||||
if (signatureFile.MaxRetrievable() != pub.SignatureLength())
|
||||
return false;
|
||||
SecByteBlock signature(pub.SignatureLength());
|
||||
signatureFile.Get(signature, signature.size());
|
||||
|
||||
VerifierFilter *verifierFilter = new VerifierFilter(pub);
|
||||
verifierFilter->Put(signature, pub.SignatureLength());
|
||||
FileSource f(messageFilename, true, verifierFilter);
|
||||
|
||||
return verifierFilter->GetLastResult();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user