50 AutoSeededRandomPool m_rng;
60 static Secp256k1PPCtx&
get()
62 static Secp256k1PPCtx ctx;
68 m_oid(ASN1::secp256k1()), m_params(m_oid), m_curve(m_params.GetCurve()),
69 m_q(m_params.GetGroupOrder()), m_qs(m_params.GetSubgroupOrder())
87 auto reps = ((kdByteLen + 7) * 8) / (CryptoPP::SHA256::BLOCKSIZE * 8);
91 bytes ctr({0, 0, 0, 1});
94 for (
unsigned i = 0; i <= reps; i++)
96 ctx.Update(ctr.data(), ctr.size());
98 ctx.Update(_s1.data(), _s1.size());
101 ctx.Final(digest.data());
105 move(digest.begin(), digest.end(), back_inserter(k));
107 if (++ctr[3] || ++ctr[2] || ++ctr[1] || ++ctr[0])
123 auto r = KeyPair::create();
126 auto key = eciesKDF(z,
bytes(), 32);
129 CryptoPP::SHA256 ctx;
130 ctx.Update(mKeyMaterial.
data(), mKeyMaterial.
size());
132 ctx.Final(mKey.data());
134 auto iv = h128::random();
136 if (cipherText.empty())
147 CryptoPP::HMAC<SHA256> hmacctx(mKey.data(), mKey.size());
149 hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
150 hmacctx.Update(_sharedMacData.
data(), _sharedMacData.
size());
151 hmacctx.Final(msg.data() + 1 +
Public::size + cipherWithIV.size());
153 io_cipher.resize(msg.size());
168 if (io_text.empty() || io_text[0] < 2 || io_text[0] > 4)
178 auto key = eciesKDF(z,
bytes(), 64);
182 CryptoPP::SHA256 ctx;
183 ctx.Update(mKeyMaterial.
data(), mKeyMaterial.
size());
184 ctx.Final(mKey.data());
192 h128 iv(cipherIV.toBytes());
195 CryptoPP::HMAC<SHA256> hmacctx(mKey.data(), mKey.size());
196 hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
197 hmacctx.Update(_sharedMacData.
data(), _sharedMacData.
size());
199 hmacctx.Final(mac.
data());
201 if (mac[i] != msgMac[i])
205 io_text.resize(plain.size());
213 auto& ctx = Secp256k1PPCtx::get();
216 Guard l(ctx.x_params);
217 e.AccessKey().Initialize(ctx.m_params, publicToPoint(_k));
220 size_t plen = io_cipher.size();
222 ciphertext.resize(e.CiphertextLength(plen));
226 e.Encrypt(ctx.m_rng, io_cipher.data(), plen, ciphertext.data());
229 memset(io_cipher.data(), 0, io_cipher.size());
230 io_cipher = std::move(ciphertext);
235 auto& ctx = Secp256k1PPCtx::get();
236 CryptoPP::ECIES<CryptoPP::ECP>::Decryptor
d;
238 Guard l(ctx.x_params);
239 d.AccessKey().Initialize(ctx.m_params, secretToExponent(_k));
248 size_t clen = io_text.size();
250 plain.resize(d.MaxPlaintextLength(io_text.size()));
255 r = d.Decrypt(ctx.m_rng, io_text.data(), clen, plain.data());
265 io_text = std::move(plain);
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Elliptical Curve Point over GF(p), where p is prime.
byte const * data() const
CryptoPP secp256k1 algorithms.
Elliptic Curve over GF(p), where p is prime.
bytesSec decryptSymNoAuth(SecureFixedHash< 16 > const &_k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
bool decryptECIES(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypt payload using ECIES standard with AES128-CTR.
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
ASN.1 object identifiers for algorthms and schemes.
std::hash for asio::adress
void encryptECIES(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypt payload using ECIES standard with AES128-CTR.
size_t messageLength
Recovered message length if isValidCoding is true, undefined otherwise.
std::vector< T > const & makeInsecure() const
vector_ref< _T > cropped(size_t _begin, size_t _count) const
Returns a decoding results.
vector_ref< byte > bytesRef
std::lock_guard< std::mutex > Guard
std::vector< byte > bytes
Multiple precision integer with arithmetic operations.
vector_ref< byte const > bytesConstRef
Elliptic Curve Parameters.
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
uint8_t const size_t const size
void copyTo(vector_ref< typename std::remove_const< _T >::type > _t) const
Copies the contents of this vector_ref to the contents of _t, up to the max size of _t...
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
void agree(Secret const &_s, Public const &_r, Secret &o_s)
bool isValidCoding
Flag to indicate the decoding is valid.
Classes and functions for Elliptic Curves over prime and binary fields.
Classes for access to the operating system's random number generators.
Template implementing constructors for public key algorithm classes.