2014-05-22 18:54:58 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-11-08 00:24:47 +00:00
|
|
|
#include <string.h>
|
2016-01-19 14:11:57 +00:00
|
|
|
#include <stdbool.h>
|
2013-11-08 00:24:47 +00:00
|
|
|
|
|
|
|
#include "bip39.h"
|
2013-11-25 21:46:54 +00:00
|
|
|
#include "hmac.h"
|
|
|
|
#include "rand.h"
|
2013-11-08 00:24:47 +00:00
|
|
|
#include "sha2.h"
|
2013-12-09 15:21:42 +00:00
|
|
|
#include "pbkdf2.h"
|
2013-11-08 00:24:47 +00:00
|
|
|
#include "bip39_english.h"
|
2016-01-19 14:11:57 +00:00
|
|
|
#include "options.h"
|
2018-01-18 14:18:09 +00:00
|
|
|
#include "memzero.h"
|
2016-01-19 14:11:57 +00:00
|
|
|
|
|
|
|
#if USE_BIP39_CACHE
|
|
|
|
|
|
|
|
static int bip39_cache_index = 0;
|
|
|
|
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL struct {
|
2016-01-19 14:11:57 +00:00
|
|
|
bool set;
|
|
|
|
char mnemonic[256];
|
|
|
|
char passphrase[64];
|
|
|
|
uint8_t seed[512 / 8];
|
|
|
|
} bip39_cache[BIP39_CACHE_SIZE];
|
|
|
|
|
|
|
|
#endif
|
2013-11-08 00:24:47 +00:00
|
|
|
|
2013-11-25 21:46:54 +00:00
|
|
|
const char *mnemonic_generate(int strength)
|
2013-11-08 00:24:47 +00:00
|
|
|
{
|
2013-11-25 21:46:54 +00:00
|
|
|
if (strength % 32 || strength < 128 || strength > 256) {
|
|
|
|
return 0;
|
2013-11-08 00:24:47 +00:00
|
|
|
}
|
2014-06-09 11:51:31 +00:00
|
|
|
uint8_t data[32];
|
2014-03-12 19:45:51 +00:00
|
|
|
random_buffer(data, 32);
|
2017-08-10 14:31:21 +00:00
|
|
|
const char *r = mnemonic_from_data(data, strength / 8);
|
2018-01-18 14:18:09 +00:00
|
|
|
memzero(data, sizeof(data));
|
2017-08-10 14:31:21 +00:00
|
|
|
return r;
|
2013-11-08 00:24:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:35:30 +00:00
|
|
|
static CONFIDENTIAL char mnemo[24 * 10];
|
2016-05-13 17:45:43 +00:00
|
|
|
|
2013-11-25 21:46:54 +00:00
|
|
|
const char *mnemonic_from_data(const uint8_t *data, int len)
|
2013-11-08 00:24:47 +00:00
|
|
|
{
|
2013-11-26 00:29:06 +00:00
|
|
|
if (len % 4 || len < 16 || len > 32) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-12 19:45:51 +00:00
|
|
|
uint8_t bits[32 + 1];
|
2013-11-08 00:24:47 +00:00
|
|
|
|
2014-03-12 19:45:51 +00:00
|
|
|
sha256_Raw(data, len, bits);
|
2014-06-09 11:51:31 +00:00
|
|
|
// checksum
|
2014-03-12 19:45:51 +00:00
|
|
|
bits[len] = bits[0];
|
2014-06-09 11:51:31 +00:00
|
|
|
// data
|
2014-03-12 19:45:51 +00:00
|
|
|
memcpy(bits, data, len);
|
2013-11-08 00:24:47 +00:00
|
|
|
|
2013-11-25 21:46:54 +00:00
|
|
|
int mlen = len * 3 / 4;
|
|
|
|
|
2014-03-12 19:45:51 +00:00
|
|
|
int i, j, idx;
|
2013-11-08 00:24:47 +00:00
|
|
|
char *p = mnemo;
|
2013-11-25 21:46:54 +00:00
|
|
|
for (i = 0; i < mlen; i++) {
|
2014-03-12 19:45:51 +00:00
|
|
|
idx = 0;
|
2013-11-08 00:24:47 +00:00
|
|
|
for (j = 0; j < 11; j++) {
|
2014-03-12 19:45:51 +00:00
|
|
|
idx <<= 1;
|
|
|
|
idx += (bits[(i * 11 + j) / 8] & (1 << (7 - ((i * 11 + j) % 8)))) > 0;
|
2013-11-08 00:24:47 +00:00
|
|
|
}
|
|
|
|
strcpy(p, wordlist[idx]);
|
|
|
|
p += strlen(wordlist[idx]);
|
2013-11-25 21:46:54 +00:00
|
|
|
*p = (i < mlen - 1) ? ' ' : 0;
|
2013-11-08 00:24:47 +00:00
|
|
|
p++;
|
|
|
|
}
|
2018-01-18 14:18:09 +00:00
|
|
|
memzero(bits, sizeof(bits));
|
2013-11-08 00:24:47 +00:00
|
|
|
|
|
|
|
return mnemo;
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:35:30 +00:00
|
|
|
void mnemonic_clear(void)
|
2016-05-13 17:45:43 +00:00
|
|
|
{
|
2019-01-23 17:35:30 +00:00
|
|
|
memzero(mnemo, sizeof(mnemo));
|
2016-05-13 17:45:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 18:02:53 +00:00
|
|
|
int mnemonic_to_entropy(const char *mnemonic, uint8_t *entropy)
|
|
|
|
{
|
2018-06-19 09:50:59 +00:00
|
|
|
if (!mnemonic) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-10 18:02:53 +00:00
|
|
|
uint32_t i = 0, n = 0;
|
2018-06-19 09:50:59 +00:00
|
|
|
|
|
|
|
while (mnemonic[i]) {
|
|
|
|
if (mnemonic[i] == ' ') {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
n++;
|
2018-07-10 18:02:53 +00:00
|
|
|
|
2018-06-19 09:50:59 +00:00
|
|
|
// check number of words
|
|
|
|
if (n != 12 && n != 18 && n != 24) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char current_word[10];
|
2018-07-10 18:02:53 +00:00
|
|
|
uint32_t j, k, ki, bi = 0;
|
2018-06-19 09:50:59 +00:00
|
|
|
uint8_t bits[32 + 1];
|
|
|
|
|
|
|
|
memzero(bits, sizeof(bits));
|
2018-07-10 18:02:53 +00:00
|
|
|
i = 0;
|
2018-06-19 09:50:59 +00:00
|
|
|
while (mnemonic[i]) {
|
|
|
|
j = 0;
|
|
|
|
while (mnemonic[i] != ' ' && mnemonic[i] != 0) {
|
|
|
|
if (j >= sizeof(current_word) - 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
current_word[j] = mnemonic[i];
|
|
|
|
i++; j++;
|
|
|
|
}
|
|
|
|
current_word[j] = 0;
|
2018-07-10 18:02:53 +00:00
|
|
|
if (mnemonic[i] != 0) {
|
|
|
|
i++;
|
|
|
|
}
|
2018-06-19 09:50:59 +00:00
|
|
|
k = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (!wordlist[k]) { // word not found
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (strcmp(current_word, wordlist[k]) == 0) { // word found on index k
|
|
|
|
for (ki = 0; ki < 11; ki++) {
|
|
|
|
if (k & (1 << (10 - ki))) {
|
|
|
|
bits[bi / 8] |= 1 << (7 - (bi % 8));
|
|
|
|
}
|
|
|
|
bi++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bi != n * 11) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-10 18:02:53 +00:00
|
|
|
memcpy(entropy, bits, sizeof(bits));
|
|
|
|
return n * 11;
|
2018-06-19 09:50:59 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 19:09:15 +00:00
|
|
|
int mnemonic_check(const char *mnemonic)
|
|
|
|
{
|
2014-03-12 19:45:51 +00:00
|
|
|
uint8_t bits[32 + 1];
|
2018-07-10 18:02:53 +00:00
|
|
|
int seed_len = mnemonic_to_entropy(mnemonic, bits);
|
|
|
|
if (seed_len != (12 * 11) && seed_len != (18 * 11) && seed_len != (24 * 11)) {
|
2014-03-12 19:45:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-10 18:02:53 +00:00
|
|
|
int words = seed_len / 11;
|
|
|
|
|
|
|
|
uint8_t checksum = bits[words * 4 / 3];
|
|
|
|
sha256_Raw(bits, words * 4 / 3, bits);
|
|
|
|
if (words == 12) {
|
|
|
|
return (bits[0] & 0xF0) == (checksum & 0xF0); // compare first 4 bits
|
|
|
|
} else if (words == 18) {
|
|
|
|
return (bits[0] & 0xFC) == (checksum & 0xFC); // compare first 6 bits
|
|
|
|
} else if (words == 24) {
|
|
|
|
return bits[0] == checksum; // compare 8 bits
|
2014-03-12 19:45:51 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2014-03-11 19:09:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 19:04:57 +00:00
|
|
|
// passphrase must be at most 256 characters otherwise it would be truncated
|
2014-03-11 19:09:15 +00:00
|
|
|
void mnemonic_to_seed(const char *mnemonic, const char *passphrase, uint8_t seed[512 / 8], void (*progress_callback)(uint32_t current, uint32_t total))
|
2013-11-08 00:24:47 +00:00
|
|
|
{
|
2016-01-19 14:11:57 +00:00
|
|
|
int mnemoniclen = strlen(mnemonic);
|
2019-01-23 19:04:57 +00:00
|
|
|
int passphraselen = strnlen(passphrase, 256);
|
|
|
|
#if USE_BIP39_CACHE
|
2016-01-19 14:11:57 +00:00
|
|
|
// check cache
|
|
|
|
if (mnemoniclen < 256 && passphraselen < 64) {
|
2016-04-30 22:50:12 +00:00
|
|
|
for (int i = 0; i < BIP39_CACHE_SIZE; i++) {
|
2016-01-19 14:11:57 +00:00
|
|
|
if (!bip39_cache[i].set) continue;
|
|
|
|
if (strcmp(bip39_cache[i].mnemonic, mnemonic) != 0) continue;
|
|
|
|
if (strcmp(bip39_cache[i].passphrase, passphrase) != 0) continue;
|
|
|
|
// found the correct entry
|
|
|
|
memcpy(seed, bip39_cache[i].seed, 512 / 8);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-30 22:50:12 +00:00
|
|
|
uint8_t salt[8 + 256];
|
2013-12-09 15:21:42 +00:00
|
|
|
memcpy(salt, "mnemonic", 8);
|
2016-01-19 14:11:57 +00:00
|
|
|
memcpy(salt + 8, passphrase, passphraselen);
|
2017-08-10 14:31:21 +00:00
|
|
|
static CONFIDENTIAL PBKDF2_HMAC_SHA512_CTX pctx;
|
2019-01-23 19:04:57 +00:00
|
|
|
pbkdf2_hmac_sha512_Init(&pctx, (const uint8_t *)mnemonic, mnemoniclen, salt, passphraselen + 8, 1);
|
2016-04-30 22:50:12 +00:00
|
|
|
if (progress_callback) {
|
|
|
|
progress_callback(0, BIP39_PBKDF2_ROUNDS);
|
|
|
|
}
|
2016-07-12 13:06:24 +00:00
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
pbkdf2_hmac_sha512_Update(&pctx, BIP39_PBKDF2_ROUNDS / 16);
|
2016-04-30 22:50:12 +00:00
|
|
|
if (progress_callback) {
|
2016-07-12 13:06:24 +00:00
|
|
|
progress_callback((i + 1) * BIP39_PBKDF2_ROUNDS / 16, BIP39_PBKDF2_ROUNDS);
|
2016-04-30 22:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pbkdf2_hmac_sha512_Final(&pctx, seed);
|
2018-01-18 14:18:09 +00:00
|
|
|
memzero(salt, sizeof(salt));
|
2016-01-19 14:11:57 +00:00
|
|
|
#if USE_BIP39_CACHE
|
|
|
|
// store to cache
|
|
|
|
if (mnemoniclen < 256 && passphraselen < 64) {
|
|
|
|
bip39_cache[bip39_cache_index].set = true;
|
|
|
|
strcpy(bip39_cache[bip39_cache_index].mnemonic, mnemonic);
|
|
|
|
strcpy(bip39_cache[bip39_cache_index].passphrase, passphrase);
|
|
|
|
memcpy(bip39_cache[bip39_cache_index].seed, seed, 512 / 8);
|
|
|
|
bip39_cache_index = (bip39_cache_index + 1) % BIP39_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-08 00:24:47 +00:00
|
|
|
}
|
2014-03-07 19:11:50 +00:00
|
|
|
|
2015-02-14 11:00:44 +00:00
|
|
|
const char * const *mnemonic_wordlist(void)
|
2014-03-07 19:11:50 +00:00
|
|
|
{
|
|
|
|
return wordlist;
|
|
|
|
}
|