diff options
| author | levlam <levlam@telegram.org> | 2025-04-15 13:32:50 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2025-04-15 13:32:50 +0300 |
| commit | 3240ba6bba65ce6ee7af3ea9bddc0f71903596f8 (patch) | |
| tree | 2117fd82558b82bbc4b8e3a6a35c9de5cc57cde3 /tdutils | |
| parent | 0f098a3207d4c8968f1fdda50e165f6654652687 (diff) | |
Improve Ed25519 length checks.
Diffstat (limited to 'tdutils')
| -rw-r--r-- | tdutils/td/utils/Ed25519.cpp | 21 | ||||
| -rw-r--r-- | tdutils/td/utils/Ed25519.h | 2 |
2 files changed, 13 insertions, 10 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); diff --git a/tdutils/td/utils/Ed25519.h b/tdutils/td/utils/Ed25519.h index f03b3d1b1..8b99a584a 100644 --- a/tdutils/td/utils/Ed25519.h +++ b/tdutils/td/utils/Ed25519.h @@ -47,7 +47,7 @@ class Ed25519 { } static Result<PublicKey> from_slice(Slice slice) { - if (slice.size() != 32) { + if (slice.size() != LENGTH) { return Status::Error("Invalid slice size"); } return PublicKey(SecureString(slice)); |
