Improved held-down key protection under Linux

This commit is contained in:
philemon 2014-08-05 00:10:31 +04:00
commit bfd0cda562
4 changed files with 18 additions and 10 deletions

View file

@ -163,6 +163,7 @@ def check_opts(opts,long_opts):
if not opt_is_in_list(val,g.hash_presets.keys(),what): return False if not opt_is_in_list(val,g.hash_presets.keys(),what): return False
elif opt == 'usr_randchars': elif opt == 'usr_randchars':
if not opt_is_int(val,what): return False if not opt_is_int(val,what): return False
if val == '0': return True
if not opt_compares(val,">=",g.min_urandchars,what): return False if not opt_compares(val,">=",g.min_urandchars,what): return False
if not opt_compares(val,"<=",g.max_urandchars,what): return False if not opt_compares(val,"<=",g.max_urandchars,what): return False
else: else:

View file

@ -139,10 +139,11 @@ def get_random_data_from_user(uchars):
# time.clock() always returns zero, so we'll use time.time() # time.clock() always returns zero, so we'll use time.time()
saved_time = time.time() saved_time = time.time()
key_data,time_data = "",[] key_data,time_data,pp = "",[],True
for i in range(uchars): for i in range(uchars):
key_data += get_char(immed_chars="ALL") key_data += get_char(immed_chars="ALL",prehold_protect=pp)
if i == 0: pp = False
msg_r("\r" + prompt % (uchars - i - 1)) msg_r("\r" + prompt % (uchars - i - 1))
now = time.time() now = time.time()
time_data.append(now - saved_time) time_data.append(now - saved_time)
@ -167,7 +168,7 @@ def get_random(length,opts):
from Crypto import Random from Crypto import Random
os_rand = Random.new().read(length) os_rand = Random.new().read(length)
if 'usr_randchars' in opts and opts['usr_randchars'] not in (0,-1): if 'usr_randchars' in opts and opts['usr_randchars'] not in (0,-1):
kwhat = "a key from random data with " kwhat = "a key from OS random data + "
if not g.user_entropy: if not g.user_entropy:
g.user_entropy = sha256( g.user_entropy = sha256(
get_random_data_from_user(opts['usr_randchars'])).digest() get_random_data_from_user(opts['usr_randchars'])).digest()

View file

@ -42,7 +42,7 @@ def _kb_hold_protect_unix():
termios.tcsetattr(fd, termios.TCSADRAIN, old) termios.tcsetattr(fd, termios.TCSADRAIN, old)
def _get_keypress_unix(prompt="",immed_chars=""): def _get_keypress_unix(prompt="",immed_chars="",prehold_protect=True):
msg_r(prompt) msg_r(prompt)
timeout = float(0.3) timeout = float(0.3)
@ -53,14 +53,18 @@ def _get_keypress_unix(prompt="",immed_chars=""):
try: try:
while True: while True:
select([sys.stdin], [], [], False) # Protect against held-down key before read()
key = select([sys.stdin], [], [], timeout)[0]
ch = sys.stdin.read(1) ch = sys.stdin.read(1)
if prehold_protect:
if key: continue
if immed_chars == "ALL" or ch in immed_chars: if immed_chars == "ALL" or ch in immed_chars:
return ch return ch
if immed_chars == "ALL_EXCEPT_ENTER" and not ch in "\n\r": if immed_chars == "ALL_EXCEPT_ENTER" and not ch in "\n\r":
return ch return ch
second_key = select([sys.stdin], [], [], timeout)[0] # Protect against long keypress
if second_key: continue key = select([sys.stdin], [], [], timeout)[0]
if key: continue
else: return ch else: return ch
except KeyboardInterrupt: except KeyboardInterrupt:
msg("\nUser interrupt") msg("\nUser interrupt")
@ -87,7 +91,7 @@ def _kb_hold_protect_mswin():
sys.exit(1) sys.exit(1)
def _get_keypress_mswin(prompt="",immed_chars=""): def _get_keypress_mswin(prompt="",immed_chars="",prehold_protect=True):
msg_r(prompt) msg_r(prompt)
timeout = float(0.5) timeout = float(0.5)

View file

@ -688,12 +688,14 @@ def export_to_hidden_incog(incog_enc,opts):
from mmgen.term import kb_hold_protect,get_char from mmgen.term import kb_hold_protect,get_char
def my_raw_input(prompt,echo=True): def my_raw_input(prompt,echo=True):
msg_r(prompt)
kb_hold_protect()
try: try:
if echo: if echo:
reply = raw_input(prompt) reply = raw_input("")
else: else:
from getpass import getpass from getpass import getpass
reply = getpass(prompt) reply = getpass("")
except KeyboardInterrupt: except KeyboardInterrupt:
msg("\nUser interrupt") msg("\nUser interrupt")
sys.exit(1) sys.exit(1)