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. import mmgen.config as g
  26. help_data = {
  27. 'desc': """Change the passphrase, hash preset or label of a {}
  28. deterministic wallet""".format(g.proj_name),
  29. 'usage': "[opts] [filename]",
  30. 'options': """
  31. -h, --help Print this help message
  32. -d, --outdir d Specify an alternate directory 'd' for output
  33. -H, --show-hash-presets Show information on available hash presets
  34. -k, --keep-old-passphrase Keep old passphrase (use when changing hash
  35. strength or label only)
  36. -L, --label l Change the wallet's label to 'l'
  37. -p, --hash-preset p Change scrypt.hash() parameters to preset 'p'
  38. (default: '{}')
  39. -P, --passwd-file f Get new passphrase from file 'f'
  40. -q, --quiet Suppress warnings; overwrite files without
  41. prompting
  42. -v, --verbose Produce more verbose output
  43. NOTE: The key ID will change if either the passphrase or hash preset
  44. are changed
  45. """.format(g.hash_preset)
  46. }
  47. short_opts = "hd:HkL:p:P:qv"
  48. long_opts = "help","outdir=","show_hash_presets","keep_old_passphrase",\
  49. "label=","hash_preset=","passwd_file=","quiet","verbose"
  50. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  51. if 'quiet' in opts: g.quiet = True
  52. if 'verbose' in opts: g.verbose = True
  53. check_opts(opts,long_opts)
  54. if 'show_hash_presets' in opts: show_hash_presets()
  55. if len(cmd_args) != 1:
  56. msg("One input file must be specified")
  57. sys.exit(2)
  58. infile = cmd_args[0]
  59. # Old key:
  60. label,metadata,hash_preset,salt,enc_seed = get_data_from_wallet(infile)
  61. seed_id,key_id = metadata[:2]
  62. # Repeat on incorrect pw entry
  63. prompt = "Enter %spassphrase: " % (""
  64. if 'keep_old_passphrase' in opts else "old ")
  65. while True:
  66. passwd = get_mmgen_passphrase(prompt,{})
  67. key = make_key(passwd, salt, hash_preset)
  68. seed = decrypt_seed(enc_seed, key, seed_id, key_id)
  69. if seed: break
  70. changed = {}
  71. if 'label' in opts:
  72. if opts['label'] != label:
  73. msg("Label changed: '%s' -> '%s'" % (label, opts['label']))
  74. changed['label'] = True
  75. else:
  76. msg("Label is unchanged: '%s'" % (label))
  77. else: opts['label'] = label # Copy the old label
  78. if 'hash_preset' in opts:
  79. if hash_preset != opts['hash_preset']:
  80. qmsg("Hash preset has changed (%s -> %s)" %
  81. (hash_preset, opts['hash_preset']))
  82. changed['preset'] = True
  83. else:
  84. msg("Hash preset is unchanged")
  85. else:
  86. opts['hash_preset'] = hash_preset
  87. if 'keep_old_passphrase' in opts:
  88. msg("Keeping old passphrase by user request")
  89. else:
  90. new_passwd = get_new_passphrase("new passphrase", opts)
  91. if new_passwd == passwd:
  92. qmsg("Passphrase is unchanged")
  93. else:
  94. qmsg("Passphrase has changed")
  95. passwd = new_passwd
  96. changed['passwd'] = True
  97. if 'preset' in changed or 'passwd' in changed: # Update key ID, salt
  98. qmsg("Will update salt and key ID")
  99. from hashlib import sha256
  100. from Crypto import Random
  101. salt = sha256(salt + Random.new().read(128)).digest()[:g.salt_len]
  102. key = make_key(passwd, salt, opts['hash_preset'])
  103. new_key_id = make_chksum_8(key)
  104. qmsg("Key ID changed: %s -> %s" % (key_id,new_key_id))
  105. key_id = new_key_id
  106. enc_seed = encrypt_seed(seed, key)
  107. elif not 'label' in changed:
  108. msg("Data unchanged. No file will be written")
  109. sys.exit(2)
  110. write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts)