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 
45 
48 using Signature = h520;
49 
51 {
52  SignatureStruct() = default;
53  SignatureStruct(Signature const& _s) { *(h520*)this = _s; }
54  SignatureStruct(h256 const& _r, h256 const& _s, byte _v): r(_r), s(_s), v(_v) {}
55  operator Signature() const { return *(h520 const*)this; }
56 
58  bool isValid() const noexcept;
59 
62  byte v = 0;
63 };
64 
66 using Secrets = std::vector<Secret>;
67 
69 Public toPublic(Secret const& _secret);
70 
73 
75 Address toAddress(Public const& _public);
76 
79 Address toAddress(Secret const& _secret);
80 
81 // Convert transaction from and nonce to address.
82 Address toAddress(Address const& _from, u256 const& _nonce);
83 
85 void encrypt(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
86 
88 bool decrypt(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
89 
91 void encryptSym(Secret const& _k, bytesConstRef _plain, bytes& o_cipher);
92 
94 bool decryptSym(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
95 
97 void encryptECIES(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
98 
101 void encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytesConstRef _plain, bytes& o_cipher);
102 
104 bool decryptECIES(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
105 
108 bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytesConstRef _cipher, bytes& o_plaintext);
109 
111 std::pair<bytes, h128> encryptSymNoAuth(SecureFixedHash<16> const& _k, bytesConstRef _plain);
112 
114 bytes encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain);
115 
117 bytesSec decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher);
118 
120 inline bytes encryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
121 inline bytes encryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
122 
124 inline bytesSec decryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
125 inline bytesSec decryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
126 
128 Public recover(Signature const& _sig, h256 const& _hash);
129 
131 Signature sign(Secret const& _k, h256 const& _hash);
132 
134 bool verify(Public const& _k, Signature const& _s, h256 const& _hash);
135 
136 // Verify signature with compressed public key
137 bool verify(PublicCompressed const& _key, h512 const& _signature, h256 const& _hash);
138 
140 bytesSec pbkdf2(std::string const& _pass, bytes const& _salt, unsigned _iterations, unsigned _dkLen = 32);
141 
143 bytesSec scrypt(std::string const& _pass, bytes const& _salt, uint64_t _n, uint32_t _r, uint32_t _p, unsigned _dkLen);
144 
148 class KeyPair
149 {
150 public:
154  KeyPair(Secret const& _sec);
155 
157  static KeyPair create();
158 
160  static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const& _password);
161 
162  Secret const& secret() const { return m_secret; }
163 
165  Public const& pub() const { return m_public; }
166 
168  Address const& address() const { return m_address; }
169 
170  bool operator==(KeyPair const& _c) const { return m_public == _c.m_public; }
171  bool operator!=(KeyPair const& _c) const { return m_public != _c.m_public; }
172 
173 private:
174  Secret m_secret;
175  Public m_public;
176  Address m_address;
177 };
178 
179 namespace crypto
180 {
181 
182 DEV_SIMPLE_EXCEPTION(InvalidState);
183 
185 h256 kdf(Secret const& _priv, h256 const& _hash);
186 
196 class Nonce
197 {
198 public:
200  static Secret get() { static Nonce s; return s.next(); }
201 
202 private:
203  Nonce() = default;
204 
206  Secret next();
207 
208  std::mutex x_value;
209  Secret m_value;
210 };
211 
212 namespace ecdh
213 {
214 
215 bool agree(Secret const& _s, Public const& _r, Secret& o_s) noexcept;
216 
217 }
218 
219 namespace ecies
220 {
221 
222 bytes kdf(Secret const& _z, bytes const& _s1, unsigned kdByteLen);
223 
224 }
225 }
226 }
dev::encryptSymNoAuth
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
Definition: Common.cpp:175
dev::SignatureStruct::s
h256 s
Definition: Common.h:61
dev::scrypt
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:313
dev::encryptSym
void encryptSym(Secret const &_k, bytesConstRef _plain, bytes &o_cipher)
Symmetric encryption.
Definition: Common.cpp:163
dev::Signature
h520 Signature
Definition: Common.h:48
dev::vector_ref< byte const >
dev::decryptSymNoAuth
bytesSec decryptSymNoAuth(SecureFixedHash< 16 > const &_k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.h:124
dev::SignatureStruct::SignatureStruct
SignatureStruct()=default
dev::encryptAES128CTR
bytes encryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _plain)
Encrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:181
dev::SecureFixedHash< 32 >
dev::toPublicCompressed
PublicCompressed toPublicCompressed(Secret const &_secret)
Convert a secret key into the public key in compressed format.
Definition: Common.cpp:93
dev::pbkdf2
bytesSec pbkdf2(std::string const &_pass, bytes const &_salt, unsigned _iterations, unsigned _dkLen=32)
Derive key via PBKDF2.
FixedHash.h
dev::h256
FixedHash< 32 > h256
Definition: FixedHash.h:356
dev::decryptSym
bool decryptSym(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Symmetric decryption.
Definition: Common.cpp:169
Address.h
dev::crypto::Nonce
Generator for non-repeating nonce material. The Nonce class should only be used when a non-repeating ...
Definition: Common.h:197
dev::KeyPair::secret
Secret const & secret() const
Definition: Common.h:162
dev::h512
FixedHash< 64 > h512
Definition: FixedHash.h:355
dev::FixedHash
Definition: FixedHash.h:47
dev::SignatureStruct::isValid
bool isValid() const noexcept
Definition: Common.cpp:72
dev::toPublic
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition: Common.cpp:80
dev::verify
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition: Common.cpp:273
dev::SignatureStruct::r
h256 r
Definition: Common.h:60
dev::secure_vector
Definition: Common.h:78
Common.h
dev::SignatureStruct
Definition: Common.h:51
dev::recover
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:221
dev::h520
FixedHash< 65 > h520
Definition: FixedHash.h:354
dev::decryptECIES
bool decryptECIES(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypt payload using ECIES standard with AES128-CTR.
Definition: Common.cpp:149
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::crypto::Nonce::get
static Secret get()
Returns the next nonce (might be read from a file).
Definition: Common.h:200
dev::SignatureStruct::v
byte v
Definition: Common.h:62
dev::SignatureStruct::SignatureStruct
SignatureStruct(Signature const &_s)
Definition: Common.h:53
Exceptions.h
dev::KeyPair::operator!=
bool operator!=(KeyPair const &_c) const
Definition: Common.h:171
dev::KeyPair::address
Address const & address() const
Retrieve the associated address of the public key.
Definition: Common.h:168
dev::toAddress
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:105
dev::crypto::ecdh::agree
bool agree(Secret const &_s, Public const &_r, Secret &o_s) noexcept
Definition: Common.cpp:388
dev::eth::Nonce
h64 Nonce
Definition: Common.h:62
dev::decryptAES128CTR
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition: Common.cpp:201
dev::sign
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:251
dev::crypto::ecies::kdf
bytes kdf(Secret const &_z, bytes const &_s1, unsigned kdByteLen)
Definition: Common.cpp:406
dev::encryptECIES
void encryptECIES(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypt payload using ECIES standard with AES128-CTR.
Definition: Common.cpp:137
std
Definition: FixedHash.h:393
dev::KeyPair::pub
Public const & pub() const
Retrieve the public key.
Definition: Common.h:165
dev::Secrets
std::vector< Secret > Secrets
A vector of secrets.
Definition: Common.h:66
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev
Definition: Address.cpp:21
dev::KeyPair
Definition: Common.h:149
dev::Public
h512 Public
Definition: Common.h:40
dev::DEV_SIMPLE_EXCEPTION
DEV_SIMPLE_EXCEPTION(BadHexCharacter)
dev::Secret
SecureFixedHash< 32 > Secret
Definition: Common.h:36
dev::KeyPair::operator==
bool operator==(KeyPair const &_c) const
Definition: Common.h:170
dev::encrypt
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
Definition: Common.cpp:120
dev::SignatureStruct::SignatureStruct
SignatureStruct(h256 const &_r, h256 const &_s, byte _v)
Definition: Common.h:54
dev::SecureFixedHash::ref
bytesConstRef ref() const
Definition: FixedHash.h:302
dev::PublicCompressed
FixedHash< 33 > PublicCompressed
Definition: Common.h:44
dev::decrypt
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
Definition: Common.cpp:127