@@ -296,8 +296,11 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key,
296
296
BigNum::mod_mul (u, y, inverse_y_plus_1, p, context);
297
297
298
298
auto pr_key = private_key.as_octet_string ();
299
+ if (pr_key.size () != PrivateKey::LENGTH) {
300
+ return Status::Error (" Wrong private key" );
301
+ }
299
302
unsigned char buf[64 ];
300
- SHA512 (Slice (pr_key).ubegin (), 32 , buf);
303
+ SHA512 (Slice (pr_key).ubegin (), pr_key. size () , buf);
301
304
buf[0 ] &= 248 ;
302
305
buf[31 ] &= 127 ;
303
306
buf[31 ] |= 64 ;
@@ -309,17 +312,15 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key,
309
312
SCOPE_EXIT {
310
313
EVP_PKEY_free (pkey_private);
311
314
};
312
- // LOG(ERROR) << buffer_to_hex(Slice(buf, 32));
313
315
314
- auto pub_key = u.to_le_binary (32 );
316
+ auto pub_key = u.to_le_binary (PublicKey::LENGTH );
315
317
auto pkey_public = EVP_PKEY_new_raw_public_key (EVP_PKEY_X25519, nullptr , Slice (pub_key).ubegin (), pub_key.size ());
316
318
if (pkey_public == nullptr ) {
317
319
return Status::Error (" Can't import public key" );
318
320
}
319
321
SCOPE_EXIT {
320
322
EVP_PKEY_free (pkey_public);
321
323
};
322
- // LOG(ERROR) << buffer_to_hex(pub_key);
323
324
324
325
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new (pkey_private, nullptr );
325
326
if (ctx == nullptr ) {
@@ -356,23 +357,25 @@ Result<SecureString> Ed25519::compute_shared_secret(const PublicKey &public_key,
356
357
357
358
Result<SecureString> Ed25519::get_public_key (Slice private_key) {
358
359
#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 ());
360
364
if (pkey_private == nullptr ) {
361
365
return Status::Error (" Invalid X25519 private key" );
362
366
}
363
367
SCOPE_EXIT {
364
368
EVP_PKEY_free (pkey_private);
365
369
};
366
370
367
- auto func = &EVP_PKEY_get_raw_public_key;
368
371
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 ) {
370
373
return Status::Error (" Failed to get raw key length" );
371
374
}
372
- CHECK (len == 32 );
375
+ CHECK (len == PublicKey::LENGTH );
373
376
374
377
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 ) {
376
379
return Status::Error (" Failed to get raw key" );
377
380
}
378
381
return std::move (result);
0 commit comments