2017-02-28 18:11:00 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# example usage: ./rng_entropy_collector.py stm32_rng_1.dat 1048576
|
|
|
|
# note: for reading large amounts of entropy, compile a firmware
|
|
|
|
# that has DEBUG_RNG == 1 as that will disable the user button
|
|
|
|
# push confirmation
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import io
|
|
|
|
import sys
|
|
|
|
from trezorlib.client import TrezorClient
|
|
|
|
from trezorlib.transport_hid import HidTransport
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2017-02-28 18:11:00 +00:00
|
|
|
def get_client():
|
2017-06-23 19:31:42 +00:00
|
|
|
devices = HidTransport.enumerate() # list all connected TREZORs on USB
|
|
|
|
if len(devices) == 0: # check whether we found any
|
2017-02-28 18:11:00 +00:00
|
|
|
return None
|
2017-09-04 11:36:31 +00:00
|
|
|
transport = devices[0] # use first connected device
|
2017-06-23 19:31:42 +00:00
|
|
|
return TrezorClient(transport) # creates object for communicating with TREZOR
|
|
|
|
|
2017-02-28 18:11:00 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
client = get_client()
|
|
|
|
if not client:
|
|
|
|
print('No TREZOR connected')
|
|
|
|
return
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
arg1 = sys.argv[1] # output file
|
|
|
|
arg2 = int(sys.argv[2], 10) # total number of how many bytes of entropy to read
|
|
|
|
step = 1024 if arg2 >= 1024 else arg2 # trezor will only return 1KB at a time
|
2017-02-28 18:11:00 +00:00
|
|
|
|
|
|
|
with io.open(arg1, 'wb') as f:
|
|
|
|
for i in xrange(0, arg2, step):
|
|
|
|
entropy = client.get_entropy(step)
|
|
|
|
f.write(entropy)
|
|
|
|
|
|
|
|
client.close()
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2017-02-28 18:11:00 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|