| 597 | } |
| 598 | |
| 599 | static int check_hsm(const char *hsm_secret_path) |
| 600 | { |
| 601 | char mnemonic[BIP39_WORDLIST_LEN]; |
| 602 | struct secret hsm_secret; |
| 603 | u8 bip32_seed[BIP39_SEED_LEN_512]; |
| 604 | size_t bip32_seed_len; |
| 605 | int exit_code; |
| 606 | char *passphrase, *err; |
| 607 | |
| 608 | /* This checks the file existence, too. */ |
| 609 | if (hsm_secret_is_encrypted(hsm_secret_path)) { |
| 610 | char *passwd; |
| 611 | |
| 612 | printf("Enter hsm_secret password:\n"); |
| 613 | fflush(stdout); |
| 614 | passwd = read_stdin_pass_with_exit_code(&err, &exit_code); |
| 615 | if (!passwd) |
| 616 | errx(exit_code, "%s", err); |
| 617 | |
| 618 | if (sodium_init() == -1) |
| 619 | errx(ERROR_LIBSODIUM, |
| 620 | "Could not initialize libsodium. Not enough entropy ?"); |
| 621 | |
| 622 | get_encrypted_hsm_secret(&hsm_secret, hsm_secret_path, passwd); |
| 623 | /* Once the encryption key derived, we don't need it anymore. */ |
| 624 | free(passwd); |
| 625 | } else |
| 626 | get_hsm_secret(&hsm_secret, hsm_secret_path); |
| 627 | |
| 628 | printf("Warning: remember that different passphrases yield different " |
| 629 | "bitcoin wallets.\n"); |
| 630 | printf("If left empty, no password is used (echo is disabled).\n"); |
| 631 | printf("Enter your passphrase: \n"); |
| 632 | fflush(stdout); |
| 633 | passphrase = read_stdin_pass_with_exit_code(&err, &exit_code); |
| 634 | if (!passphrase) |
| 635 | errx(exit_code, "%s", err); |
| 636 | if (strlen(passphrase) == 0) { |
| 637 | free(passphrase); |
| 638 | passphrase = NULL; |
| 639 | } |
| 640 | |
| 641 | read_mnemonic(mnemonic); |
| 642 | if (bip39_mnemonic_to_seed(mnemonic, passphrase, bip32_seed, sizeof(bip32_seed), &bip32_seed_len) != WALLY_OK) |
| 643 | errx(ERROR_LIBWALLY, "Unable to derive BIP32 seed from BIP39 mnemonic"); |
| 644 | |
| 645 | /* We only use first 32 bytes */ |
| 646 | if (memcmp(bip32_seed, hsm_secret.data, sizeof(hsm_secret.data)) != 0) |
| 647 | errx(ERROR_KEYDERIV, "resulting hsm_secret did not match"); |
| 648 | |
| 649 | printf("OK\n"); |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | int main(int argc, char *argv[]) |
| 654 | { |
no test coverage detected