Browse Source

Merge branch 'develop' of https://github.com/ethereum/cpp-ethereum into develop

cl-refactor
lotrf3 10 years ago
parent
commit
7250a25e16
  1. 3
      CMakeLists.txt
  2. 9
      libdevcrypto/SecretStore.cpp
  3. 11
      libdevcrypto/SecretStore.h
  4. 2
      libethash/internal.c
  5. 6
      libethcore/Ethash.cpp
  6. 6
      libethcore/EthashAux.cpp
  7. 50
      libscrypt/crypto-scrypt-saltgen.c
  8. 44
      libscrypt/crypto_scrypt-hash.c
  9. 11
      libscrypt/libscrypt.h
  10. 64
      test/libdevcrypto/SecretStore.cpp

3
CMakeLists.txt

@ -32,7 +32,8 @@ option(USENPM "Use npm to recompile ethereum.js if it was changed" OFF)
option(PROFILING "Build in support for profiling" OFF) option(PROFILING "Build in support for profiling" OFF)
set(BUNDLE "none" CACHE STRING "Predefined bundle of software to build (none, full, user, tests, minimal).") set(BUNDLE "none" CACHE STRING "Predefined bundle of software to build (none, full, user, tests, minimal).")
option(MINER "Build the miner component" ON) option(MINER "Build the CLI miner component" ON)
option(ETHKEY "Build the CLI key manager component" ON)
option(SOLIDITY "Build the Solidity language components" ON) option(SOLIDITY "Build the Solidity language components" ON)
option(SERPENT "Build the Serpent language components" ON) option(SERPENT "Build the Serpent language components" ON)
option(TOOLS "Build the tools components" ON) option(TOOLS "Build the tools components" ON)

9
libdevcrypto/SecretStore.cpp

