Skip to content

Commit 8f25b30

Browse files
Merge pull request #49 from awordforthat/issue47/fix-short-name-errors
Issue47/fix short name errors
2 parents 6698610 + 1e53c32 commit 8f25b30

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

.pylintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ indent-after-paren=4
219219
# tab).
220220
indent-string=' '
221221

222+
# Names that are allowed even though they violate standard naming conventions.
223+
good-names=cs
224+
222225
# Maximum number of characters on a single line.
223226
max-line-length=100
224227

adafruit_tinylora/adafruit_tinylora.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ def send_packet(self, lora_packet, packet_length, timeout):
297297
):
298298
self._write_u8(pair[0], pair[1])
299299
# fill the FIFO buffer with the LoRa payload
300-
for k in range(packet_length):
301-
self._write_u8(0x00, lora_packet[k])
300+
for packet_index in range(packet_length):
301+
self._write_u8(0x00, lora_packet[packet_index])
302302
# switch RFM to TX operating mode
303303
self._write_u8(_REG_OPERATING_MODE, _MODE_TX)
304304
# wait for TxDone IRQ, poll for timeout.

adafruit_tinylora/adafruit_tinylora_encryption.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def encrypt_payload(self, data):
6666
incomplete_block_size = len(data) % 16
6767
if incomplete_block_size != 0:
6868
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:
7373
block_a[0] = 0x01
7474
block_a[1] = 0x00
7575
block_a[2] = 0x00
@@ -87,21 +87,21 @@ def encrypt_payload(self, data):
8787
block_a[12] = 0x00
8888
block_a[13] = 0x00
8989
block_a[14] = 0x00
90-
block_a[15] = i
90+
block_a[15] = block_counter
9191
# calculate S
9292
self._aes_encrypt(block_a, self._app_key)
9393
# 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
9898
else:
9999
if incomplete_block_size == 0:
100100
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
105105

106106
def _aes_encrypt(self, data, key):
107107
"""Performs 9 rounds of AES encryption on data per TinyLoRa spec.
@@ -155,10 +155,10 @@ def _aes_calculate_key(self, num_round, round_key):
155155
round_const = 0x01
156156
# add round_const calculation
157157
while num_round != 1:
158-
b = round_const & 0x80
158+
byte = round_const & 0x80
159159
round_const <<= 1
160160
round_const &= 0xFF
161-
if b == 0x80:
161+
if byte == 0x80:
162162
round_const ^= 0x1B
163163
num_round -= 1
164164
# Calculate first temp
@@ -169,10 +169,10 @@ def _aes_calculate_key(self, num_round, round_key):
169169
# XOR tmp_arr[0] wth round_const first
170170
tmp_arr[0] ^= round_const
171171
# 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)]
176176

177177
@staticmethod
178178
def _aes_add_round_key(round_key, state):
@@ -218,8 +218,8 @@ def _aes_mix_columns(self, state):
218218
"""AES MixColumns Step: Multiplies each column of the state array with xtime.
219219
:param bytearray state: State array.
220220
"""
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])
223223

224224
@staticmethod
225225
def _aes_shift_rows(arr):
@@ -270,30 +270,30 @@ def calculate_mic(self, lora_packet, lora_packet_length, mic):
270270
# aes encryption on block_b
271271
self._aes_encrypt(block_b, self._network_key)
272272
# 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]
275275
block_counter = 1
276276
# calculate until n-1 packet blocks
277-
k = 0 # ptr
277+
data_pointer = 0
278278
while block_counter < num_blocks:
279279
# 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
283283
# XOR new_data with old_data
284284
self._xor_data(new_data, old_data)
285285
# aes encrypt new_data
286286
self._aes_encrypt(new_data, self._network_key)
287287
# 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]
290290
# increase block_counter
291291
block_counter += 1
292292
# perform calculation on last block
293293
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
297297
# xor with key 1
298298
self._xor_data(new_data, key_k1)
299299
# xor with old data
@@ -302,14 +302,14 @@ def calculate_mic(self, lora_packet, lora_packet_length, mic):
302302
self._aes_encrypt(new_data, self._network_key)
303303
else:
304304
# 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
313313
# perform xor with key 2
314314
self._xor_data(new_data, key_k2)
315315
# perform xor with old data
@@ -346,22 +346,22 @@ def _mic_generate_keys(self, key_1, key_2):
346346
@staticmethod
347347
def _shift_left(data):
348348
"""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:
352352
overflow = 1
353353
else:
354354
overflow = 0
355355
else:
356356
overflow = 0
357357
# shift 1b left
358-
data[i] = ((data[i] << 1) + overflow) & 0xFF
358+
data[byte_index] = ((data[byte_index] << 1) + overflow) & 0xFF
359359

360360
@staticmethod
361361
def _xor_data(new_data, old_data):
362362
"""XOR two data arrays
363363
:param bytearray new_data: Calculated data.
364364
:param bytearray old_data: data to be xor'd.
365365
"""
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

Comments
 (0)