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>
45 LogicError(
"SecretKey::SecretKey: invalid size");
61 *
out++ = (v >> 16) & 0xff;
62 *
out++ = (v >> 8) & 0xff;
97 Throw<std::runtime_error>(
"Unable to derive generator from seed");
156 Throw<std::runtime_error>(
"Unable to derive generator from seed");
163 secp256k1_pubkey pubkey;
164 if (secp256k1_ec_pubkey_create(
166 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_create failed");
170 if (secp256k1_ec_pubkey_serialize(
175 SECP256K1_EC_COMPRESSED) != 1)
176 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_serialize failed");
193 if (secp256k1_ec_seckey_tweak_add(
214 LogicError(
"sign: secp256k1 required for digest signing");
216 BOOST_ASSERT(sk.
size() == 32);
217 secp256k1_ecdsa_signature sig_imp;
218 if (secp256k1_ecdsa_sign(
221 reinterpret_cast<unsigned char const*
>(
digest.data()),
222 reinterpret_cast<unsigned char const*
>(sk.
data()),
223 secp256k1_nonce_function_rfc6979,
225 LogicError(
"sign: secp256k1_ecdsa_sign failed");
227 unsigned char sig[72];
228 size_t len =
sizeof(sig);
229 if (secp256k1_ecdsa_signature_serialize_der(
231 LogicError(
"sign: secp256k1_ecdsa_signature_serialize_der failed");
255 secp256k1_ecdsa_signature sig_imp;
256 if (secp256k1_ecdsa_sign(
259 reinterpret_cast<unsigned char const*
>(
digest.data()),
260 reinterpret_cast<unsigned char const*
>(sk.
data()),
261 secp256k1_nonce_function_rfc6979,
263 LogicError(
"sign: secp256k1_ecdsa_sign failed");
265 unsigned char sig[72];
266 size_t len =
sizeof(sig);
267 if (secp256k1_ecdsa_signature_serialize_der(
270 "sign: secp256k1_ecdsa_signature_serialize_der failed");
308 LogicError(
"generateSecretKey: unknown key type");
317 secp256k1_pubkey pubkey_imp;
318 if (secp256k1_ec_pubkey_create(
321 reinterpret_cast<unsigned char const*
>(sk.
data())) != 1)
323 "derivePublicKey: secp256k1_ec_pubkey_create failed");
325 unsigned char pubkey[33];
327 if (secp256k1_ec_pubkey_serialize(
332 SECP256K1_EC_COMPRESSED) != 1)
334 "derivePublicKey: secp256k1_ec_pubkey_serialize failed");
339 unsigned char buf[33];
341 ed25519_publickey(sk.
data(), &buf[1]);
380 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 end() const noexcept
const_iterator begin() 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::array< std::uint8_t, 33 > generator_
std::pair< PublicKey, SecretKey > operator()(std::size_t ordinal) const
Generate the nth key pair.
Generator(Seed const &seed)
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.
secp256k1_context const * secp256k1Context()
sha512_half_hasher_s::result_type sha512Half_s(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::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
std::string decodeBase58Token(std::string const &s, TokenType type)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::string strHex(FwdIt begin, FwdIt end)
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
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)
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
Returns the SHA512-Half digest of a message.