Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ set(COMMON_SOURCES
./src/amount.cpp
./src/base58.cpp
./src/bip38.cpp
./src/bip39.cpp
./src/chainparams.cpp
./src/coins.cpp
./src/compressor.cpp
Expand All @@ -459,6 +460,7 @@ set(COMMON_SOURCES
./src/core_read.cpp
./src/core_write.cpp
./src/hash.cpp
./src/hdchain.cpp
./src/invalid.cpp
./src/key.cpp
./src/keystore.cpp
Expand Down
5 changes: 5 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ BITCOIN_CORE_H = \
allocators.h \
amount.h \
base58.h \
bip39.h \
bip39_english.h \
bip38.h \
bloom.h \
blocksignature.h \
Expand Down Expand Up @@ -111,6 +113,7 @@ BITCOIN_CORE_H = \
obfuscation-relay.h \
wallet/db.h \
hash.h \
hdchain.h \
httprpc.h \
httpserver.h \
init.h \
Expand Down Expand Up @@ -363,6 +366,7 @@ libbitcoin_common_a_SOURCES = \
allocators.cpp \
amount.cpp \
base58.cpp \
bip39.cpp \
bip38.cpp \
chainparams.cpp \
coins.cpp \
Expand All @@ -375,6 +379,7 @@ libbitcoin_common_a_SOURCES = \
core_read.cpp \
core_write.cpp \
hash.cpp \
hdchain.cpp \
invalid.cpp \
key.cpp \
keystore.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ JSON_TEST_FILES = \
test/data/sig_noncanonical.json \
test/data/base58_encode_decode.json \
test/data/base58_keys_invalid.json \
test/data/bip39_vectors.json \
test/data/script_invalid.json \
test/data/tx_invalid.json \
test/data/tx_valid.json \
Expand All @@ -39,6 +40,7 @@ BITCOIN_TESTS =\
test/base32_tests.cpp \
test/base58_tests.cpp \
test/base64_tests.cpp \
test/bip39_tests.cpp \
test/budget_tests.cpp \
test/checkblock_tests.cpp \
test/Checkpoints_tests.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ struct zero_after_free_allocator : public std::allocator<T> {

// This is exactly like std::string, but with a custom allocator.
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;

typedef std::vector<unsigned char, secure_allocator<unsigned char> > SecureVector;
// Byte-vector that clears its contents before deletion.
typedef std::vector<char, zero_after_free_allocator<char> > CSerializeData;

Expand Down
162 changes: 162 additions & 0 deletions src/bip39.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* Copyright (c) 2013-2014 Tomas Dzetkulic
* Copyright (c) 2013-2014 Pavol Rusnak
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

// Source:
// https://github.com/trezor/trezor-crypto
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This files was deprecated (the whole trezor repository was) and there is an open discussion about PBKDF2 weakness.

Thoughts?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless im wrong,i dont see the whole repo being depricated,more so they moved to a new repo.Since i didnt see deprication mentioned anywhere on their repo.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked their new repo,it uses mostly the same code in bip39 if im not mistaken,but in c.

Copy link
Copy Markdown

@furszy furszy Jan 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the repository was moved to other place, not updated anymore, and the code was ported to another language plus some changes were done. Then the code is deprecated. The "mostly the same code" in this key creation code is not enough.

If we are going to back port code from other place, we need to ensure that it's the latest one to not end up having problems that were already solved in other place.

Sorry if I sound hard but this piece of code is in charge of the master key creation, we must go secure in this area.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
These were the changes,What they (trezor) have done is move those files to a new repo and refactored bip39 code,other than that i dont see any changes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is important is what it's inside of those commits and where they moved from cpp to c code (what changes they did there and how that changes this code -- if it changes it or not.)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that there is no previous history. Instead of refactor the file name, they created this .c as a new one.


#include "bip39.h"
#include "bip39_english.h"
#include "crypto/sha256.h"
#include "random.h"

#include <openssl/evp.h>

SecureString CMnemonic::Generate(int strength)
{
if (strength % 32 || strength < 128 || strength > 256) {
return SecureString();
}
SecureVector data(32);
GetStrongRandBytes(&data[0], 32);
SecureString mnemonic = FromData(data, strength / 8);
return mnemonic;
}

// SecureString CMnemonic::FromData(const uint8_t *data, int len)
SecureString CMnemonic::FromData(const SecureVector& data, int len)
{
if (len % 4 || len < 16 || len > 32) {
return SecureString();
}

SecureVector checksum(32);
CSHA256().Write(&data[0], len).Finalize(&checksum[0]);

// data
SecureVector bits(len);
memcpy(&bits[0], &data[0], len);
// checksum
bits.push_back(checksum[0]);

int mlen = len * 3 / 4;
SecureString mnemonic;

int i, j, idx;
for (i = 0; i < mlen; i++) {
idx = 0;
for (j = 0; j < 11; j++) {
idx <<= 1;
idx += (bits[(i * 11 + j) / 8] & (1 << (7 - ((i * 11 + j) % 8)))) > 0;
}
mnemonic.append(wordlist[idx]);
if (i < mlen - 1) {
mnemonic += ' ';
}
}

return mnemonic;
}

bool CMnemonic::Check(SecureString mnemonic)
{
if (mnemonic.empty()) {
return false;
}

uint32_t nWordCount{};

for (size_t i = 0; i < mnemonic.size(); ++i) {
if (mnemonic[i] == ' ') {
nWordCount++;
}
}
nWordCount++;
// check number of words
if (nWordCount != 12 && nWordCount != 18 && nWordCount != 24) {
return false;
}

SecureString ssCurrentWord;
SecureVector bits(32 + 1);

uint32_t nWordIndex, ki, nBitsCount{};

for (size_t i = 0; i < mnemonic.size(); ++i)
{
ssCurrentWord = "";
while (i + ssCurrentWord.size() < mnemonic.size() && mnemonic[i + ssCurrentWord.size()] != ' ') {
if (ssCurrentWord.size() >= 9) {
return false;
}
ssCurrentWord += mnemonic[i + ssCurrentWord.size()];
}
i += ssCurrentWord.size();
nWordIndex = 0;
for (;;) {
if (!wordlist[nWordIndex]) { // word not found
return false;
}
if (ssCurrentWord == wordlist[nWordIndex]) { // word found on index nWordIndex
for (ki = 0; ki < 11; ki++) {
if (nWordIndex & (1 << (10 - ki))) {
bits[nBitsCount / 8] |= 1 << (7 - (nBitsCount % 8));
}
nBitsCount++;
}
break;
}
nWordIndex++;
}
}
if (nBitsCount != nWordCount * 11) {
return false;
}
bits[32] = bits[nWordCount * 4 / 3];
CSHA256().Write(&bits[0], nWordCount * 4 / 3).Finalize(&bits[0]);

bool fResult = 0;
if (nWordCount == 12) {
fResult = (bits[0] & 0xF0) == (bits[32] & 0xF0); // compare first 4 bits
} else
if (nWordCount == 18) {
fResult = (bits[0] & 0xFC) == (bits[32] & 0xFC); // compare first 6 bits
} else
if (nWordCount == 24) {
fResult = bits[0] == bits[32]; // compare 8 bits
}

return fResult;
}

// passphrase must be at most 256 characters or code may crash
void CMnemonic::ToSeed(SecureString mnemonic, SecureString passphrase, SecureVector& seedRet)
{
SecureString ssSalt = SecureString("mnemonic") + passphrase;
SecureVector vchSalt(ssSalt.begin(), ssSalt.end());
seedRet.resize(64);
// int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
// const unsigned char *salt, int saltlen, int iter,
// const EVP_MD *digest,
// int keylen, unsigned char *out);
PKCS5_PBKDF2_HMAC(mnemonic.c_str(), mnemonic.size(), &vchSalt[0], vchSalt.size(), 2048, EVP_sha512(), 64, &seedRet[0]);
}
39 changes: 39 additions & 0 deletions src/bip39.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2013-2014 Tomas Dzetkulic
* Copyright (c) 2013-2014 Pavol Rusnak
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef PIVX_BIP39_H
#define PIVX_BIP39_H

#include "allocators.h"

class CMnemonic
{
public:
static SecureString Generate(int strength); // strength in bits
static SecureString FromData(const SecureVector& data, int len);
static bool Check(SecureString mnemonic);
// passphrase must be at most 256 characters or code may crash
static void ToSeed(SecureString mnemonic, SecureString passphrase, SecureVector& seedRet);
};

#endif
Loading