Browse Source

add ecies encrypt/decrypt support to common crypto

cl-refactor
subtly 10 years ago
parent
commit
5967cb93f2
  1. 14
      libdevcrypto/Common.cpp
  2. 7
      libdevcrypto/CryptoPP.h
  3. 1
      libdevcrypto/EC.cpp
  4. 37
      libdevcrypto/EC.h
  5. 1
      libdevcrypto/ECDHE.cpp
  6. 18
      test/TestHelperCrypto.h
  7. 18
      test/crypto.cpp

14
libdevcrypto/Common.cpp

@ -22,6 +22,7 @@
#include "Common.h"
#include <random>
#include <secp256k1/secp256k1.h>
#include "EC.h"
#include "SHA3.h"
using namespace std;
using namespace dev;
@ -110,14 +111,19 @@ KeyPair KeyPair::fromEncryptedSeed(bytesConstRef _seed, std::string const& _pass
void dev::encrypt(Public _k, bytesConstRef _plain, bytes& _cipher)
{
(void)_k;
_cipher = _plain.toBytes();
bytes io = _plain.toBytes();
crypto::encrypt(_k, io);
_cipher = std::move(io);
}
bool dev::decrypt(Secret _k, bytesConstRef _cipher, bytes& _plain)
{
(void)_k;
_plain = _cipher.toBytes();
bytes io = _cipher.toBytes();
crypto::decrypt(_k, io);
if (io.size())
_plain = std::move(io);
else
return false;
return true;
}

7
libdevcrypto/CryptoPP.h

@ -40,16 +40,19 @@ inline CryptoPP::AutoSeededRandomPool& PRNG() { static CryptoPP::AutoSeededRando
/// EC curve used by CryptoPP
inline CryptoPP::OID const& secp256k1() { static CryptoPP::OID curve = CryptoPP::ASN1::secp256k1(); return curve; }
///
/// Conversion from bytes to cryptopp point
CryptoPP::ECP::Point PointFromPublic(Public const& _p);
///
/// Conversion from bytes to cryptopp exponent
CryptoPP::Integer ExponentFromSecret(Secret const& _s);
/// Conversion from cryptopp exponent Integer to bytes
void PublicFromExponent(CryptoPP::Integer const& _k, Public& _s);
/// Conversion from cryptopp public key to bytes
void PublicFromDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k, Public& _p);
/// Conversion from cryptopp private key to bytes
void SecretFromDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> const& _k, Secret& _s);
}

1
libdevcrypto/EC.cpp

@ -80,6 +80,7 @@ SecretKeyRef::SecretKeyRef()
for (unsigned i = 0; i < 32; ++i)
m_secret[i] = (byte)d(s_eng);
/// todo: check key validity w/cryptopp
KeyPair ret(m_secret);
if (ret.address())
break;

37
libdevcrypto/EC.h

@ -23,36 +23,19 @@
#pragma once
#include "CryptoPP.h"
#include "Common.h"
namespace dev
{
namespace crypto
{
/// ECDSA Signature
using Signature = FixedHash<65>;
/// Secret nonce from trusted key exchange.
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);
/// Encrypts text (in place).
void encrypt(Public const& _k, bytes& _text);
/// Encrypt _text into _cipher.
//void encrypt(Public const& _k, bytesConstRef& _text, bytesRef& _cipher);
/// Decrypts text (in place).
void decrypt(Secret const& _k, bytes& _text);
class SecretKeyRef
{
public:
@ -75,21 +58,29 @@ private:
Secret m_secret;
};
/// [ECDHE Trusted Key Exchange]:
/// ECDSA Signature
using Signature = FixedHash<65>;
/// Secret nonce from trusted key exchange.
using Nonce = h256;
/// Public key with nonce corresponding to trusted key exchange.
typedef std::pair<Nonce,Public> PublicTrust;
/**
* @brief EC KeyPair
* @deprecated
*/
class ECKeyPair
{
friend class ECDHETKeyExchange;
public:
static ECKeyPair create();
/// TO BE REMOVED
private:
ECKeyPair() {};
friend class ECDHETKeyExchange;
std::map<Address,PublicTrust> m_trustEgress;
std::set<Nonce> m_trustIngress;
};

1
libdevcrypto/ECDHE.cpp

@ -23,7 +23,6 @@
#include "ECDHE.h"
using namespace dev::crypto;
using namespace CryptoPP;
ECDHE::ECDHE()
{

18
test/TestHelperCrypto.h

@ -21,23 +21,7 @@
#pragma once
#pragma warning(push)
#pragma warning(disable:4100 4244)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
#pragma GCC diagnostic ignored "-Wextra"
#include <osrng.h>
#include <eccrypto.h> // secp256k1
#include <oids.h> // ec domain
#include <ecp.h> // ec prime field
#include <files.h> // cryptopp buffer
#include <aes.h>
#include <modes.h> // aes modes
#pragma warning(pop)
#pragma GCC diagnostic pop
#include <libdevcrypto/CryptoPP.h>
using namespace std;
using namespace CryptoPP;

18
test/crypto.cpp

@ -37,6 +37,24 @@ using namespace CryptoPP;
BOOST_AUTO_TEST_SUITE(devcrypto)
BOOST_AUTO_TEST_CASE(common_crypt)
{
string message("Now is the time for all good persons to come to the aide of humanity.");
bytes m = asBytes(message);
bytesConstRef bcr(&m);
SecretKeyRef k;
bytes cipher;
encrypt(k.pub(), bcr, cipher);
assert(cipher != asBytes(message) && cipher.size() > 0);
bytes plain;
decrypt(k.sec(), bytesConstRef(&cipher), plain);
assert(asString(plain) == message);
assert(plain == asBytes(message));
}
BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1)
{
ECIES<ECP>::Decryptor d(pp::PRNG(), pp::secp256k1());

Loading…
Cancel
Save