TARAXA
Common.h
Go to the documentation of this file.
1 // Aleth: Ethereum C++ client, tools and libraries.
2 // Copyright 2014-2019 Aleth Authors.
3 // Licensed under the GNU General Public License, Version 3.
8 #pragma once
9 
10 #include <libdevcore/Address.h>
11 #include <libdevcore/Common.h>
12 #include <libdevcore/Exceptions.h>
13 #include <libdevcore/FixedHash.h>
14 
15 #include <mutex>
16 
17 namespace dev {
18 
20 
23 using Public = h512;
24 
28 
31 using Signature = h520;
32 
37 
39  SignatureStruct() = default;
40  SignatureStruct(Signature const& _s) { *(h520*)this = _s; }
41  SignatureStruct(h256 const& _r, h256 const& _s, byte _v) : r(_r), s(_s), v(_v) {}
43  v = ::byte(u256(s) >> 255);
44  s = u256(s) & ((u256(1) << 255) - 1);
45  }
46  operator Signature() const { return *reinterpret_cast<h520 const*>(this); }
47 
49  bool isValid() const noexcept;
50 
53  ::byte v = 0;
54 };
55 
58 h512 toCompact(const Signature& _s);
59 
62  CompactSignatureStruct(const CompactSignature& _s) { *(h512*)this = _s; }
64  CompactSignatureStruct(h256 const& _r, h256 const& _vs) : r(_r), vs(_vs) {}
65  operator CompactSignature() const { return *reinterpret_cast<h512 const*>(this); }
66 
68  bool isValid() const noexcept;
69 
71  h256 vs;
72 };
73 
75 using Secrets = std::vector<Secret>;
76 
78 Public toPublic(Secret const& _secret);
79 
81 Public toPublic(PublicCompressed const& _publicCompressed);
82 
85 
87 Address toAddress(Public const& _public);
88 
91 Address toAddress(Secret const& _secret);
92 
94 void encrypt(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
95 
97 bool decrypt(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
98 
100 void encryptECIES(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
101 
104 void encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytesConstRef _plain, bytes& o_cipher);
105 
107 bool decryptECIES(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
108 
111 bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytesConstRef _cipher, bytes& o_plaintext);
112 
114 std::pair<bytes, h128> encryptSymNoAuth(SecureFixedHash<16> const& _k, bytesConstRef _plain);
115 
117 bytes encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain);
118 
120 bytesSec decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher);
121 
123 inline bytes encryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _plain) {
124  return encryptAES128CTR(_k.ref(), _iv, _plain);
125 }
126 inline bytes encryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _plain) {
127  return encryptAES128CTR(_k.ref(), _iv, _plain);
128 }
129 
131 inline bytesSec decryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _cipher) {
132  return decryptAES128CTR(_k.ref(), _iv, _cipher);
133 }
134 inline bytesSec decryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _cipher) {
135  return decryptAES128CTR(_k.ref(), _iv, _cipher);
136 }
137 
139 Public recover(Signature const& _sig, h256 const& _hash);
140 
142 Signature sign(Secret const& _k, h256 const& _hash);
143 
145 bool verify(Public const& _k, Signature const& _s, h256 const& _hash);
146 
147 // Verify signature with compressed public key
148 bool verify(PublicCompressed const& _key, h512 const& _signature, h256 const& _hash);
149 
154 class KeyPair {
155  public:
159  KeyPair(Secret const& _sec);
160 
162  static KeyPair create();
163 
165  static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const& _password);
166 
167  Secret const& secret() const { return m_secret; }
168 
170  Public const& pub() const { return m_public; }
171 
173  Address const& address() const { return m_address; }
174 
175  bool operator==(KeyPair const& _c) const { return m_public == _c.m_public; }
176  bool operator!=(KeyPair const& _c) const { return m_public != _c.m_public; }
177 
178  private:
182 };
183 
184 namespace crypto {
185 
186 DEV_SIMPLE_EXCEPTION(InvalidState);
187 
189 h256 kdf(Secret const& _priv, h256 const& _hash);
190 
200 class Nonce {
201  public:
203  static Secret get() {
204  static Nonce s;
205  return s.next();
206  }
207 
208  private:
209  Nonce() = default;
210 
212  Secret next();
213 
214  std::mutex x_value;
216 };
217 
218 namespace ecdh {
219 
220 bool agree(Secret const& _s, Public const& _r, Secret& o_s) noexcept;
221 
222 }
223 
224 namespace ecies {
225 
226 bytes kdf(Secret const& _z, bytes const& _s1, unsigned kdByteLen);
227 
228 }
229 } // namespace crypto
230 } // namespace dev
Definition: Common.h:154
bool operator==(KeyPair const &_c) const
Definition: Common.h:175
Secret const & secret() const
Definition: Common.h:167
Public const & pub() const
Retrieve the public key.
Definition: Common.h:170
Address m_address
Definition: Common.h:181
Public m_public
Definition: Common.h:180
Address const & address() const
Retrieve the associated address of the public key.
Definition: Common.h:173
bool operator!=(KeyPair const &_c) const
Definition: Common.h:176
Secret m_secret
Definition: Common.h:179
bytesConstRef ref() const
Definition: FixedHash.h:392
Generator for non-repeating nonce material. The Nonce class should only be used when a non-repeating ...
Definition: Common.h:200
static Secret get()
Returns the next nonce (might be read from a file).
Definition: Common.h:203
Secret m_value
Definition: Common.h:215
std::mutex x_value
Definition: Common.h:214
Definition: Common.h:51
uint8_t byte
Definition: Common.h:25
bool agree(Secret const &_s, Public const &_r, Secret &o_s) noexcept
Definition: Common.cpp:276
bytes kdf(Secret const &_z, bytes const &_s1, unsigned kdByteLen)
Definition: Common.cpp:292
Definition: Address.h:13
FixedHash< 33 > PublicCompressed
Definition: Common.h:27
SecureFixedHash< 32 > Secret
Definition: Common.h:19
h512 CompactSignature
Definition: Common.h:36
h520 Signature
Definition: Common.h:31
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:95
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:169
h512 toCompact(const Signature &_s)
Definition: Common.cpp:214
h512 Public
Definition: Common.h:23
bytesSec decryptSymNoAuth(SecureFixedHash< 16 > const &_k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.h:131
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition: Common.cpp:220
DEV_SIMPLE_EXCEPTION(BadHexCharacter)
bool decryptECIES(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypt payload using ECIES standard with AES128-CTR.
Definition: Common.cpp:123
bytes encryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _plain)
Encrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:139
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:154
FixedHash< 65 > h520
Definition: FixedHash.h:451
std::vector<::byte > bytes
Definition: Common.h:46
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition: Common.cpp:57
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
Definition: Common.cpp:99
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:195
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
Definition: Common.cpp:105
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:98
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
Definition: Common.cpp:134
PublicCompressed toPublicCompressed(Secret const &_secret)
Convert a secret key into the public key in compressed format.
Definition: Common.cpp:85
FixedHash< 32 > h256
Definition: FixedHash.h:453
std::vector< Secret > Secrets
A vector of secrets.
Definition: Common.h:75
void encryptECIES(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypt payload using ECIES standard with AES128-CTR.
Definition: Common.cpp:113
FixedHash< 64 > h512
Definition: FixedHash.h:452
std::hash for asio::adress
Definition: FixedHash.h:483
Definition: Common.h:60
CompactSignatureStruct(h256 const &_r, h256 const &_vs)
Definition: Common.h:64
CompactSignatureStruct(const Signature &_s)
Definition: Common.h:63
CompactSignatureStruct(const CompactSignature &_s)
Definition: Common.h:62
Definition: Common.h:38
h256 r
Definition: Common.h:51
SignatureStruct(Signature const &_s)
Definition: Common.h:40
SignatureStruct(const CompactSignature &_s)
Definition: Common.h:42
bool isValid() const noexcept
Definition: Common.cpp:43
SignatureStruct()=default
h256 s
Definition: Common.h:52
SignatureStruct(h256 const &_r, h256 const &_s, byte _v)
Definition: Common.h:41
::byte v
Definition: Common.h:53