2013-11-08 00:24:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2014-01-10 19:22:33 +00:00
|
|
|
#define PBKDF2_ROUNDS 2048
|
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-03-12 19:45:51 +00:00
|
|
|
static uint8_t data[32];
|
|
|
|
random_buffer(data, 32);
|
|
|
|
return mnemonic_from_data(data, strength / 8);
|
2013-11-08 00:24:47 +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];
|
|
|
|
memcpy(bits, data, len);
|
2013-11-08 00:24:47 +00:00
|
|
|
|
2014-03-12 19:45:51 +00:00
|
|
|
sha256_Raw(data, len, bits);
|
|
|
|
bits[len] = bits[0];
|
|
|
|
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-11 19:09:15 +00:00
|
|
|
static char mnemo[24 * 10];
|
2013-11-25 21:46:54 +00:00
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mnemo;
|
|
|
|
}
|
|
|
|
|
2014-03-11 19:09:15 +00:00
|
|
|
int mnemonic_check(const char *mnemonic)
|
|
|
|
{
|
2014-03-12 19:45:51 +00:00
|
|
|
if (!mnemonic) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i, n;
|
|
|
|
|
|
|
|
i = 0; n = 0;
|
|
|
|
while (mnemonic[i]) {
|
|
|
|
if (mnemonic[i] == ' ') {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
// check number of words
|
|
|
|
if (n != 12 && n != 18 && n != 24) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char current_word[10];
|
|
|
|
uint32_t j, k, ki, bi;
|
|
|
|
uint8_t bits[32 + 1];
|
|
|
|
memset(bits, 0, sizeof(bits));
|
|
|
|
i = 0; bi = 0;
|
|
|
|
while (mnemonic[i]) {
|
|
|
|
j = 0;
|
|
|
|
while (mnemonic[i] != ' ' && mnemonic[i] != 0) {
|
|
|
|
if (j >= sizeof(current_word)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
current_word[j] = mnemonic[i];
|
|
|
|
i++; j++;
|
|
|
|
}
|
|
|
|
current_word[j] = 0;
|
|
|
|
if (mnemonic[i] != 0) i++;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
bits[32] = bits[n * 4 / 3];
|
|
|
|
sha256_Raw(bits, n * 4 / 3, bits);
|
|
|
|
if (n == 12) {
|
|
|
|
return (bits[0] & 0xF0) == (bits[32] & 0xF0); // compare first 4 bits
|
|
|
|
} else
|
|
|
|
if (n == 18) {
|
|
|
|
return (bits[0] & 0xFC) == (bits[32] & 0xFC); // compare first 6 bits
|
|
|
|
} else
|
|
|
|
if (n == 24) {
|
|
|
|
return bits[0] == bits[32]; // compare 8 bits
|
|
|
|
}
|
|
|
|
return 0;
|
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
|
|
|
{
|
2013-12-09 15:21:42 +00:00
|
|
|
static uint8_t salt[8 + 256 + 4];
|
|
|
|
int saltlen = strlen(passphrase);
|
|
|
|
memcpy(salt, "mnemonic", 8);
|
|
|
|
memcpy(salt + 8, passphrase, saltlen);
|
|
|
|
saltlen += 8;
|
2014-03-11 19:09:15 +00:00
|
|
|
pbkdf2((const uint8_t *)mnemonic, strlen(mnemonic), salt, saltlen, PBKDF2_ROUNDS, seed, 512 / 8, progress_callback);
|
2013-11-08 00:24:47 +00:00
|
|
|
}
|
2014-03-07 19:11:50 +00:00
|
|
|
|
|
|
|
const char **mnemonic_wordlist(void)
|
|
|
|
{
|
|
|
|
return wordlist;
|
|
|
|
}
|