bip39.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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) {
  84. memzero(mnemo, sizeof(mnemo));
  85. }
  86. int mnemonic_to_bits(const char* mnemonic, uint8_t* bits) {
  87. if(!mnemonic) {
  88. return 0;
  89. }
  90. uint32_t i = 0, n = 0;
  91. while(mnemonic[i]) {
  92. if(mnemonic[i] == ' ') {
  93. n++;
  94. }
  95. i++;
  96. }
  97. n++;
  98. // check that number of words is valid for BIP-39:
  99. // (a) between 128 and 256 bits of initial entropy (12 - 24 words)
  100. // (b) number of bits divisible by 33 (1 checksum bit per 32 input bits)
  101. // - that is, (n * 11) % 33 == 0, so n % 3 == 0
  102. if(n < 12 || n > 24 || (n % 3)) {
  103. return 0;
  104. }
  105. char current_word[10] = {0};
  106. uint32_t j = 0, ki = 0, bi = 0;
  107. uint8_t result[32 + 1] = {0};
  108. memzero(result, sizeof(result));
  109. i = 0;
  110. while(mnemonic[i]) {
  111. j = 0;
  112. while(mnemonic[i] != ' ' && mnemonic[i] != 0) {
  113. if(j >= sizeof(current_word) - 1) {
  114. return 0;
  115. }
  116. current_word[j] = mnemonic[i];
  117. i++;
  118. j++;
  119. }
  120. current_word[j] = 0;
  121. if(mnemonic[i] != 0) {
  122. i++;
  123. }
  124. int k = mnemonic_find_word(current_word);
  125. if(k < 0) { // word not found
  126. return 0;
  127. }
  128. for(ki = 0; ki < 11; ki++) {
  129. if(k & (1 << (10 - ki))) {
  130. result[bi / 8] |= 1 << (7 - (bi % 8));
  131. }
  132. bi++;
  133. }
  134. }
  135. if(bi != n * 11) {
  136. return 0;
  137. }
  138. memcpy(bits, result, sizeof(result));
  139. memzero(result, sizeof(result));
  140. // returns amount of entropy + checksum BITS
  141. return n * 11;
  142. }
  143. int mnemonic_check(const char* mnemonic) {
  144. uint8_t bits[32 + 1] = {0};
  145. int mnemonic_bits_len = mnemonic_to_bits(mnemonic, bits);
  146. if(mnemonic_bits_len != (12 * 11) && mnemonic_bits_len != (18 * 11) &&
  147. mnemonic_bits_len != (24 * 11)) {
  148. return 0;
  149. }
  150. int words = mnemonic_bits_len / 11;
  151. uint8_t checksum = bits[words * 4 / 3];
  152. sha256_Raw(bits, words * 4 / 3, bits);
  153. if(words == 12) {
  154. return (bits[0] & 0xF0) == (checksum & 0xF0); // compare first 4 bits
  155. } else if(words == 18) {
  156. return (bits[0] & 0xFC) == (checksum & 0xFC); // compare first 6 bits
  157. } else if(words == 24) {
  158. return bits[0] == checksum; // compare 8 bits
  159. }
  160. return 0;
  161. }
  162. // passphrase must be at most 256 characters otherwise it would be truncated
  163. void mnemonic_to_seed(
  164. const char* mnemonic,
  165. const char* passphrase,
  166. uint8_t seed[512 / 8],
  167. void (*progress_callback)(uint32_t current, uint32_t total)) {
  168. int mnemoniclen = strlen(mnemonic);
  169. int passphraselen = strlen(passphrase);
  170. if(passphraselen > 256) passphraselen = 256;
  171. #if USE_BIP39_CACHE
  172. // check cache
  173. if(mnemoniclen < 256 && passphraselen < 64) {
  174. for(int i = 0; i < BIP39_CACHE_SIZE; i++) {
  175. if(!bip39_cache[i].set) continue;
  176. if(strcmp(bip39_cache[i].mnemonic, mnemonic) != 0) continue;
  177. if(strcmp(bip39_cache[i].passphrase, passphrase) != 0) continue;
  178. // found the correct entry
  179. memcpy(seed, bip39_cache[i].seed, 512 / 8);
  180. return;
  181. }
  182. }
  183. #endif
  184. uint8_t salt[8 + 256] = {0};
  185. memcpy(salt, "mnemonic", 8);
  186. memcpy(salt + 8, passphrase, passphraselen);
  187. static CONFIDENTIAL PBKDF2_HMAC_SHA512_CTX pctx;
  188. pbkdf2_hmac_sha512_Init(
  189. &pctx, (const uint8_t*)mnemonic, mnemoniclen, salt, passphraselen + 8, 1);
  190. if(progress_callback) {
  191. progress_callback(0, BIP39_PBKDF2_ROUNDS);
  192. }
  193. for(int i = 0; i < 16; i++) {
  194. pbkdf2_hmac_sha512_Update(&pctx, BIP39_PBKDF2_ROUNDS / 16);
  195. if(progress_callback) {
  196. progress_callback((i + 1) * BIP39_PBKDF2_ROUNDS / 16, BIP39_PBKDF2_ROUNDS);
  197. }
  198. }
  199. pbkdf2_hmac_sha512_Final(&pctx, seed);
  200. memzero(salt, sizeof(salt));
  201. #if USE_BIP39_CACHE
  202. // store to cache
  203. if(mnemoniclen < 256 && passphraselen < 64) {
  204. bip39_cache[bip39_cache_index].set = true;
  205. strcpy(bip39_cache[bip39_cache_index].mnemonic, mnemonic);
  206. strcpy(bip39_cache[bip39_cache_index].passphrase, passphrase);
  207. memcpy(bip39_cache[bip39_cache_index].seed, seed, 512 / 8);
  208. bip39_cache_index = (bip39_cache_index + 1) % BIP39_CACHE_SIZE;
  209. }
  210. #endif
  211. }
  212. // binary search for finding the word in the wordlist
  213. int mnemonic_find_word(const char* word) {
  214. int lo = 0, hi = BIP39_WORD_COUNT - 1;
  215. while(lo <= hi) {
  216. int mid = lo + (hi - lo) / 2;
  217. int cmp = strcmp(word, BIP39_WORDLIST_ENGLISH[mid]);
  218. if(cmp == 0) {
  219. return mid;
  220. }
  221. if(cmp > 0) {
  222. lo = mid + 1;
  223. } else {
  224. hi = mid - 1;
  225. }
  226. }
  227. return -1;
  228. }
  229. const char* mnemonic_complete_word(const char* prefix, int len) {
  230. // we need to perform linear search,
  231. // because we want to return the first match
  232. for(int i = 0; i < BIP39_WORD_COUNT; i++) {
  233. if(strncmp(BIP39_WORDLIST_ENGLISH[i], prefix, len) == 0) {
  234. return BIP39_WORDLIST_ENGLISH[i];
  235. }
  236. }
  237. return NULL;
  238. }
  239. const char* mnemonic_get_word(int index) {
  240. if(index >= 0 && index < BIP39_WORD_COUNT) {
  241. return BIP39_WORDLIST_ENGLISH[index];
  242. } else {
  243. return NULL;
  244. }
  245. }
  246. uint32_t mnemonic_word_completion_mask(const char* prefix, int len) {
  247. if(len <= 0) {
  248. return 0x3ffffff; // all letters (bits 1-26 set)
  249. }
  250. uint32_t res = 0;
  251. for(int i = 0; i < BIP39_WORD_COUNT; i++) {
  252. const char* word = BIP39_WORDLIST_ENGLISH[i];
  253. if(strncmp(word, prefix, len) == 0 && word[len] >= 'a' && word[len] <= 'z') {
  254. res |= 1 << (word[len] - 'a');
  255. }
  256. }
  257. return res;
  258. }