Skip to content

Commit

Permalink
Merge branch 'ethereum_sign'
Browse files Browse the repository at this point in the history
  • Loading branch information
sinev-valentine committed Sep 27, 2018
2 parents e109882 + 93dfe4b commit b32577d
Show file tree
Hide file tree
Showing 132 changed files with 28,654 additions and 77 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ project( KeychainApp )
cmake_minimum_required( VERSION 3.0.2 )

add_subdirectory(./keychain_lib)
add_subdirectory(./array-io-core)
add_subdirectory(./libraries/secp256k1_ext)
add_subdirectory(./libraries/ethereum)
add_subdirectory(./libraries/fc_light)

add_subdirectory(./keychain_linux/passentry_gui)

if( MSVC )
Expand Down
1 change: 0 additions & 1 deletion array-io-core
Submodule array-io-core deleted from 7f05aa
18 changes: 7 additions & 11 deletions keychain_cmd_app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project( KeychainCmdApp )
cmake_minimum_required( VERSION 3.0.2 )

set(CMAKE_CXX_FLAGS "-std=c++14")
set( CMAKE_EXE_LINKER_FLAGS "--verbose")

SET(BOOST_COMPONENTS)
LIST(APPEND BOOST_COMPONENTS thread date_time system filesystem program_options signals serialization chrono unit_test_framework context locale iostreams)
Expand All @@ -21,22 +22,18 @@ SET(Boost_LIBRARIES ${BOOST_LIBRARIES_TEMP} ${Boost_LIBRARIES})
if( MSVC )
include_directories(
"../keychain_lib/include"
"../array-io-core/libraries/utilities/include"
"../array-io-core/libraries/db/include"
"../array-io-core/libraries/chain/include"
"../array-io-core/libraries/fc/include"
"../libraries/fc_light/include"
"../libraries/ethereum/include"
"../libraries/secp256k1/include"
"${OPENSSL_ROOT_DIR}/include"
${Boost_INCLUDE_DIR})
else ( MSVC )
include_directories(
"../keychain_lib/include"
"../keychain_linux/passentry_cmd"
"../array-io-core/libraries/utilities/include"
"../array-io-core/libraries/db/include"
"../array-io-core/libraries/chain/include"
"../array-io-core/libraries/fc/include"
"../libraries/fc_light/include"
"../libraries/ethereum/include"
"../libraries/secp256k1/include"
"${OPENSSL_ROOT_DIR}/include"
"/usr/include/libcxxabi"
${Boost_INCLUDE_DIR})
Expand All @@ -61,11 +58,10 @@ else()
endif()

find_library(LIB_XI NAME Xi HINTS "/usr/lib/x86_64-linux-gnu/")
#find_library(LIB_FC_LIGTH NAME FC_LIGHT )

if( MSVC )
target_link_libraries(keychain keychain_common fc_light secp256k1 graphene_utilities ${LIB_OPENSSL} ${Boost_LIBRARIES})
target_link_libraries(keychain keychain_common fc_light ether ${LIB_OPENSSL} ${Boost_LIBRARIES})
else ( MSVC )
target_link_libraries(keychain keychain_common ${LIB_PTHREAD} fc_light secp256k1 graphene_utilities graphene_chain ${LIB_OPENSSL} ${Boost_LIBRARIES} ${X11_LIBRARIES} ${LIB_XI})
target_link_libraries(keychain keychain_common ${LIB_PTHREAD} fc_light ether ${LIB_OPENSSL} ${Boost_LIBRARIES} ${X11_LIBRARIES} ${LIB_XI})
endif ( MSVC )

19 changes: 9 additions & 10 deletions keychain_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project( KeychainLib )

cmake_minimum_required( VERSION 3.0.2 )

set(CMAKE_CXX_FLAGS "-std=c++14")
set(CMAKE_CXX_FLAGS "-std=c++14 --verbose")

