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/protocol/Seed.h>
21 #include <ripple/basics/Buffer.h>
22 #include <ripple/protocol/AccountID.h>
23 #include <ripple/protocol/PublicKey.h>
24 #include <ripple/protocol/SecretKey.h>
25 #include <ripple/protocol/digest.h>
26 #include <ripple/basics/contract.h>
27 #include <ripple/crypto/RFC1751.h>
28 #include <ripple/crypto/csprng.h>
29 #include <ripple/beast/crypto/secure_erase.h>
30 #include <ripple/beast/utility/rngfill.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");
47  slice.data(), buf_.size());
48 }
49 
50 Seed::Seed (uint128 const& seed)
51 {
52  if (seed.size() != buf_.size())
53  LogicError("Seed::Seed: invalid size");
55  seed.data(), buf_.size());
56 }
57 
58 //------------------------------------------------------------------------------
59 
60 Seed
62 {
65  buffer.data(),
66  buffer.size(),
67  crypto_prng());
68  Seed seed (makeSlice (buffer));
69  beast::secure_erase(buffer.data(), buffer.size());
70  return seed;
71 }
72 
73 Seed
74 generateSeed (std::string const& passPhrase)
75 {
77  h(passPhrase.data(), passPhrase.size());
78  auto const digest =
80  return Seed({ digest.data(), 16 });
81 }
82 
83 template <>
84 boost::optional<Seed>
85 parseBase58 (std::string const& s)
86 {
87  auto const result = decodeBase58Token(s, TokenType::FamilySeed);
88  if (result.empty())
89  return boost::none;
90  if (result.size() != 16)
91  return boost::none;
92  return Seed(makeSlice(result));
93 }
94 
95 boost::optional<Seed>
97 {
98  if (str.empty ())
99  return boost::none;
100 
101  if (parseBase58<AccountID>(str) ||
102  parseBase58<PublicKey>(TokenType::NodePublic, str) ||
103  parseBase58<PublicKey>(TokenType::AccountPublic, str) ||
104  parseBase58<SecretKey>(TokenType::NodePrivate, str) ||
105  parseBase58<SecretKey>(TokenType::AccountSecret, str))
106  {
107  return boost::none;
108  }
109 
110  {
111  uint128 seed;
112 
113  if (seed.SetHexExact (str))
114  return Seed { Slice(seed.data(), seed.size()) };
115  }
116 
117  if (auto seed = parseBase58<Seed> (str))
118  return seed;
119 
120  {
121  std::string key;
122  if (RFC1751::getKeyFromEnglish (key, str) == 1)
123  {
124  Blob const blob (key.rbegin(), key.rend());
125  return Seed{ uint128{blob} };
126  }
127  }
128 
129  return generateSeed (str);
130 }
131 
133 seedAs1751 (Seed const& seed)
134 {
135  std::string key;
136 
138  seed.data(),
139  seed.data() + 16,
140  std::back_inserter(key));
141 
142  std::string encodedKey;
143  RFC1751::getEnglishFromKey (encodedKey, key);
144  return encodedKey;
145 }
146 
147 }
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:77
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:199
std::string
STL class.
cstring
ripple::detail::basic_sha512_half_hasher
Returns the SHA512-Half digest of a message.
Definition: digest.h:192
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
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:111
iterator
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:87
ripple::Seed::~Seed
~Seed()
Destroy the seed.
Definition: Seed.cpp:37
ripple::base_uint::data
pointer data()
Definition: base_uint.h:102
ripple::RFC1751::getKeyFromEnglish
static int getKeyFromEnglish(std::string &strKey, std::string const &strHuman)
Convert words seperated by spaces into a 128 bit key in big-endian format.
Definition: RFC1751.cpp:442
algorithm
ripple::base_uint::size
constexpr static std::size_t size()
Definition: base_uint.h:417
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:44
ripple::base_uint
Definition: base_uint.h:65
ripple::TokenType::FamilySeed
@ FamilySeed
ripple::seedAs1751
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition: Seed.cpp:133
std::array
STL class.
ripple::randomSeed
Seed randomSeed()
Create a seed using secure random numbers.
Definition: Seed.cpp:61
ripple::parseGenericSeed
boost::optional< Seed > parseGenericSeed(std::string const &str)
Attempt to parse a string as a seed.
Definition: Seed.cpp:96
beast::secure_erase
void secure_erase(void *dest, std::size_t bytes)
Guaranteed to fill memory with zeroes.
Definition: secure_erase.h:83
ripple::Seed::buf_
std::array< uint8_t, 16 > buf_
Definition: Seed.h:35
ripple::base_uint::SetHexExact
bool SetHexExact(const char *psz)
Parse a hex string into a base_uint The string must contain exactly bytes * 2 hex characters and must...
Definition: base_uint.h:327
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:471
ripple::generateSeed
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition: Seed.cpp:74
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:201
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:32
ripple::decodeBase58Token
static std::string decodeBase58Token(std::string const &s, TokenType type, InverseArray const &inv)
Definition: tokens.cpp:264
ripple::LogicError
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
Definition: contract.cpp:50
ripple::TokenType::AccountPublic
@ AccountPublic
std::string::empty
T empty(T... args)
ripple::TokenType::NodePublic
@ NodePublic
std::memcpy
T memcpy(T... args)
ripple::parseBase58
boost::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
Definition: AccountID.cpp:36
std::array::data
T data(T... args)
ripple::TokenType::NodePrivate
@ NodePrivate
std::string::rbegin
T rbegin(T... args)
ripple::Seed::data
std::uint8_t const * data() const
Definition: Seed.h:57