forked from vstoykovbg/doublerandom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomness_mixer.py
311 lines (219 loc) · 9.6 KB
/
randomness_mixer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/python3
import time
from os.path import exists
from hashlib import sha512
from os import urandom
import binascii
from Crypto.Util.strxor import strxor
import threading
import subprocess
from os import devnull
DEVNULL = open(devnull, 'wb')
mutex = threading.Lock()
global_ask_for_volume = True
def get_random_bytes(howmany):
if exists("/dev/random"):
with open("/dev/random", 'rb') as f:
return f.read(howmany)
else:
print ("/dev/random not found, using os.urandom instead.")
return urandom(howmany)
def get_random_sound():
return subprocess.Popen(["arecord", "-f", "cd", "-t", "raw", "-d", "5"],
stdout=subprocess.PIPE,stderr=DEVNULL).communicate()[0]
def haveged_1024_bytes():
return subprocess.Popen(["haveged", "-n", "1024", "-f", "-"],
stdout=subprocess.PIPE,stderr=DEVNULL).communicate()[0]
def get_time():
if "time_ns" in dir(time):
timenow = time.time_ns()
else:
timenow = time.time()
return timenow
def on_move(x, y):
global global_buffer
global global_counter
global global_hash
my_time = get_time()
this_data_chunk = bytes ( str(my_time).encode('utf-8') )
this_data_chunk += bytes ( b' ' + str(x).encode('utf-8') )
this_data_chunk += bytes ( b' ' + str(y).encode('utf-8') )
with mutex:
print(global_counter, this_data_chunk, " ")
print ("\033[A\033[A") # up up (and go to new line) = up
global_buffer += this_data_chunk
buffer_len=len(global_buffer)
if buffer_len > 1024 * 15:
global_hash = sha512(global_buffer + global_hash).digest()
global_buffer = bytearray()
global_buffer += global_hash
assert isinstance(global_buffer, bytearray)
global_counter += 1
def print_tolerant_error(msg, detail):
print("\n\n" + msg, detail)
print("However, this script is fault tolerant, so we continue.\n")
def get_hash_with_mouse():
from pynput import mouse
counter_max = 10
global global_hash
global global_buffer
global global_counter
# Improving efficiency of concatenation by using bytearray instead of bytestring
# https://www.guyrutenberg.com/2020/04/04/fast-bytes-concatenation-in-python/
global_buffer = bytearray()
global_hash = get_random_bytes(32)
hash_sound = get_random_bytes(32)
global_buffer += get_random_bytes(32)
global_counter = 0
haveged_chunk = b''
assert isinstance(global_buffer, bytearray)
test_hash = global_hash
listener = mouse.Listener(on_move=on_move)
listener.start()
print("Please boost the microphone input volume and connect a microphone")
print("or other noise source. The script will \"listen\" to the noise")
print("while you move the mouse.\n")
print("Please move the mouse over areas of the screen where this script can \"see\"")
print("the movements. You can confirm that the script sees the mouse movements")
print("by observing the change in the digits below.")
print("On systems using Xwayland the mouse movements are visible only over")
print("some areas.")
try:
while True:
random_sound_chunk = get_random_sound()
if len(random_sound_chunk) < 100000:
with mutex:
print_tolerant_error("⚠ We got from arecord something unexpected.", "")
hash_sound = sha512(hash_sound + random_sound_chunk).digest()
if global_counter > counter_max:
break
except Exception as detail:
with mutex:
print_tolerant_error("⚠ Trying to get data from arecord failed.", detail)
try:
haveged_chunk = haveged_1024_bytes()
except Exception as detail:
with mutex:
print_tolerant_error("⚠ Trying to get data from haveged failed.", detail)
# in case get_random_sound() fails
while True:
if global_counter > counter_max:
break
time.sleep(1)
listener.stop()
time.sleep(0.1)
print("Randomness collection from the mice is complete.")
assert global_hash != test_hash
user_entropy = input("Please type some random data (from dice rolls, decks of cards, etc): ").encode('utf-8')
global_hash = sha512(bytes(global_buffer + global_hash + get_random_bytes(256) + haveged_chunk + hash_sound + user_entropy)).digest()
return strxor( global_hash, get_random_bytes(64) )
def randomness_from_dev_input_mice():
print ("\nPlease move the mouse.")
local_buffer = bytearray()
with open( "/dev/input/mice", "rb" ) as file:
outer_range = 64
for counter in range(1, outer_range +1):
for counter2 in range(128):
buf = file.read(3);
my_time = get_time()
local_buffer += str(my_time).encode('utf-8') + buf
print (my_time, counter, "from", outer_range, " ")
print ("\033[A\033[A") # up up (and go to new line) = up
return sha512(bytes(local_buffer)).digest()
def get_hash_with_mouse_alternative():
hash_sound = get_random_bytes(32)
haveged_chunk = b''
user_entropy = b''
global global_ask_for_volume
try:
if global_ask_for_volume:
print("Please boost the microphone input volume and connect a microphone")
print("or other noise source.")
user_entropy = input("Press Enter to continue.").encode('utf-8')
print ("\033[A\033[A") # up up (and go to new line) = up
global_ask_for_volume = False
for counter in range(10):
print ("Reading data from sound input... Iteration: ", counter)
print ("\033[A\033[A") # up up (and go to new line) = up
random_sound_chunk = get_random_sound()
if len(random_sound_chunk) < 100000:
print_tolerant_error("⚠ We got from arecord something unexpected.", "")
hash_sound = sha512(hash_sound + random_sound_chunk).digest()
except Exception as detail:
print_tolerant_error("⚠ Trying to get data from arecord failed.", detail)
try:
haveged_chunk = haveged_1024_bytes()
except Exception as detail:
print_tolerant_error("⚠ Trying to get data from haveged failed.", detail)
mice_hash = b''
try:
mice_hash = randomness_from_dev_input_mice()
except Exception as detail:
print_tolerant_error("⚠ Trying to get data from /dev/input/mice failed.", detail)
user_entropy += input("Please type some random data (from dice rolls, decks of cards, etc): ").encode('utf-8')
hash_64_bytes = sha512(get_random_bytes(256) + mice_hash + haveged_chunk + hash_sound + user_entropy).digest()
return strxor( hash_64_bytes, get_random_bytes(64) )
def get_hash_with_haveged_and_arecord():
hash_sound = get_random_bytes(32)
haveged_chunk = b''
user_entropy = b''
global global_ask_for_volume
try:
if global_ask_for_volume:
print("Please boost the microphone input volume and connect a microphone")
print("or other noise source.")
user_entropy = input("Press Enter to continue.").encode('utf-8')
print ("\033[A\033[A") # up up (and go to new line) = up
global_ask_for_volume = False
for counter in range(10):
print ("Reading data from sound input... Iteration: ", counter)
print ("\033[A\033[A") # up up (and go to new line) = up
random_sound_chunk = get_random_sound()
if len(random_sound_chunk) < 100000:
print_tolerant_error("⚠ We got from arecord something unexpected.", "")
hash_sound = sha512(hash_sound + random_sound_chunk).digest()
except Exception as detail:
print_tolerant_error("⚠ Trying to get data from arecord failed.", detail)
try:
haveged_chunk = haveged_1024_bytes()
except Exception as detail:
print_tolerant_error("⚠ Trying to get data from haveged failed.", detail)
user_entropy += input("Please type some random data (from dice rolls, decks of cards, etc): ").encode('utf-8')
hash_64_bytes = sha512(get_random_bytes(256) + haveged_chunk + hash_sound + user_entropy).digest()
return strxor( hash_64_bytes, get_random_bytes(64) )
def get_hash(use_mouse=True):
this_hash = b''
if use_mouse:
try:
this_hash = get_hash_with_mouse()
except Exception as detail:
print_tolerant_error("⚠ Trying to use get_hash_with_mouse() failed.", detail)
if this_hash == b'':
this_hash = get_hash_with_mouse_alternative()
else:
this_hash = get_hash_with_haveged_and_arecord()
assert len(this_hash) == 64
return this_hash
def main():
this_hash = get_hash()
try:
from mnemonic import Mnemonic
from hashlib import sha256
myrandom = strxor(get_random_bytes(32), sha256(bytes(this_hash + urandom(1024))).digest())
print ("\n\n BIP39 mnemonic: \n\n")
words = Mnemonic('english').to_mnemonic(myrandom)
test = Mnemonic('english').to_entropy(words)
# Hmmm, It works without this line:
#test = bytes(test)
if test == myrandom:
print(words, "\n")
else:
print("Oops. Fatal error.")
quit();
except Exception as detail:
print_tolerant_error("⚠ Error. Can't create mnemonic. ", detail)
print ("Here are some random base64 data instead.")
print ("\nRandom base64 encoded data:\n")
print (binascii.b2a_base64(this_hash).decode("utf-8"))
if __name__ == "__main__":
main()