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/Blob.h>
21#include <xrpl/basics/Slice.h>
22#include <xrpl/basics/base_uint.h>
23#include <xrpl/basics/contract.h>
24#include <xrpl/beast/utility/rngfill.h>
25#include <xrpl/crypto/RFC1751.h>
26#include <xrpl/crypto/csprng.h>
27#include <xrpl/crypto/secure_erase.h>
28#include <xrpl/protocol/AccountID.h>
29#include <xrpl/protocol/PublicKey.h>
30#include <xrpl/protocol/SecretKey.h>
31#include <xrpl/protocol/Seed.h>
32#include <xrpl/protocol/digest.h>
33#include <xrpl/protocol/tokens.h>
34
35#include <algorithm>
36#include <array>
37#include <cstdint>
38#include <cstring>
39#include <iterator>
40#include <optional>
41
42namespace ripple {
43
48
49Seed::Seed(Slice const& slice)
50{
51 if (slice.size() != buf_.size())
52 LogicError("Seed::Seed: invalid size");
53 std::memcpy(buf_.data(), slice.data(), buf_.size());
54}
55
56Seed::Seed(uint128 const& seed)
57{
58 if (seed.size() != buf_.size())
59 LogicError("Seed::Seed: invalid size");
60 std::memcpy(buf_.data(), seed.data(), buf_.size());
61}
62
63//------------------------------------------------------------------------------
64
65Seed
67{
69 beast::rngfill(buffer.data(), buffer.size(), crypto_prng());
70 Seed seed(makeSlice(buffer));
71 secure_erase(buffer.data(), buffer.size());
72 return seed;
73}
74
75Seed
76generateSeed(std::string const& passPhrase)
77{
79 h(passPhrase.data(), passPhrase.size());
81 return Seed({digest.data(), 16});
82}
83
84template <>
87{
88 auto const result = decodeBase58Token(s, TokenType::FamilySeed);
89 if (result.empty())
90 return std::nullopt;
91 if (result.size() != 16)
92 return std::nullopt;
93 return Seed(makeSlice(result));
94}
95
97parseGenericSeed(std::string const& str, bool rfc1751)
98{
99 if (str.empty())
100 return std::nullopt;
101
102 if (parseBase58<AccountID>(str) ||
103 parseBase58<PublicKey>(TokenType::NodePublic, str) ||
104 parseBase58<PublicKey>(TokenType::AccountPublic, str) ||
105 parseBase58<SecretKey>(TokenType::NodePrivate, str) ||
106 parseBase58<SecretKey>(TokenType::AccountSecret, str))
107 {
108 return std::nullopt;
109 }
110
111 {
112 uint128 seed;
113
114 if (seed.parseHex(str))
115 return Seed{Slice(seed.data(), seed.size())};
116 }
117
118 if (auto seed = parseBase58<Seed>(str))
119 return seed;
120
121 if (rfc1751)
122 {
123 std::string key;
124 if (RFC1751::getKeyFromEnglish(key, str) == 1)
125 {
126 Blob const blob(key.rbegin(), key.rend());
127 return Seed{uint128{blob}};
128 }
129 }
130
131 return generateSeed(str);
132}
133
135seedAs1751(Seed const& seed)
136{
137 std::string key;
138
139 std::reverse_copy(seed.data(), seed.data() + 16, std::back_inserter(key));
140
141 std::string encodedKey;
142 RFC1751::getEnglishFromKey(encodedKey, key);
143 return encodedKey;
144}
145
146} // 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:496
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:463
Seeds are used to generate deterministic secret keys.
Definition Seed.h:34
Seed()=delete
std::array< uint8_t, 16 > buf_
Definition Seed.h:36
~Seed()
Destroy the seed.
Definition Seed.cpp:44
std::uint8_t const * data() const
Definition Seed.h:59
An immutable linear range of bytes.
Definition Slice.h:46
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:98
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:81
Integers of any length that is a multiple of 32-bits.
Definition base_uint.h:86
static constexpr std::size_t size()
Definition base_uint.h:526
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:503
T data(T... args)
T empty(T... args)
T is_same_v
T memcpy(T... args)
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition rngfill.h:34
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:66
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition Seed.cpp:135
std::string decodeBase58Token(std::string const &s, TokenType type)
Definition tokens.cpp:209
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition tokens.cpp:156
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:244
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751=true)
Attempt to parse a string as a seed.
Definition Seed.cpp:97
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:76
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
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:172