udpated hmac code signing - stil not working I guess

This commit is contained in:
DJ2LS 2023-08-07 16:59:59 +02:00
parent 338dbc1d92
commit 52b4478f22
3 changed files with 31 additions and 9 deletions

View file

@ -919,16 +919,17 @@ class DATA:
# check if hmac signing enabled
if TNC.enable_hmac:
hmac_digest = hmac.new(TNC.hmac_salt, data_frame, hashlib.sha256).digest()[:4]
# now check if we have valid hmac signature
if hmac_digest == data_frame_crc_received:
salt_found = helpers.search_hmac_salt(self.dxcallsign, self.mycallsign, data_frame_crc, token_iters=100)
if salt_found:
# hmac digest received
self.arq_process_received_data_frame(data_frame, snr, signed=True)
else:
# hmac signature wrong
self.arq_process_received_data_frame(data_frame, snr, signed=False)
elif data_frame_crc == data_frame_crc_received:
self.arq_process_received_data_frame(data_frame, snr, signed=False)
else:
self.send_data_to_socket_queue(
freedata="tnc-message",
@ -1289,7 +1290,7 @@ class DATA:
tempbuffer = []
self.rpt_request_buffer = []
# Append data frames with n_frames_per_burst to tempbuffer
for n_frame in range(0, n_frames_per_burst):
for n_frame in range(n_frames_per_burst):
arqheader = bytearray()
arqheader[:1] = bytes([FR_TYPE.BURST_01.value + n_frame])
#####arqheader[:1] = bytes([FR_TYPE.BURST_01.value])
@ -1663,7 +1664,7 @@ class DATA:
print(self.rpt_request_buffer)
tempbuffer_rptframes = []
for i in range(0, len(missing_area)):
for i in range(len(missing_area)):
print(missing_area[i])
missing_frames_buffer_position = missing_area[i] - 1
tempbuffer_rptframes.append(self.rpt_request_buffer[missing_frames_buffer_position])
@ -2134,7 +2135,7 @@ class DATA:
return True
return False
arq_transmit
def arq_open_data_channel(
self, mycallsign
) -> bool:

View file

@ -13,6 +13,8 @@ import structlog
import numpy as np
import threading
import mesh
import hashlib
import hmac
log = structlog.get_logger("helpers")
@ -494,7 +496,7 @@ def bool_to_string(state):
def get_hmac_salt(dxcallsign: bytes, mycallsign: bytes):
filename = f"freedata_hmac_tokens_{int(time.time())}_{dxcallsign}_{mycallsign}.txt"
filename = f"freedata_hmac_tokens_{dxcallsign}_{mycallsign}.txt"
try:
with open(filename, "w") as file:
line = file.readlines()
@ -503,6 +505,25 @@ def get_hmac_salt(dxcallsign: bytes, mycallsign: bytes):
except Exception:
return False
def search_hmac_salt(dxcallsign: bytes, mycallsign: bytes, search_token, data_frame, token_iters):
try:
filename = f"freedata_hmac_tokens_{dxcallsign}_{mycallsign}.txt"
with open(filename, "w") as file:
token_list = file.readlines()
token_iters = min(token_iters, len(token_list))
for _ in range(1, token_iters + 1):
key = token_list[len(token_list) - _][:-1]
search_digest = hmac.new(key, data_frame, hashlib.sha256).digest()[:4]
if search_token == search_digest:
return True
return False
except Exception:
return False
def delete_last_line_from_hmac_list(filename):
try:
linearray = []

View file

@ -17,9 +17,9 @@ def create_hmac_salts(dxcallsign: str, mycallsign: str, num_tokens: int = 10000)
"""
try:
# Create and write random strings to a file
with open(f"freedata_hmac_tokens_{int(time.time())}_{dxcallsign}_{mycallsign}.txt", "w") as file:
with open(f"freedata_hmac_tokens_{dxcallsign}_{mycallsign}.txt", "w") as file:
for _ in range(num_tokens):
random_str = np.random.bytes(16).hex()
random_str = np.random.bytes(4).hex()
file.write(random_str + '\n')
except Exception:
print("error creating hmac file")