Browse Source

Improved held-down key protection under Linux

philemon 10 years ago
parent
commit
bfd0cda562
4 changed files with 18 additions and 10 deletions
  1. 1 0
      mmgen/Opts.py
  2. 4 3
      mmgen/crypto.py
  3. 9 5
      mmgen/term.py
  4. 4 2
      mmgen/util.py

+ 1 - 0
mmgen/Opts.py

@@ -163,6 +163,7 @@ def check_opts(opts,long_opts):
 			if not opt_is_in_list(val,g.hash_presets.keys(),what): return False
 		elif opt == 'usr_randchars':
 			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.max_urandchars,what): return False
 		else:

+ 4 - 3
mmgen/crypto.py

@@ -139,10 +139,11 @@ def get_random_data_from_user(uchars):
 	# time.clock() always returns zero, so we'll use time.time()
 	saved_time = time.time()
 
-	key_data,time_data = "",[]
+	key_data,time_data,pp = "",[],True
 
 	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))
 		now = time.time()
 		time_data.append(now - saved_time)
@@ -167,7 +168,7 @@ def get_random(length,opts):
 	from Crypto import Random
 	os_rand = Random.new().read(length)
 	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:
 			g.user_entropy = sha256(
 				get_random_data_from_user(opts['usr_randchars'])).digest()

+ 9 - 5
mmgen/term.py

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

+ 4 - 2
mmgen/util.py

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