SET(BOOST_COMPONENTS)
LIST(APPEND BOOST_COMPONENTS thread date_time system filesystem program_options signals serialization chrono unit_test_framework context locale iostreams)
Expand All @@ -21,21 +21,19 @@ SET(Boost_LIBRARIES ${BOOST_LIBRARIES_TEMP} ${Boost_LIBRARIES})
if( MSVC )
include_directories(
"./include/keychain_lib"
"../array-io-core/libraries/utilities/include"
"../array-io-core/libraries/db/include"
"../array-io-core/libraries/chain/include"
"../array-io-core/libraries/fc/include"
"../libraries/fc_light/include"
"../libraries/secp256k1/include"
"../libraries/secp256k1_ext/include"
"../libraries/ethereum/include"
"${OPENSSL_ROOT_DIR}/include"
${Boost_INCLUDE_DIR})
else ( MSVC )
include_directories(
"./include/keychain_lib"
"../array-io-core/libraries/utilities/include"
"../array-io-core/libraries/db/include"
"../array-io-core/libraries/chain/include"
"../array-io-core/libraries/fc/include"
"../libraries/fc_light/include"
"../libraries/secp256k1/include"
"../libraries/secp256k1_ext/include"
"../libraries/ethereum/include"
"${OPENSSL_ROOT_DIR}/include"
"/usr/include/libcxxabi"
${Boost_INCLUDE_DIR})
Expand All @@ -45,4 +43,5 @@ endif( MSVC )
file(GLOB KEYCHAIN_LIB_SOURCES "./src/*.cpp" )
file(GLOB KEYCHAIN_LIB_INCLUDE "./include/keychain_lib/*.hpp" )

add_library(keychain_common STATIC ${KEYCHAIN_LIB_SOURCES})
add_library(keychain_common STATIC ${KEYCHAIN_LIB_SOURCES} ${KEYCHAIN_LIB_INCLUDE})

4 changes: 1 addition & 3 deletions keychain_lib/include/keychain_lib/keychain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
#include <iostream>
#include <fstream>

#include <fc/crypto/elliptic.hpp>
#include <fc/crypto/hex.hpp>
#include <fc_light/crypto/hex.hpp>
#include <fc_light/variant.hpp>
#include <graphene/utilities/key_conversion.hpp>

#include <boost/filesystem.hpp>
#include <boost/signals2.hpp>
Expand Down
54 changes: 35 additions & 19 deletions keychain_lib/include/keychain_lib/keychain_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
#include <fc_light/variant.hpp>
#include <fc_light/io/json.hpp>
#include <fc_light/exception/exception.hpp>
#include <fc/crypto/hex.hpp>
#include <fc_light/crypto/hex.hpp>

#include <fc_light/reflect/variant.hpp>

#include <graphene/utilities/key_conversion.hpp>
#include <boost/signals2.hpp>

#include "key_file_parser.hpp"
#include "key_encryptor.hpp"
#include "sign_define.hpp"
#include <ethereum/core/FixedHash.h>
#include <ethereum/crypto/Common.h>
#include <secp256k1_ext.hpp>


namespace keychain_app {

Expand Down Expand Up @@ -78,10 +81,10 @@ fc_light::variant open_keyfile(const char_t* filename)
}

void create_keyfile(const char* filename, const fc_light::variant& keyfile_var);
secp256_private_key get_priv_key_from_str(const std::string& str);
fc::sha256 get_hash(const keychain_app::unit_list_t &list);
std::vector<unsigned char> get_hash(const keychain_app::unit_list_t &list);
size_t from_hex(const std::string& hex_str, unsigned char* out_data, size_t out_data_len );
std::string to_hex(const uint8_t* data, size_t length);

/*{
using out_map = std::map<std::string, nlohmann::json>;
using out_map_val = out_map::value_type;
Expand All @@ -90,6 +93,8 @@ std::string to_hex(const uint8_t* data, size_t length);
return result;
}*/



