Fabcoin Core  0.16.2
P2P Digital Currency
Common.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
25 #pragma once
26 
27 #include <mutex>
28 #include <libdevcore/Common.h>
29 #include <libdevcore/FixedHash.h>
30 #include <libdevcore/Exceptions.h>
31 
32 namespace dev
33 {
34 
36 
39 using Public = h512;
40 
43 using Signature = h520;
44 
46 {
47  SignatureStruct() = default;
48  SignatureStruct(Signature const& _s) { *(h520*)this = _s; }
49  SignatureStruct(h256 const& _r, h256 const& _s, byte _v): r(_r), s(_s), v(_v) {}
50  operator Signature() const { return *(h520 const*)this; }
51 
53  bool isValid() const noexcept;
54 
57  byte v = 0;
58 };
59 
62 using Address = h160;
63 
65 extern Address ZeroAddress;
66 
68 using Addresses = h160s;
69 
71 using AddressHash = std::unordered_set<h160>;
72 
74 using Secrets = std::vector<Secret>;
75 
77 Public toPublic(Secret const& _secret);
78 
80 Address toAddress(Public const& _public);
81 
84 Address toAddress(Secret const& _secret);
85 
86 // Convert transaction from and nonce to address.
87 Address toAddress(Address const& _from, u256 const& _nonce);
88 
90 void encrypt(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
91 
93 bool decrypt(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
94 
96 void encryptSym(Secret const& _k, bytesConstRef _plain, bytes& o_cipher);
97 
99 bool decryptSym(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
100 
102 void encryptECIES(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
103 
106 void encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytesConstRef _plain, bytes& o_cipher);
107 
109 bool decryptECIES(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
110 
113 bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytesConstRef _cipher, bytes& o_plaintext);
114 
116 std::pair<bytes, h128> encryptSymNoAuth(SecureFixedHash<16> const& _k, bytesConstRef _plain);
117 
119 bytes encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain);
120 
122 bytesSec decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher);
123 
125 inline bytes encryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
126 inline bytes encryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
127 
129 inline bytesSec decryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
130 inline bytesSec decryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
131 
133 Public recover(Signature const& _sig, h256 const& _hash);
134 
136 Signature sign(Secret const& _k, h256 const& _hash);
137 
139 bool verify(Public const& _k, Signature const& _s, h256 const& _hash);
140 
142 bytesSec pbkdf2(std::string const& _pass, bytes const& _salt, unsigned _iterations, unsigned _dkLen = 32);
143 
145 bytesSec scrypt(std::string const& _pass, bytes const& _salt, uint64_t _n, uint32_t _r, uint32_t _p, unsigned _dkLen);
146 
150 class KeyPair
151 {
152 public:
154  KeyPair() = default;
155 
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 
187 DEV_SIMPLE_EXCEPTION(InvalidState);
188 
190 h256 kdf(Secret const& _priv, h256 const& _hash);
191 
201 class Nonce
202 {
203 public:
205  static Secret get() { static Nonce s; return s.next(); }
206 
207 private:
208  Nonce() = default;
209 
211  Secret next();
212 
213  std::mutex x_value;
215 };
216 
217 namespace ecdh
218 {
219 
220 void agree(Secret const& _s, Public const& _r, Secret& o_s);
221 
222 }
223 }
224 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
uint8_t byte
Definition: Common.h:57
void encryptSym(Secret const &_k, bytesConstRef _plain, bytes &o_cipher)
Symmetric encryption.
Definition: Common.cpp:145
Secret m_secret
Definition: Common.h:179
bytesSec decryptSymNoAuth(SecureFixedHash< 16 > const &_k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.h:129
bytesSec pbkdf2(std::string const &_pass, bytes const &_salt, unsigned _iterations, unsigned _dkLen=32)
Derive key via PBKDF2.
bool decryptECIES(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypt payload using ECIES standard with AES128-CTR.
Definition: Common.cpp:131
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition: Common.cpp:255
Simple class that represents a "key pair".
Definition: Common.h:150
Generator for non-repeating nonce material.
Definition: Common.h:201
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
Definition: Common.cpp:157
std::vector< Secret > Secrets
A vector of secrets.
Definition: Common.h:74
Address m_address
Definition: Common.h:181
bool operator==(KeyPair const &_c) const
Definition: Common.h:175
FixedHash< 65 > h520
Definition: FixedHash.h:338
void encryptECIES(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypt payload using ECIES standard with AES128-CTR.
Definition: Common.cpp:119
Secret const & secret() const
Definition: Common.h:167
FixedHash< 64 > h512
Definition: FixedHash.h:339
SignatureStruct(h256 const &_r, h256 const &_s, byte _v)
Definition: Common.h:49
SignatureStruct()=default
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition: Common.cpp:67
bytesConstRef ref() const
Definition: FixedHash.h:292
bool decryptSym(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Symmetric decryption.
Definition: Common.cpp:151
Public const & pub() const
Retrieve the public key.
Definition: Common.h:170
bool operator!=(KeyPair const &_c) const
Definition: Common.h:176
Address ZeroAddress
The zero address.
Definition: Common.cpp:65
Public m_public
Definition: Common.h:180
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:203
std::vector< byte > bytes
Definition: Common.h:75
std::mutex x_value
Definition: Common.h:213
h256 kdf(Secret const &_priv, h256 const &_hash)
Key derivation.
Definition: Common.cpp:322
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
SignatureStruct(Signature const &_s)
Definition: Common.h:48
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
h520 Signature
A signature: 65 bytes: r: [0, 32), s: [32, 64), v: 64.
Definition: Common.h:43
h160s Addresses
A vector of Ethereum addresses.
Definition: Common.h:68
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:87
Address const & address() const
Retrieve the associated address of the public key.
Definition: Common.h:173
bytesSec scrypt(std::string const &_pass, bytes const &_salt, uint64_t _n, uint32_t _r, uint32_t _p, unsigned _dkLen)
Derive key via Scrypt.
Definition: Common.cpp:280
DEV_SIMPLE_EXCEPTION(BadHexCharacter)
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:233
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:183
std::vector< h160 > h160s
Definition: FixedHash.h:346
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
Definition: Common.cpp:102
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
Definition: Common.cpp:109
Secret m_value
Definition: Common.h:214
void agree(Secret const &_s, Public const &_r, Secret &o_s)
Definition: Common.cpp:348
bytes encryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _plain)
Encrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:163
FixedHash< 20 > h160
Definition: FixedHash.h:341
h64 Nonce
Definition: Common.h:70
bool isValid() const noexcept
Definition: Common.cpp:57
std::unordered_set< h160 > AddressHash
A hash set of Ethereum addresses.
Definition: Common.h:71