1#include <xrpl/protocol/SecretKey.h>
3#include <xrpl/basics/Buffer.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/basics/strHex.h>
8#include <xrpl/beast/utility/rngfill.h>
9#include <xrpl/crypto/csprng.h>
10#include <xrpl/crypto/secure_erase.h>
11#include <xrpl/protocol/KeyType.h>
12#include <xrpl/protocol/PublicKey.h>
13#include <xrpl/protocol/Seed.h>
14#include <xrpl/protocol/detail/secp256k1.h>
15#include <xrpl/protocol/digest.h>
16#include <xrpl/protocol/tokens.h>
18#include <boost/utility/string_view.hpp>
46 logicError(
"SecretKey::SecretKey: invalid size");
62 *out++ = (v >> 16) & 0xff;
63 *out++ = (v >> 8) & 0xff;
163 secp256k1_pubkey pubkey;
165 logicError(
"derivePublicKey: secp256k1_ec_pubkey_create failed");
169 if (secp256k1_ec_pubkey_serialize(
171 logicError(
"derivePublicKey: secp256k1_ec_pubkey_serialize failed");
188 if (secp256k1_ec_seckey_tweak_add(
secp256k1Context(), rpk.data(), tweak.data()) == 1)
208 logicError(
"sign: secp256k1 required for digest signing");
210 BOOST_ASSERT(sk.
size() == 32);
211 secp256k1_ecdsa_signature sigImp;
212 if (secp256k1_ecdsa_sign(
215 reinterpret_cast<unsigned char const*
>(
digest.data()),
216 reinterpret_cast<unsigned char const*
>(sk.
data()),
217 secp256k1_nonce_function_rfc6979,
219 logicError(
"sign: secp256k1_ecdsa_sign failed");
221 unsigned char sig[72];
222 size_t len =
sizeof(sig);
223 if (secp256k1_ecdsa_signature_serialize_der(
secp256k1Context(), sig, &len, &sigImp) != 1)
224 logicError(
"sign: secp256k1_ecdsa_signature_serialize_der failed");
247 secp256k1_ecdsa_signature sigImp;
248 if (secp256k1_ecdsa_sign(
251 reinterpret_cast<unsigned char const*
>(
digest.data()),
252 reinterpret_cast<unsigned char const*
>(sk.
data()),
253 secp256k1_nonce_function_rfc6979,
255 logicError(
"sign: secp256k1_ecdsa_sign failed");
257 unsigned char sig[72];
258 size_t len =
sizeof(sig);
259 if (secp256k1_ecdsa_signature_serialize_der(
secp256k1Context(), sig, &len, &sigImp) !=
261 logicError(
"sign: secp256k1_ecdsa_signature_serialize_der failed");
299 logicError(
"generateSecretKey: unknown key type");
308 secp256k1_pubkey pubkeyImp;
309 if (secp256k1_ec_pubkey_create(
312 reinterpret_cast<unsigned char const*
>(sk.
data())) != 1)
313 logicError(
"derivePublicKey: secp256k1_ec_pubkey_create failed");
315 unsigned char pubkey[33];
317 if (secp256k1_ec_pubkey_serialize(
318 secp256k1Context(), pubkey, &len, &pubkeyImp, SECP256K1_EC_COMPRESSED) != 1)
319 logicError(
"derivePublicKey: secp256k1_ec_pubkey_serialize failed");
324 unsigned char buf[33];
326 ed25519_publickey(sk.
data(), &buf[1]);
365 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 toString() const
Convert the secret key to a hexadecimal string.
Seeds are used to generate deterministic secret keys.
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.
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 copyUInt32(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.
detail::BasicSha512HalfHasher< false > sha512_half_hasher
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
void secureErase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
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)
sha512_half_hasher_s::result_type sha512HalfS(Args const &... args)
Returns the SHA512-Half of a series of objects.
CsprngEngine & cryptoPrng()
The default cryptographically secure PRNG.
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.
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
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.
std::string decodeBase58Token(std::string const &s, TokenType type)
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
std::enable_if_t< std::is_same_v< T, char >||std::is_same_v< T, unsigned char >, Slice > makeSlice(std::array< T, N > const &a)