rippled
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 <ripple/basics/Buffer.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/beast/utility/rngfill.h>
23 #include <ripple/crypto/RFC1751.h>
24 #include <ripple/crypto/csprng.h>
25 #include <ripple/crypto/secure_erase.h>
26 #include <ripple/protocol/AccountID.h>
27 #include <ripple/protocol/PublicKey.h>
28 #include <ripple/protocol/SecretKey.h>
29 #include <ripple/protocol/Seed.h>
30 #include <ripple/protocol/digest.h>
31 #include <algorithm>
32 #include <cstring>
33 #include <iterator>
34 
35 namespace ripple {
36 
38 {
40 }
41 
42 Seed::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 
49 Seed::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 
58 Seed
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 
68 Seed
69 generateSeed(std::string const& passPhrase)
70 {
72  h(passPhrase.data(), passPhrase.size());
74  return Seed({digest.data(), 16});
75 }
76 
77 template <>
79 parseBase58(std::string const& s)
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 
90 parseGenericSeed(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 
128 seedAs1751(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
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
std::reverse_copy
T reverse_copy(T... args)
ripple::makeSlice
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:241
std::string
STL class.
cstring
ripple::detail::basic_sha512_half_hasher
Returns the SHA512-Half digest of a message.
Definition: digest.h:166
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
std::vector< unsigned char >
std::array::size
T size(T... args)
std::back_inserter
T back_inserter(T... args)
ripple::crypto_prng
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
Definition: csprng.cpp:99
iterator
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
ripple::decodeBase58Token
std::string decodeBase58Token(std::string const &s, TokenType type)
Decode a token of given type encoded using Base58Check and the XRPL alphabet.
Definition: tokens.cpp:223
ripple::Seed::~Seed
~Seed()
Destroy the seed.
Definition: Seed.cpp:37
ripple::base_uint::data
pointer data()
Definition: base_uint.h:122
ripple::RFC1751::getKeyFromEnglish
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:450
algorithm
ripple::base_uint::size
constexpr static std::size_t size()
Definition: base_uint.h:519
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:47
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::TokenType::FamilySeed
@ FamilySeed
ripple::seedAs1751
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition: Seed.cpp:128
std::array< std::uint8_t, 16 >
ripple::randomSeed
Seed randomSeed()
Create a seed using secure random numbers.
Definition: Seed.cpp:59
ripple::parseGenericSeed
std::optional< Seed > parseGenericSeed(std::string const &str, bool rfc1751)
Attempt to parse a string as a seed.
Definition: Seed.cpp:90
ripple::Seed::buf_
std::array< uint8_t, 16 > buf_
Definition: Seed.h:35
std::string::rend
T rend(T... args)
ripple::RFC1751::getEnglishFromKey
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:483
ripple::generateSeed
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition: Seed.cpp:69
ripple::Seed::Seed
Seed()=delete
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::basic_sha512_half_hasher::result_type
uint256 result_type
Definition: digest.h:174
ripple::Seed
Seeds are used to generate deterministic secret keys.
Definition: Seed.h:32
ripple::TokenType::AccountSecret
@ AccountSecret
beast::rngfill
void rngfill(void *buffer, std::size_t bytes, Generator &g)
Definition: rngfill.h:33
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:48
ripple::TokenType::AccountPublic
@ AccountPublic
std::string::empty
T empty(T... args)
ripple::TokenType::NodePublic
@ NodePublic
std::optional
std::memcpy
T memcpy(T... args)
ripple::base_uint::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:496
ripple::parseBase58
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:114
std::array::data
T data(T... args)
ripple::TokenType::NodePrivate
@ NodePrivate
std::string::rbegin
T rbegin(T... args)
ripple::secure_erase
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
Definition: secure_erase.cpp:26
ripple::Seed::data
std::uint8_t const * data() const
Definition: Seed.h:58