Ethereum  PoC-8
The C++ Implementation of Ethereum
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/Address.h>
29 #include <libdevcore/Common.h>
30 #include <libdevcore/Exceptions.h>
31 #include <libdevcore/FixedHash.h>
32 
33 namespace dev
34 {
35 
37 
40 using Public = h512;
41 
44 using Signature = h520;
45 
47 {
48  SignatureStruct() = default;
49  SignatureStruct(Signature const& _s) { *(h520*)this = _s; }
50  SignatureStruct(h256 const& _r, h256 const& _s, byte _v): r(_r), s(_s), v(_v) {}
51  operator Signature() const { return *(h520 const*)this; }
52 
54  bool isValid() const noexcept;
55 
58  byte v = 0;
59 };
60 
62 using Secrets = std::vector<Secret>;
63 
65 Public toPublic(Secret const& _secret);
66 
68 Address toAddress(Public const& _public);
69 
72 Address toAddress(Secret const& _secret);
73 
74 // Convert transaction from and nonce to address.
75 Address toAddress(Address const& _from, u256 const& _nonce);
76 
78 void encrypt(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
79 
81 bool decrypt(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
82 
84 void encryptSym(Secret const& _k, bytesConstRef _plain, bytes& o_cipher);
85 
87 bool decryptSym(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
88 
90 void encryptECIES(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
91 
94 void encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytesConstRef _plain, bytes& o_cipher);
95 
97 bool decryptECIES(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
98 
101 bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytesConstRef _cipher, bytes& o_plaintext);
102 
104 std::pair<bytes, h128> encryptSymNoAuth(SecureFixedHash<16> const& _k, bytesConstRef _plain);
105 
107 bytes encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain);
108 
110 bytesSec decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher);
111 
113 inline bytes encryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
114 inline bytes encryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
115 
117 inline bytesSec decryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
118 inline bytesSec decryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
119 
121 Public recover(Signature const& _sig, h256 const& _hash);
122 
124 Signature sign(Secret const& _k, h256 const& _hash);
125 
127 bool verify(Public const& _k, Signature const& _s, h256 const& _hash);
128 
130 bytesSec pbkdf2(std::string const& _pass, bytes const& _salt, unsigned _iterations, unsigned _dkLen = 32);
131 
133 bytesSec scrypt(std::string const& _pass, bytes const& _salt, uint64_t _n, uint32_t _r, uint32_t _p, unsigned _dkLen);
134 
138 class KeyPair
139 {
140 public:
144  KeyPair(Secret const& _sec);
145 
147  static KeyPair create();
148 
150  static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const& _password);
151 
152  Secret const& secret() const { return m_secret; }
153 
155  Public const& pub() const { return m_public; }
156 
158  Address const& address() const { return m_address; }
159 
160  bool operator==(KeyPair const& _c) const { return m_public == _c.m_public; }
161  bool operator!=(KeyPair const& _c) const { return m_public != _c.m_public; }
162 
163 private:
164  Secret m_secret;
165  Public m_public;
166  Address m_address;
167 };
168 
169 namespace crypto
170 {
171 
172 DEV_SIMPLE_EXCEPTION(InvalidState);
173 
175 h256 kdf(Secret const& _priv, h256 const& _hash);
176 
186 class Nonce
187 {
188 public:
190  static Secret get() { static Nonce s; return s.next(); }
191 
192 private:
193  Nonce() = default;
194 
196  Secret next();
197 
198  std::mutex x_value;
199  Secret m_value;
200 };
201 
202 namespace ecdh
203 {
204 
205 bool agree(Secret const& _s, Public const& _r, Secret& o_s) noexcept;
206 
207 }
208 
209 namespace ecies
210 {
211 
212 bytes kdf(Secret const& _z, bytes const& _s1, unsigned kdByteLen);
213 
214 }
215 }
216 }
bytesConstRef ref() const
Definition: FixedHash.h:300
Definition: Address.cpp:20
bool agree(Secret const &_s, Public const &_r, Secret &o_s) noexcept
Definition: Common.cpp:354
uint8_t byte
Definition: Common.h:57
void encryptSym(Secret const &_k, bytesConstRef _plain, bytes &o_cipher)
Symmetric encryption.
Definition: Common.cpp:144
bytesSec decryptSymNoAuth(SecureFixedHash< 16 > const &_k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.h:117
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:130
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition: Common.cpp:254
Generator for non-repeating nonce material. The Nonce class should only be used when a non-repeating ...
Definition: Common.h:186
secure_vector< byte > bytesSec
Definition: Common.h:115
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
Definition: Common.cpp:156
std::vector< Secret > Secrets
A vector of secrets.
Definition: Common.h:62
SecureFixedHash< 32 > Secret
Definition: Common.h:36
Definition: FixedHash.h:390
FixedHash< 65 > h520
Definition: FixedHash.h:352
void encryptECIES(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypt payload using ECIES standard with AES128-CTR.
Definition: Common.cpp:118
h512 Public
Definition: Common.h:40
FixedHash< 64 > h512
Definition: FixedHash.h:353
SignatureStruct(h256 const &_r, h256 const &_s, byte _v)
Definition: Common.h:50
SignatureStruct()=default
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition: Common.cpp:66
bool decryptSym(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Symmetric decryption.
Definition: Common.cpp:150
Secret const & secret() const
Definition: Common.h:152
bytes kdf(Secret const &_z, bytes const &_s1, unsigned kdByteLen)
Definition: Common.cpp:372
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:202
std::vector< byte > bytes
Definition: Common.h:72
bool operator!=(KeyPair const &_c) const
Definition: Common.h:161
SignatureStruct(Signature const &_s)
Definition: Common.h:49
FixedHash< 32 > h256
Definition: FixedHash.h:354
h520 Signature
Definition: Common.h:44
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:86
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:279
DEV_SIMPLE_EXCEPTION(BadHexCharacter)
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:232
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:182
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
bool operator==(KeyPair const &_c) const
Definition: Common.h:160
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
Definition: Common.cpp:101
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
Definition: Common.cpp:108
Address const & address() const
Retrieve the associated address of the public key.
Definition: Common.h:158
bytes encryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _plain)
Encrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:162
Public const & pub() const
Retrieve the public key.
Definition: Common.h:155
h64 Nonce
Definition: Common.h:81
bool isValid() const noexcept
Definition: Common.cpp:58