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 <>
78 boost::optional<Seed>
79 parseBase58(std::string const& s)
80 {
81  auto const result = decodeBase58Token(s, TokenType::FamilySeed);
82  if (result.empty())
83  return boost::none;
84  if (result.size() != 16)
85  return boost::none;
86  return Seed(makeSlice(result));
87 }
88 
89 boost::optional<Seed>
91 {
92  if (str.empty())
93  return boost::none;
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 boost::none;
102  }
103 
104  {
105  uint128 seed;
106 
107  if (seed.SetHexExact(str))
108  return Seed{Slice(seed.data(), seed.size())};
109  }
110 
111  if (auto seed = parseBase58<Seed>(str))
112  return seed;
113 
114  {
115  std::string key;
116  if (RFC1751::getKeyFromEnglish(key, str) == 1)
117  {
118  Blob const blob(key.rbegin(), key.rend());
119  return Seed{uint128{blob}};
120  }
121  }
122 
123  return generateSeed(str);
124 }
125 
127 seedAs1751(Seed const& seed)
128 {
129  std::string key;
130 
131  std::reverse_copy(seed.data(), seed.data() + 16, std::back_inserter(key));
132 
133  std::string encodedKey;
134  RFC1751::getEnglishFromKey(encodedKey, key);
135  return encodedKey;
136 }
137 
138 } // namespace ripple
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:194
std::string
STL class.
cstring
ripple::detail::basic_sha512_half_hasher
Returns the SHA512-Half digest of a message.
Definition: digest.h:175
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:108
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:103
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:450
algorithm
ripple::base_uint::size
constexpr static std::size_t size()
Definition: base_uint.h:462
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:43
ripple::base_uint
Definition: base_uint.h:63
ripple::TokenType::FamilySeed
@ FamilySeed
ripple::seedAs1751
std::string seedAs1751(Seed const &seed)
Encode a Seed in RFC1751 format.
Definition: Seed.cpp:127
std::array
STL class.
ripple::randomSeed
Seed randomSeed()
Create a seed using secure random numbers.
Definition: Seed.cpp:59
ripple::parseGenericSeed
boost::optional< Seed > parseGenericSeed(std::string const &str)
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
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:370
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:183
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:256
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::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::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