15 mbedtls_pk_init(&
m_Pk);
25 mbedtls_pk_init(&
m_Pk);
27 int res =
ParsePublic(a_PublicKeyData.data(), a_PublicKeyData.size());
30 LOGWARNING(
"Failed to parse public key: -0x%x", res);
31 ASSERT(!
"Cannot parse PubKey");
42 mbedtls_pk_init(&
m_Pk);
44 int res =
ParsePrivate(a_PrivateKeyData.data(), a_PrivateKeyData.size(), a_Password);
47 LOGWARNING(
"Failed to parse private key: -0x%x", res);
48 ASSERT(!
"Cannot parse PrivKey");
59 mbedtls_pk_free(&
m_Pk);
70 size_t DecryptedLen = a_DecryptedMaxLength;
71 int res = mbedtls_pk_decrypt(&
m_Pk,
72 a_EncryptedData, a_EncryptedLength,
73 a_DecryptedData, &DecryptedLen, a_DecryptedMaxLength,
80 return static_cast<int>(DecryptedLen);
91 size_t EncryptedLength = a_EncryptedMaxLength;
92 int res = mbedtls_pk_encrypt(&
m_Pk,
93 a_PlainData, a_PlainLength, a_EncryptedData, &EncryptedLength, a_EncryptedMaxLength,
100 return static_cast<int>(EncryptedLength);
111 return mbedtls_pk_parse_public_key(&
m_Pk,
static_cast<const unsigned char *
>(a_Data), a_NumBytes);
123 AString keyData(
static_cast<const char *
>(a_Data), a_NumBytes);
125 if (a_Password.empty())
127 return mbedtls_pk_parse_key(&
m_Pk,
reinterpret_cast<const unsigned char *
>(keyData.data()), a_NumBytes + 1,
nullptr, 0, mbedtls_ctr_drbg_random,
m_CtrDrbg.
GetInternal());
131 return mbedtls_pk_parse_key(
133 reinterpret_cast<const unsigned char *
>(keyData.data()), a_NumBytes + 1,
134 reinterpret_cast<const unsigned char *
>(a_Password.c_str()), a_Password.size(),
146 return (mbedtls_pk_get_type(&
m_Pk) != MBEDTLS_PK_NONE);
void LOGWARNING(std::string_view a_Format, const Args &... args)
int ParsePrivate(const void *a_Data, size_t a_NumBytes, const AString &a_Password)
Parses the specified data into a private key representation.
mbedtls_pk_context m_Pk
The mbedTLS representation of the key data.
int Encrypt(const Byte *a_PlainData, size_t a_PlainLength, Byte *a_EncryptedData, size_t a_EncryptedMaxLength)
Encrypts the data using the stored public key Both a_EncryptedData and a_DecryptedData must be at lea...
int ParsePublic(const void *a_Data, size_t a_NumBytes)
Parses the specified data into a public key representation.
bool IsValid(void) const
Returns true if the contained key is valid.
cCryptoKey(void)
Constructs an empty key instance.
int Decrypt(const Byte *a_EncryptedData, size_t a_EncryptedLength, Byte *a_DecryptedData, size_t a_DecryptedMaxLength)
Decrypts the data using the stored public key Both a_EncryptedData and a_DecryptedData must be at lea...
cCtrDrbgContext m_CtrDrbg
The random generator used in encryption and decryption.
mbedtls_ctr_drbg_context * GetInternal(void)
Returns the internal context ptr.
int Initialize(const void *a_Custom, size_t a_CustomSize)
Initializes the context.