📄 cryptlib.h
字号:
virtual void Attach(BufferedTransformation *newAttachment); //@}protected: static int DecrementPropagation(int propagation) {return propagation != 0 ? propagation - 1 : 0;}};//! returns a reference to a BufferedTransformation object that discards all inputBufferedTransformation & TheBitBucket();//! interface for crypto material, such as public and private keys, and crypto parametersclass CryptoMaterial : public NameValuePairs{public: //! exception thrown when invalid crypto material is detected class 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);}};//! interface for generatable crypto material, such as private keys and crypto parametersclass 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 keysclass PublicKey : virtual public CryptoMaterial{};//! interface for private keysclass PrivateKey : public GeneratableCryptoMaterial{};//! interface for crypto prametersclass CryptoParameters : public GeneratableCryptoMaterial{};//! interface for asymmetric algorithmsclass 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 keysclass 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 keysclass 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 algorithmsclass 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 PK_CryptoSystem{public: virtual ~PK_CryptoSystem() {} //! maximum length of plaintext for a given ciphertext length /*! \note This function returns 0 if cipherTextLength is not valid (too long or too short). */ virtual unsigned int MaxPlaintextLength(unsigned int cipherTextLength) const =0; //! calculate length of ciphertext given length of plaintext /*! \note This function returns 0 if plainTextLength is not valid (too long). */ virtual unsigned int CiphertextLength(unsigned int plainTextLength) const =0;#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const {return MaxPlaintextLength(cipherTextLength);} unsigned int CipherTextLength(unsigned int plainTextLength) const {return CiphertextLength(plainTextLength);}#endif};//! interface for public-key encryptorsclass PK_Encryptor : virtual public PK_CryptoSystem, public PublicKeyAlgorithm{public: //! . class InvalidPlaintextLength : public Exception { public: InvalidPlaintextLength() : Exception(OTHER_ERROR, "PK_Encryptor: invalid plaintext length") {} }; //! encrypt a byte string /*! \pre CipherTextLength(plainTextLength) != 0 (i.e., plainText isn't too long) \pre size of cipherText == CipherTextLength(plainTextLength) */ virtual void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const =0; //! create a new encryption filter /*! \note caller is responsible for deleting the returned pointer */ virtual BufferedTransformation * CreateEncryptionFilter(RandomNumberGenerator &rng, BufferedTransformation *attachment=NULL) const;};//! interface for public-key decryptorsclass PK_Decryptor : virtual public PK_CryptoSystem, public PrivateKeyAlgorithm{public: //! decrypt a byte string, and return the length of plaintext /*! \pre size of plainText == MaxPlainTextLength(cipherTextLength) bytes. \return the actual length of the plaintext, or 0 if decryption fails. */ virtual DecodingResult Decrypt(RandomNumberGenerator &rng, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const =0; //! create a new decryption filter /*! \note caller is responsible for deleting the returned pointer */ virtual BufferedTransformation * CreateDecryptionFilter(RandomNumberGenerator &rng, BufferedTransformation *attachment=NULL) const;};//! interface for encryptors and decryptors with fixed length ciphertext/*! A simplified interface is provided for crypto systems (such as RSA) whose ciphertext length and maximum plaintext length depend only on the key.*/class PK_FixedLengthCryptoSystem : virtual public PK_CryptoSystem{public: //! virtual unsigned int FixedMaxPlaintextLength() const =0; //! virtual unsigned int FixedCiphertextLength() const =0; unsigned int MaxPlaintextLength(unsigned int cipherTextLength) const; unsigned int CiphertextLength(unsigned int plainTextLength) const; #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const {return MaxPlaintextLength(cipherTextLength);} unsigned int CipherTextLength(unsigned int plainTextLength) const {return CiphertextLength(plainTextLength);} unsigned int MaxPlainTextLength() const {return FixedMaxPlaintextLength();} unsigned int CipherTextLength() const {return FixedCiphertextLength();}#endif};//! interface for encryptors with fixed length ciphertextclass PK_FixedLengthEncryptor : public PK_Encryptor, virtual public PK_FixedLengthCryptoSystem{};//! interface for decryptors with fixed length ciphertextclass 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 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -