2021-03-09 22:24:35 +00:00
|
|
|
#include "types.hpp"
|
2019-01-13 16:30:07 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/buffer.hpp>
|
2022-10-05 18:05:25 +00:00
|
|
|
#include <llarp/util/file.hpp>
|
2019-06-24 16:26:15 +00:00
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
#include <iterator>
|
|
|
|
|
2022-02-17 18:44:31 +00:00
|
|
|
#include <oxenc/hex.h>
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2020-01-27 21:30:41 +00:00
|
|
|
#include <sodium/crypto_sign.h>
|
|
|
|
#include <sodium/crypto_sign_ed25519.h>
|
2020-01-28 21:55:36 +00:00
|
|
|
#include <sodium/crypto_scalarmult_ed25519.h>
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
PubKey::FromString(const std::string& str)
|
|
|
|
{
|
2020-06-29 21:46:25 +00:00
|
|
|
if (str.size() != 2 * size())
|
|
|
|
return false;
|
2022-02-17 18:44:31 +00:00
|
|
|
oxenc::from_hex(str.begin(), str.end(), begin());
|
2020-06-29 21:46:25 +00:00
|
|
|
return true;
|
2019-01-13 16:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
PubKey::ToString() const
|
|
|
|
{
|
2022-02-17 18:44:31 +00:00
|
|
|
return oxenc::to_hex(begin(), end());
|
2019-01-13 16:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-05-27 03:42:01 +00:00
|
|
|
SecretKey::LoadFromFile(const fs::path& fname)
|
2019-01-13 16:30:07 +00:00
|
|
|
{
|
2022-10-05 18:05:25 +00:00
|
|
|
size_t sz;
|
|
|
|
std::array<byte_t, 128> tmp;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sz = util::slurp_file(fname, tmp.data(), tmp.size());
|
|
|
|
}
|
|
|
|
catch (const std::exception&)
|
2019-01-13 16:30:07 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-15 00:42:50 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz == size())
|
2019-01-13 16:30:07 +00:00
|
|
|
{
|
|
|
|
// is raw buffer
|
2022-10-05 18:05:25 +00:00
|
|
|
std::copy_n(tmp.begin(), sz, begin());
|
2019-01-13 16:30:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-10-05 18:05:25 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-13 16:30:07 +00:00
|
|
|
return BDecode(&buf);
|
|
|
|
}
|
|
|
|
|
2020-01-27 21:30:41 +00:00
|
|
|
bool
|
|
|
|
SecretKey::Recalculate()
|
|
|
|
{
|
2020-01-30 16:38:39 +00:00
|
|
|
PrivateKey key;
|
|
|
|
PubKey pubkey;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!toPrivate(key) || !key.toPublic(pubkey))
|
2020-01-30 16:38:39 +00:00
|
|
|
return false;
|
|
|
|
std::memcpy(data() + 32, pubkey.data(), 32);
|
|
|
|
return true;
|
2020-01-27 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 16:34:05 +00:00
|
|
|
bool
|
|
|
|
SecretKey::toPrivate(PrivateKey& key) const
|
|
|
|
{
|
2020-01-31 21:05:50 +00:00
|
|
|
// Ed25519 calculates a 512-bit hash from the seed; the first half (clamped)
|
|
|
|
// is the private key; the second half is the hash that gets used in
|
|
|
|
// signing.
|
2020-01-30 16:34:05 +00:00
|
|
|
unsigned char h[crypto_hash_sha512_BYTES];
|
2020-04-07 18:38:56 +00:00
|
|
|
if (crypto_hash_sha512(h, data(), 32) < 0)
|
2020-01-30 16:34:05 +00:00
|
|
|
return false;
|
|
|
|
h[0] &= 248;
|
|
|
|
h[31] &= 63;
|
|
|
|
h[31] |= 64;
|
2020-01-31 20:38:08 +00:00
|
|
|
std::memcpy(key.data(), h, 64);
|
2020-01-30 16:34:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PrivateKey::toPublic(PubKey& pubkey) const
|
|
|
|
{
|
|
|
|
return crypto_scalarmult_ed25519_base_noclamp(pubkey.data(), data()) != -1;
|
|
|
|
}
|
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
bool
|
2020-05-27 03:42:01 +00:00
|
|
|
SecretKey::SaveToFile(const fs::path& fname) const
|
2019-01-13 16:30:07 +00:00
|
|
|
{
|
2022-10-05 18:05:25 +00:00
|
|
|
std::string tmp(128, 0);
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncode(&buf))
|
2019-01-13 16:30:07 +00:00
|
|
|
return false;
|
2022-10-05 18:05:25 +00:00
|
|
|
|
|
|
|
tmp.resize(buf.cur - buf.base);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
util::dump_file(fname, tmp);
|
2019-01-13 16:30:07 +00:00
|
|
|
}
|
2022-10-05 18:05:25 +00:00
|
|
|
catch (const std::exception&)
|
|
|
|
{
|
2019-01-13 16:30:07 +00:00
|
|
|
return false;
|
2022-10-05 18:05:25 +00:00
|
|
|
}
|
|
|
|
return true;
|
2019-01-13 16:30:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 15:45:18 +00:00
|
|
|
bool
|
2020-05-27 03:42:01 +00:00
|
|
|
IdentitySecret::LoadFromFile(const fs::path& fname)
|
2019-01-21 15:45:18 +00:00
|
|
|
{
|
2022-10-05 18:05:25 +00:00
|
|
|
std::array<byte_t, SIZE> buf;
|
|
|
|
size_t sz;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sz = util::slurp_file(fname, buf.data(), buf.size());
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
llarp::LogError("failed to load service node seed: ", e.what());
|
2019-01-21 15:45:18 +00:00
|
|
|
return false;
|
2022-10-05 18:05:25 +00:00
|
|
|
}
|
|
|
|
if (sz != SIZE)
|
2019-01-21 15:45:18 +00:00
|
|
|
{
|
2022-10-05 18:05:25 +00:00
|
|
|
llarp::LogError("service node seed size invalid: ", sz, " != ", SIZE);
|
2019-01-21 15:45:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-10-05 18:05:25 +00:00
|
|
|
std::copy(buf.begin(), buf.end(), begin());
|
2019-01-21 15:45:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:51:10 +00:00
|
|
|
byte_t*
|
2020-01-27 21:30:41 +00:00
|
|
|
Signature::Lo()
|
2019-03-20 14:51:10 +00:00
|
|
|
{
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte_t*
|
2020-01-27 21:30:41 +00:00
|
|
|
Signature::Lo() const
|
2019-03-20 14:51:10 +00:00
|
|
|
{
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
|
|
|
|
byte_t*
|
2020-01-27 21:30:41 +00:00
|
|
|
Signature::Hi()
|
2019-03-20 14:51:10 +00:00
|
|
|
{
|
|
|
|
return data() + 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
const byte_t*
|
2020-01-27 21:30:41 +00:00
|
|
|
Signature::Hi() const
|
2019-03-20 14:51:10 +00:00
|
|
|
{
|
|
|
|
return data() + 32;
|
|
|
|
}
|
2019-01-13 16:30:07 +00:00
|
|
|
} // namespace llarp
|