Skip to content

Commit 3240ba6

Browse files
committed
Improve Ed25519 length checks.
1 parent 0f098a3 commit 3240ba6

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

‎tdutils/td/utils/Ed25519.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,11 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key,
296296
BigNum::mod_mul(u, y, inverse_y_plus_1, p, context);
297297

298298
auto pr_key = private_key.as_octet_string();
299+
if (pr_key.size() != PrivateKey::LENGTH) {
300+
return Status::Error("Wrong private key");
301+
}
299302
unsigned char buf[64];
300-
SHA512(Slice(pr_key).ubegin(), 32, buf);
303+
SHA512(Slice(pr_key).ubegin(), pr_key.size(), buf);
301304
buf[0] &= 248;
302305
buf[31] &= 127;
303306
buf[31] |= 64;
@@ -309,17 +312,15 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key,
309312
SCOPE_EXIT {
310313
EVP_PKEY_free(pkey_private);
311314
};
312-
// LOG(ERROR) << buffer_to_hex(Slice(buf, 32));
313315

314-
auto pub_key = u.to_le_binary(32);
316+
auto pub_key = u.to_le_binary(PublicKey::LENGTH);
315317
auto pkey_public = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, nullptr, Slice(pub_key).ubegin(), pub_key.size());
316318
if (pkey_public == nullptr) {
317319
return Status::Error("Can't import public key");
318320
}
319321
SCOPE_EXIT {
320322
EVP_PKEY_free(pkey_public);
321323
};
322-
// LOG(ERROR) << buffer_to_hex(pub_key);
323324

324325
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey_private, nullptr);
325326
if (ctx == nullptr) {
@@ -356,23 +357,25 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key,
356357

357358
Result<SecureString> Ed25519::get_public_key(Slice private_key) {
358359
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
359-
auto pkey_private = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, nullptr, private_key.ubegin(), 32);
360+
if (private_key.size() != PrivateKey::LENGTH) {
361+
return Status::Error("Invalid X25519 private key");
362+
}
363+
auto pkey_private = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, nullptr, private_key.ubegin(), private_key.size());
360364
if (pkey_private == nullptr) {
361365
return Status::Error("Invalid X25519 private key");
362366
}
363367
SCOPE_EXIT {
364368
EVP_PKEY_free(pkey_private);
365369
};
366370

367-
auto func = &EVP_PKEY_get_raw_public_key;
368371
size_t len = 0;
369-
if (func(pkey_private, nullptr, &len) == 0) {
372+
if (EVP_PKEY_get_raw_public_key(pkey_private, nullptr, &len) == 0) {
370373
return Status::Error("Failed to get raw key length");
371374
}
372-
CHECK(len == 32);
375+
CHECK(len == PublicKey::LENGTH);
373376

374377
SecureString result(len);
375-
if (func(pkey_private, result.as_mutable_slice().ubegin(), &len) == 0) {
378+
if (EVP_PKEY_get_raw_public_key(pkey_private, result.as_mutable_slice().ubegin(), &len) == 0) {
376379
return Status::Error("Failed to get raw key");
377380
}
378381
return std::move(result);

‎tdutils/td/utils/Ed25519.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Ed25519 {
4747
}
4848

4949
static Result<PublicKey> from_slice(Slice slice) {
50-
if (slice.size() != 32) {
50+
if (slice.size() != LENGTH) {
5151
return Status::Error("Invalid slice size");
5252
}
5353
return PublicKey(SecureString(slice));

0 commit comments

Comments
 (0)