passwdlist.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  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. passwdlist: Password list class for the MMGen suite
  20. """
  21. from collections import namedtuple
  22. from .util import ymsg,is_int,die
  23. from .obj import ImmutableAttr,ListItemAttr,MMGenPWIDString,TwComment
  24. from .key import PrivKey
  25. from .addr import MMGenPasswordType,AddrIdx,AddrListID
  26. from .addrlist import (
  27. AddrListChksum,
  28. AddrListIDStr,
  29. AddrListEntryBase,
  30. AddrList,
  31. )
  32. class PasswordListEntry(AddrListEntryBase):
  33. passwd = ListItemAttr(str,typeconv=False) # TODO: create Password type
  34. idx = ImmutableAttr(AddrIdx)
  35. comment = ListItemAttr(TwComment,reassign_ok=True)
  36. sec = ListItemAttr(PrivKey,include_proto=True)
  37. class PasswordList(AddrList):
  38. entry_type = PasswordListEntry
  39. main_attr = 'passwd'
  40. desc = 'password'
  41. gen_desc = 'password'
  42. gen_desc_pl = 's'
  43. gen_addrs = False
  44. gen_keys = False
  45. gen_passwds = True
  46. pw_len = None
  47. dfl_pw_fmt = 'b58'
  48. pwinfo = namedtuple('passwd_info',['min_len','max_len','dfl_len','valid_lens','desc','chk_func'])
  49. pw_info = {
  50. # 32**25 < 2**128 < 32**26
  51. 'b32': pwinfo(10, 42 ,24, None, 'base32 password', 'baseconv.is_b32_str'),
  52. # 58**21 < 2**128 < 58**22
  53. 'b58': pwinfo(8, 36 ,20, None, 'base58 password', 'baseconv.is_b58_str'),
  54. 'bip39': pwinfo(12, 24 ,24, [12,18,24],'BIP39 mnemonic', 'bip39.is_bip39_mnemonic'),
  55. 'xmrseed': pwinfo(25, 25, 25, [25], 'Monero new-style mnemonic','xmrseed.is_xmrseed'),
  56. 'hex': pwinfo(32, 64 ,64, [32,48,64],'hexadecimal password', 'util.is_hex_str'),
  57. }
  58. chksum_rec_f = lambda foo,e: (str(e.idx), e.passwd)
  59. feature_warn_fs = 'WARNING: {!r} is a potentially dangerous feature. Use at your own risk!'
  60. hex2bip39 = False
  61. def __init__(
  62. self,
  63. cfg,
  64. proto,
  65. infile = None,
  66. seed = None,
  67. pw_idxs = None,
  68. pw_id_str = None,
  69. pw_len = None,
  70. pw_fmt = None,
  71. chk_params_only = False,
  72. skip_chksum_msg = False ):
  73. self.cfg = cfg
  74. self.proto = proto # proto is ignored
  75. if not cfg.debug_addrlist:
  76. self.dmsg_sc = self.noop
  77. if infile:
  78. self.infile = infile
  79. # sets self.pw_id_str, self.pw_fmt, self.pw_len, self.chk_func:
  80. self.data = self.file.parse_file(infile)
  81. else:
  82. if not chk_params_only:
  83. for k in (seed,pw_idxs):
  84. assert k
  85. self.pw_id_str = MMGenPWIDString(pw_id_str)
  86. self.set_pw_fmt(pw_fmt)
  87. self.set_pw_len(pw_len)
  88. if chk_params_only:
  89. return
  90. if self.hex2bip39:
  91. ymsg(self.feature_warn_fs.format(pw_fmt))
  92. self.set_pw_len_vs_seed_len(seed) # sets self.bip39, self.xmrseed, self.xmrproto self.baseconv
  93. self.al_id = AddrListID( sid=seed.sid, mmtype=MMGenPasswordType(self.proto,'P') )
  94. self.data = self.generate(seed,pw_idxs)
  95. self.num_addrs = len(self.data)
  96. self.fmt_data = ''
  97. self.chksum = AddrListChksum(self)
  98. fs = f'{self.al_id.sid}-{self.pw_id_str}-{self.pw_fmt_disp}-{self.pw_len}[{{}}]'
  99. self.id_str = AddrListIDStr(self,fs)
  100. if not skip_chksum_msg:
  101. self.do_chksum_msg(record=not infile)
  102. def set_pw_fmt(self,pw_fmt):
  103. if pw_fmt == 'hex2bip39':
  104. self.hex2bip39 = True
  105. self.pw_fmt = 'bip39'
  106. self.pw_fmt_disp = 'hex2bip39'
  107. else:
  108. self.pw_fmt = pw_fmt
  109. self.pw_fmt_disp = pw_fmt
  110. if self.pw_fmt not in self.pw_info:
  111. die( 'InvalidPasswdFormat',
  112. f'{self.pw_fmt!r}: invalid password format. Valid formats: {", ".join(self.pw_info)}' )
  113. def chk_pw_len(self,passwd=None):
  114. if passwd is None:
  115. assert self.pw_len,'either passwd or pw_len must be set'
  116. pw_len = self.pw_len
  117. fs = '{l}: invalid user-requested length for {b} ({c}{m})'
  118. else:
  119. pw_len = len(passwd)
  120. fs = '{pw}: {b} has invalid length {l} ({c}{m} characters)'
  121. d = self.pw_info[self.pw_fmt]
  122. if d.valid_lens:
  123. if pw_len not in d.valid_lens:
  124. die(2, fs.format( l=pw_len, b=d.desc, c='not one of ', m=d.valid_lens, pw=passwd ))
  125. elif pw_len > d.max_len:
  126. die(2, fs.format( l=pw_len, b=d.desc, c='>', m=d.max_len, pw=passwd ))
  127. elif pw_len < d.min_len:
  128. die(2, fs.format( l=pw_len, b=d.desc, c='<', m=d.min_len, pw=passwd ))
  129. def set_pw_len(self,pw_len):
  130. d = self.pw_info[self.pw_fmt]
  131. if pw_len is None:
  132. self.pw_len = d.dfl_len
  133. return
  134. if not is_int(pw_len):
  135. die(2,f'{pw_len!r}: invalid user-requested password length (not an integer)')
  136. self.pw_len = int(pw_len)
  137. self.chk_pw_len()
  138. def set_pw_len_vs_seed_len(self,seed):
  139. pf = self.pw_fmt
  140. if pf == 'hex':
  141. pw_bytes = self.pw_len // 2
  142. good_pw_len = seed.byte_len * 2
  143. elif pf == 'bip39':
  144. from .bip39 import bip39
  145. self.bip39 = bip39()
  146. pw_bytes = bip39.nwords2seedlen(self.pw_len,in_bytes=True)
  147. good_pw_len = bip39.seedlen2nwords(seed.byte_len,in_bytes=True)
  148. elif pf == 'xmrseed':
  149. from .xmrseed import xmrseed
  150. from .protocol import init_proto
  151. self.xmrseed = xmrseed()
  152. self.xmrproto = init_proto( self.cfg, 'xmr' )
  153. pw_bytes = xmrseed().seedlen_map_rev[self.pw_len]
  154. try:
  155. good_pw_len = xmrseed().seedlen_map[seed.byte_len]
  156. except:
  157. die(1,f'{seed.byte_len*8}: unsupported seed length for Monero new-style mnemonic')
  158. elif pf in ('b32','b58'):
  159. pw_int = (32 if pf == 'b32' else 58) ** self.pw_len
  160. pw_bytes = pw_int.bit_length() // 8
  161. from .baseconv import baseconv
  162. self.baseconv = baseconv(self.pw_fmt)
  163. good_pw_len = len( baseconv(pf).frombytes(b'\xff'*seed.byte_len) )
  164. else:
  165. raise NotImplementedError(f'{pf!r}: unknown password format')
  166. if pw_bytes > seed.byte_len:
  167. die(1,
  168. f'Cannot generate passwords with more entropy than underlying seed! ({len(seed.data)*8} bits)\n' +
  169. (f'Re-run the command with --passwd-len={good_pw_len}' if pf in ('bip39','hex') else
  170. 'Re-run the command, specifying a password length of {} or less')
  171. )
  172. if pf in ('bip39','hex') and pw_bytes < seed.byte_len:
  173. from .ui import keypress_confirm
  174. if not keypress_confirm(
  175. self.cfg,
  176. f'WARNING: requested {self.pw_info[pf].desc} length has less entropy ' +
  177. 'than underlying seed!\nIs this what you want?',
  178. default_yes = True ):
  179. die(1,'Exiting at user request')
  180. def gen_passwd(self,secbytes):
  181. assert self.pw_fmt in self.pw_info
  182. if self.pw_fmt == 'hex':
  183. # take most significant part
  184. return secbytes.hex()[:self.pw_len]
  185. elif self.pw_fmt == 'bip39':
  186. pw_len_bytes = self.bip39.nwords2seedlen( self.pw_len, in_bytes=True )
  187. # take most significant part
  188. return ' '.join( self.bip39.fromhex(secbytes[:pw_len_bytes].hex()) )
  189. elif self.pw_fmt == 'xmrseed':
  190. pw_len_bytes = self.xmrseed.seedlen_map_rev[self.pw_len]
  191. bytes_preproc = self.xmrproto.preprocess_key(
  192. secbytes[:pw_len_bytes], # take most significant part
  193. None )
  194. return ' '.join( self.xmrseed.frombytes(bytes_preproc) )
  195. else:
  196. # take least significant part
  197. return self.baseconv.frombytes(
  198. secbytes,
  199. pad = self.pw_len,
  200. tostr = True )[-self.pw_len:]
  201. def check_format(self,pw):
  202. if not self.chk_func(pw):
  203. raise ValueError(f'Password is not valid {self.pw_info[self.pw_fmt].desc} data')
  204. pwlen = len(pw.split()) if self.pw_fmt in ('bip39','xmrseed') else len(pw)
  205. if pwlen != self.pw_len:
  206. raise ValueError(f'Password has incorrect length ({pwlen} != {self.pw_len})')
  207. return True
  208. def scramble_seed(self,seed):
  209. # Changing either pw_fmt or pw_len will cause a different, unrelated
  210. # set of passwords to be generated: this is what we want.
  211. # NB: In original implementation, pw_id_str was 'baseN', not 'bN'
  212. scramble_key = f'{self.pw_fmt}:{self.pw_len}:{self.pw_id_str}'
  213. if self.hex2bip39:
  214. pwlen = self.bip39.nwords2seedlen(self.pw_len,in_hex=True)
  215. scramble_key = f'hex:{pwlen}:{self.pw_id_str}'
  216. self.dmsg_sc('str',scramble_key)
  217. from .crypto import Crypto
  218. return Crypto(self.cfg).scramble_seed(seed,scramble_key.encode())