mmgen-passchg 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013-2014 by philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen-passchg: Change a mmgen deterministic wallet's passphrase, label or
  20. hash preset
  21. """
  22. import sys
  23. from mmgen.Opts import *
  24. from mmgen.util import *
  25. from mmgen.crypto import *
  26. import mmgen.config as g
  27. help_data = {
  28. 'prog_name': g.prog_name,
  29. 'desc': """Change the passphrase, hash preset or label of an {}
  30. deterministic wallet""".format(g.proj_name),
  31. 'usage': "[opts] [filename]",
  32. 'options': """
  33. -h, --help Print this help message
  34. -d, --outdir= d Specify an alternate directory 'd' for output
  35. -H, --show-hash-presets Show information on available hash presets
  36. -k, --keep-old-passphrase Keep old passphrase (use when changing hash
  37. strength or label only)
  38. -L, --label= l Change the wallet's label to 'l'
  39. -p, --hash-preset= p Change scrypt.hash() parameters to preset 'p'
  40. (default: '{g.hash_preset}')
  41. -P, --passwd-file= f Get new passphrase from file 'f'
  42. -r, --usr-randchars= n Get 'n' characters of additional randomness from
  43. user (min={g.min_urandchars}, max={g.max_urandchars})
  44. -q, --quiet Suppress warnings; overwrite files without
  45. prompting
  46. -v, --verbose Produce more verbose output
  47. """.format(g=g),
  48. 'notes': """
  49. NOTE: The key ID will change if either the passphrase or hash preset are
  50. changed
  51. """
  52. }
  53. opts,cmd_args = parse_opts(sys.argv,help_data)
  54. if 'quiet' in opts: g.quiet = True
  55. if 'verbose' in opts: g.verbose = True
  56. if 'show_hash_presets' in opts: show_hash_presets()
  57. if len(cmd_args) != 1:
  58. msg("One input file must be specified")
  59. sys.exit(2)
  60. infile = cmd_args[0]
  61. # Old key:
  62. label,metadata,hash_preset,salt,enc_seed = get_data_from_wallet(infile)
  63. seed_id,key_id = metadata[:2]
  64. # Repeat on incorrect pw entry
  65. prompt = "Enter %spassphrase: " % (""
  66. if 'keep_old_passphrase' in opts else "old ")
  67. while True:
  68. passwd = get_mmgen_passphrase(prompt,{})
  69. key = make_key(passwd, salt, hash_preset)
  70. seed = decrypt_seed(enc_seed, key, seed_id, key_id)
  71. if seed: break
  72. changed = {}
  73. if 'label' in opts:
  74. if opts['label'] != label:
  75. msg("Label changed: '%s' -> '%s'" % (label, opts['label']))
  76. changed['label'] = True
  77. else:
  78. msg("Label is unchanged: '%s'" % (label))
  79. else: opts['label'] = label # Copy the old label
  80. if 'hash_preset' in opts:
  81. if hash_preset != opts['hash_preset']:
  82. qmsg("Hash preset has changed (%s -> %s)" %
  83. (hash_preset, opts['hash_preset']))
  84. changed['preset'] = True
  85. else:
  86. msg("Hash preset is unchanged")
  87. else:
  88. opts['hash_preset'] = hash_preset
  89. if 'keep_old_passphrase' in opts:
  90. msg("Keeping old passphrase by user request")
  91. else:
  92. new_passwd = get_new_passphrase("new passphrase", opts)
  93. if new_passwd == passwd:
  94. qmsg("Passphrase is unchanged")
  95. else:
  96. qmsg("Passphrase has changed")
  97. passwd = new_passwd
  98. changed['passwd'] = True
  99. if 'preset' in changed or 'passwd' in changed: # Update key ID, salt
  100. qmsg("Will update salt and key ID")
  101. from hashlib import sha256
  102. salt = sha256(salt + get_random(128,opts)).digest()[:g.salt_len]
  103. key = make_key(passwd, salt, opts['hash_preset'])
  104. new_key_id = make_chksum_8(key)
  105. qmsg("Key ID changed: %s -> %s" % (key_id,new_key_id))
  106. key_id = new_key_id
  107. enc_seed = encrypt_seed(seed, key)
  108. elif not 'label' in changed:
  109. msg("Data unchanged. No file will be written")
  110. sys.exit(2)
  111. write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts)