1#include <xrpl/basics/Buffer.h>
2#include <xrpl/basics/Slice.h>
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/basics/strHex.h>
6#include <xrpl/beast/utility/rngfill.h>
7#include <xrpl/crypto/csprng.h>
8#include <xrpl/crypto/secure_erase.h>
9#include <xrpl/protocol/KeyType.h>
10#include <xrpl/protocol/PublicKey.h>
11#include <xrpl/protocol/SecretKey.h>
12#include <xrpl/protocol/Seed.h>
13#include <xrpl/protocol/detail/secp256k1.h>
14#include <xrpl/protocol/digest.h>
15#include <xrpl/protocol/tokens.h>
17#include <boost/utility/string_view.hpp>
44 LogicError(
"SecretKey::SecretKey: invalid size");
60 *
out++ = (v >> 16) & 0xff;
61 *
out++ = (v >> 8) & 0xff;
96 Throw<std::runtime_error>(
"Unable to derive generator from seed");
155 Throw<std::runtime_error>(
"Unable to derive generator from seed");
162 secp256k1_pubkey pubkey;
163 if (secp256k1_ec_pubkey_create(
165 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_create failed");
169 if (secp256k1_ec_pubkey_serialize(
174 SECP256K1_EC_COMPRESSED) != 1)
175 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_serialize failed");
192 if (secp256k1_ec_seckey_tweak_add(
213 LogicError(
"sign: secp256k1 required for digest signing");
215 BOOST_ASSERT(sk.
size() == 32);
216 secp256k1_ecdsa_signature sig_imp;
217 if (secp256k1_ecdsa_sign(
220 reinterpret_cast<unsigned char const*
>(
digest.data()),
221 reinterpret_cast<unsigned char const*
>(sk.
data()),
222 secp256k1_nonce_function_rfc6979,
224 LogicError(
"sign: secp256k1_ecdsa_sign failed");
226 unsigned char sig[72];
227 size_t len =
sizeof(sig);
228 if (secp256k1_ecdsa_signature_serialize_der(
230 LogicError(
"sign: secp256k1_ecdsa_signature_serialize_der failed");
254 secp256k1_ecdsa_signature sig_imp;
255 if (secp256k1_ecdsa_sign(
258 reinterpret_cast<unsigned char const*
>(
digest.data()),
259 reinterpret_cast<unsigned char const*
>(sk.
data()),
260 secp256k1_nonce_function_rfc6979,
262 LogicError(
"sign: secp256k1_ecdsa_sign failed");
264 unsigned char sig[72];
265 size_t len =
sizeof(sig);
266 if (secp256k1_ecdsa_signature_serialize_der(
269 "sign: secp256k1_ecdsa_signature_serialize_der failed");
307 LogicError(
"generateSecretKey: unknown key type");
316 secp256k1_pubkey pubkey_imp;
317 if (secp256k1_ec_pubkey_create(
320 reinterpret_cast<unsigned char const*
>(sk.
data())) != 1)
322 "derivePublicKey: secp256k1_ec_pubkey_create failed");
324 unsigned char pubkey[33];
326 if (secp256k1_ec_pubkey_serialize(
331 SECP256K1_EC_COMPRESSED) != 1)
333 "derivePublicKey: secp256k1_ec_pubkey_serialize failed");
338 unsigned char buf[33];
340 ed25519_publickey(sk.
data(), &buf[1]);
379 if (result.size() != 32)
Like std::vector<char> but better.
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
std::uint8_t const * data() const noexcept
Slice slice() const noexcept
std::uint8_t const * data() const
std::string to_string() const
Convert the secret key to a hexadecimal string.
Seeds are used to generate deterministic secret keys.
const_iterator begin() const noexcept
const_iterator end() const noexcept
std::uint8_t const * data() const
An immutable linear range of bytes.
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
std::size_t size() const noexcept
Returns the number of bytes in the storage.
static constexpr std::size_t size()
Produces a sequence of secp256k1 key pairs.
uint256 calculateTweak(std::uint32_t seq) const
std::pair< PublicKey, SecretKey > operator()(std::size_t ordinal) const
Generate the nth key pair.
Generator(Seed const &seed)
std::array< std::uint8_t, 33 > generator_
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
void copy_uint32(std::uint8_t *out, std::uint32_t v)
uint256 deriveDeterministicRootKey(Seed const &seed)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string strHex(FwdIt begin, FwdIt end)
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
secp256k1_context const * secp256k1Context()
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
sha512_half_hasher_s::result_type sha512Half_s(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
std::string decodeBase58Token(std::string const &s, TokenType type)
Returns the SHA512-Half digest of a message.