@ -165,12 +165,17 @@ void SecretStore::load(std::string const& _keysPath)
h128 SecretStore::readKey(std::string const& _file, bool _deleteFile) h128 SecretStore::readKey(std::string const& _file, bool _deleteFile)
{ {
cdebug << "Reading" << _file; cdebug << "Reading" << _file;
js::mValue u = upgraded(contentsString(_file)); return readKeyContent(contentsString(_file), _deleteFile ? _file : string());
}
h128 SecretStore::readKeyContent(std::string const& _content, std::string const& _file)
{
js::mValue u = upgraded(_content);
if (u.type() == js::obj_type) if (u.type() == js::obj_type)
{ {
js::mObject& o = u.get_obj(); js::mObject& o = u.get_obj();
auto uuid = fromUUID(o["id"].get_str()); auto uuid = fromUUID(o["id"].get_str());
m_keys[uuid] = make_pair(js::write_string(o["crypto"], false), _deleteFile ? _file : string()); m_keys[uuid] = make_pair(js::write_string(o["crypto"], false), _file);
return uuid; return uuid;
} }
else else

11
libdevcrypto/SecretStore.h

@ -43,6 +43,7 @@ public:
bytes secret(h128 const& _uuid, std::function<std::string()> const& _pass, bool _useCache = true) const; bytes secret(h128 const& _uuid, std::function<std::string()> const& _pass, bool _useCache = true) const;
h128 importKey(std::string const& _file) { auto ret = readKey(_file, false); if (ret) save(); return ret; } h128 importKey(std::string const& _file) { auto ret = readKey(_file, false); if (ret) save(); return ret; }
h128 importKeyContent(std::string const& _content) { auto ret = readKeyContent(_content, std::string()); if (ret) save(); return ret; }
h128 importSecret(bytes const& _s, std::string const& _pass); h128 importSecret(bytes const& _s, std::string const& _pass);
bool recode(h128 const& _uuid, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt); bool recode(h128 const& _uuid, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt);
void kill(h128 const& _uuid); void kill(h128 const& _uuid);
@ -52,16 +53,20 @@ public:
// Clear any cached keys. // Clear any cached keys.
void clearCache() const; void clearCache() const;
// Doesn't save().
h128 readKey(std::string const& _file, bool _deleteFile);
h128 readKeyContent(std::string const& _content, std::string const& _file = std::string());
void save(std::string const& _keysPath);
void save() { save(m_path); }
static std::string defaultPath() { return getDataDir("web3") + "/keys"; } static std::string defaultPath() { return getDataDir("web3") + "/keys"; }
private: private:
void save(std::string const& _keysPath);
void load(std::string const& _keysPath); void load(std::string const& _keysPath);
void save() { save(m_path); }
void load() { load(m_path); } void load() { load(m_path); }
static std::string encrypt(bytes const& _v, std::string const& _pass, KDF _kdf = KDF::Scrypt); static std::string encrypt(bytes const& _v, std::string const& _pass, KDF _kdf = KDF::Scrypt);
static bytes decrypt(std::string const& _v, std::string const& _pass); static bytes decrypt(std::string const& _v, std::string const& _pass);
h128 readKey(std::string const& _file, bool _deleteFile);
mutable std::unordered_map<h128, bytes> m_cached; mutable std::unordered_map<h128, bytes> m_cached;
std::unordered_map<h128, std::pair<std::string, std::string>> m_keys; std::unordered_map<h128, std::pair<std::string, std::string>> m_keys;

2
libethash/internal.c

@ -368,7 +368,7 @@ static bool ethash_mmap(struct ethash_full* ret, FILE* f)
if ((fd = ethash_fileno(ret->file)) == -1) { if ((fd = ethash_fileno(ret->file)) == -1) {
return false; return false;
} }
mmapped_data= mmap( mmapped_data = mmap(
NULL, NULL,
(size_t)ret->file_size + ETHASH_DAG_MAGIC_NUM_SIZE, (size_t)ret->file_size + ETHASH_DAG_MAGIC_NUM_SIZE,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,

6
libethcore/Ethash.cpp

@ -142,8 +142,12 @@ void Ethash::CPUMiner::workLoop()
WorkPackage w = work(); WorkPackage w = work();
EthashAux::FullType dag; EthashAux::FullType dag;
while (!shouldStop() && !(dag = EthashAux::full(w.seedHash, true))) while (!shouldStop() && !dag)
{
while (!shouldStop() && EthashAux::computeFull(w.seedHash, true) != 100)
this_thread::sleep_for(chrono::milliseconds(500)); this_thread::sleep_for(chrono::milliseconds(500));
dag = EthashAux::full(w.seedHash, false);
}
h256 boundary = w.boundary; h256 boundary = w.boundary;
unsigned hashCount = 1; unsigned hashCount = 1;

6
libethcore/EthashAux.cpp

@ -133,7 +133,9 @@ bytesConstRef EthashAux::LightAllocation::data() const
EthashAux::FullAllocation::FullAllocation(ethash_light_t _light, ethash_callback_t _cb) EthashAux::FullAllocation::FullAllocation(ethash_light_t _light, ethash_callback_t _cb)
{ {
// cdebug << "About to call ethash_full_new...";
full = ethash_full_new(_light, _cb); full = ethash_full_new(_light, _cb);
// cdebug << "Called OK.";
if (!full) if (!full)
BOOST_THROW_EXCEPTION(ExternalFunctionFailure("ethash_full_new()")); BOOST_THROW_EXCEPTION(ExternalFunctionFailure("ethash_full_new()"));
} }
@ -170,9 +172,9 @@ EthashAux::FullType EthashAux::full(h256 const& _seedHash, bool _createIfMissing
if (_createIfMissing || computeFull(_seedHash, false) == 100) if (_createIfMissing || computeFull(_seedHash, false) == 100)
{ {
s_dagCallback = _f; s_dagCallback = _f;
cnote << "Loading from libethash..."; // cnote << "Loading from libethash...";
ret = make_shared<FullAllocation>(l->light, dagCallbackShim); ret = make_shared<FullAllocation>(l->light, dagCallbackShim);
cnote << "Done loading."; // cnote << "Done loading.";
DEV_GUARDED(get()->x_fulls) DEV_GUARDED(get()->x_fulls)
get()->m_fulls[_seedHash] = get()->m_lastUsedFull = ret; get()->m_fulls[_seedHash] = get()->m_lastUsedFull = ret;

50
libscrypt/crypto-scrypt-saltgen.c

@ -1,50 +0,0 @@
#ifndef _MSC_VER
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#ifndef S_SPLINT_S /* Including this here triggers a known bug in splint */
//#include <unistd.h>
#endif
#define RNGDEV "/dev/urandom"
int libscrypt_salt_gen(uint8_t *salt, size_t len)
{
unsigned char buf[len];
size_t data_read = 0;
int urandom = open(RNGDEV, O_RDONLY);
if (urandom < 0)
{
return -1;
}
while (data_read < len) {
ssize_t result = read(urandom, buf + data_read, len - data_read);
if (result < 0)
{
if (errno == EINTR || errno == EAGAIN) {
continue;
}
else {
(void)close(urandom);
return -1;
}
}
data_read += result;
}
/* Failures on close() shouldn't occur with O_RDONLY */
(void)close(urandom);
memcpy(salt, buf, len);
return 0;
}
#endif

44
libscrypt/crypto_scrypt-hash.c

@ -1,44 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "b64.h"
#include "libscrypt.h"
int libscrypt_hash(char *dst, const char *passphrase, uint32_t N, uint8_t r,
uint8_t p)
{
int retval;
uint8_t salt[SCRYPT_SALT_LEN];
uint8_t hashbuf[SCRYPT_HASH_LEN];
char outbuf[256];
char saltbuf[256];
if(libscrypt_salt_gen(salt, SCRYPT_SALT_LEN) == -1)
{
return 0;
}
retval = libscrypt_scrypt((const uint8_t*)passphrase, strlen(passphrase),
(uint8_t*)salt, SCRYPT_SALT_LEN, N, r, p, hashbuf, sizeof(hashbuf));
if(retval == -1)
return 0;
retval = libscrypt_b64_encode((unsigned char*)hashbuf, sizeof(hashbuf),
outbuf, sizeof(outbuf));
if(retval == -1)
return 0;
retval = libscrypt_b64_encode((unsigned char *)salt, sizeof(salt),
saltbuf, sizeof(saltbuf));
if(retval == -1)
return 0;
retval = libscrypt_mcf(N, r, p, saltbuf, outbuf, dst);
if(retval != 1)
return 0;
return 1;
}

11
libscrypt/libscrypt.h

@ -33,17 +33,6 @@ int libscrypt_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t,
int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt,
const char *hash, char *mcf); const char *hash, char *mcf);
#ifndef _MSC_VER
/* Generates a salt. Uses /dev/urandom/
*/
int libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len);
/* Creates a hash of a passphrase using a randomly generated salt */
/* Returns >0 on success, or 0 for fail */
int libscrypt_hash(char *dst, const char* passphrase, uint32_t N, uint8_t r,
uint8_t p);
#endif
/* Checks a given MCF against a password */ /* Checks a given MCF against a password */
int libscrypt_check(char *mcf, const char *password); int libscrypt_check(char *mcf, const char *password);

64
test/libdevcrypto/SecretStore.cpp

@ -0,0 +1,64 @@
/*
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 SecretStore.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2015
* Secret store test functions.
*/
#include <fstream>
#include <random>
#include <boost/test/unit_test.hpp>
#include "../JsonSpiritHeaders.h"
#include <libdevcrypto/SecretStore.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/TrieDB.h>
#include <libdevcore/TrieHash.h>
#include "MemTrie.h"
#include "../TestHelper.h"
using namespace std;
using namespace dev;
namespace js = json_spirit;
BOOST_AUTO_TEST_SUITE(KeyStore)
BOOST_AUTO_TEST_CASE(basic_tests)
{
string testPath = test::getTestPath();
testPath += "/KeyStoreTests";
cnote << "Testing Key Store...";
js::mValue v;
string s = asString(contents(testPath + "/basic_tests.json"));
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'KeyStoreTests/basic_tests.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);
for (auto& i: v.get_obj())
{
cnote << i.first;
js::mObject& o = i.second.get_obj();
SecretStore store(".");
h128 u = store.readKeyContent(js::write_string(o["json"], false));
cdebug << "read uuid" << u;
bytes s = store.secret(u, [&](){ return o["password"].get_str(); });
cdebug << "got secret" << toHex(s);
BOOST_REQUIRE_EQUAL(toHex(s), o["priv"].get_str());
}
}
BOOST_AUTO_TEST_SUITE_END()
Loading…
Cancel
Save