mmgen-passchg 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013 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.utils 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. -v, --verbose Produce more verbose output
  41. NOTE: The key ID will change if either the passphrase or hash preset
  42. are changed
  43. """.format(g.hash_preset)
  44. }
  45. short_opts = "hd:HkL:p:P:v"
  46. long_opts = "help","outdir=","show_hash_presets","keep_old_passphrase",\
  47. "label=","hash_preset=","passwd_file=","verbose"
  48. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  49. if 'show_hash_presets' in opts: show_hash_presets()
  50. check_opts(opts,long_opts)
  51. if len(cmd_args) != 1:
  52. msg("One input file must be specified")
  53. sys.exit(2)
  54. infile = cmd_args[0]
  55. # Old key:
  56. label,metadata,hash_preset,salt,enc_seed = get_data_from_wallet(infile,opts)
  57. seed_id,key_id = metadata[:2]
  58. # Repeat on incorrect pw entry
  59. prompt = "Enter %spassphrase: " % (""
  60. if 'keep_old_passphrase' in opts else "old ")
  61. while True:
  62. passwd = get_mmgen_passphrase(prompt,{})
  63. key = make_key(passwd, salt, hash_preset)
  64. seed = decrypt_seed(enc_seed, key, seed_id, key_id)
  65. if seed: break
  66. changed = {}
  67. if 'label' in opts:
  68. if opts['label'] != label:
  69. msg("Label changed: '%s' -> '%s'" % (label, opts['label']))
  70. changed['label'] = True
  71. else:
  72. msg("Label is unchanged: '%s'" % (label))
  73. else: opts['label'] = label # Copy the old label
  74. if 'hash_preset' in opts:
  75. if hash_preset != opts['hash_preset']:
  76. msg("Hash preset has changed (%s -> %s)" %
  77. (hash_preset, opts['hash_preset']))
  78. changed['preset'] = True
  79. else:
  80. msg("Hash preset is unchanged")
  81. else:
  82. opts['hash_preset'] = hash_preset
  83. if 'keep_old_passphrase' in opts:
  84. msg("Keeping old passphrase by user request")
  85. else:
  86. new_passwd = get_new_passphrase("new passphrase", opts)
  87. if new_passwd == passwd:
  88. msg("Passphrase is unchanged")
  89. else:
  90. msg("Passphrase has changed")
  91. passwd = new_passwd
  92. changed['passwd'] = True
  93. if 'preset' in changed or 'passwd' in changed: # Update key ID, salt
  94. msg("Will update salt and key ID")
  95. from hashlib import sha256
  96. from Crypto import Random
  97. salt = sha256(salt + Random.new().read(128)).digest()[:g.salt_len]
  98. key = make_key(passwd, salt, opts['hash_preset'])
  99. new_key_id = make_chksum_8(key)
  100. msg("Key ID changed: %s -> %s" % (key_id,new_key_id))
  101. key_id = new_key_id
  102. enc_seed = encrypt_seed(seed, key, opts)
  103. elif not 'label' in changed:
  104. msg("Data unchanged. No file will be written")
  105. sys.exit(2)
  106. write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts)