-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLE_test.py
197 lines (141 loc) · 5.21 KB
/
BLE_test.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
from bluedot.btcomm import BluetoothServer
from PIL import Image
import pyqrcode
from bluedot.btcomm import BluetoothAdapter
from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305
from cryptography import exceptions
import base64
import time
import os
from signal import pause
#def of global constants:
RECEV_IMGS_PATH = 'Recev_Images/'
LOGNAME = 'logfile_' + time.strftime("%d-%m-%y") + '.txt'
CAMERA_SERIAL = ''
first_conn = 0
if os.path.exists('key.code'):
print('key.code leido de archivo')
else:
chacha_key = ChaCha20Poly1305.generate_key()
a = BluetoothAdapter()
MAC = a.address
block = chacha_key + MAC.encode() + time.strftime("%H%M%S").encode()
print('Len of block: ' + repr(len(block)))
block64 = base64.encodestring(block)
print('Len of block64: ' + repr(len(block64)))
keypair = pyqrcode.create(block64.decode())
print('key.code escrito en archivo')
keycode = open('key.code', 'wb')
keycode.write(chacha_key) #escribir clave con write
keycode.close()
keypair.png('keycode.png', scale=7)
a.allow_pairing(40)
first_conn = 1
keycode = open('key.code', 'rb')
CHACHAKEY = keycode.read() #Cargar clave con read
print(CHACHAKEY)
image_sbin = ''
image_name = ''
def BLUEcryptosend(plaintext):
aad = bytes(time.strftime("%d%m%y"), 'utf-8')
chacha = ChaCha20Poly1305(CHACHAKEY)
nonce = os.urandom(3) + aad + os.urandom(3)
ct = chacha.encrypt(nonce, plaintext, aad)
ct64 = base64.encodestring(ct + nonce + aad)
s.send(ct64.decode())
print(ct64.decode())
return ct64.decode()
def BLUEdecrypt(ciphertext_base):
try:
ciphertext = base64.decodestring(ciphertext_base.encode())
aad = ciphertext[len(ciphertext)-6:] ##bin
nonce = ciphertext[len(ciphertext)-18:len(ciphertext)-6] ##bin
ct = ciphertext[:len(ciphertext)-18] ##bin
try:
chacha = ChaCha20Poly1305(CHACHAKEY)
data = chacha.decrypt(nonce, ct, aad)
print('OK')
return data
except: ##mejorar Except
print('ERROR DECRYPTING')
return b'-1'
except:
print('ERROR DECRYPTING')
return b'-1'
def save_image(img_data, image_name):
image = open(RECEV_IMGS_PATH + image_name, 'wb')
image.write(img_data)
image.close()
def logwrite(text):
open_mode = ''
if os.path.exists(LOGNAME):
open_mode = 'a'
else:
open_mode = 'w'
logfile = open(LOGNAME, open_mode)
logfile.write('['+ time.strftime("%H:%M:%S") +']'+ text +'\n')
logfile.close()
def data_received(data):
global image_name
global first_conn
if(first_conn == 1):
msg_bin = BLUEdecrypt(data)
msg = msg_bin.decode()
global HOUR
CAMERA_SERIAL_prov = msg[:20]
HOUR = msg[20:]
print(CAMERA_SERIAL_prov)
if(HOUR[:2] == time.strftime('%H')) and (int(time.strftime('%M'))- int(HOUR[2:4]) <= 1):
global CAMERA_SERIAL
CAMERA_SERIAL = CAMERA_SERIAL_prov
first_conn = 0
else:
if(len(data) < 1008) and (image_name == ''):
print(data)
msg_bin = BLUEdecrypt(data)
msg = msg_bin.decode()
if(msg == '-1'):
print('Error en la decodificación')
BLUEcryptosend(b'NACK') #Do nothing, wait for a new packet.
elif(msg[0:6]=='0x0011'):
logwrite('[SYS]: ' + msg.lstrip('0x0011'))
BLUEcryptosend(b'0x1001ACK')
elif(msg[0:6]=='0x1100'):
logwrite('[IMG]: ' + msg.lstrip('0x1100'))
image_name = msg.lstrip('0x1100')
BLUEcryptosend(b'0x1001ACK')
elif(msg[0:6]=='0x1001'):
if(msg.lstrip('0x1001') == 'ACK'):
a=1
#ACK recibido del cliente
elif(msg.lstrip('0x1001') == 'NACK'):
#NACK recibido del cliente:
##Reenvío de ACK
BLUEcryptosend(b'0x1001ACK')
else:
global image_sbin
image_sbin += data
if(len(data) < 1008):
print(image_sbin)
msg_bin = BLUEdecrypt(image_sbin)
try:
image_data = base64.decodestring(msg_bin)
save_image(image_data, image_name)
BLUEcryptosend(b'0x1001ACK')
image_sbin = ''
image_name = ''
except:
image_sbin = ''
image_name = ''
BLUEcryptosend(b'0x1001NACK')
s = BluetoothServer(data_received)
if(first_conn == 1):
time.sleep(60)
if(first_conn == 1):
print('Echando de la lista de emparejados')
if(s.client_connected):
os.system('./Throw_out.sh "'+s.client_address+'" | bluetoothctl -a')
os.remove('key.code')
elif(first_conn ==0):
print('Emparejamiento correcto')
pause()