forked from trustcrypto/python-onlykey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa_decrypt_testkey.py
More file actions
197 lines (147 loc) · 4.52 KB
/
rsa_decrypt_testkey.py
File metadata and controls
197 lines (147 loc) · 4.52 KB
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
# coding: utf-8
from __future__ import print_function
from builtins import input
from builtins import chr
from builtins import range
import hashlib
import time
import os
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from Crypto.Random import get_random_bytes
from Crypto import Random
import binascii
from onlykey import OnlyKey, Message
print('Initialize OnlyKey client...')
ok = OnlyKey()
print('Done')
print()
time.sleep(2)
ok.read_string(timeout_ms=100)
empty = 'a'
while not empty:
empty = ok.read_string(timeout_ms=100)
time.sleep(1)
print('You should see your OnlyKey blink 3 times')
print()
print('Enter RSA slot number to use for decryption (1 - 4)')
print()
slot = int(eval(input()))
print('Trying to read the public RSA N part 1...')
ok.send_message(msg=Message.OKGETPUBKEY, payload=chr(slot)) #, payload=[1, 1])
time.sleep(1.5)
for _ in range(10):
ok_pubkey1 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey1) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey1))
print('Trying to read the public RSA N part 2...')
for _ in range(10):
ok_pubkey2 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey2) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey2))
print('Trying to read the public RSA N part 3...')
for _ in range(10):
ok_pubkey3 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey3) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey3))
print('Trying to read the public RSA N part 4...')
for _ in range(10):
ok_pubkey4 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey4) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey4))
print('Trying to read the public RSA N part 5...')
for _ in range(10):
ok_pubkey5 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey5) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey5))
print('Trying to read the public RSA N part 6...')
for _ in range(10):
ok_pubkey6 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey6) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey6))
print('Trying to read the public RSA N part 7...')
for _ in range(10):
ok_pubkey7 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey7) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey7))
print('Trying to read the public RSA N part 8...')
for _ in range(10):
ok_pubkey8 = ok.read_bytes(64, to_str=True, timeout_ms=1000)
if len(ok_pubkey8) == 64:
break
time.sleep(1)
print()
print('received=', repr(ok_pubkey8))
if not ok_pubkey2:
raise Exception('failed to set the RSA key')
print('Received Public Key generated by OnlyKey')
ok_pubkey = ok_pubkey1 + ok_pubkey2 + ok_pubkey3 + ok_pubkey4 + ok_pubkey5 + ok_pubkey6 + ok_pubkey7 + ok_pubkey8
print('Public N=', repr(ok_pubkey))
print()
print('Key Size =', len(ok_pubkey))
print()
random_generator = Random.new().read
key = RSA.generate((len(ok_pubkey)*8), random_generator) #generate pub and priv key
ok_pubkey = ok_pubkey.encode("HEX")
n = int(ok_pubkey, 16)
e = int('10001', 16)
key = RSA.construct((n, e))
print('N =', repr(key.n))
print()
message = 'Secret message'
#h = SHA.new(message)
cipher = PKCS1_v1_5.new(key)
ciphertext = cipher.encrypt(message)
#hex_enc_data = bin2hex(enc_data)
print('encrypted payload = ', repr(ciphertext))
print()
# Compute the challenge pin
h = hashlib.sha256()
h.update(ciphertext)
d = h.digest()
assert len(d) == 32
def get_button(byte):
ibyte = ord(byte)
if ibyte < 6:
return 1
return ibyte % 5 + 1
b1, b2, b3 = get_button(d[0]), get_button(d[15]), get_button(d[31])
print('Sending the payload to the OnlyKey...')
ok.send_large_message2(msg=Message.OKDECRYPT, payload=ciphertext, slot_id=slot)
print('Please enter the 3 digit challenge code on OnlyKey (and press ENTER if necessary)')
print('{} {} {}'.format(b1, b2, b3))
eval(input())
print('Trying to read the decrypted data from OnlyKey...')
ok_decrypted = ''
while ok_decrypted == '':
time.sleep(0.5)
ok_decrypted = ok.read_bytes(len(message), to_str=True)
print('Decrypted by OnlyKey, data=', repr(ok_decrypted))
print('Original Message =', repr(message))
print('Assert that the original message matches the data generated on the OnlyKey')
assert repr(ok_decrypted) == repr(message)
print('Ok, data matches')
print()
print('Done')