20 #include <ripple/basics/contract.h>
21 #include <ripple/basics/strHex.h>
22 #include <ripple/beast/utility/rngfill.h>
23 #include <ripple/crypto/GenerateDeterministicKey.h>
24 #include <ripple/crypto/csprng.h>
25 #include <ripple/crypto/secure_erase.h>
26 #include <ripple/protocol/SecretKey.h>
27 #include <ripple/protocol/digest.h>
28 #include <ripple/protocol/impl/secp256k1.h>
30 #include <ed25519-donna/ed25519.h>
47 LogicError(
"SecretKey::SecretKey: invalid size");
101 LogicError(
"sign: secp256k1 required for digest signing");
103 BOOST_ASSERT(sk.
size() == 32);
104 secp256k1_ecdsa_signature sig_imp;
105 if (secp256k1_ecdsa_sign(
108 reinterpret_cast<unsigned char const*
>(
digest.data()),
109 reinterpret_cast<unsigned char const*
>(sk.
data()),
110 secp256k1_nonce_function_rfc6979,
112 LogicError(
"sign: secp256k1_ecdsa_sign failed");
114 unsigned char sig[72];
115 size_t len =
sizeof(sig);
116 if (secp256k1_ecdsa_signature_serialize_der(
118 LogicError(
"sign: secp256k1_ecdsa_signature_serialize_der failed");
142 secp256k1_ecdsa_signature sig_imp;
143 if (secp256k1_ecdsa_sign(
146 reinterpret_cast<unsigned char const*
>(
digest.data()),
147 reinterpret_cast<unsigned char const*
>(sk.
data()),
148 secp256k1_nonce_function_rfc6979,
150 LogicError(
"sign: secp256k1_ecdsa_sign failed");
152 unsigned char sig[72];
153 size_t len =
sizeof(sig);
154 if (secp256k1_ecdsa_signature_serialize_der(
157 "sign: secp256k1_ecdsa_signature_serialize_der failed");
201 LogicError(
"generateSecretKey: unknown key type");
210 secp256k1_pubkey pubkey_imp;
211 if (secp256k1_ec_pubkey_create(
214 reinterpret_cast<unsigned char const*
>(sk.
data())) != 1)
216 "derivePublicKey: secp256k1_ec_pubkey_create failed");
218 unsigned char pubkey[33];
220 if (secp256k1_ec_pubkey_serialize(
225 SECP256K1_EC_COMPRESSED) != 1)
227 "derivePublicKey: secp256k1_ec_pubkey_serialize failed");
232 unsigned char buf[33];
234 ed25519_publickey(sk.
data(), &buf[1]);
267 boost::optional<SecretKey>
273 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.
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::pair< PublicKey, SecretKey > operator()(Seed const &seed, std::size_t ordinal) const
Generate the nth key pair.
Returns the SHA512-Half digest of a message.
boost::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
Produces a sequence of secp256k1 key pairs.
An immutable linear range of bytes.
uint256 generateRootDeterministicPrivateKey(uint128 const &seed)
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
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.
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
Integers of any length that is a multiple of 32-bits.
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.
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.
Blob generateRootDeterministicPublicKey(uint128 const &seed)
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
uint256 generatePrivateDeterministicKey(Blob const &family, uint128 const &seed, int n)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Generator(Seed const &seed)
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)
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
std::string strHex(FwdIt begin, FwdIt end)
Blob generatePublicDeterministicKey(Blob const &generator, int n)
boost::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
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