Skip to content

Issue47/fix short name errors #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ indent-after-paren=4
# tab).
indent-string=' '

# Names that are allowed even though they violate standard naming conventions.
good-names=cs

# Maximum number of characters on a single line.
max-line-length=100

Expand Down
4 changes: 2 additions & 2 deletions adafruit_tinylora/adafruit_tinylora.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ def send_packet(self, lora_packet, packet_length, timeout):
):
self._write_u8(pair[0], pair[1])
# fill the FIFO buffer with the LoRa payload
for k in range(packet_length):
self._write_u8(0x00, lora_packet[k])
for packet_index in range(packet_length):
self._write_u8(0x00, lora_packet[packet_index])
# switch RFM to TX operating mode
self._write_u8(_REG_OPERATING_MODE, _MODE_TX)
# wait for TxDone IRQ, poll for timeout.
Expand Down
92 changes: 46 additions & 46 deletions adafruit_tinylora/adafruit_tinylora_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def encrypt_payload(self, data):
incomplete_block_size = len(data) % 16
if incomplete_block_size != 0:
num_blocks += 1
# k = data ptr
k = 0
i = 1
while i <= num_blocks:

data_pointer = 0
block_counter = 1
while block_counter <= num_blocks:
block_a[0] = 0x01
block_a[1] = 0x00
block_a[2] = 0x00
Expand All @@ -87,21 +87,21 @@ def encrypt_payload(self, data):
block_a[12] = 0x00
block_a[13] = 0x00
block_a[14] = 0x00
block_a[15] = i
block_a[15] = block_counter
# calculate S
self._aes_encrypt(block_a, self._app_key)
# check for last block
if i != num_blocks:
for j in range(16):
data[k] ^= block_a[j]
k += 1
if block_counter != num_blocks:
for byte_index in range(16):
data[data_pointer] ^= block_a[byte_index]
data_pointer += 1
else:
if incomplete_block_size == 0:
incomplete_block_size = 16
for j in range(incomplete_block_size):
data[k] ^= block_a[j]
k += 1
i += 1
for byte_index in range(incomplete_block_size):
data[data_pointer] ^= block_a[byte_index]
data_pointer += 1
block_counter += 1

def _aes_encrypt(self, data, key):
"""Performs 9 rounds of AES encryption on data per TinyLoRa spec.
Expand Down Expand Up @@ -155,10 +155,10 @@ def _aes_calculate_key(self, num_round, round_key):
round_const = 0x01
# add round_const calculation
while num_round != 1:
b = round_const & 0x80
byte = round_const & 0x80
round_const <<= 1
round_const &= 0xFF
if b == 0x80:
if byte == 0x80:
round_const ^= 0x1B
num_round -= 1
# Calculate first temp
Expand All @@ -169,10 +169,10 @@ def _aes_calculate_key(self, num_round, round_key):
# XOR tmp_arr[0] wth round_const first
tmp_arr[0] ^= round_const
# then calculate new round key
for i in range(4):
for j in range(4):
round_key[j + (i << 2)] ^= tmp_arr[j]
tmp_arr[j] = round_key[j + (i << 2)]
for row in range(4):
for col in range(4):
round_key[col + (row << 2)] ^= tmp_arr[col]
tmp_arr[col] = round_key[col + (row << 2)]

@staticmethod
def _aes_add_round_key(round_key, state):
Expand Down Expand Up @@ -218,8 +218,8 @@ def _aes_mix_columns(self, state):
"""AES MixColumns Step: Multiplies each column of the state array with xtime.
:param bytearray state: State array.
"""
for i in range(4):
self._mix_single_column(state[i])
for column_index in range(4):
self._mix_single_column(state[column_index])

@staticmethod
def _aes_shift_rows(arr):
Expand Down Expand Up @@ -270,30 +270,30 @@ def calculate_mic(self, lora_packet, lora_packet_length, mic):
# aes encryption on block_b
self._aes_encrypt(block_b, self._network_key)
# copy block_b to old_data
for i in range(16):
old_data[i] = block_b[i]
for byte_index in range(16):
old_data[byte_index] = block_b[byte_index]
block_counter = 1
# calculate until n-1 packet blocks
k = 0 # ptr
data_pointer = 0
while block_counter < num_blocks:
# copy data into array
for i in range(16):
new_data[i] = lora_packet[k]
k += 1
for byte_index in range(16):
new_data[byte_index] = lora_packet[data_pointer]
data_pointer += 1
# XOR new_data with old_data
self._xor_data(new_data, old_data)
# aes encrypt new_data
self._aes_encrypt(new_data, self._network_key)
# copy new_data to old_data
for i in range(16):
old_data[i] = new_data[i]
for byte_index in range(16):
old_data[byte_index] = new_data[byte_index]
# increase block_counter
block_counter += 1
# perform calculation on last block
if incomplete_block_size == 0:
for i in range(16):
new_data[i] = lora_packet[k]
k += 1
for byte_index in range(16):
new_data[byte_index] = lora_packet[data_pointer]
data_pointer += 1
# xor with key 1
self._xor_data(new_data, key_k1)
# xor with old data
Expand All @@ -302,14 +302,14 @@ def calculate_mic(self, lora_packet, lora_packet_length, mic):
self._aes_encrypt(new_data, self._network_key)
else:
# copy the remaining data
for i in range(16):
if i < incomplete_block_size:
new_data[i] = lora_packet[k]
k += 1
if i == incomplete_block_size:
new_data[i] = 0x80
if i > incomplete_block_size:
new_data[i] = 0x00
for byte_index in range(16):
if byte_index < incomplete_block_size:
new_data[byte_index] = lora_packet[data_pointer]
data_pointer += 1
if byte_index == incomplete_block_size:
new_data[byte_index] = 0x80
if byte_index > incomplete_block_size:
new_data[byte_index] = 0x00
# perform xor with key 2
self._xor_data(new_data, key_k2)
# perform xor with old data
Expand Down Expand Up @@ -346,22 +346,22 @@ def _mic_generate_keys(self, key_1, key_2):
@staticmethod
def _shift_left(data):
"""Shifts data bytearray left by 1"""
for i in range(16):
if i < 15:
if (data[i + 1] & 0x80) == 0x80:
for byte_index in range(16):
if byte_index < 15:
if (data[byte_index + 1] & 0x80) == 0x80:
overflow = 1
else:
overflow = 0
else:
overflow = 0
# shift 1b left
data[i] = ((data[i] << 1) + overflow) & 0xFF
data[byte_index] = ((data[byte_index] << 1) + overflow) & 0xFF

@staticmethod
def _xor_data(new_data, old_data):
"""XOR two data arrays
:param bytearray new_data: Calculated data.
:param bytearray old_data: data to be xor'd.
"""
for i in range(16):
new_data[i] ^= old_data[i]
for byte_index in range(16):
new_data[byte_index] ^= old_data[byte_index]