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());
116 hmacctx.Final(msg.data() + 1 +
Public::size + cipherWithIV.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());
158 h128 iv(cipherIV.toBytes());
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);
vector_ref< _T > cropped(size_t _begin, size_t _count) const
bool agree(Secret const &_s, Public const &_r, Secret &o_s) noexcept
bytesSec decryptSymNoAuth(SecureFixedHash< 16 > const &_k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
void encrypt(Public const &_k, bytes &io_cipher)
Encrypts text (replace input). (ECIES w/XOR-SHA1)
static KeyPair create()
Create a new, randomly generated object.
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...
byte const * data() const
bool decryptECIES(Secret const &_k, bytes &io_text)
Decrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
vector_ref< byte > bytesRef
std::lock_guard< std::mutex > Guard
bytes kdf(Secret const &_z, bytes const &_s1, unsigned kdByteLen)
std::vector< byte > bytes
vector_ref< byte const > bytesConstRef
void encryptECIES(Public const &_k, bytes &io_cipher)
Encrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
void decrypt(Secret const &_k, bytes &io_text)
Decrypts text (replace input). (ECIES w/XOR-SHA1)
static FixedHash random()
std::vector< T > const & makeInsecure() const
static Secp256k1PP * get()