From eeb63b8901a9c049f1bb594abb9ce9b4a9c47620 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 11 Jan 2021 16:39:43 +0000 Subject: zlib -> libdeflate (#5085) + Use libdeflate + Use std::byte * Fix passing temporary to string_view + Emulate make_unique_for_overwrite --- src/mbedTLS++/RsaPrivateKey.cpp | 52 ++++++----------------------------------- 1 file changed, 7 insertions(+), 45 deletions(-) (limited to 'src/mbedTLS++/RsaPrivateKey.cpp') diff --git a/src/mbedTLS++/RsaPrivateKey.cpp b/src/mbedTLS++/RsaPrivateKey.cpp index 704a2a1d0..3fd429dc0 100644 --- a/src/mbedTLS++/RsaPrivateKey.cpp +++ b/src/mbedTLS++/RsaPrivateKey.cpp @@ -55,7 +55,7 @@ bool cRsaPrivateKey::Generate(unsigned a_KeySizeBits) -AString cRsaPrivateKey::GetPubKeyDER(void) +ContiguousByteBuffer cRsaPrivateKey::GetPubKeyDER(void) { class cPubKey { @@ -96,21 +96,21 @@ AString cRsaPrivateKey::GetPubKeyDER(void) int res = mbedtls_pk_write_pubkey_der(PkCtx, buf, sizeof(buf)); if (res < 0) { - return AString(); + return {}; } - return AString(reinterpret_cast(buf + sizeof(buf) - res), static_cast(res)); + return { reinterpret_cast(buf + sizeof(buf) - res), static_cast(res) }; } -int cRsaPrivateKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength) +int cRsaPrivateKey::Decrypt(const ContiguousByteBufferView a_EncryptedData, Byte * a_DecryptedData, size_t a_DecryptedMaxLength) { - if (a_EncryptedLength < m_Rsa.len) + if (a_EncryptedData.size() < m_Rsa.len) { LOGD("%s: Invalid a_EncryptedLength: got %u, exp at least %u", - __FUNCTION__, static_cast(a_EncryptedLength), static_cast(m_Rsa.len) + __FUNCTION__, static_cast(a_EncryptedData.size()), static_cast(m_Rsa.len) ); ASSERT(!"Invalid a_DecryptedMaxLength!"); return -1; @@ -126,7 +126,7 @@ int cRsaPrivateKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLeng size_t DecryptedLength; int res = mbedtls_rsa_pkcs1_decrypt( &m_Rsa, mbedtls_ctr_drbg_random, m_CtrDrbg.GetInternal(), MBEDTLS_RSA_PRIVATE, &DecryptedLength, - a_EncryptedData, a_DecryptedData, a_DecryptedMaxLength + reinterpret_cast(a_EncryptedData.data()), a_DecryptedData, a_DecryptedMaxLength ); if (res != 0) { @@ -134,41 +134,3 @@ int cRsaPrivateKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLeng } return static_cast(DecryptedLength); } - - - - - -int cRsaPrivateKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength) -{ - if (a_EncryptedMaxLength < m_Rsa.len) - { - LOGD("%s: Invalid a_EncryptedMaxLength: got %u, exp at least %u", - __FUNCTION__, static_cast(a_EncryptedMaxLength), static_cast(m_Rsa.len) - ); - ASSERT(!"Invalid a_DecryptedMaxLength!"); - return -1; - } - if (a_PlainLength < m_Rsa.len) - { - LOGD("%s: Invalid a_PlainLength: got %u, exp at least %u", - __FUNCTION__, static_cast(a_PlainLength), static_cast(m_Rsa.len) - ); - ASSERT(!"Invalid a_PlainLength!"); - return -1; - } - int res = mbedtls_rsa_pkcs1_encrypt( - &m_Rsa, mbedtls_ctr_drbg_random, m_CtrDrbg.GetInternal(), MBEDTLS_RSA_PRIVATE, - a_PlainLength, a_PlainData, a_EncryptedData - ); - if (res != 0) - { - return -1; - } - return static_cast(m_Rsa.len); -} - - - - - -- cgit v1.2.3