20 #include <ripple/basics/contract.h>
21 #include <ripple/basics/strHex.h>
22 #include <ripple/beast/utility/rngfill.h>
23 #include <ripple/crypto/csprng.h>
24 #include <ripple/crypto/secure_erase.h>
25 #include <ripple/protocol/SecretKey.h>
26 #include <ripple/protocol/digest.h>
27 #include <ripple/protocol/impl/secp256k1.h>
46 LogicError(
"SecretKey::SecretKey: invalid size");
62 *
out++ = (v >> 16) & 0xff;
63 *
out++ = (v >> 8) & 0xff;
98 Throw<std::runtime_error>(
"Unable to derive generator from seed");
157 Throw<std::runtime_error>(
"Unable to derive generator from seed");
164 secp256k1_pubkey pubkey;
165 if (secp256k1_ec_pubkey_create(
167 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_create failed");
171 if (secp256k1_ec_pubkey_serialize(
176 SECP256K1_EC_COMPRESSED) != 1)
177 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_serialize failed");
194 if (secp256k1_ec_privkey_tweak_add(
215 LogicError(
"sign: secp256k1 required for digest signing");
217 BOOST_ASSERT(sk.
size() == 32);
218 secp256k1_ecdsa_signature sig_imp;
219 if (secp256k1_ecdsa_sign(
222 reinterpret_cast<unsigned char const*
>(
digest.data()),
223 reinterpret_cast<unsigned char const*
>(sk.
data()),
224 secp256k1_nonce_function_rfc6979,
226 LogicError(
"sign: secp256k1_ecdsa_sign failed");
228 unsigned char sig[72];
229 size_t len =
sizeof(sig);
230 if (secp256k1_ecdsa_signature_serialize_der(
232 LogicError(
"sign: secp256k1_ecdsa_signature_serialize_der failed");
256 secp256k1_ecdsa_signature sig_imp;
257 if (secp256k1_ecdsa_sign(
260 reinterpret_cast<unsigned char const*
>(
digest.data()),
261 reinterpret_cast<unsigned char const*
>(sk.
data()),
262 secp256k1_nonce_function_rfc6979,
264 LogicError(
"sign: secp256k1_ecdsa_sign failed");
266 unsigned char sig[72];
267 size_t len =
sizeof(sig);
268 if (secp256k1_ecdsa_signature_serialize_der(
271 "sign: secp256k1_ecdsa_signature_serialize_der failed");
309 LogicError(
"generateSecretKey: unknown key type");
318 secp256k1_pubkey pubkey_imp;
319 if (secp256k1_ec_pubkey_create(
322 reinterpret_cast<unsigned char const*
>(sk.
data())) != 1)
324 "derivePublicKey: secp256k1_ec_pubkey_create failed");
326 unsigned char pubkey[33];
328 if (secp256k1_ec_pubkey_serialize(
333 SECP256K1_EC_COMPRESSED) != 1)
335 "derivePublicKey: secp256k1_ec_pubkey_serialize failed");
340 unsigned char buf[33];
342 ed25519_publickey(sk.
data(), &buf[1]);
381 if (result.size() != 32)
std::uint8_t const * data() const noexcept
std::size_t size() const noexcept
Returns the number of bytes in the storage.
uint256 calculateTweak(std::uint32_t seq) const
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)
Returns the SHA512-Half digest of a message.
An immutable linear range of bytes.
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
std::array< std::uint8_t, 33 > generator_
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
std::string decodeBase58Token(std::string const &s, TokenType type)
Decode a token of given type encoded using Base58Check and the XRPL alphabet.
Slice slice() const noexcept
Like std::vector<char> but better.
std::pair< PublicKey, SecretKey > operator()(std::size_t ordinal) const
Generate the nth key pair.
sha512_half_hasher_s::result_type sha512Half_s(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
constexpr static std::size_t size()
secp256k1_context const * secp256k1Context()
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
Generator(Seed const &seed)
void copy_uint32(std::uint8_t *out, std::uint32_t v)
Produces a sequence of secp256k1 key pairs.
std::uint8_t const * data() const
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
const_iterator begin() const noexcept
Seeds are used to generate deterministic secret keys.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &m)
Generate a signature for a message.
void rngfill(void *buffer, std::size_t bytes, Generator &g)
uint256 deriveDeterministicRootKey(Seed const &seed)
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::string strHex(FwdIt begin, FwdIt end)
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
const_iterator end() const noexcept
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
std::string to_string() const
Convert the secret key to a hexadecimal string.
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
std::uint8_t const * data() const