forked from JsBergbau/MiTemperature2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptoFunctions.py
61 lines (57 loc) · 1.94 KB
/
cryptoFunctions.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
import struct
import binascii
from Cryptodome.Cipher import AES
#pycryptodome
def parse_value(hexvalue):
vlength = len(hexvalue)
# print("vlength:", vlength, "hexvalue", hexvalue.hex(), "typecode", typecode)
if vlength == 3:
temp = hexvalue[0]/2 - 40
humi = hexvalue[1]/2
batt = hexvalue[2] & 0x7F
trg = hexvalue[2] >> 7
#print("Temperature:", temp, "Humidity:", humi, "Battery:", batt, "Trg:", trg)
return temp, humi, batt
if vlength == 6:
(temp, humi, batt, trg) = struct.unpack("<hHBB", hexvalue)
#print("Temperature:", temp/100, "Humidity:", humi/100, "Battery:", batt, "Trg:", trg)
return temp/100, humi/100, batt
#print("MsgLength:", vlength, "HexValue:", hexvalue.hex())
return None
def decrypt_payload(payload, key, nonce, mac):
token = payload[-4:] # mic
cipherpayload = payload[:-4] # EncodeData
#print("Nonce: %s" % nonce.hex())
#print("CryptData: %s" % cipherpayload.hex(), "Mic: %s" % token.hex())
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce, mac_len=4)
cipher.update(b"\x11")
data = None
try:
data = cipher.decrypt_and_verify(cipherpayload, token)
except ValueError as error:
mac=mac.hex()
macReversed=""
for x in range(-1,-len(mac),-2):
macReversed += mac[x-1] + mac[x] + ":"
macReversed = macReversed.upper()[:-1]
print("ERROR: Decryption failed with sensor MAC (probably wrong key provided):", macReversed)
return None
#print("DecryptData:", data.hex())
#print()
# if parse_value(data) != None:
# return 1
# #print('??')
# return None
return parse_value(data)
def decrypt_aes_ccm(key, mac, data):
#print("MAC:", mac.hex(), "Binkey:", key.hex())
#print()
adslength = len(data)
if adslength > 8 and data[0] <= adslength and data[0] > 7 and data[1] == 0x16 and data[2] == 0x1a and data[3] == 0x18:
pkt = data[:data[0]+1]
# nonce: mac[6] + head[4] + cnt[1]
nonce = b"".join([mac, pkt[:5]])
return decrypt_payload(pkt[5:], key, nonce, mac)
else:
print("Error: format packet!")
return None