Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mnemonic support #400

Merged
merged 17 commits into from Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion qa/pull-tester/rpc-tests.py
Expand Up @@ -173,7 +173,7 @@
'hardfork-451.py',
'hardfork-452.py',
'staticr-tx-send.py',

'mnemonic.py',
'sendtoaddress.py',
'stakeimmaturebalance.py',
'rpc-help.py',
Expand Down
55 changes: 55 additions & 0 deletions qa/rpc-tests/mnemonic.py
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
# Copyright (c) 2019 Navcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

from test_framework.test_framework import NavCoinTestFramework
from test_framework.util import (
start_node,
assert_equal,
slow_gen,
)
import os

class MnemonicTest(NavCoinTestFramework):

def __init__(self):
super().__init__()
self.setup_clean_chain = True
self.num_nodes = 1
self.languages = ["english", "spanish", "italian", "japanese",\
"french", "russian", "czech", "ukrainian",\
"simplified chinese", "traditional chinese"]
self.mnemonics = {}

def setup_network(self):
self.nodes = []
self.nodes.append(start_node(0, self.options.tmpdir, []))
self.is_network_split = False

def run_test (self):
# Record masterkeyid in base58
masterkeyid = self.nodes[0].dumpmasterprivkey()

# Record default mnemonic in English
mnemonic_eng = self.nodes[0].dumpmnemonic()

# Record mnemonics in other languages
for language in self.languages:
self.mnemonics[language] = self.nodes[0].dumpmnemonic(language)

print("Restoring from mnemonic ...")
self.check_mnemonic_works(masterkeyid, mnemonic_eng)

for language in self.languages:
self.check_mnemonic_works(masterkeyid, self.mnemonics[language], language)

def check_mnemonic_works(self, masterprivkey, mnemonic, language="english"):
self.stop_node(0)
os.remove(self.options.tmpdir + "/node0/devnet/wallet.dat")

self.nodes[0] = start_node(0, self.options.tmpdir, ["-importmnemonic=" + mnemonic, "-mnemoniclanguage=" + language])
assert_equal(masterprivkey, self.nodes[0].dumpmasterprivkey())

if __name__ == '__main__':
MnemonicTest().main ()
16 changes: 16 additions & 0 deletions src/Makefile.am
Expand Up @@ -156,6 +156,8 @@ NAVCOIN_CORE_H = \
wallet/rpcwallet.h \
wallet/wallet.h \
wallet/walletdb.h \
mnemonic/dictionary.h \
mnemonic/mnemonic.h \
zmq/zmqabstractnotifier.h \
zmq/zmqconfig.h\
zmq/zmqnotificationinterface.h \
Expand Down Expand Up @@ -229,6 +231,8 @@ libnavcoin_wallet_a_CPPFLAGS = $(AM_CPPFLAGS) $(NAVCOIN_INCLUDES)
libnavcoin_wallet_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libnavcoin_wallet_a_SOURCES = \
consensus/cfund.cpp \
mnemonic/dictionary.cpp \
mnemonic/mnemonic.cpp \
wallet/crypter.cpp \
wallet/db.cpp \
wallet/navtech.cpp \
Expand All @@ -252,6 +256,18 @@ crypto_libnavcoin_crypto_a_SOURCES = \
crypto/hmac_sha512.h \
crypto/ripemd160.cpp \
crypto/ripemd160.h \
crypto/external/hmac_sha256.c \
crypto/external/hmac_sha256.h \
crypto/external/hmac_sha512.c \
crypto/external/hmac_sha512.h \
crypto/external/pkcs5_pbkdf2.c \
crypto/external/pkcs5_pbkdf2.h \
crypto/external/sha256.c \
crypto/external/sha256.h \
crypto/external/sha512.c \
crypto/external/sha512.h \
crypto/external/zeroize.c \
crypto/external/zeroize.h \
crypto/sha1.cpp \
crypto/sha1.h \
crypto/sha256.cpp \
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile.test.include
Expand Up @@ -149,14 +149,14 @@ NAVCOIN_TESTS =\

