rippled
Loading...
Searching...
No Matches
Seed.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/Buffer.h>
21#include <xrpl/basics/contract.h>
22#include <xrpl/beast/utility/rngfill.h>
23#include <xrpl/crypto/RFC1751.h>
24#include <xrpl/crypto/csprng.h>
25#include <xrpl/crypto/secure_erase.h>
26#include <xrpl/protocol/AccountID.h>
27#include <xrpl/protocol/PublicKey.h>
28#include <xrpl/protocol/SecretKey.h>
29#include <xrpl/protocol/Seed.h>
30#include <xrpl/protocol/digest.h>
31#include <algorithm>
32#include <cstring>
33#include <iterator>
34
35namespace ripple {
36
38{
40}
41
42Seed::Seed(Slice const& slice)
43{
44 if (slice.size() != buf_.size())
45 LogicError("Seed::Seed: invalid size");
46 std::memcpy(buf_.data(), slice.data(), buf_.size());
47}
48
49Seed::Seed(uint128 const& seed)
50{
51 if (seed.size() != buf_.size())
52 LogicError("Seed::Seed: invalid size");
53 std::memcpy(buf_.data(), seed.data(), buf_.size());
54}
55
56//------------------------------------------------------------------------------
57
58Seed
60{
62 beast::rngfill(buffer.data(), buffer.size(), crypto_prng());
63 Seed seed(makeSlice(buffer));
64 secure_erase(buffer.data(), buffer.size());
65 return seed;
66}
67
68Seed
69generateSeed(std::string const& passPhrase)
70{
72 h(passPhrase.data(), passPhrase.size());
74 return Seed({digest.data(), 16});
75}
76
77template <>
80{
81 auto const result = decodeBase58Token(s, TokenType::FamilySeed);
82 if (result.empty())
83 return std::nullopt;
84 if (result.size() != 16)
85 return std::nullopt;
86 return Seed(makeSlice(result));
87}
88
90parseGenericSeed(std::string const& str, bool rfc1751)
91{
92 if (str.empty())
93 return std::nullopt;
94
95 if (parseBase58<AccountID>(str) ||
96 parseBase58<PublicKey>(TokenType::NodePublic, str) ||
97 parseBase58<PublicKey>(TokenType::AccountPublic, str) ||
98 parseBase58<SecretKey>(TokenType::NodePrivate, str) ||
99 parseBase58<SecretKey>(TokenType::AccountSecret, str))
100 {
101 return std::nullopt;
102 }
103
104 {
105 uint128 seed;
106
107 if (seed.parseHex(str))
108 return Seed{Slice(seed.data(), seed.size())};
109 }
110
111 if (auto seed = parseBase58<Seed>(str))
112 return seed;
113
114 if (rfc1751)
115 {
116 std::string key;
117 if (RFC1751::getKeyFromEnglish(key, str) == 1)
118 {
119 Blob const blob(key.rbegin(), key.rend());
120 return Seed{uint128{blob}};
121 }
122 }
123
124 return generateSeed(str);
125}
126
128seedAs1751(Seed const& seed)
129{
130 std::string key;
131
132 std::reverse_copy(seed.data(), seed.data() + 16, std::back_inserter(key));
133
134 std::string encodedKey;
135 RFC1751::getEnglishFromKey(encodedKey, key);
136 return encodedKey;
137}
138
139} // namespace ripple
T back_inserter(T... args)
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:487
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:454
Seeds are used to generate deterministic secret keys.
Definition: Seed.h:33
Seed()=delete
std::array< uint8_t, 16 > buf_
Definition: Seed.h:35
~Seed()
Destroy the seed.
Definition: Seed.cpp:37
std::uint8_t const * data() const
Definition: Seed.h:58
An immutable linear range of bytes.
Definition: Slice.h:45
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:85
pointer data()
Definition: base_uint.h:124
static constexpr std::size_t size()
Definition: base_uint.h:525
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:502
T data(T... args)
T empty(T... args)
T memcpy(T... args)
void rngfill(void *buffer, std::size_t bytes, Generator &g)
Definition: rngfill.h:33
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:116
Seed randomSeed()
Create a seed using secure random numbers.
Definition: Seed.cpp:59
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
Definition: csprng.cpp:99
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition: Seed.cpp:128
std::string decodeBase58Token(std::string const &s, TokenType type)
Definition: tokens.cpp:205
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:152
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)
Definition: Slice.h:243
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751=true)
Attempt to parse a string as a seed.
Definition: Seed.cpp:90
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition: Seed.cpp:69
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:48
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
T rbegin(T... args)
T rend(T... args)
T reverse_copy(T... args)
T size(T... args)
Returns the SHA512-Half digest of a message.
Definition: digest.h:171