Browse Source

Export and encrypt via dev::Public. more cryptopp abstraction.

cl-refactor
subtly 10 years ago
parent
commit
b97d9bbd6f
  1. 40
      libdevcrypto/CryptoPP.cpp
  2. 68
      libdevcrypto/CryptoPP.h
  3. 44
      libdevcrypto/EC.cpp
  4. 52
      libdevcrypto/EC.h
  5. 22
      test/crypto.cpp

40
libdevcrypto/CryptoPP.cpp

@ -0,0 +1,40 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file CryptoPP.h
* @author Alex Leverington <nessence@gmail.com>
* @date 2014
*
* CryptoPP wrappers
*/
#include "CryptoPP.h"
using namespace dev::crypto;
using namespace CryptoPP;
dev::Public pp::exportPublicKey(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k) {
Public p;
ECP::Point q(_k.GetPublicElement());
q.x.Encode(&p.data()[0], 32);
q.y.Encode(&p.data()[32], 32);
return p;
}
pp::ECKeyPair::ECKeyPair():
m_decryptor(pp::PRNG(), pp::secp256k1())
{
}

68
libdevcrypto/CryptoPP.h

@ -0,0 +1,68 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file CryptoPP.h
* @author Alex Leverington <nessence@gmail.com>
* @date 2014
*
* CryptoPP wrappers
*/
#pragma once
#include "Common.h"
#include "CryptoHeaders.h"
namespace dev
{
namespace crypto
{
namespace pp
// cryptopp wrappers
{
/// RNG used by CryptoPP
inline CryptoPP::AutoSeededRandomPool& PRNG() { static CryptoPP::AutoSeededRandomPool prng; return prng; }
/// EC curve used by CryptoPP
inline CryptoPP::OID const& secp256k1() { static CryptoPP::OID curve = CryptoPP::ASN1::secp256k1(); return curve; }
Public exportPublicKey(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k);
/**
* @brief CryptoPP-specific EC keypair
*/
class ECKeyPair
{
public:
/// Export address
Address const& address() const { return m_address; }
/// Export Public key
Public const& publicKey() const { return m_public; }
protected:
ECKeyPair();
CryptoPP::ECIES<CryptoPP::ECP>::Decryptor m_decryptor;
Address m_address;
Public m_public;
};
}
}
}

44
libdevcrypto/EC.cpp

