24 #include <cryptopp/eccrypto.h>
25 #include <cryptopp/osrng.h>
26 #include <cryptopp/oids.h>
45 CryptoPP::AutoSeededRandomPool m_rng;
48 CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP> m_params;
50 CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>::EllipticCurve m_curve;
52 CryptoPP::Integer m_q;
53 CryptoPP::Integer m_qs;
55 static Secp256k1PPCtx& get()
57 static Secp256k1PPCtx ctx;
63 m_oid(CryptoPP::ASN1::secp256k1()), m_params(m_oid), m_curve(m_params.GetCurve()),
64 m_q(m_params.GetGroupOrder()), m_qs(m_params.GetSubgroupOrder())
68 inline CryptoPP::ECP::Point publicToPoint(
Public const& _p) { CryptoPP::Integer x(_p.
data(), 32); CryptoPP::Integer y(_p.
data() + 32, 32);
return CryptoPP::ECP::Point(x,y); }
70 inline CryptoPP::Integer secretToExponent(
Secret const& _s) {
return CryptoPP::Integer(_s.
data(),
Secret::size); }
95 ctx.Update(mKeyMaterial.
data(), mKeyMaterial.
size());
97 ctx.Final(mKey.data());
101 if (cipherText.empty())
112 CryptoPP::HMAC<CryptoPP::SHA256> hmacctx(mKey.data(), mKey.size());
114 hmacctx.Update(cipherWithIV.
data(), cipherWithIV.
size());
115 hmacctx.Update(_sharedMacData.
data(), _sharedMacData.
size());
118 io_cipher.resize(msg.size());
133 if (io_text.empty() || io_text[0] < 2 || io_text[0] > 4)
148 CryptoPP::SHA256 ctx;
149 ctx.Update(mKeyMaterial.
data(), mKeyMaterial.
size());
150 ctx.Final(mKey.data());
161 CryptoPP::HMAC<CryptoPP::SHA256> hmacctx(mKey.data(), mKey.size());
162 hmacctx.Update(cipherWithIV.
data(), cipherWithIV.
size());
163 hmacctx.Update(_sharedMacData.
data(), _sharedMacData.
size());
165 hmacctx.Final(mac.
data());
167 if (mac[i] != msgMac[i])
171 io_text.resize(plain.size());
179 auto& ctx = Secp256k1PPCtx::get();
181 #pragma GCC diagnostic push
182 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
183 #pragma clang diagnostic push
184 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
185 CryptoPP::ECIES<CryptoPP::ECP>::Encryptor e;
186 #pragma GCC diagnostic pop
187 #pragma clang diagnostic pop
190 Guard l(ctx.x_params);
191 e.AccessKey().Initialize(ctx.m_params, publicToPoint(_k));
195 size_t plen = io_cipher.size();
197 ciphertext.resize(e.CiphertextLength(plen));
201 e.Encrypt(ctx.m_rng, io_cipher.data(), plen, ciphertext.data());
204 memset(io_cipher.data(), 0, io_cipher.size());
205 io_cipher = std::move(ciphertext);
210 auto& ctx = Secp256k1PPCtx::get();
212 #pragma GCC diagnostic push
213 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
214 #pragma clang diagnostic push
215 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
216 CryptoPP::ECIES<CryptoPP::ECP>::Decryptor d;
217 #pragma GCC diagnostic pop
218 #pragma clang diagnostic pop
221 Guard l(ctx.x_params);
222 d.AccessKey().Initialize(ctx.m_params, secretToExponent(_k));
231 size_t clen = io_text.size();
233 plain.resize(d.MaxPlaintextLength(io_text.size()));
235 CryptoPP::DecodingResult r;
238 r = d.Decrypt(ctx.m_rng, io_text.data(), clen, plain.data());
241 if (!r.isValidCoding)
247 io_text.resize(r.messageLength);
248 io_text = std::move(plain);