subtly
10 years ago
8 changed files with 381 additions and 77 deletions
@ -0,0 +1,49 @@ |
|||||
|
/*
|
||||
|
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 EC.cpp
|
||||
|
* @author Alex Leverington <nessence@gmail.com> |
||||
|
* @date 2014 |
||||
|
* |
||||
|
* Ethereum-specific data structures & algorithms. |
||||
|
*/ |
||||
|
|
||||
|
#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 <files.h> |
||||
|
#pragma warning(pop) |
||||
|
#pragma GCC diagnostic pop |
||||
|
#include "EC.H" |
||||
|
|
||||
|
using namespace std; |
||||
|
using namespace dev::crypto; |
||||
|
using namespace CryptoPP; |
||||
|
|
||||
|
ECKeyPair ECKeyPair::create() |
||||
|
{ |
||||
|
ECKeyPair k; |
||||
|
ECIES<ECP>::Decryptor d(PRNG(), secp256k1()); |
||||
|
k.m_sec = d.GetKey(); |
||||
|
ECIES<ECP>::Encryptor e(d); |
||||
|
k.m_pub = e.GetKey(); |
||||
|
return k; |
||||
|
} |
@ -0,0 +1,78 @@ |
|||||
|
/*
|
||||
|
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 EC.h
|
||||
|
* @author Alex Leverington <nessence@gmail.com> |
||||
|
* @date 2014 |
||||
|
* |
||||
|
* Ethereum-specific data structures & algorithms. |
||||
|
*/ |
||||
|
|
||||
|
#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" |
||||
|
#pragma GCC diagnostic ignored "-Wunused-function" |
||||
|
#include <osrng.h> |
||||
|
#include <oids.h> |
||||
|
#include <filters.h> |
||||
|
#include <eccrypto.h> |
||||
|
#include <ecp.h> |
||||
|
#pragma warning(pop) |
||||
|
#pragma GCC diagnostic pop |
||||
|
#include "Common.h" |
||||
|
|
||||
|
namespace dev |
||||
|
{ |
||||
|
namespace crypto |
||||
|
{ |
||||
|
|
||||
|
inline CryptoPP::AutoSeededRandomPool& PRNG() |
||||
|
{ |
||||
|
static CryptoPP::AutoSeededRandomPool prng; |
||||
|
return prng; |
||||
|
} |
||||
|
|
||||
|
inline CryptoPP::OID secp256k1() |
||||
|
{ |
||||
|
return CryptoPP::ASN1::secp256k1(); |
||||
|
} |
||||
|
|
||||
|
class ECKeyPair |
||||
|
{ |
||||
|
public: |
||||
|
static ECKeyPair create(); |
||||
|
CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> pub() { return m_pub; } // deprecate
|
||||
|
CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> sec() { return m_sec; } // deprecate
|
||||
|
|
||||
|
private: |
||||
|
ECKeyPair() {} |
||||
|
CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> m_pub; |
||||
|
CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> m_sec; |
||||
|
}; |
||||
|
|
||||
|
//class ECDHE;
|
||||
|
//bytes ECSign(KeyPair, bytesConstRef);
|
||||
|
//bool ECVerify(Public, bytesConstRef);
|
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
/*
|
||||
|
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 ECIES.cpp
|
||||
|
* @author Alex Leverington <nessence@gmail.com> |
||||
|
* @date 2014 |
||||
|
* |
||||
|
* Ethereum-specific data structures & algorithms. |
||||
|
*/ |
||||
|
|
||||
|
#include "EC.h" |
||||
|
#include "ECIES.h" |
||||
|
|
||||
|
using namespace std; |
||||
|
using namespace dev; |
||||
|
using namespace dev::crypto; |
||||
|
using namespace CryptoPP; |
||||
|
|
||||
|
ECIESEncryptor::ECIESEncryptor(ECKeyPair* _k) |
||||
|
{ |
||||
|
m_encryptor.AccessKey().AccessGroupParameters().Initialize(secp256k1()); |
||||
|
m_encryptor.AccessKey().SetPublicElement(_k->pub().GetPublicElement()); |
||||
|
} |
||||
|
|
||||
|
void ECIESEncryptor::encrypt(bytes& _message) |
||||
|
{ |
||||
|
std::string c; |
||||
|
StringSource ss(_message.data(), _message.size(), true, new PK_EncryptorFilter(PRNG(), m_encryptor, new StringSink(c))); |
||||
|
bzero(_message.data(), _message.size() * sizeof(byte)); |
||||
|
_message = std::move(bytesRef(c).toBytes()); |
||||
|
} |
||||
|
|
||||
|
ECIESDecryptor::ECIESDecryptor(ECKeyPair* _k) |
||||
|
{ |
||||
|
m_decryptor.AccessKey().AccessGroupParameters().Initialize(secp256k1()); |
||||
|
m_decryptor.AccessKey().SetPrivateExponent(_k->sec().GetPrivateExponent()); |
||||
|
} |
||||
|
|
||||
|
bytes ECIESDecryptor::decrypt(bytesConstRef& _c) |
||||
|
{ |
||||
|
std::string p; |
||||
|
StringSource ss(_c.data(), _c.size(), true, new PK_DecryptorFilter(PRNG(), m_decryptor, new StringSink(p))); |
||||
|
return std::move(bytesRef(p).toBytes()); |
||||
|
} |
||||
|
|
@ -0,0 +1,79 @@ |
|||||
|
/*
|
||||
|
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 ECIES.h
|
||||
|
* @author Alex Leverington <nessence@gmail.com> |
||||
|
* @date 2014 |
||||
|
* |
||||
|
* Ethereum-specific data structures & algorithms. |
||||
|
*/ |
||||
|
|
||||
|
#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 <eccrypto.h> |
||||
|
#include <ecp.h> |
||||
|
#include <files.h> |
||||
|
#include <filters.h> |
||||
|
#pragma warning(pop) |
||||
|
#pragma GCC diagnostic pop |
||||
|
#include <libdevcore/Exceptions.h> |
||||
|
#include "Common.h" |
||||
|
|
||||
|
namespace dev |
||||
|
{ |
||||
|
namespace crypto |
||||
|
{ |
||||
|
|
||||
|
/**
|
||||
|
* @brief ECIES Encryption |
||||
|
*/ |
||||
|
class ECIESEncryptor |
||||
|
{ |
||||
|
public: |
||||
|
ECIESEncryptor(ECKeyPair* _k); |
||||
|
|
||||
|
/// Encrypt _message. (object will be resized and replaced with cipher)
|
||||
|
void encrypt(bytes& _message); |
||||
|
|
||||
|
private: |
||||
|
CryptoPP::ECIES<CryptoPP::ECP>::Encryptor m_encryptor; |
||||
|
}; |
||||
|
|
||||
|
/**
|
||||
|
* @brief ECIES Decryption |
||||
|
*/ |
||||
|
class ECIESDecryptor |
||||
|
{ |
||||
|
public: |
||||
|
ECIESDecryptor(ECKeyPair* _k); |
||||
|
|
||||
|
/// Decrypt cipher to plain.
|
||||
|
bytes decrypt(bytesConstRef& _c); |
||||
|
|
||||
|
private: |
||||
|
CryptoPP::ECIES<CryptoPP::ECP>::Decryptor m_decryptor; |
||||
|
}; |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,48 @@ |
|||||
|
/*
|
||||
|
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 SHA3MAC.cpp
|
||||
|
* @author Alex Leverington <nessence@gmail.com> |
||||
|
* @date 2014 |
||||
|
*/ |
||||
|
|
||||
|
#pragma GCC diagnostic ignored "-Wunused-function" |
||||
|
#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 <sha3.h> |
||||
|
#pragma warning(pop) |
||||
|
#pragma GCC diagnostic pop |
||||
|
#include "SHA3MAC.h" |
||||
|
|
||||
|
using namespace dev; |
||||
|
using namespace dev::crypto; |
||||
|
using namespace CryptoPP; |
||||
|
|
||||
|
void sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output) |
||||
|
{ |
||||
|
CryptoPP::SHA3_256 ctx; |
||||
|
ctx.Update((byte*)_secret.data(), _secret.size()); |
||||
|
ctx.Update((byte*)_plain.data(), _plain.size()); |
||||
|
assert(_output.size() >= 32); |
||||
|
ctx.Final(_output.data()); |
||||
|
} |
||||
|
|
@ -0,0 +1,38 @@ |
|||||
|
/*
|
||||
|
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 SHA3MAC.h
|
||||
|
* @author Alex Leverington <nessence@gmail.com> |
||||
|
* @date 2014 |
||||
|
* |
||||
|
* Ethereum-specific data structures & algorithms. |
||||
|
*/ |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <libdevcore/Common.h> |
||||
|
#include <libdevcore/FixedHash.h> |
||||
|
|
||||
|
namespace dev |
||||
|
{ |
||||
|
namespace crypto |
||||
|
{ |
||||
|
|
||||
|
void sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
|
Loading…
Reference in new issue