Virgil Security Crypto library
2.1.2
|
Provides asymmetric ciphers algorithms (PK). More...
#include <VirgilAsymmetricCipher.h>
Public Member Functions | |
Creation methods | |
VirgilAsymmetricCipher () | |
Create object that is not initialized with specific algorithm yet. More... | |
Info | |
size_t | keySize () const |
Provides size in bits of the underlying key. More... | |
size_t | keyLength () const |
Provides the length in bytes of the underlying key. More... | |
Keys export | |
virgil::crypto::VirgilByteArray | exportPrivateKeyToDER (const virgil::crypto::VirgilByteArray &pwd=virgil::crypto::VirgilByteArray()) const |
Provides private key. More... | |
virgil::crypto::VirgilByteArray | exportPublicKeyToDER () const |
Provides public key. More... | |
virgil::crypto::VirgilByteArray | exportPrivateKeyToPEM (const virgil::crypto::VirgilByteArray &pwd=virgil::crypto::VirgilByteArray()) const |
Provides private key. More... | |
virgil::crypto::VirgilByteArray | exportPublicKeyToPEM () const |
Provides public key. More... | |
Keys low level management | |
| |
virgil::crypto::VirgilKeyPair::Type | getKeyType () const |
Return type of the underlying key. More... | |
void | setKeyType (virgil::crypto::VirgilKeyPair::Type keyType) |
Change type of the underlying key. More... | |
virgil::crypto::VirgilByteArray | getPublicKeyBits () const |
Return number of the underlying public key. More... | |
void | setPublicKeyBits (const virgil::crypto::VirgilByteArray &bits) |
Set number of the underlying public key. More... | |
Encryption / Decryption | |
virgil::crypto::VirgilByteArray | encrypt (const virgil::crypto::VirgilByteArray &in) const |
Encrypts given message. More... | |
virgil::crypto::VirgilByteArray | decrypt (const virgil::crypto::VirgilByteArray &in) const |
Decrypts given message. More... | |
Sign / Verify | |
virgil::crypto::VirgilByteArray | sign (const virgil::crypto::VirgilByteArray &digest, int hashType) const |
Sign given hash. More... | |
bool | verify (const virgil::crypto::VirgilByteArray &digest, const virgil::crypto::VirgilByteArray &sign, int hashType) const |
Verify given hash with given sign. More... | |
VirgilAsn1Compatible implementation | |
virtual size_t | asn1Write (virgil::crypto::foundation::asn1::VirgilAsn1Writer &asn1Writer, size_t childWrittenBytes=0) const override |
Write object state to the writer. More... | |
virtual void | asn1Read (virgil::crypto::foundation::asn1::VirgilAsn1Reader &asn1Reader) override |
Read object state from the reader. More... | |
Public Member Functions inherited from virgil::crypto::foundation::asn1::VirgilAsn1Compatible | |
virgil::crypto::VirgilByteArray | toAsn1 () const |
Save object state to the ASN.1 structure. | |
void | fromAsn1 (const virgil::crypto::VirgilByteArray &asn1) |
Restore object state from the ASN.1 structure. | |
virtual | ~VirgilAsn1Compatible () noexcept |
Polymorphic destructor. | |
Static Public Member Functions | |
Keys validation | |
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. More... | |
static bool | isPublicKeyValid (const virgil::crypto::VirgilByteArray &key) |
Check if given public key has a valid format. More... | |
static void | checkPublicKey (const virgil::crypto::VirgilByteArray &key) |
Check if given public key has a valid format. More... | |
static bool | checkPrivateKeyPassword (const virgil::crypto::VirgilByteArray &key, const virgil::crypto::VirgilByteArray &pwd) |
Check if given private key and it's password matches. More... | |
static bool | isPrivateKeyEncrypted (const virgil::crypto::VirgilByteArray &privateKey) |
Check if given private key is encrypted. More... | |
Keys management | |
void | setPrivateKey (const virgil::crypto::VirgilByteArray &key, const virgil::crypto::VirgilByteArray &pwd=virgil::crypto::VirgilByteArray()) |
Configures private key. More... | |
void | setPublicKey (const virgil::crypto::VirgilByteArray &key) |
Configures public key. More... | |
void | genKeyPair (VirgilKeyPair::Type type) |
Generates private and public keys. More... | |
void | genKeyPairFrom (const VirgilAsymmetricCipher &other) |
Generates private and public keys of the same type from the given context. More... | |
static VirgilByteArray | computeShared (const VirgilAsymmetricCipher &publicContext, const VirgilAsymmetricCipher &privateContext) |
Compute shared secret key on a given contexts. More... | |
Additional Inherited Members | |
Protected Member Functions inherited from virgil::crypto::foundation::asn1::VirgilAsn1Compatible | |
virtual void | checkRequiredField (const VirgilByteArray ¶m) const |
If given parameter is empty exception will be thrown. More... | |
Provides asymmetric ciphers algorithms (PK).
virgil::crypto::foundation::VirgilAsymmetricCipher::VirgilAsymmetricCipher | ( | ) |
Create object that is not initialized with specific algorithm yet.
|
overridevirtual |
Read object state from the reader.
asn1Reader | reader payloaded with ASN.1 to be read. |
Implements virgil::crypto::foundation::asn1::VirgilAsn1Compatible.
|
overridevirtual |
Write object state to the writer.
asn1Writer | writer that should be payloaded by subclasses. |
childWrittenBytes | count of bytes that was written by subclasses. |
Implements virgil::crypto::foundation::asn1::VirgilAsn1Compatible.
|
static |
Check if given private key and it's password matches.
key | - private key in DER or PEM format. |
pwd | - private key password. |
VirgilCryptoException | with VirgilCryptoError::InvalidPrivateKey, if private key is invalid. |
|
static |
Check if given public key has a valid format.
Ensure that given public key has the valid format PEM or DER.
key | - public key to be checked. |
VirgilCryptoException | with VirgilCryptoError::InvalidPublicKey, if public key is invalid. |
|
static |
Compute shared secret key on a given contexts.
Prerequisites:
publicContext | - public context. |
privateContext | - private context. |
VirgilCryptoException | with VirgilCryptoError::NotInitialized, if public or private context are not initialized with specific algorithm. |
VirgilCryptoException | with VirgilCryptoError::InvalidArgument, if prerequisites broken. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::decrypt | ( | const virgil::crypto::VirgilByteArray & | in | ) | const |
Decrypts given message.
Decrypt given message with known private key, configured with setPrivateKey method, or genKeyPair method.
in | - message to be decrypted. |
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if current context does not support decryption. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::encrypt | ( | const virgil::crypto::VirgilByteArray & | in | ) | const |
Encrypts given message.
Encrypt given message with known public key, configured with setPublicKey method, or genKeyPair method.
in | - message to be encrypted. |
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if current context does not support encryption. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::exportPrivateKeyToDER | ( | const virgil::crypto::VirgilByteArray & | pwd = virgil::crypto::VirgilByteArray() | ) | const |
Provides private key.
pwd | - private key password (max length is 31 byte). |
VirgilCryptoException | with VirgilCryptoError::InvalidState, if private key can not be exported. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::exportPrivateKeyToPEM | ( | const virgil::crypto::VirgilByteArray & | pwd = virgil::crypto::VirgilByteArray() | ) | const |
Provides private key.
pwd | - private key password (max length is 31 byte). |
VirgilCryptoException | with VirgilCryptoError::InvalidState, if private key can not be exported. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::exportPublicKeyToDER | ( | ) | const |
Provides public key.
VirgilCryptoException | with VirgilCryptoError::InvalidState, if public key can not be exported. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::exportPublicKeyToPEM | ( | ) | const |
Provides public key.
VirgilCryptoException | with VirgilCryptoError::InvalidState, if public key can not be exported. |
void virgil::crypto::foundation::VirgilAsymmetricCipher::genKeyPair | ( | VirgilKeyPair::Type | type | ) |
Generates private and public keys.
Generate private and public keys in the current context.
type | - keypair type. |
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if key pair can't be generated with given type. |
void virgil::crypto::foundation::VirgilAsymmetricCipher::genKeyPairFrom | ( | const VirgilAsymmetricCipher & | other | ) |
Generates private and public keys of the same type from the given context.
other | - donor context. |
VirgilCryptoException | with VirgilCryptoError::NotInitialized, if donor context does not contain own key pair. |
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if key pair can't be generated with given type. |
virgil::crypto::VirgilKeyPair::Type virgil::crypto::foundation::VirgilAsymmetricCipher::getKeyType | ( | ) | const |
Return type of the underlying key.
VirgilCryptoException | with VirgilCryptoError::NotInitialized, if key type is unknown. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::getPublicKeyBits | ( | ) | const |
Return number of the underlying public key.
Legend:
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if given key type not allowed for this operation. |
|
static |
Check if a public-private pair of keys matches.
publicKey | - public key in DER or PEM format. |
privateKey | - private key in DER or PEM format. |
privateKeyPassword | - private key password if exists. |
|
static |
Check if given private key is encrypted.
privateKey | - private key in DER or PEM format. |
VirgilCryptoException | with VirgilCryptoError::InvalidPrivateKey, if private key is invalid. |
|
static |
Check if given public key has a valid format.
Ensure that given public key has the valid format PEM or DER.
key | - public key to be checked. |
size_t virgil::crypto::foundation::VirgilAsymmetricCipher::keyLength | ( | ) | const |
Provides the length in bytes of the underlying key.
size_t virgil::crypto::foundation::VirgilAsymmetricCipher::keySize | ( | ) | const |
Provides size in bits of the underlying key.
void virgil::crypto::foundation::VirgilAsymmetricCipher::setKeyType | ( | virgil::crypto::VirgilKeyPair::Type | keyType | ) |
Change type of the underlying key.
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if given key type not allowed for this operation. |
void virgil::crypto::foundation::VirgilAsymmetricCipher::setPrivateKey | ( | const virgil::crypto::VirgilByteArray & | key, |
const virgil::crypto::VirgilByteArray & | pwd = virgil::crypto::VirgilByteArray() |
||
) |
Configures private key.
Parse given private key and set it to the current context.
key | - private key in DER or PEM format. |
pwd | - private key password if exists. |
VirgilCryptoException | with VirgilCryptoError::InvalidPrivateKey, if private key is invalid. |
VirgilCryptoException | with VirgilCryptoError::InvalidPrivateKeyPassword, if private key password mismatch. |
void virgil::crypto::foundation::VirgilAsymmetricCipher::setPublicKey | ( | const virgil::crypto::VirgilByteArray & | key | ) |
Configures public key.
Parse given public key and set it to the current context.
key | - public key in DER or PEM format. |
VirgilCryptoException | with VirgilCryptoError::InvalidPublicKey, if public key is invalid. |
void virgil::crypto::foundation::VirgilAsymmetricCipher::setPublicKeyBits | ( | const virgil::crypto::VirgilByteArray & | bits | ) |
Set number of the underlying public key.
Legend:
VirgilCryptoException | with VirgilCryptoError::InvalidArgument, if given key size is unexpected. |
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if given key type not allowed for this operation. |
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilAsymmetricCipher::sign | ( | const virgil::crypto::VirgilByteArray & | digest, |
int | hashType | ||
) | const |
Sign given hash.
Sign given hash with known private key, configured with setPrivateKey method, or genKeyPair method.
digest | - digest to be signed. |
hashType | - type of the hash algorithm that was used to get digest |
VirgilCryptoException | with VirgilCryptoError::UnsupportedAlgorithm, if current context does not support sign or connected algorithms (Hash, RNG, etc). |
bool virgil::crypto::foundation::VirgilAsymmetricCipher::verify | ( | const virgil::crypto::VirgilByteArray & | digest, |
const virgil::crypto::VirgilByteArray & | sign, | ||
int | hashType | ||
) | const |
Verify given hash with given sign.
Verify given hash with known public key, configured with setPrivateKey method, or genKeyPair method, and with given sign.
digest | - digest to be verified. |
sign | - signed digest to be used during verification. |
hashType | - type of the hash algorithm that was used to get digest |