forked from mweimerskirch/smarty_dsmr_proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.py
261 lines (222 loc) · 10.2 KB
/
decrypt.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
import serial
import binascii
import argparse
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import (Cipher, algorithms, modes)
from cryptography.exceptions import InvalidTag
has_dsmr_parser = True
try:
from dsmr_parser import telegram_specifications
from dsmr_parser.parsers import TelegramParser
from dsmr_parser import obis_references
import dsmr_parser.obis_name_mapping
except ImportError:
has_dsmr_parser = False
class SmartyProxy():
def __init__(self):
# Constants that describe the individual steps of the state machine:
# Initial state. Input is ignored until start byte is detected.
self.STATE_IGNORING = 0
# Start byte (hex "DB") has been detected.
self.STATE_STARTED = 1
# Length of system title has been read.
self.STATE_HAS_SYSTEM_TITLE_LENGTH = 2
# System title has been read.
self.STATE_HAS_SYSTEM_TITLE = 3
# Additional byte after the system title has been read.
self.STATE_HAS_SYSTEM_TITLE_SUFFIX = 4
# Length of remaining data has been read.
self.STATE_HAS_DATA_LENGTH = 5
# Additional byte after the remaining data length has been read.
self.STATE_HAS_SEPARATOR = 6
# Frame counter has been read.
self.STATE_HAS_FRAME_COUNTER = 7
# Payload has been read.
self.STATE_HAS_PAYLOAD = 8
# GCM tag has been read.
self.STATE_HAS_GCM_TAG = 9
# All input has been read. After this, we switch back to STATE_IGNORING and wait for a new start byte.
self.STATE_DONE = 10
# Command line arguments
self._args = {}
# Serial connection from which we read the data from the smart meter
self._connection = None
# Initial empty values. These will be filled as content is read
# and they will be reset each time we go back to the initial state.
self._state = self.STATE_IGNORING
self._buffer = ""
self._buffer_length = 0
self._next_state = 0
self._system_title_length = 0
self._system_title = b""
self._data_length_bytes = b"" # length of "remaining data" in bytes
self._data_length = 0 # length of "remaining data" as an integer
self._frame_counter = b""
self._payload = b""
self._gcm_tag = b""
def main(self):
parser = argparse.ArgumentParser()
parser.add_argument('key', help="Decryption key")
parser.add_argument('-i', '--serial-input-port', required=False, default="/dev/ttyUSB0", help="Input port. Defaults to /dev/ttyUSB0.")
parser.add_argument('-o', '--serial-output-port', required=False, help="Output port, e.g. /dev/pts/2.")
parser.add_argument('-a', '--aad', required=False, default="3000112233445566778899AABBCCDDEEFF", help="Additional authenticated data")
parser.add_argument('-p', '--parse', action='store_true', required=False, default=False, help="Parse and pretty print DSMR v5 telegram")
self._args = parser.parse_args()
self.connect()
while True:
self.process()
# Connect to the serial port when we run the script
def connect(self):
try:
self._connection = serial.Serial(
port=self._args.serial_input_port,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
)
except (serial.SerialException, OSError) as err:
print("ERROR")
# Start processing incoming data
def process(self):
try:
raw_data = self._connection.read()
except serial.SerialException:
return
# Read and parse the stream from the serial port byte by byte.
# This parsing works as a state machine (see the definitions in the __init__ method).
# See also the official documentation on http://smarty.creos.net/wp-content/uploads/P1PortSpecification.pdf
# For better human readability, we use the hexadecimal representation of the input.
hex_input = binascii.hexlify(raw_data)
# Initial state. Input is ignored until start byte is detected.
if self._state == self.STATE_IGNORING:
if hex_input == b'db':
self._state = self.STATE_STARTED
self._buffer = b""
self._buffer_length = 1
self._system_title_length = 0
self._system_title = b""
self._data_length = 0
self._data_length_bytes = b""
self._frame_counter = b""
self._payload = b""
self._gcm_tag = b""
else:
return
# Start byte (hex "DB") has been detected.
elif self._state == self.STATE_STARTED:
self._state = self.STATE_HAS_SYSTEM_TITLE_LENGTH
self._system_title_length = int(hex_input, 16)
self._buffer_length = self._buffer_length + 1
self._next_state = 2 + self._system_title_length # start bytes + system title length
# Length of system title has been read.
elif self._state == self.STATE_HAS_SYSTEM_TITLE_LENGTH:
if self._buffer_length > self._next_state:
self._system_title += hex_input
self._state = self.STATE_HAS_SYSTEM_TITLE
self._next_state = self._next_state + 2 # read two more bytes
else:
self._system_title += hex_input
# System title has been read.
elif self._state == self.STATE_HAS_SYSTEM_TITLE:
if hex_input == b'82':
self._next_state = self._next_state + 1
self._state = self.STATE_HAS_SYSTEM_TITLE_SUFFIX # Ignore separator byte
else:
print("ERROR, expected 0x82 separator byte not found, dropping frame")
self._state = self.STATE_IGNORING
# Additional byte after the system title has been read.
elif self._state == self.STATE_HAS_SYSTEM_TITLE_SUFFIX:
if self._buffer_length > self._next_state:
self._data_length_bytes += hex_input
self._data_length = int(self._data_length_bytes, 16)
self._state = self.STATE_HAS_DATA_LENGTH
else:
self._data_length_bytes += hex_input
# Length of remaining data has been read.
elif self._state == self.STATE_HAS_DATA_LENGTH:
self._state = self.STATE_HAS_SEPARATOR # Ignore separator byte
self._next_state = self._next_state + 1 + 4 # separator byte + 4 bytes for framecounter
# Additional byte after the remaining data length has been read.
elif self._state == self.STATE_HAS_SEPARATOR:
if self._buffer_length > self._next_state:
self._frame_counter += hex_input
print("Framecounter")
print(self._frame_counter)
self._state = self.STATE_HAS_FRAME_COUNTER
self._next_state = self._next_state + self._data_length - 17
else:
self._frame_counter += hex_input
# Frame counter has been read.
elif self._state == self.STATE_HAS_FRAME_COUNTER:
if self._buffer_length > self._next_state:
self._payload += hex_input
self._state = self.STATE_HAS_PAYLOAD
self._next_state = self._next_state + 12
else:
self._payload += hex_input
# Payload has been read.
elif self._state == self.STATE_HAS_PAYLOAD:
# All input has been read. After this, we switch back to STATE_IGNORING and wait for a new start byte.
if self._buffer_length > self._next_state:
self._gcm_tag += hex_input
self._state = self.STATE_DONE
else:
self._gcm_tag += hex_input
self._buffer += hex_input
self._buffer_length = self._buffer_length + 1
if self._state == self.STATE_DONE:
# print(self._buffer)
self.analyze()
self._state = self.STATE_IGNORING
# Once we have a full encrypted "telegram", put everything together for decryption.
def analyze(self):
key = binascii.unhexlify(self._args.key)
additional_data = binascii.unhexlify(self._args.aad)
iv = binascii.unhexlify(self._system_title + self._frame_counter)
payload = binascii.unhexlify(self._payload)
gcm_tag = binascii.unhexlify(self._gcm_tag)
try:
decryption = self.decrypt(
key,
additional_data,
iv,
payload,
gcm_tag
)
if has_dsmr_parser and self._args.parse:
try:
parser = TelegramParser(telegram_specifications.V5)
telegram = parser.parse(decryption.decode())
for key in telegram:
print("%s: %s" % (dsmr_parser.obis_name_mapping.EN[key], telegram[key]))
except:
print("ERROR: Cannot parse DSMR Telegram")
print(decryption)
else:
print(decryption)
if self._args.serial_output_port:
self.write_to_serial_port(decryption)
except InvalidTag:
print("ERROR: Invalid Tag.")
# Do the actual decryption (AES-GCM)
def decrypt(self, key, additional_data, iv, payload, gcm_tag):
decryptor = Cipher(
algorithms.AES(key),
modes.GCM(iv, gcm_tag, 12),
backend=default_backend()
).decryptor()
decryptor.authenticate_additional_data(additional_data)
return decryptor.update(payload) + decryptor.finalize()
# Write the decrypted data to a serial port (e.g. one created with socat)
def write_to_serial_port(self, decryption):
ser = serial.Serial(
port=self._args.serial_output_port,
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
)
ser.write(decryption)
ser.close()
if __name__ == '__main__':
smarty_proxy = SmartyProxy()
smarty_proxy.main()