📄 cryptlib.h
字号:
/*! This class provides an interface common to signers and verifiers
for querying scheme properties.
*/
class PK_SignatureScheme
{
public:
//! invalid key exception, may be thrown by any function in this class if the private or public key has a length that can't be used
class InvalidKeyLength : public Exception
{
public:
InvalidKeyLength(const std::string &message) : Exception(OTHER_ERROR, message) {}
};
//! key too short exception, may be thrown by any function in this class if the private or public key is too short to sign or verify anything
class KeyTooShort : public InvalidKeyLength
{
public:
KeyTooShort() : InvalidKeyLength("PK_Signer: key too short for this signature scheme") {}
};
virtual ~PK_SignatureScheme() {}
//! signature length if it only depends on the key, otherwise 0
virtual unsigned int SignatureLength() const =0;
//! maximum signature length produced for a given length of recoverable message part
virtual unsigned int MaxSignatureLength(unsigned int recoverablePartLength = 0) const {return SignatureLength();}
//! length of longest message that can be recovered, or 0 if this signature scheme does not support message recovery
virtual unsigned int MaxRecoverableLength() const =0;
//! length of longest message that can be recovered from a signature of given length, or 0 if this signature scheme does not support message recovery
virtual unsigned int MaxRecoverableLengthFromSignatureLength(unsigned int signatureLength) const =0;
//! requires a random number generator to sign
/*! if this returns false, NullRNG() can be passed to functions that take RandomNumberGenerator & */
virtual bool IsProbabilistic() const =0;
//! whether or not a non-recoverable message part can be signed
virtual bool AllowNonrecoverablePart() const =0;
//! if this function returns true, during verification you must input the signature before the message, otherwise you can input it at anytime */
virtual bool SignatureUpfront() const {return false;}
//! whether you must input the recoverable part before the non-recoverable part during signing
virtual bool RecoverablePartFirst() const =0;
};
//! interface for accumulating messages to be signed or verified
/*! Only Update() should be called
on this class. No other functions inherited from HashTransformation should be called.
*/
class PK_MessageAccumulator : public HashTransformation
{
public:
//! should not be called on PK_MessageAccumulator
unsigned int DigestSize() const
{throw NotImplemented("PK_MessageAccumulator: DigestSize() should not be called");}
//! should not be called on PK_MessageAccumulator
void TruncatedFinal(byte *digest, unsigned int digestSize)
{throw NotImplemented("PK_MessageAccumulator: TruncatedFinal() should not be called");}
};
//! interface for public-key signers
class PK_Signer : virtual public PK_SignatureScheme, public PrivateKeyAlgorithm
{
public:
//! create a new HashTransformation to accumulate the message to be signed
virtual PK_MessageAccumulator * NewSignatureAccumulator(RandomNumberGenerator &rng = NullRNG()) const =0;
virtual void InputRecoverableMessage(PK_MessageAccumulator &messageAccumulator, const byte *recoverableMessage, unsigned int recoverableMessageLength) const =0;
//! sign and delete messageAccumulator (even in case of exception thrown)
/*! \pre size of signature == MaxSignatureLength()
\return actual signature length
*/
virtual unsigned int Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const;
//! sign and restart messageAccumulator
/*! \pre size of signature == MaxSignatureLength()
\return actual signature length
*/
virtual unsigned int SignAndRestart(RandomNumberGenerator &rng, PK_MessageAccumulator &messageAccumulator, byte *signature, bool restart=true) const =0;
//! sign a message
/*! \pre size of signature == MaxSignatureLength()
\return actual signature length
*/
virtual unsigned int SignMessage(RandomNumberGenerator &rng, const byte *message, unsigned int messageLen, byte *signature) const;
//! sign a recoverable message
/*! \pre size of signature == MaxSignatureLength(recoverableMessageLength)
\return actual signature length
*/
virtual unsigned int SignMessageWithRecovery(RandomNumberGenerator &rng, const byte *recoverableMessage, unsigned int recoverableMessageLength,
const byte *nonrecoverableMessage, unsigned int nonrecoverableMessageLength, byte *signature) const;
};
//! interface for public-key signature verifiers
/*! The Recover* functions throw NotImplemented if the signature scheme does not support
message recovery.
The Verify* functions throw InvalidDataFormat if the scheme does support message
recovery and the signature contains a non-empty recoverable message part. The
Recovery* functions should be used in that case.
*/
class PK_Verifier : virtual public PK_SignatureScheme, public PublicKeyAlgorithm
{
public:
//! create a new HashTransformation to accumulate the message to be verified
virtual PK_MessageAccumulator * NewVerificationAccumulator() const =0;
//! input signature into a message accumulator
virtual void InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, unsigned int signatureLength) const =0;
//! check whether messageAccumulator contains a valid signature and message, and delete messageAccumulator (even in case of exception thrown)
virtual bool Verify(PK_MessageAccumulator *messageAccumulator) const;
//! check whether messageAccumulator contains a valid signature and message, and restart messageAccumulator
virtual bool VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const =0;
//! check whether input signature is a valid signature for input message
virtual bool VerifyMessage(const byte *message, unsigned int messageLen,
const byte *signature, unsigned int signatureLength) const;
//! recover a message from its signature
/*! \pre size of recoveredMessage == MaxRecoverableLengthFromSignatureLength(signatureLength)
*/
virtual DecodingResult Recover(byte *recoveredMessage, PK_MessageAccumulator *messageAccumulator) const;
//! recover a message from its signature
/*! \pre size of recoveredMessage == MaxRecoverableLengthFromSignatureLength(signatureLength)
*/
virtual DecodingResult RecoverAndRestart(byte *recoveredMessage, PK_MessageAccumulator &messageAccumulator) const =0;
//! recover a message from its signature
/*! \pre size of recoveredMessage == MaxRecoverableLengthFromSignatureLength(signatureLength)
*/
virtual DecodingResult RecoverMessage(byte *recoveredMessage,
const byte *nonrecoverableMessage, unsigned int nonrecoverableMessageLength,
const byte *signature, unsigned int signatureLength) const;
};
//! interface for domains of simple key agreement protocols
/*! A key agreement domain is a set of parameters that must be shared
by two parties in a key agreement protocol, along with the algorithms
for generating key pairs and deriving agreed values.
*/
class SimpleKeyAgreementDomain : public KeyAgreementAlgorithm
{
public:
//! return length of agreed value produced
virtual unsigned int AgreedValueLength() const =0;
//! return length of private keys in this domain
virtual unsigned int PrivateKeyLength() const =0;
//! return length of public keys in this domain
virtual unsigned int PublicKeyLength() const =0;
//! generate private key
/*! \pre size of privateKey == PrivateKeyLength() */
virtual void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const =0;
//! generate public key
/*! \pre size of publicKey == PublicKeyLength() */
virtual void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const =0;
//! generate private/public key pair
/*! \note equivalent to calling GeneratePrivateKey() and then GeneratePublicKey() */
virtual void GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const;
//! derive agreed value from your private key and couterparty's public key, return false in case of failure
/*! \note If you have previously validated the public key, use validateOtherPublicKey=false to save time.
/*! \pre size of agreedValue == AgreedValueLength()
\pre length of privateKey == PrivateKeyLength()
\pre length of otherPublicKey == PublicKeyLength()
*/
virtual bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const =0;
#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
bool ValidateDomainParameters(RandomNumberGenerator &rng) const
{return GetCryptoParameters().Validate(rng, 2);}
#endif
};
//! interface for domains of authenticated key agreement protocols
/*! In an authenticated key agreement protocol, each party has two
key pairs. The long-lived key pair is called the static key pair,
and the short-lived key pair is called the ephemeral key pair.
*/
class AuthenticatedKeyAgreementDomain : public KeyAgreementAlgorithm
{
public:
//! return length of agreed value produced
virtual unsigned int AgreedValueLength() const =0;
//! return length of static private keys in this domain
virtual unsigned int StaticPrivateKeyLength() const =0;
//! return length of static public keys in this domain
virtual unsigned int StaticPublicKeyLength() const =0;
//! generate static private key
/*! \pre size of privateKey == PrivateStaticKeyLength() */
virtual void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const =0;
//! generate static public key
/*! \pre size of publicKey == PublicStaticKeyLength() */
virtual void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const =0;
//! generate private/public key pair
/*! \note equivalent to calling GenerateStaticPrivateKey() and then GenerateStaticPublicKey() */
virtual void GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const;
//! return length of ephemeral private keys in this domain
virtual unsigned int EphemeralPrivateKeyLength() const =0;
//! return length of ephemeral public keys in this domain
virtual unsigned int EphemeralPublicKeyLength() const =0;
//! generate ephemeral private key
/*! \pre size of privateKey == PrivateEphemeralKeyLength() */
virtual void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const =0;
//! generate ephemeral public key
/*! \pre size of publicKey == PublicEphemeralKeyLength() */
virtual void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const =0;
//! generate private/public key pair
/*! \note equivalent to calling GenerateEphemeralPrivateKey() and then GenerateEphemeralPublicKey() */
virtual void GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const;
//! derive agreed value from your private keys and couterparty's public keys, return false in case of failure
/*! \note The ephemeral public key will always be validated.
If you have previously validated the static public key, use validateStaticOtherPublicKey=false to save time.
\pre size of agreedValue == AgreedValueLength()
\pre length of staticPrivateKey == StaticPrivateKeyLength()
\pre length of ephemeralPrivateKey == EphemeralPrivateKeyLength()
\pre length of staticOtherPublicKey == StaticPublicKeyLength()
\pre length of ephemeralOtherPublicKey == EphemeralPublicKeyLength()
*/
virtual bool Agree(byte *agreedValue,
const byte *staticPrivateKey, const byte *ephemeralPrivateKey,
const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
bool validateStaticOtherPublicKey=true) const =0;
#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
bool ValidateDomainParameters(RandomNumberGenerator &rng) const
{return GetCryptoParameters().Validate(rng, 2);}
#endif
};
// interface for password authenticated key agreement protocols, not implemented yet
#if 0
//! interface for protocol sessions
/*! The methods should be called in the following order:
InitializeSession(rng, parameters); // or call initialize method in derived class
while (true)
{
if (OutgoingMessageAvailable())
{
length = GetOutgoingMessageLength();
GetOutgoingMessage(message);
; // send outgoing message
}
if (LastMessageProcessed())
break;
; // receive incoming message
ProcessIncomingMessage(message);
}
; // call methods in derived class to obtain result of protocol session
*/
class ProtocolSession
{
public:
//! exception thrown when an invalid protocol message is processed
class ProtocolError : public Exception
{
public:
ProtocolError(ErrorType errorType, const std::string &s) : Exception(errorType, s) {}
};
//! exception thrown when a function is called unexpectedly
/*! for example calling ProcessIncomingMessage() when ProcessedLastMessage() == true */
class UnexpectedMethodCall : public Exception
{
public:
UnexpectedMethodCall(const std::string &s) : Exception(OTHER_ERROR, s) {}
};
ProtocolSession() : m_rng(NULL), m_throwOnProtocolError(true), m_validState(false) {}
virtual ~ProtocolSession() {}
virtual void InitializeSession(RandomNumberGenerator &rng, const NameValuePairs ¶meters) =0;
bool GetThrowOnProtocolError() const {return m_throwOnProtocolError;}
void SetThrowOnProtocolError(bool throwOnProtocolError) {m_throwOnProtocolError = throwOnProtocolError;}
bool HasValidState() const {return m_validState;}
virtual bool OutgoingMessageAvailable() const =0;
virtual unsigned
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -