bip39.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdbool.h>
  24. #include <string.h>
  25. #include "bip39.h"
  26. #include "hmac.h"
  27. #include "memzero.h"
  28. #include "options.h"
  29. #include "pbkdf2.h"
  30. #include "rand.h"
  31. #include "sha2.h"
  32. #if USE_BIP39_CACHE
  33. static int bip39_cache_index = 0;
  34. static CONFIDENTIAL struct {
  35. bool set;
  36. char mnemonic[256];
  37. char passphrase[64];
  38. uint8_t seed[512 / 8];
  39. } bip39_cache[BIP39_CACHE_SIZE];
  40. void bip39_cache_clear(void) {
  41. memzero(bip39_cache, sizeof(bip39_cache));
  42. bip39_cache_index = 0;
  43. }
  44. #endif
  45. const char *mnemonic_generate(int strength) {
  46. if (strength % 32 || strength < 128 || strength > 256) {
  47. return 0;
  48. }
  49. uint8_t data[32] = {0};
  50. random_buffer(data, 32);
  51. const char *r = mnemonic_from_data(data, strength / 8);
  52. memzero(data, sizeof(data));
  53. return r;
  54. }
  55. static CONFIDENTIAL char mnemo[24 * 10];
  56. const char *mnemonic_from_data(const uint8_t *data, int len) {
  57. if (len % 4 || len < 16 || len > 32) {
  58. return 0;
  59. }
  60. uint8_t bits[32 + 1] = {0};
  61. sha256_Raw(data, len, bits);
  62. // checksum
  63. bits[len] = bits[0];
  64. // data
  65. memcpy(bits, data, len);
  66. int mlen = len * 3 / 4;
  67. int i = 0, j = 0, idx = 0;
  68. char *p = mnemo;
  69. for (i = 0; i < mlen; i++) {
  70. idx = 0;
  71. for (j = 0; j < 11; j++) {
  72. idx <<= 1;
  73. idx += (bits[(i * 11 + j) / 8] & (1 << (7 - ((i * 11 + j) % 8)))) > 0;
  74. }
  75. strcpy(p, BIP39_WORDLIST_ENGLISH[idx]);
  76. p += strlen(BIP39_WORDLIST_ENGLISH[idx]);
  77. *p = (i < mlen - 1) ? ' ' : 0;
  78. p++;
  79. }
  80. memzero(bits, sizeof(bits));
  81. return mnemo;
  82. }
  83. void mnemonic_clear(void) { memzero(mnemo, sizeof(mnemo)); }
  84. int mnemonic_to_bits(const char *mnemonic, uint8_t *bits) {
  85. if (!mnemonic) {
  86. return 0;
  87. }
  88. uint32_t i = 0, n = 0;
  89. while (mnemonic[i]) {
  90. if (mnemonic[i] == ' ') {
  91. n++;
  92. }
  93. i++;
  94. }
  95. n++;
  96. // check that number of words is valid for BIP-39:
  97. // (a) between 128 and 256 bits of initial entropy (12 - 24 words)
  98. // (b) number of bits divisible by 33 (1 checksum bit per 32 input bits)
  99. // - that is, (n * 11) % 33 == 0, so n % 3 == 0
  100. if (n < 12 || n > 24 || (n % 3)) {
  101. return 0;
  102. }
  103. char current_word[10] = {0};
  104. uint32_t j = 0, ki = 0, bi = 0;
  105. uint8_t result[32 + 1] = {0};
  106. memzero(result, sizeof(result));
  107. i = 0;
  108. while (mnemonic[i]) {
  109. j = 0;
  110. while (mnemonic[i] != ' ' && mnemonic[i] != 0) {
  111. if (j >= sizeof(current_word) - 1) {
  112. return 0;
  113. }
  114. current_word[j] = mnemonic[i];
  115. i++;
  116. j++;
  117. }
  118. current_word[j] = 0;
  119. if (mnemonic[i] != 0) {
  120. i++;
  121. }
  122. int k = mnemonic_find_word(current_word);
  123. if (k < 0) { // word not found
  124. return 0;
  125. }
  126. for (ki = 0; ki < 11; ki++) {
  127. if (k & (1 << (10 - ki))) {
  128. result[bi / 8] |= 1 << (7 - (bi % 8));
  129. }
  130. bi++;
  131. }
  132. }
  133. if (bi != n * 11) {
  134. return 0;
  135. }
  136. memcpy(bits, result, sizeof(result));
  137. memzero(result, sizeof(result));
  138. // returns amount of entropy + checksum BITS
  139. return n * 11;
  140. }
  141. int mnemonic_check(const char *mnemonic) {
  142. uint8_t bits[32 + 1] = {0};
  143. int mnemonic_bits_len = mnemonic_to_bits(mnemonic, bits);
  144. if (mnemonic_bits_len != (12 * 11) && mnemonic_bits_len != (18 * 11) &&
  145. mnemonic_bits_len != (24 * 11)) {
  146. return 0;
  147. }
  148. int words = mnemonic_bits_len / 11;
  149. uint8_t checksum = bits[words * 4 / 3];
  150. sha256_Raw(bits, words * 4 / 3, bits);
  151. if (words == 12) {
  152. return (bits[0] & 0xF0) == (checksum & 0xF0); // compare first 4 bits
  153. } else if (words == 18) {
  154. return (bits[0] & 0xFC) == (checksum & 0xFC); // compare first 6 bits
  155. } else if (words == 24) {
  156. return bits[0] == checksum; // compare 8 bits
  157. }
  158. return 0;
  159. }
  160. // passphrase must be at most 256 characters otherwise it would be truncated
  161. void mnemonic_to_seed(const char *mnemonic, const char *passphrase,
  162. uint8_t seed[512 / 8],
  163. void (*progress_callback)(uint32_t current,
  164. uint32_t total)) {
  165. int mnemoniclen = strlen(mnemonic);
  166. int passphraselen = strlen(passphrase);
  167. if (passphraselen > 256) passphraselen = 256;
  168. #if USE_BIP39_CACHE
  169. // check cache
  170. if (mnemoniclen < 256 && passphraselen < 64) {
  171. for (int i = 0; i < BIP39_CACHE_SIZE; i++) {
  172. if (!bip39_cache[i].set) continue;
  173. if (strcmp(bip39_cache[i].mnemonic, mnemonic) != 0) continue;
  174. if (strcmp(bip39_cache[i].passphrase, passphrase) != 0) continue;
  175. // found the correct entry
  176. memcpy(seed, bip39_cache[i].seed, 512 / 8);
  177. return;
  178. }
  179. }
  180. #endif
  181. uint8_t salt[8 + 256] = {0};
  182. memcpy(salt, "mnemonic", 8);
  183. memcpy(salt + 8, passphrase, passphraselen);
  184. static CONFIDENTIAL PBKDF2_HMAC_SHA512_CTX pctx;
  185. pbkdf2_hmac_sha512_Init(&pctx, (const uint8_t *)mnemonic, mnemoniclen, salt,
  186. passphraselen + 8, 1);
  187. if (progress_callback) {
  188. progress_callback(0, BIP39_PBKDF2_ROUNDS);
  189. }
  190. for (int i = 0; i < 16; i++) {
  191. pbkdf2_hmac_sha512_Update(&pctx, BIP39_PBKDF2_ROUNDS / 16);
  192. if (progress_callback) {
  193. progress_callback((i + 1) * BIP39_PBKDF2_ROUNDS / 16,
  194. BIP39_PBKDF2_ROUNDS);
  195. }
  196. }
  197. pbkdf2_hmac_sha512_Final(&pctx, seed);
  198. memzero(salt, sizeof(salt));
  199. #if USE_BIP39_CACHE
  200. // store to cache
  201. if (mnemoniclen < 256 && passphraselen < 64) {
  202. bip39_cache[bip39_cache_index].set = true;
  203. strcpy(bip39_cache[bip39_cache_index].mnemonic, mnemonic);
  204. strcpy(bip39_cache[bip39_cache_index].passphrase, passphrase);
  205. memcpy(bip39_cache[bip39_cache_index].seed, seed, 512 / 8);
  206. bip39_cache_index = (bip39_cache_index + 1) % BIP39_CACHE_SIZE;
  207. }
  208. #endif
  209. }
  210. // binary search for finding the word in the wordlist
  211. int mnemonic_find_word(const char *word) {
  212. int lo = 0, hi = BIP39_WORD_COUNT - 1;
  213. while (lo <= hi) {
  214. int mid = lo + (hi - lo) / 2;
  215. int cmp = strcmp(word, BIP39_WORDLIST_ENGLISH[mid]);
  216. if (cmp == 0) {
  217. return mid;
  218. }
  219. if (cmp > 0) {
  220. lo = mid + 1;
  221. } else {
  222. hi = mid - 1;
  223. }
  224. }
  225. return -1;
  226. }
  227. const char *mnemonic_complete_word(const char *prefix, int len) {
  228. // we need to perform linear search,
  229. // because we want to return the first match
  230. for (int i = 0; i < BIP39_WORD_COUNT; i++) {
  231. if (strncmp(BIP39_WORDLIST_ENGLISH[i], prefix, len) == 0) {
  232. return BIP39_WORDLIST_ENGLISH[i];
  233. }
  234. }
  235. return NULL;
  236. }
  237. const char *mnemonic_get_word(int index) {
  238. if (index >= 0 && index < BIP39_WORD_COUNT) {
  239. return BIP39_WORDLIST_ENGLISH[index];
  240. } else {
  241. return NULL;
  242. }
  243. }
  244. uint32_t mnemonic_word_completion_mask(const char *prefix, int len) {
  245. if (len <= 0) {
  246. return 0x3ffffff; // all letters (bits 1-26 set)
  247. }
  248. uint32_t res = 0;
  249. for (int i = 0; i < BIP39_WORD_COUNT; i++) {
  250. const char *word = BIP39_WORDLIST_ENGLISH[i];
  251. if (strncmp(word, prefix, len) == 0 && word[len] >= 'a' &&
  252. word[len] <= 'z') {
  253. res |= 1 << (word[len] - 'a');
  254. }
  255. }
  256. return res;
  257. }