lotrf3
10 years ago
10 changed files with 92 additions and 116 deletions
@ -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 |
@ -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; |
|||
} |
@ -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…
Reference in new issue