test_test_navcoin_SOURCES = $(NAVCOIN_TESTS) $(JSON_TEST_FILES) $(RAW_TEST_FILES)
test_test_navcoin_CPPFLAGS = $(AM_CPPFLAGS) $(NAVCOIN_INCLUDES) -I$(builddir)/test/ $(TESTDEFS)
test_test_navcoin_LDADD = $(LIBNAVCOIN_SERVER) $(LIBNAVCOIN_CLI) $(LIBNAVCOIN_COMMON) $(LIBNAVCOIN_UTIL) $(LIBNAVCOIN_CONSENSUS) $(LIBNAVCOIN_CRYPTO) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) \
test_test_navcoin_LDADD = $(LIBNAVCOIN_SERVER) $(LIBNAVCOIN_CLI) $(LIBNAVCOIN_COMMON) $(LIBNAVCOIN_UTIL) $(LIBNAVCOIN_CONSENSUS) $(LIBUNIVALUE) $(LIBLEVELDB) $(LIBMEMENV) \
$(BOOST_LIBS) $(BOOST_UNIT_TEST_FRAMEWORK_LIB) $(LIBSECP256K1)
test_test_navcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
if ENABLE_WALLET
test_test_navcoin_LDADD += $(LIBNAVCOIN_WALLET)
endif

test_test_navcoin_LDADD += $(LIBNAVCOIN_CONSENSUS) $(BDB_LIBS) $(SSL_LIBS) $(UNBOUND_LIBS) $(CURL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBNAVCOIN_ZMQ) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS)
test_test_navcoin_LDADD += $(LIBNAVCOIN_CRYPTO) $(LIBNAVCOIN_CONSENSUS) $(BDB_LIBS) $(SSL_LIBS) $(UNBOUND_LIBS) $(CURL_LIBS) $(CRYPTO_LIBS) $(MINIUPNPC_LIBS) $(LIBNAVCOIN_ZMQ) $(EVENT_LIBS) $(EVENT_PTHREADS_LIBS)
test_test_navcoin_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) -static

if ENABLE_ZMQ
Expand Down
39 changes: 27 additions & 12 deletions src/compat/byteswap.h
@@ -1,34 +1,47 @@
// Copyright (c) 2014 The Bitcoin developers
// Copyright (c) 2014-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef NAVCOIN_COMPAT_BYTESWAP_H
#define NAVCOIN_COMPAT_BYTESWAP_H

#if defined(HAVE_CONFIG_H)
#include "config/navcoin-config.h"
#endif
#ifndef BITCOIN_COMPAT_BYTESWAP_H
#define BITCOIN_COMPAT_BYTESWAP_H

#include <stdint.h>

#if defined(HAVE_BYTESWAP_H)
#include <byteswap.h>
#endif

#if defined(MAC_OSX)

#if !defined(bswap_16)

// Mac OS X / Darwin features; we include a check for bswap_16 because if it is already defined, protobuf has
// defined these macros for us already; if it isn't, we do it ourselves. In either case, we get the exact same
// result regardless which path was taken
#include <libkern/OSByteOrder.h>
#define bswap_16(x) OSSwapInt16(x)
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)

#endif // !defined(bswap_16)

#else
// Non-Mac OS X / non-Darwin

#if HAVE_DECL_BSWAP_16 == 0
inline uint16_t bswap_16(uint16_t x)
{
return (x >> 8) | ((x & 0x00ff) << 8);
return (x >> 8) | (x << 8);
}
#endif // HAVE_DECL_BSWAP16
#endif // HAVE_DECL_BSWAP16 == 0

#if HAVE_DECL_BSWAP_32 == 0
inline uint32_t bswap_32(uint32_t x)
{
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
}
#endif // HAVE_DECL_BSWAP32
#endif // HAVE_DECL_BSWAP32 == 0

#if HAVE_DECL_BSWAP_64 == 0
inline uint64_t bswap_64(uint64_t x)
Expand All @@ -42,6 +55,8 @@ inline uint64_t bswap_64(uint64_t x)
| ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56));
}
#endif // HAVE_DECL_BSWAP64
#endif // HAVE_DECL_BSWAP64 == 0

