📄 cryptlib.h
字号:
lword CopyTo(BufferedTransformation &target, lword copyMax=LWORD_MAX, const std::string &channel=NULL_CHANNEL) const
{return CopyRangeTo(target, 0, copyMax, channel);}
//! copy copyMax bytes of the buffered output, starting at position (relative to current position), to target as input
lword CopyRangeTo(BufferedTransformation &target, lword position, lword copyMax=LWORD_MAX, const std::string &channel=NULL_CHANNEL) const
{lword i = position; CopyRangeTo2(target, i, i+copyMax, channel); return i-position;}
#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
unsigned long MaxRetrieveable() const {return MaxRetrievable();}
#endif
//@}
//! \name RETRIEVAL OF MULTIPLE MESSAGES
//@{
//!
virtual lword TotalBytesRetrievable() const;
//! number of times MessageEnd() has been received minus messages retrieved or skipped
virtual unsigned int NumberOfMessages() const;
//! returns true if NumberOfMessages() > 0
virtual bool AnyMessages() const;
//! start retrieving the next message
/*!
Returns false if no more messages exist or this message
is not completely retrieved.
*/
virtual bool GetNextMessage();
//! skip count number of messages
virtual unsigned int SkipMessages(unsigned int count=UINT_MAX);
//!
unsigned int TransferMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL)
{TransferMessagesTo2(target, count, channel); return count;}
//!
unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL) const;
//!
virtual void SkipAll();
//!
void TransferAllTo(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL)
{TransferAllTo2(target, channel);}
//!
void CopyAllTo(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL) const;
virtual bool GetNextMessageSeries() {return false;}
virtual unsigned int NumberOfMessagesInThisSeries() const {return NumberOfMessages();}
virtual unsigned int NumberOfMessageSeries() const {return 0;}
//@}
//! \name NON-BLOCKING TRANSFER OF OUTPUT
//@{
//! upon return, byteCount contains number of bytes that have finished being transfered, and returns the number of bytes left in the current transfer block
virtual size_t TransferTo2(BufferedTransformation &target, lword &byteCount, const std::string &channel=NULL_CHANNEL, bool blocking=true) =0;
//! upon return, begin contains the start position of data yet to be finished copying, and returns the number of bytes left in the current transfer block
virtual size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const =0;
//! upon return, messageCount contains number of messages that have finished being transfered, and returns the number of bytes left in the current transfer block
size_t TransferMessagesTo2(BufferedTransformation &target, unsigned int &messageCount, const std::string &channel=NULL_CHANNEL, bool blocking=true);
//! returns the number of bytes left in the current transfer block
size_t TransferAllTo2(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL, bool blocking=true);
//@}
//! \name CHANNELS
//@{
struct NoChannelSupport : public NotImplemented
{NoChannelSupport() : NotImplemented("BufferedTransformation: this object doesn't support multiple channels") {}};
size_t ChannelPut(const std::string &channel, byte inByte, bool blocking=true)
{return ChannelPut(channel, &inByte, 1, blocking);}
size_t ChannelPut(const std::string &channel, const byte *inString, size_t length, bool blocking=true)
{return ChannelPut2(channel, inString, length, 0, blocking);}
size_t ChannelPutModifiable(const std::string &channel, byte *inString, size_t length, bool blocking=true)
{return ChannelPutModifiable2(channel, inString, length, 0, blocking);}
size_t ChannelPutWord16(const std::string &channel, word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
size_t ChannelPutWord32(const std::string &channel, word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
bool ChannelMessageEnd(const std::string &channel, int propagation=-1, bool blocking=true)
{return !!ChannelPut2(channel, NULL, 0, propagation < 0 ? -1 : propagation+1, blocking);}
size_t ChannelPutMessageEnd(const std::string &channel, const byte *inString, size_t length, int propagation=-1, bool blocking=true)
{return ChannelPut2(channel, inString, length, propagation < 0 ? -1 : propagation+1, blocking);}
virtual byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
virtual size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking);
virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true);
virtual bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
virtual void SetRetrievalChannel(const std::string &channel);
//@}
//! \name ATTACHMENT
/*! Some BufferedTransformation objects (e.g. Filter objects)
allow other BufferedTransformation objects to be attached. When
this is done, the first object instead of buffering its output,
sents that output to the attached object as input. The entire
attachment chain is deleted when the anchor object is destructed.
*/
//@{
//! returns whether this object allows attachment
virtual bool Attachable() {return false;}
//! returns the object immediately attached to this object or NULL for no attachment
virtual BufferedTransformation *AttachedTransformation() {assert(!Attachable()); return 0;}
//!
virtual const BufferedTransformation *AttachedTransformation() const
{return const_cast<BufferedTransformation *>(this)->AttachedTransformation();}
//! delete the current attachment chain and replace it with newAttachment
virtual void Detach(BufferedTransformation *newAttachment = 0)
{assert(!Attachable()); throw NotImplemented("BufferedTransformation: this object is not attachable");}
//! add newAttachment to the end of attachment chain
virtual void Attach(BufferedTransformation *newAttachment);
//@}
protected:
static int DecrementPropagation(int propagation)
{return propagation != 0 ? propagation - 1 : 0;}
private:
byte m_buf[4]; // for ChannelPutWord16 and ChannelPutWord32, to ensure buffer isn't deallocated before non-blocking operation completes
};
//! returns a reference to a BufferedTransformation object that discards all input
BufferedTransformation & TheBitBucket();
//! interface for crypto material, such as public and private keys, and crypto parameters
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CryptoMaterial : public NameValuePairs
{
public:
//! exception thrown when invalid crypto material is detected
class CRYPTOPP_DLL InvalidMaterial : public InvalidDataFormat
{
public:
explicit InvalidMaterial(const std::string &s) : InvalidDataFormat(s) {}
};
//! assign values from source to this object
/*! \note This function can be used to create a public key from a private key. */
virtual void AssignFrom(const NameValuePairs &source) =0;
//! check this object for errors
/*! \param level denotes the level of thoroughness:
0 - using this object won't cause a crash or exception (rng is ignored)
1 - this object will probably function (encrypt, sign, etc.) correctly (but may not check for weak keys and such)
2 - make sure this object will function correctly, and do reasonable security checks
3 - do checks that may take a long time
\return true if the tests pass */
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0;
//! throws InvalidMaterial if this object fails Validate() test
virtual void ThrowIfInvalid(RandomNumberGenerator &rng, unsigned int level) const
{if (!Validate(rng, level)) throw InvalidMaterial("CryptoMaterial: this object contains invalid values");}
// virtual std::vector<std::string> GetSupportedFormats(bool includeSaveOnly=false, bool includeLoadOnly=false);
//! save key into a BufferedTransformation
virtual void Save(BufferedTransformation &bt) const
{throw NotImplemented("CryptoMaterial: this object does not support saving");}
//! load key from a BufferedTransformation
/*! \throws KeyingErr if decode fails
\note Generally does not check that the key is valid.
Call ValidateKey() or ThrowIfInvalidKey() to check that. */
virtual void Load(BufferedTransformation &bt)
{throw NotImplemented("CryptoMaterial: this object does not support loading");}
//! \return whether this object supports precomputation
virtual bool SupportsPrecomputation() const {return false;}
//! do precomputation
/*! The exact semantics of Precompute() is varies, but
typically it means calculate a table of n objects
that can be used later to speed up computation. */
virtual void Precompute(unsigned int n)
{assert(!SupportsPrecomputation()); throw NotImplemented("CryptoMaterial: this object does not support precomputation");}
//! retrieve previously saved precomputation
virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation)
{assert(!SupportsPrecomputation()); throw NotImplemented("CryptoMaterial: this object does not support precomputation");}
//! save precomputation for later use
virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const
{assert(!SupportsPrecomputation()); throw NotImplemented("CryptoMaterial: this object does not support precomputation");}
// for internal library use
void DoQuickSanityCheck() const {ThrowIfInvalid(NullRNG(), 0);}
#ifdef __SUNPRO_CC
// Sun Studio 11/CC 5.8 workaround: it generates incorrect code when casting to an empty virtual base class
char m_sunCCworkaround;
#endif
};
//! interface for generatable crypto material, such as private keys and crypto parameters
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE GeneratableCryptoMaterial : virtual public CryptoMaterial
{
public:
//! generate a random key or crypto parameters
/*! \throws KeyingErr if algorithm parameters are invalid, or if a key can't be generated
(e.g., if this is a public key object) */
virtual void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms = g_nullNameValuePairs)
{throw NotImplemented("GeneratableCryptoMaterial: this object does not support key/parameter generation");}
//! calls the above function with a NameValuePairs object that just specifies "KeySize"
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize);
};
//! interface for public keys
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PublicKey : virtual public CryptoMaterial
{
};
//! interface for private keys
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PrivateKey : public GeneratableCryptoMaterial
{
};
//! interface for crypto prameters
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CryptoParameters : public GeneratableCryptoMaterial
{
};
//! interface for asymmetric algorithms
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AsymmetricAlgorithm : public Algorithm
{
public:
//! returns a reference to the crypto material used by this object
virtual CryptoMaterial & AccessMaterial() =0;
//! returns a const reference to the crypto material used by this object
virtual const CryptoMaterial & GetMaterial() const =0;
//! for backwards compatibility, calls AccessMaterial().Load(bt)
void BERDecode(BufferedTransformation &bt)
{AccessMaterial().Load(bt);}
//! for backwards compatibility, calls GetMaterial().Save(bt)
void DEREncode(BufferedTransformation &bt) const
{GetMaterial().Save(bt);}
};
//! interface for asymmetric algorithms using public keys
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PublicKeyAlgorithm : public AsymmetricAlgorithm
{
public:
// VC60 workaround: no co-variant return type
CryptoMaterial & AccessMaterial() {return AccessPublicKey();}
const CryptoMaterial & GetMaterial() const {return GetPublicKey();}
virtual PublicKey & AccessPublicKey() =0;
virtual const PublicKey & GetPublicKey() const {return const_cast<PublicKeyAlgorithm *>(this)->AccessPublicKey();}
};
//! interface for asymmetric algorithms using private keys
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PrivateKeyAlgorithm : public AsymmetricAlgorithm
{
public:
CryptoMaterial & AccessMaterial() {return AccessPrivateKey();}
const CryptoMaterial & GetMaterial() const {return GetPrivateKey();}
virtual PrivateKey & AccessPrivateKey() =0;
virtual const PrivateKey & GetPrivateKey() const {return const_cast<PrivateKeyAlgorithm *>(this)->AccessPrivateKey();}
};
//! interface for key agreement algorithms
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE KeyAgreementAlgorithm : public AsymmetricAlgorithm
{
public:
CryptoMaterial & AccessMaterial() {return AccessCryptoParameters();}
const CryptoMaterial & GetMaterial() const {return GetCryptoParameters();}
virtual CryptoParameters & AccessCryptoParameters() =0;
virtual const CryptoParameters & GetCryptoParameters() const {return const_cast<KeyAgreementAlgorithm *>(this)->AccessCryptoParameters();}
};
//! interface for public-key encryptors and decryptors
/*! This class provides an interface common to encryptors and decryptors
for querying their plaintext and ciphertext lengths.
*/
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_CryptoSystem
{
public:
virtual ~PK_CryptoSystem() {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -