Virgil Security Crypto library  2.6.3
VirgilAsymmetricCipher.h
1 
37 #ifndef VIRGIL_CRYPTO_ASYMMETRIC_CIPHER_H
38 #define VIRGIL_CRYPTO_ASYMMETRIC_CIPHER_H
39 
40 #include <cstdlib>
41 #include <memory>
42 
43 #include "../VirgilByteArray.h"
44 #include "../VirgilKeyPair.h"
45 #include "asn1/VirgilAsn1Compatible.h"
46 
47 namespace virgil { namespace crypto { namespace foundation {
48 
53 public:
57 
67 
71 
76  size_t keySize() const;
77 
82  size_t keyLength() const;
84 
88 
98  static bool isKeyPairMatch(
99  const virgil::crypto::VirgilByteArray& publicKey,
100  const virgil::crypto::VirgilByteArray& privateKey,
102 
103 
112  static bool isPublicKeyValid(const virgil::crypto::VirgilByteArray& key);
113 
122  static void checkPublicKey(const virgil::crypto::VirgilByteArray& key);
123 
131  static bool checkPrivateKeyPassword(
134 
135 
142  static bool isPrivateKeyEncrypted(const virgil::crypto::VirgilByteArray& privateKey);
144 
148 
159  void setPrivateKey(
162 
172 
181  void genKeyPair(VirgilKeyPair::Type type);
182 
191  void genKeyPairFrom(const VirgilAsymmetricCipher& other);
192 
208  void genKeyPairFromKeyMaterial(VirgilKeyPair::Type type, const VirgilByteArray& keyMaterial);
209 
225  const VirgilAsymmetricCipher& publicContext,
226  const VirgilAsymmetricCipher& privateContext);
228 
232 
241 
248 
257 
265 
272 
279 
287 
299 
314 
318 
331 
345 
349 
362  virgil::crypto::VirgilByteArray sign(const virgil::crypto::VirgilByteArray& digest, int hashType) const;
363 
375  bool verify(
376  const virgil::crypto::VirgilByteArray& digest,
377  const virgil::crypto::VirgilByteArray& sign, int hashType) const;
379 
382  virtual size_t asn1Write(
385  size_t childWrittenBytes = 0) const override;
386 
387  virtual void asn1Read(virgil::crypto::foundation::asn1::VirgilAsn1Reader& asn1Reader) override;
389 public:
392 
393  VirgilAsymmetricCipher& operator=(VirgilAsymmetricCipher&& rhs) noexcept;
394 
395  virtual ~VirgilAsymmetricCipher() noexcept;
397 
398 private:
402  void checkState() const;
403 
404  size_t calculateExportedPublicKeySizeMaxDER() const;
405  size_t calculateExportedPublicKeySizeMaxPEM() const;
406  size_t calculateExportedPrivateKeySizeMaxDER(size_t encryptionOverhead) const;
407  size_t calculateExportedPrivateKeySizeMaxPEM(size_t encryptionOverhead) const;
408  VirgilByteArray generateParametersPBES() const;
409  static VirgilByteArray adjustBufferWithPEM(const VirgilByteArray& buffer, int size);
410  static VirgilByteArray adjustBufferWithDER(const VirgilByteArray& buffer, int size);
411 
412 private:
413  class Impl;
414 
415  std::unique_ptr<Impl> impl_;
416 };
417 
418 }}}
419 
420 #endif /* VIRGIL_CRYPTO_ASYMMETRIC_CIPHER_H */
virgil::crypto::VirgilByteArray exportPublicKeyToDER() const
Provides public key.
virgil::crypto::VirgilByteArray exportPrivateKeyToPEM(const virgil::crypto::VirgilByteArray &pwd=virgil::crypto::VirgilByteArray()) const
Provides private key.
This class provides methods for reading ASN.1 data structure.
Definition: VirgilAsn1Reader.h:53
virtual size_t asn1Write(virgil::crypto::foundation::asn1::VirgilAsn1Writer &asn1Writer, size_t childWrittenBytes=0) const override
Write object state to the writer.
Provides asymmetric ciphers algorithms (PK).
Definition: VirgilAsymmetricCipher.h:52
static VirgilByteArray computeShared(const VirgilAsymmetricCipher &publicContext, const VirgilAsymmetricCipher &privateContext)
Compute shared secret key on a given contexts.
This class provides interface that allow to save and restore object state in the ASN.1 structure.
Definition: VirgilAsn1Compatible.h:59
static bool isPublicKeyValid(const virgil::crypto::VirgilByteArray &key)
Check if given public key has a valid format.
virtual void asn1Read(virgil::crypto::foundation::asn1::VirgilAsn1Reader &asn1Reader) override
Read object state from the reader.
virgil::crypto::VirgilByteArray exportPrivateKeyToDER(const virgil::crypto::VirgilByteArray &pwd=virgil::crypto::VirgilByteArray()) const
Provides private key.
virgil::crypto::VirgilByteArray getPublicKeyBits() const
Return number of the underlying public key.
This class provides methods for writing ASN.1 data structure.
Definition: VirgilAsn1Writer.h:54
VirgilAsymmetricCipher()
Create object that is not initialized with specific algorithm yet.
Type
Specific key algorithm type.
Definition: VirgilKeyPair.h:78
void setPublicKeyBits(const virgil::crypto::VirgilByteArray &bits)
Set number of the underlying public key.
static bool isKeyPairMatch(const virgil::crypto::VirgilByteArray &publicKey, const virgil::crypto::VirgilByteArray &privateKey, const virgil::crypto::VirgilByteArray &privateKeyPassword=virgil::crypto::VirgilByteArray())
Check if a public-private pair of keys matches.
void genKeyPairFromKeyMaterial(VirgilKeyPair::Type type, const VirgilByteArray &keyMaterial)
Generates private and public keys from the given key material.
Root namespace for all Virgil Security libraries.
Definition: VirgilAsn1Compatible.h:46
size_t keySize() const
Provides size in bits of the underlying key.
std::vector< unsigned char > VirgilByteArray
This type represents a sequence of bytes.
Definition: VirgilByteArray.h:50
virgil::crypto::VirgilKeyPair::Type getKeyType() const
Return type of the underlying key.
static bool isPrivateKeyEncrypted(const virgil::crypto::VirgilByteArray &privateKey)
Check if given private key is encrypted.
static void checkPublicKey(const virgil::crypto::VirgilByteArray &key)
Check if given public key has a valid format.
virgil::crypto::VirgilByteArray exportPublicKeyToPEM() const
Provides public key.
virgil::crypto::VirgilByteArray sign(const virgil::crypto::VirgilByteArray &digest, int hashType) const
Sign given hash.
void genKeyPair(VirgilKeyPair::Type type)
Generates private and public keys.
void setPrivateKey(const virgil::crypto::VirgilByteArray &key, const virgil::crypto::VirgilByteArray &pwd=virgil::crypto::VirgilByteArray())
Configures private key.
void setPublicKey(const virgil::crypto::VirgilByteArray &key)
Configures public key.
bool verify(const virgil::crypto::VirgilByteArray &digest, const virgil::crypto::VirgilByteArray &sign, int hashType) const
Verify given hash with given sign.
void genKeyPairFrom(const VirgilAsymmetricCipher &other)
Generates private and public keys of the same type from the given context.
virgil::crypto::VirgilByteArray decrypt(const virgil::crypto::VirgilByteArray &in) const
Decrypts given message.
size_t keyLength() const
Provides the length in bytes of the underlying key.
virgil::crypto::VirgilByteArray encrypt(const virgil::crypto::VirgilByteArray &in) const
Encrypts given message.
static bool checkPrivateKeyPassword(const virgil::crypto::VirgilByteArray &key, const virgil::crypto::VirgilByteArray &pwd)
Check if given private key and it&#39;s password matches.
void setKeyType(virgil::crypto::VirgilKeyPair::Type keyType)
Change type of the underlying key.