diff options
Diffstat (limited to 'tdutils/td/utils/Ed25519.cpp')
| -rw-r--r-- | tdutils/td/utils/Ed25519.cpp | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/tdutils/td/utils/Ed25519.cpp b/tdutils/td/utils/Ed25519.cpp index 21137cf3a..1867a8260 100644 --- a/tdutils/td/utils/Ed25519.cpp +++ b/tdutils/td/utils/Ed25519.cpp @@ -296,8 +296,11 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key, BigNum::mod_mul(u, y, inverse_y_plus_1, p, context); auto pr_key = private_key.as_octet_string(); + if (pr_key.size() != PrivateKey::LENGTH) { + return Status::Error("Wrong private key"); + } unsigned char buf[64]; - SHA512(Slice(pr_key).ubegin(), 32, buf); + SHA512(Slice(pr_key).ubegin(), pr_key.size(), buf); buf[0] &= 248; buf[31] &= 127; buf[31] |= 64; @@ -309,9 +312,8 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key, SCOPE_EXIT { EVP_PKEY_free(pkey_private); }; - // LOG(ERROR) << buffer_to_hex(Slice(buf, 32)); - auto pub_key = u.to_le_binary(32); + auto pub_key = u.to_le_binary(PublicKey::LENGTH); auto pkey_public = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, nullptr, Slice(pub_key).ubegin(), pub_key.size()); if (pkey_public == nullptr) { return Status::Error("Can't import public key"); @@ -319,7 +321,6 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key, SCOPE_EXIT { EVP_PKEY_free(pkey_public); }; - // LOG(ERROR) << buffer_to_hex(pub_key); EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey_private, nullptr); if (ctx == nullptr) { @@ -356,7 +357,10 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key, Result<SecureString> Ed25519::get_public_key(Slice private_key) { #if OPENSSL_VERSION_NUMBER >= 0x10101000L - auto pkey_private = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, nullptr, private_key.ubegin(), 32); + if (private_key.size() != PrivateKey::LENGTH) { + return Status::Error("Invalid X25519 private key"); + } + auto pkey_private = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, nullptr, private_key.ubegin(), private_key.size()); if (pkey_private == nullptr) { return Status::Error("Invalid X25519 private key"); } @@ -364,15 +368,14 @@ Result<SecureString> Ed25519::get_public_key(Slice private_key) { EVP_PKEY_free(pkey_private); }; - auto func = &EVP_PKEY_get_raw_public_key; size_t len = 0; - if (func(pkey_private, nullptr, &len) == 0) { + if (EVP_PKEY_get_raw_public_key(pkey_private, nullptr, &len) == 0) { return Status::Error("Failed to get raw key length"); } - CHECK(len == 32); + CHECK(len == PublicKey::LENGTH); SecureString result(len); - if (func(pkey_private, result.as_mutable_slice().ubegin(), &len) == 0) { + if (EVP_PKEY_get_raw_public_key(pkey_private, result.as_mutable_slice().ubegin(), &len) == 0) { return Status::Error("Failed to get raw key"); } return std::move(result); |