@ -32,25 +32,30 @@
#include <files.h>
#pragma warning(pop)
#pragma GCC diagnostic pop
#include "CryptoPP.h"
#include "SHA3.h"
#include "EC.h"
// CryptoPP and dev conflict so dev and pp namespace are used explicitly
using namespace std;
using namespace dev;
using namespace dev::crypto;
using namespace CryptoPP;
dev::Public pp::exportPublicKey(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k) {
Public p;
ECP::Point q(_k.GetPublicElement());
q.x.Encode(&p.data()[0], 32);
q.y.Encode(&p.data()[32], 32);
return p;
}
pp::ECKeyPair::ECKeyPair():
m_decryptor(pp::PRNG(), pp::secp256k1())
void dev::crypto::encrypt(bytes& _plain, Public const& _key)
{
Integer x(&_key.data()[0], 32);
Integer y(&_key.data()[32], 32);
DL_PublicKey_EC<ECP> p;
p.Initialize(pp::secp256k1(), ECP::Point(x,y));
ECIES<ECP>::Encryptor e(p);
// todo: determine size and use _plain as input and output.
std::string c;
StringSource ss(_plain.data(), _plain.size(), true, new PK_EncryptorFilter(pp::PRNG(), e, new StringSink(c)));
bzero(_plain.data(), _plain.size() * sizeof(byte));
_plain = std::move(asBytes(c));
}
ECKeyPair ECKeyPair::create()
@ -74,25 +79,6 @@ void ECKeyPair::encrypt(bytes& _text)
_text = std::move(asBytes(c));
}
void ECKeyPair::encrypt(bytes& _plain, Public _key)
{
const char* xbytes = (char*)&_key[0];
Integer x(xbytes);
const char* ybytes = (char*)&_key[32];
Integer y(ybytes);
DL_PublicKey_EC<ECP> p;
p.Initialize(pp::secp256k1(), ECP::Point(x,y));
ECIES<ECP>::Encryptor e(p);
// todo: determine size and use _plain as input and output.
std::string c;
StringSource ss(_plain.data(), _plain.size(), true, new PK_EncryptorFilter(pp::PRNG(), e, new StringSink(c)));
bzero(_plain.data(), _plain.size() * sizeof(byte));
_plain = std::move(asBytes(c));
}
dev::bytes ECKeyPair::decrypt(bytesConstRef _c)
{
std::string p;

52
libdevcrypto/EC.h

@ -23,7 +23,7 @@
#pragma once
#include "CryptoHeaders.h"
#include "CryptoPP.h"
#include "Common.h"
namespace dev
@ -31,38 +31,6 @@ namespace dev
namespace crypto
{
namespace pp
// cryptopp wrappers
{
/// RNG used by CryptoPP
inline CryptoPP::AutoSeededRandomPool& PRNG() { static CryptoPP::AutoSeededRandomPool prng; return prng; }
/// EC curve used by CryptoPP
inline CryptoPP::OID const& secp256k1() { static CryptoPP::OID curve = CryptoPP::ASN1::secp256k1(); return curve; }
Public exportPublicKey(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k);
/**
* @brief CryptoPP-specific EC keypair
*/
class ECKeyPair
{
public:
/// Create a new, randomly generated keypair.
Address const& address() const { return m_address; }
Public const& publicKey() const { return m_public; }
protected:
ECKeyPair();
CryptoPP::ECIES<CryptoPP::ECP>::Decryptor m_decryptor;
Address m_address;
Public m_public;
};
}
/// ECDSA Signature
using Signature = FixedHash<65>;
@ -72,6 +40,15 @@ using Nonce = h256;
/// Public key with nonce corresponding to trusted key exchange.
typedef std::pair<Nonce,Public> PublicTrust;
/// Recover public key from signature.
//Public recover(Signature const& _sig, h256 _messageHash);
/// Replaces text with ciphertext.
void encrypt(bytes& _text, Public const& _key);
/// @returns ciphertext.
//bytes encrypt(bytesConstRef _text, Public const& _key);
/**
* @brief EC KeyPair
* @todo remove secret access
@ -86,15 +63,6 @@ class ECKeyPair: public pp::ECKeyPair
public:
static ECKeyPair create();
/// Replaces text with ciphertext.
static void encrypt(bytes& _text, Public _key);
/// @returns ciphertext.
static bytes encrypt(bytesConstRef _text, Public _key);
/// Recover public key from signature.
static Public recover(Signature _sig, h256 _messageHash);
/// Sign message.
Signature sign(h256 _messageHash);

22
test/crypto.cpp

@ -28,7 +28,7 @@
#include <libethereum/Transaction.h>
#include <boost/test/unit_test.hpp>
#include <libdevcrypto/EC.h>
#include <libdevcrypto/ECIES.h>
//#include <libdevcrypto/ECIES.h>
#include "TestHelperCrypto.h"
using namespace std;
@ -38,6 +38,21 @@ using namespace CryptoPP;
BOOST_AUTO_TEST_SUITE(devcrypto)
BOOST_AUTO_TEST_CASE(cryptopp_public_export_import)
{
ECIES<ECP>::Decryptor d(pp::PRNG(), pp::secp256k1());
ECIES<ECP>::Encryptor e(d.GetKey());
Public p = pp::exportPublicKey(e.GetKey());
Integer x(&p[0], 32);
Integer y(&p[32], 32);
DL_PublicKey_EC<ECP> pub;
pub.Initialize(pp::secp256k1(), ECP::Point(x,y));
assert(pub == e.GetKey());
}
BOOST_AUTO_TEST_CASE(eckeypair_encrypt)
{
ECKeyPair k = ECKeyPair::create();
@ -50,6 +65,11 @@ BOOST_AUTO_TEST_CASE(eckeypair_encrypt)
bytes p = k.decrypt(&b);
assert(p == asBytes(original));
encrypt(p, k.publicKey());
assert(p != asBytes(original));
// todo: test decrypt w/Secret
}
BOOST_AUTO_TEST_CASE(ecies)

Loading…
Cancel
Save