xrpld
Loading...
Searching...
No Matches
Seed.cpp
1#include <xrpl/protocol/Seed.h>
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/beast/utility/rngfill.h>
8#include <xrpl/crypto/RFC1751.h>
9#include <xrpl/crypto/csprng.h>
10#include <xrpl/crypto/secure_erase.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/PublicKey.h>
13#include <xrpl/protocol/SecretKey.h>
14#include <xrpl/protocol/digest.h>
15#include <xrpl/protocol/tokens.h>
16
17#include <algorithm>
18#include <array>
19#include <cstdint>
20#include <cstring>
21#include <iterator>
22#include <optional>
23
24namespace xrpl {
25
27{
28 secureErase(buf_.data(), buf_.size());
29}
30
31Seed::Seed(Slice const& slice)
32{
33 if (slice.size() != buf_.size())
34 logicError("Seed::Seed: invalid size");
35 std::memcpy(buf_.data(), slice.data(), buf_.size());
36}
37
38Seed::Seed(uint128 const& seed)
39{
40 if (seed.size() != buf_.size())
41 logicError("Seed::Seed: invalid size");
42 std::memcpy(buf_.data(), seed.data(), buf_.size());
43}
44
45//------------------------------------------------------------------------------
46
47Seed
49{
51 beast::rngfill(buffer.data(), buffer.size(), cryptoPrng());
52 Seed const seed(makeSlice(buffer));
53 secureErase(buffer.data(), buffer.size());
54 return seed;
55}
56
57Seed
58generateSeed(std::string const& passPhrase)
59{
61 h(passPhrase.data(), passPhrase.size());
63 return Seed({digest.data(), 16});
64}
65
66template <>
69{
70 auto const result = decodeBase58Token(s, TokenType::FamilySeed);
71 if (result.empty())
72 return std::nullopt;
73 if (result.size() != 16)
74 return std::nullopt;
75 return Seed(makeSlice(result));
76}
77
79parseGenericSeed(std::string const& str, bool rfc1751)
80{
81 if (str.empty())
82 return std::nullopt;
83
88 {
89 return std::nullopt;
90 }
91
92 {
93 uint128 seed;
94
95 if (seed.parseHex(str))
96 return Seed{Slice(seed.data(), seed.size())};
97 }
98
99 if (auto seed = parseBase58<Seed>(str))
100 return seed;
101
102 if (rfc1751)
103 {
104 std::string key;
105 if (RFC1751::getKeyFromEnglish(key, str) == 1)
106 {
107 Blob const blob(key.rbegin(), key.rend());
108 return Seed{uint128::fromRaw(blob)};
109 }
110 }
111
112 return generateSeed(str);
113}
114
116seedAs1751(Seed const& seed)
117{
118 std::string key;
119
120 std::reverse_copy(seed.data(), seed.data() + 16, std::back_inserter(key));
121
122 std::string encodedKey;
123 RFC1751::getEnglishFromKey(encodedKey, key);
124 return encodedKey;
125}
126
127} // namespace xrpl
T back_inserter(T... args)
static BaseUInt fromRaw(Container const &c)
Definition base_uint.h:294
pointer data()
Definition base_uint.h:106
static constexpr std::size_t size()
Definition base_uint.h:530
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:507
static void getEnglishFromKey(std::string &strHuman, std::string const &strKey)
Convert to human from a 128 bit key in big-endian format.
Definition RFC1751.cpp:421
static int getKeyFromEnglish(std::string &strKey, std::string const &strHuman)
Convert words separated by spaces into a 128 bit key in big-endian format.
Definition RFC1751.cpp:391
Seeds are used to generate deterministic secret keys.
Definition Seed.h:14
Seed()=delete
std::array< uint8_t, 16 > buf_
Definition Seed.h:16
~Seed()
Destroy the seed.
Definition Seed.cpp:26
std::uint8_t const * data() const
Definition Seed.h:39
An immutable linear range of bytes.
Definition Slice.h:26
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:78
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:61
T data(T... args)
T empty(T... args)
T memcpy(T... args)
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition rngfill.h:14
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:139
void secureErase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Seed randomSeed()
Create a seed using secure random numbers.
Definition Seed.cpp:48
BaseUInt< 128 > uint128
Definition base_uint.h:560
CsprngEngine & cryptoPrng()
The default cryptographically secure PRNG.
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition Seed.cpp:116
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:58
void logicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
detail::BasicSha512HalfHasher< true > sha512_half_hasher_s
Definition digest.h:197
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
std::string decodeBase58Token(std::string const &s, TokenType type)
Definition tokens.cpp:188
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751=true)
Attempt to parse a string as a seed.
Definition Seed.cpp:79
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)
Definition Slice.h:215
T rbegin(T... args)
T rend(T... args)
T reverse_copy(T... args)
T size(T... args)