Fix loki-sn-keys ed25519 key restoration

crypto_sign_seed_keypair() does not safely work when seed==sk, so read
and store the seed separately to fix it.
This commit is contained in:
Jason Rhinelander 2020-11-27 13:26:36 -04:00
parent b54f8ae845
commit 6c50740375

View file

@ -282,15 +282,16 @@ int restore(bool ed25519, std::list<std::string_view> args) {
return error(7, "Invalid input: provide the secret key as 64 hex characters");
std::array<unsigned char, crypto_sign_SECRETKEYBYTES> skey;
std::array<unsigned char, crypto_sign_PUBLICKEYBYTES> pubkey;
std::array<unsigned char, crypto_sign_SEEDBYTES> seed;
std::optional<std::array<unsigned char, crypto_sign_PUBLICKEYBYTES>> pubkey_expected;
lokimq::from_hex(skey_hex.begin(), skey_hex.begin() + 64, skey.begin());
lokimq::from_hex(skey_hex.begin(), skey_hex.begin() + 64, seed.begin());
if (skey_hex.size() == 128)
lokimq::from_hex(skey_hex.begin() + 64, skey_hex.end(), pubkey_expected.emplace().begin());
if (ed25519) {
crypto_sign_seed_keypair(pubkey.data(), skey.data(), skey.data());
crypto_sign_seed_keypair(pubkey.data(), skey.data(), seed.data());
} else {
pubkey = pubkey_from_privkey(skey);
pubkey = pubkey_from_privkey(seed);
}
std::cout << "\nPublic key: " << lokimq::to_hex(pubkey.begin(), pubkey.end()) << "\n";
@ -326,7 +327,7 @@ int restore(bool ed25519, std::list<std::string_view> args) {
if (ed25519)
out.write(reinterpret_cast<const char*>(skey.data()), skey.size());
else
out.write(reinterpret_cast<const char*>(skey.data()), 32);
out.write(reinterpret_cast<const char*>(seed.data()), seed.size());
if (!out.good())
return error(2, "Failed to write to output file '" + filename + "': " + std::strerror(errno));