@@ -66,10 +66,10 @@ def encrypt_payload(self, data):
66
66
incomplete_block_size = len (data ) % 16
67
67
if incomplete_block_size != 0 :
68
68
num_blocks += 1
69
- # k = data ptr
70
- k = 0
71
- i = 1
72
- while i <= num_blocks :
69
+
70
+ data_pointer = 0
71
+ block_counter = 1
72
+ while block_counter <= num_blocks :
73
73
block_a [0 ] = 0x01
74
74
block_a [1 ] = 0x00
75
75
block_a [2 ] = 0x00
@@ -87,21 +87,21 @@ def encrypt_payload(self, data):
87
87
block_a [12 ] = 0x00
88
88
block_a [13 ] = 0x00
89
89
block_a [14 ] = 0x00
90
- block_a [15 ] = i
90
+ block_a [15 ] = block_counter
91
91
# calculate S
92
92
self ._aes_encrypt (block_a , self ._app_key )
93
93
# check for last block
94
- if i != num_blocks :
95
- for j in range (16 ):
96
- data [k ] ^= block_a [j ]
97
- k += 1
94
+ if block_counter != num_blocks :
95
+ for byte_index in range (16 ):
96
+ data [data_pointer ] ^= block_a [byte_index ]
97
+ data_pointer += 1
98
98
else :
99
99
if incomplete_block_size == 0 :
100
100
incomplete_block_size = 16
101
- for j in range (incomplete_block_size ):
102
- data [k ] ^= block_a [j ]
103
- k += 1
104
- i += 1
101
+ for byte_index in range (incomplete_block_size ):
102
+ data [data_pointer ] ^= block_a [byte_index ]
103
+ data_pointer += 1
104
+ block_counter += 1
105
105
106
106
def _aes_encrypt (self , data , key ):
107
107
"""Performs 9 rounds of AES encryption on data per TinyLoRa spec.
@@ -155,10 +155,10 @@ def _aes_calculate_key(self, num_round, round_key):
155
155
round_const = 0x01
156
156
# add round_const calculation
157
157
while num_round != 1 :
158
- b = round_const & 0x80
158
+ byte = round_const & 0x80
159
159
round_const <<= 1
160
160
round_const &= 0xFF
161
- if b == 0x80 :
161
+ if byte == 0x80 :
162
162
round_const ^= 0x1B
163
163
num_round -= 1
164
164
# Calculate first temp
@@ -169,10 +169,10 @@ def _aes_calculate_key(self, num_round, round_key):
169
169
# XOR tmp_arr[0] wth round_const first
170
170
tmp_arr [0 ] ^= round_const
171
171
# then calculate new round key
172
- for i in range (4 ):
173
- for j in range (4 ):
174
- round_key [j + (i << 2 )] ^= tmp_arr [j ]
175
- tmp_arr [j ] = round_key [j + (i << 2 )]
172
+ for row in range (4 ):
173
+ for col in range (4 ):
174
+ round_key [col + (row << 2 )] ^= tmp_arr [col ]
175
+ tmp_arr [col ] = round_key [col + (row << 2 )]
176
176
177
177
@staticmethod
178
178
def _aes_add_round_key (round_key , state ):
@@ -218,8 +218,8 @@ def _aes_mix_columns(self, state):
218
218
"""AES MixColumns Step: Multiplies each column of the state array with xtime.
219
219
:param bytearray state: State array.
220
220
"""
221
- for i in range (4 ):
222
- self ._mix_single_column (state [i ])
221
+ for column_index in range (4 ):
222
+ self ._mix_single_column (state [column_index ])
223
223
224
224
@staticmethod
225
225
def _aes_shift_rows (arr ):
@@ -270,30 +270,30 @@ def calculate_mic(self, lora_packet, lora_packet_length, mic):
270
270
# aes encryption on block_b
271
271
self ._aes_encrypt (block_b , self ._network_key )
272
272
# copy block_b to old_data
273
- for i in range (16 ):
274
- old_data [i ] = block_b [i ]
273
+ for byte_index in range (16 ):
274
+ old_data [byte_index ] = block_b [byte_index ]
275
275
block_counter = 1
276
276
# calculate until n-1 packet blocks
277
- k = 0 # ptr
277
+ data_pointer = 0
278
278
while block_counter < num_blocks :
279
279
# copy data into array
280
- for i in range (16 ):
281
- new_data [i ] = lora_packet [k ]
282
- k += 1
280
+ for byte_index in range (16 ):
281
+ new_data [byte_index ] = lora_packet [data_pointer ]
282
+ data_pointer += 1
283
283
# XOR new_data with old_data
284
284
self ._xor_data (new_data , old_data )
285
285
# aes encrypt new_data
286
286
self ._aes_encrypt (new_data , self ._network_key )
287
287
# copy new_data to old_data
288
- for i in range (16 ):
289
- old_data [i ] = new_data [i ]
288
+ for byte_index in range (16 ):
289
+ old_data [byte_index ] = new_data [byte_index ]
290
290
# increase block_counter
291
291
block_counter += 1
292
292
# perform calculation on last block
293
293
if incomplete_block_size == 0 :
294
- for i in range (16 ):
295
- new_data [i ] = lora_packet [k ]
296
- k += 1
294
+ for byte_index in range (16 ):
295
+ new_data [byte_index ] = lora_packet [data_pointer ]
296
+ data_pointer += 1
297
297
# xor with key 1
298
298
self ._xor_data (new_data , key_k1 )
299
299
# xor with old data
@@ -302,14 +302,14 @@ def calculate_mic(self, lora_packet, lora_packet_length, mic):
302
302
self ._aes_encrypt (new_data , self ._network_key )
303
303
else :
304
304
# copy the remaining data
305
- for i in range (16 ):
306
- if i < incomplete_block_size :
307
- new_data [i ] = lora_packet [k ]
308
- k += 1
309
- if i == incomplete_block_size :
310
- new_data [i ] = 0x80
311
- if i > incomplete_block_size :
312
- new_data [i ] = 0x00
305
+ for byte_index in range (16 ):
306
+ if byte_index < incomplete_block_size :
307
+ new_data [byte_index ] = lora_packet [data_pointer ]
308
+ data_pointer += 1
309
+ if byte_index == incomplete_block_size :
310
+ new_data [byte_index ] = 0x80
311
+ if byte_index > incomplete_block_size :
312
+ new_data [byte_index ] = 0x00
313
313
# perform xor with key 2
314
314
self ._xor_data (new_data , key_k2 )
315
315
# perform xor with old data
@@ -346,22 +346,22 @@ def _mic_generate_keys(self, key_1, key_2):
346
346
@staticmethod
347
347
def _shift_left (data ):
348
348
"""Shifts data bytearray left by 1"""
349
- for i in range (16 ):
350
- if i < 15 :
351
- if (data [i + 1 ] & 0x80 ) == 0x80 :
349
+ for byte_index in range (16 ):
350
+ if byte_index < 15 :
351
+ if (data [byte_index + 1 ] & 0x80 ) == 0x80 :
352
352
overflow = 1
353
353
else :
354
354
overflow = 0
355
355
else :
356
356
overflow = 0
357
357
# shift 1b left
358
- data [i ] = ((data [i ] << 1 ) + overflow ) & 0xFF
358
+ data [byte_index ] = ((data [byte_index ] << 1 ) + overflow ) & 0xFF
359
359
360
360
@staticmethod
361
361
def _xor_data (new_data , old_data ):
362
362
"""XOR two data arrays
363
363
:param bytearray new_data: Calculated data.
364
364
:param bytearray old_data: data to be xor'd.
365
365
"""
366
- for i in range (16 ):
367
- new_data [i ] ^= old_data [i ]
366
+ for byte_index in range (16 ):
367
+ new_data [byte_index ] ^= old_data [byte_index ]
0 commit comments