From 5967cb93f2e3afb815d7de98ebe04beb91202d11 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 19:06:31 +0200 Subject: [PATCH] add ecies encrypt/decrypt support to common crypto --- libdevcrypto/Common.cpp | 14 ++++++++++---- libdevcrypto/CryptoPP.h | 7 +++++-- libdevcrypto/EC.cpp | 1 + libdevcrypto/EC.h | 37 ++++++++++++++----------------------- libdevcrypto/ECDHE.cpp | 1 - test/TestHelperCrypto.h | 18 +----------------- test/crypto.cpp | 18 ++++++++++++++++++ 7 files changed, 49 insertions(+), 47 deletions(-) diff --git a/libdevcrypto/Common.cpp b/libdevcrypto/Common.cpp index 482a63cfc..a763c6283 100644 --- a/libdevcrypto/Common.cpp +++ b/libdevcrypto/Common.cpp @@ -22,6 +22,7 @@ #include "Common.h" #include #include +#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; } diff --git a/libdevcrypto/CryptoPP.h b/libdevcrypto/CryptoPP.h index 40be68aaa..5537b4409 100644 --- a/libdevcrypto/CryptoPP.h +++ b/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 const& _k, Public& _p); +/// Conversion from cryptopp private key to bytes void SecretFromDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC const& _k, Secret& _s); } diff --git a/libdevcrypto/EC.cpp b/libdevcrypto/EC.cpp index 04e9c681c..084539c17 100644 --- a/libdevcrypto/EC.cpp +++ b/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; diff --git a/libdevcrypto/EC.h b/libdevcrypto/EC.h index 1cae5c4f6..0a8394922 100644 --- a/libdevcrypto/EC.h +++ b/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 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 PublicTrust; + /** * @brief EC KeyPair * @deprecated */ class ECKeyPair { - friend class ECDHETKeyExchange; -public: - static ECKeyPair create(); + /// TO BE REMOVED -private: - ECKeyPair() {}; - + friend class ECDHETKeyExchange; std::map m_trustEgress; std::set m_trustIngress; }; diff --git a/libdevcrypto/ECDHE.cpp b/libdevcrypto/ECDHE.cpp index 8251c187d..ca6e483bf 100644 --- a/libdevcrypto/ECDHE.cpp +++ b/libdevcrypto/ECDHE.cpp @@ -23,7 +23,6 @@ #include "ECDHE.h" using namespace dev::crypto; -using namespace CryptoPP; ECDHE::ECDHE() { diff --git a/test/TestHelperCrypto.h b/test/TestHelperCrypto.h index 24104f118..01e97c21f 100644 --- a/test/TestHelperCrypto.h +++ b/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 -#include // secp256k1 -#include // ec domain -#include // ec prime field -#include // cryptopp buffer -#include -#include // aes modes -#pragma warning(pop) -#pragma GCC diagnostic pop +#include using namespace std; using namespace CryptoPP; diff --git a/test/crypto.cpp b/test/crypto.cpp index 40e0a6a41..3662bb83d 100644 --- a/test/crypto.cpp +++ b/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::Decryptor d(pp::PRNG(), pp::secp256k1());