struct json_response
{
json_response(){}
Expand Down Expand Up @@ -197,18 +202,24 @@ struct keychain_command<command_te::sign> : keychain_command_base
try {
auto params = params_variant.as<params_t>();
unit_list_t unit_list;
fc::ecc::private_key private_key;

dev::Secret private_key;

if (!params.chainid.empty())
unit_list.push_back(fc::sha256(params.chainid));
{
std::vector<char> chain(32);
auto chain_len = keychain_app::from_hex(params.chainid, (unsigned char*) chain.data(), chain.size());
unit_list.push_back(std::move(chain));
}

//NOTE: using vector instead array because move semantic is implemented in the vector
std::vector<char> buf(1024);
auto trans_len = fc::from_hex(params.transaction, buf.data(), buf.size());
auto trans_len = keychain_app::from_hex(params.transaction, (unsigned char*) buf.data(), buf.size());
buf.resize(trans_len);
unit_list.push_back(buf);

keyfile_format::keyfile_t keyfile;

unit_list.push_back(buf);
if (params.keyname.empty())
std::runtime_error("Error: keyname is not specified");

Expand Down Expand Up @@ -239,9 +250,15 @@ struct keychain_command<command_te::sign> : keychain_command_base
{
key_data = std::move(keyfile.keyinfo.priv_key_data.as<std::string>());
}
private_key = get_priv_key_from_str(key_data);
auto signature = private_key.sign_compact(get_hash(unit_list));

int pk_len = keychain_app::from_hex(key_data, (unsigned char*) private_key.data(), 32);
std::array<unsigned char, 65> signature = {0};

sign_bitshares(
signature,
get_hash(unit_list).data(),
(unsigned char *) private_key.data()
);

json_response response(to_hex(signature.begin(), signature.size()).c_str(), id);
fc_light::variant res(response);
return fc_light::json::to_pretty_string(res);
Expand Down Expand Up @@ -278,15 +295,14 @@ struct keychain_command<command_te::create>: keychain_command_base
{
auto params = params_variant.as<params_t>();
keyfile_format::keyfile_t keyfile;
std::string wif_key;
fc::ecc::public_key_data public_key_data;
std::string pr_hex, pb_hex;
switch (params.curve)
{
case keyfile_format::keyfile_t::keyinfo_t::curve_etype::secp256k1:
{
auto priv_key = fc::ecc::private_key::generate();
public_key_data = priv_key.get_public_key().serialize();
wif_key = std::move(graphene::utilities::key_to_wif(priv_key));
dev::KeyPair keys = dev::KeyPair::create();
pb_hex = keys.pub().hex();
pr_hex = to_hex(reinterpret_cast<const uint8_t *>(keys.secret().data()), 32);
}
break;
default:
Expand All @@ -300,15 +316,15 @@ struct keychain_command<command_te::create>: keychain_command_base
if (passwd.empty())
throw std::runtime_error("Error: can't get password");
auto& encryptor = encryptor_singletone::instance();
auto enc_data = encryptor.encrypt_keydata(params.cipher, passwd, wif_key);
auto enc_data = encryptor.encrypt_keydata(params.cipher, passwd, pr_hex);
keyfile.keyinfo.priv_key_data = fc_light::variant(enc_data);
keyfile.keyinfo.encrypted = true;
}
else{
keyfile.keyinfo.priv_key_data = std::move(wif_key);
keyfile.keyinfo.priv_key_data = std::move(pr_hex);
keyfile.keyinfo.encrypted = false;
}
keyfile.keyinfo.public_key = to_hex(reinterpret_cast<const uint8_t *>(public_key_data.begin()), public_key_data.size());
keyfile.keyinfo.public_key = pb_hex;
keyfile.keyname = params.keyname;
keyfile.uid_hash = keychain->uid_hash;
keyfile.filetype = keyfile_format::TYPE_KEY;
Expand Down
5 changes: 1 addition & 4 deletions keychain_lib/include/keychain_lib/sign_define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
#ifndef KEYCHAINAPP_SIGN_DEFINE_HPP
#define KEYCHAINAPP_SIGN_DEFINE_HPP

#include <fc/crypto/elliptic.hpp>
#include <boost/variant.hpp>

namespace keychain_app
{

using signature_t = fc::ecc::compact_signature;
using unit_t = boost::variant< fc::sha256, std::vector<char> >;
using unit_t = boost::variant< std::vector<char> >;
using unit_list_t = std::vector< unit_t >;
using secp256_private_key = fc::ecc::private_key;
static const char* KEY_DEFAULT_PATH = "./key_data";

}
Expand Down
21 changes: 15 additions & 6 deletions keychain_lib/src/key_encryptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <random>
#include "key_encryptor.hpp"
#include "keychain_commands.hpp"
#include <openssl/sha.h>

using namespace keychain_app;

Expand Down Expand Up @@ -50,9 +51,13 @@ keyfile_format::encrypted_data encryptor_singletone::encrypt_keydata(keyfile_for
// to find out reason)
//The solution (from lib/fc) is to create hash from password string and encrypt data on hash key
const char* key_data = key.data();
auto key_hash = fc::sha512::hash(key_data, key.size());

if(1 != EVP_EncryptInit_ex(m_ctx, get_cipher(etype), NULL, reinterpret_cast<const uint8_t*>(&key_hash),
unsigned char key_hash[64];
SHA512_CTX ctx;
SHA512_Init( &ctx);
SHA512_Update( &ctx, key_data, key.size());
SHA512_Final(key_hash, &ctx);

if(1 != EVP_EncryptInit_ex(m_ctx, get_cipher(etype), NULL, reinterpret_cast<const uint8_t*>(key_hash),
reinterpret_cast<const uint8_t*>(enc_data.iv.c_str())))
{
ERR_print_errors_fp(stderr);
Expand Down Expand Up @@ -96,9 +101,13 @@ std::string encryptor_singletone::decrypt_keydata(const byte_seq_t& key, keyfile
// to find out reason)
//The solution (from lib/fc) is to create hash from password string and encrypt data on hash key
const char* key_data =key.data();
auto key_hash = fc::sha512::hash(key_data, key.size());

if(1 != EVP_DecryptInit_ex(m_ctx, get_cipher(data.cipher_type), NULL, reinterpret_cast<const uint8_t*>(&key_hash),
unsigned char key_hash[64];
SHA512_CTX ctx;
SHA512_Init( &ctx);
SHA512_Update( &ctx, key_data, key.size());
SHA512_Final(key_hash, &ctx);

if(1 != EVP_DecryptInit_ex(m_ctx, get_cipher(data.cipher_type), NULL, reinterpret_cast<const uint8_t*>(key_hash),
reinterpret_cast<const uint8_t*>(data.iv.c_str())))
{
// ERR_print_errors_fp(stderr);
Expand Down
3 changes: 0 additions & 3 deletions keychain_lib/src/keychain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
#include "keychain.hpp"
#include "key_file_parser.hpp"
#include "keychain_commands.hpp"
#include <fc/crypto/sha256.hpp>
#include <fc/crypto/elliptic.hpp>

//TODO: it is unclear
// why if we do not include this file we cannot compile reflect parser for keychain_app::keyfile_format::key_file
#include <graphene/chain/protocol/transaction.hpp>

#include <boost/hana/for_each.hpp>

Expand Down
32 changes: 13 additions & 19 deletions keychain_lib/src/keychain_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <iostream>
#include <array>

#include <graphene/utilities/key_conversion.hpp>
#include <fc_light/io/json.hpp>

#include "keychain_commands.hpp"
#include <openssl/sha.h>

std::string keychain_app::to_hex(const uint8_t* data, size_t length)
{
Expand Down Expand Up @@ -48,39 +48,33 @@ size_t keychain_app::from_hex( const std::string& hex_str, unsigned char* out_da
return out_pos - out_data;
}

fc::sha256 keychain_app::get_hash(const keychain_app::unit_list_t &list)
std::vector<unsigned char> keychain_app::get_hash(const keychain_app::unit_list_t &list)
{
class unit_visitor: public boost::static_visitor<>
{
public:
unit_visitor(fc::sha256::encoder& enc): m_enc(enc){}
unit_visitor() {SHA256_Init( &ctx); }

void operator()(const fc::sha256& val)
void operator()(const std::vector<char>& val)
{
m_enc << val;
SHA256_Update( &ctx, static_cast<const char*>(val.data()), val.size());
}

void operator()(const std::vector<char>& val)
std::vector<unsigned char> result()
{
return m_enc.write(static_cast<const char*>(val.data()), val.size());
std::vector<unsigned char> out(32);
SHA256_Final(out.data(), &ctx);
return out;
}
fc::sha256::encoder& m_enc;
SHA256_CTX ctx;
};

fc::sha256::encoder enc;
unit_visitor var_visitor(enc);

unit_visitor var_visitor;
std::for_each(list.begin(), list.end(),[&var_visitor](const unit_t& val){
boost::apply_visitor(var_visitor, val );
});
return enc.result();
}

keychain_app::secp256_private_key keychain_app::get_priv_key_from_str(const std::string& str)
{
auto result = graphene::utilities::wif_to_key(str);
if(!result)
throw std::runtime_error("Error: can't get private key from wif string");
return *result;
return var_visitor.result();
}

namespace bfs = keychain_app::bfs;
Expand Down

0 comments on commit b32577d

Please sign in to comment.