#endif // defined(MAC_OSX)

#endif // NAVCOIN_COMPAT_BYTESWAP_H
#endif // BITCOIN_COMPAT_BYTESWAP_H
91 changes: 91 additions & 0 deletions src/crypto/external/hmac_sha256.c
@@ -0,0 +1,91 @@
/* libsodium: hmac_hmacsha256.c, v0.4.5 2014/04/16 */
/**
* Copyright 2005,2007,2009 Colin Percival. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "hmac_sha256.h"

#include <stdint.h>
#include <string.h>
#include "sha256.h"
#include "zeroize.h"

void HMACSHA256(const uint8_t* input, size_t length, const uint8_t* key,
size_t key_length, uint8_t digest[HMACSHA256_DIGEST_LENGTH])
{
HMACSHA256CTX context;
HMACSHA256Init(&context, key, key_length);
HMACSHA256Update(&context, input, length);
HMACSHA256Final(&context, digest);
}

void HMACSHA256Final(HMACSHA256CTX* context,
uint8_t digest[HMACSHA256_DIGEST_LENGTH])
{
uint8_t hash[HMACSHA256_DIGEST_LENGTH];

SHA256Final(&context->ictx, hash);
SHA256Update(&context->octx, hash, HMACSHA256_DIGEST_LENGTH);
SHA256Final(&context->octx, digest);

zeroize((void*)hash, sizeof hash);
}

void HMACSHA256Init(HMACSHA256CTX* context, const uint8_t* key,
size_t key_length)
{
size_t i;
uint8_t pad[SHA256_BLOCK_LENGTH];
uint8_t key_hash[SHA256_DIGEST_LENGTH];

if (key_length > SHA256_BLOCK_LENGTH)
{
SHA256Init(&context->ictx);
SHA256Update(&context->ictx, key, key_length);
SHA256Final(&context->ictx, key_hash);
key = key_hash;
key_length = SHA256_DIGEST_LENGTH;
}

SHA256Init(&context->ictx);
memset(pad, 0x36, SHA256_BLOCK_LENGTH);

for (i = 0; i < key_length; i++)
pad[i] ^= key[i];

SHA256Update(&context->ictx, pad, SHA256_BLOCK_LENGTH);
SHA256Init(&context->octx);
memset(pad, 0x5c, SHA256_BLOCK_LENGTH);

for (i = 0; i < key_length; i++)
pad[i] ^= key[i];

SHA256Update(&context->octx, pad, SHA256_BLOCK_LENGTH);
zeroize((void*)key_hash, sizeof key_hash);
}

void HMACSHA256Update(HMACSHA256CTX* context, const uint8_t* input,
size_t length)
{
SHA256Update(&context->ictx, input, length);
}
63 changes: 63 additions & 0 deletions src/crypto/external/hmac_sha256.h
@@ -0,0 +1,63 @@
/* libsodium: hmac_hmacsha512.c, v0.4.5 2014/04/16 */
/**
* Copyright 2005,2007,2009 Colin Percival. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef LIBBITCOIN_HMACSHA256_H
#define LIBBITCOIN_HMACSHA256_H

#include <stdint.h>
#include <stddef.h>
#include "sha256.h"

#define HMACSHA256_DIGEST_LENGTH 32U

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct HMACSHA256CTX
{
SHA256CTX ctx;
SHA256CTX ictx;
SHA256CTX octx;
} HMACSHA256CTX;

void HMACSHA256(const uint8_t* input, size_t length, const uint8_t* key,
size_t key_length, uint8_t digest[HMACSHA256_DIGEST_LENGTH]);

void HMACSHA256Final(HMACSHA256CTX* context,
uint8_t digest[HMACSHA256_DIGEST_LENGTH]);

void HMACSHA256Init(HMACSHA256CTX* context, const uint8_t* key,
size_t key_length);

void HMACSHA256Update(HMACSHA256CTX* context, const uint8_t* input,
size_t length);

#ifdef __cplusplus
}
#endif

#endif