2021-02-28 04:38:57 +00:00
|
|
|
#
|
2021-11-04 10:28:42 +00:00
|
|
|
# chitu_crypt.py
|
2021-02-28 04:38:57 +00:00
|
|
|
# Customizations for Chitu boards
|
|
|
|
#
|
2021-11-04 10:28:42 +00:00
|
|
|
import pioutil
|
|
|
|
if pioutil.is_pio_build():
|
2022-08-06 05:09:46 +00:00
|
|
|
import struct,uuid,marlin
|
|
|
|
|
|
|
|
board = marlin.env.BoardConfig()
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
def calculate_crc(contents, seed):
|
|
|
|
accumulating_xor_value = seed;
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
for i in range(0, len(contents), 4):
|
|
|
|
value = struct.unpack('<I', contents[ i : i + 4])[0]
|
|
|
|
accumulating_xor_value = accumulating_xor_value ^ value
|
|
|
|
return accumulating_xor_value
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
def xor_block(r0, r1, block_number, block_size, file_key):
|
|
|
|
# This is the loop counter
|
|
|
|
loop_counter = 0x0
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# This is the key length
|
|
|
|
key_length = 0x18
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# This is an initial seed
|
|
|
|
xor_seed = 0x4BAD
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# This is the block counter
|
|
|
|
block_number = xor_seed * block_number
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
#load the xor key from the file
|
|
|
|
r7 = file_key
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
for loop_counter in range(0, block_size):
|
|
|
|
# meant to make sure different bits of the key are used.
|
|
|
|
xor_seed = int(loop_counter / key_length)
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# IP is a scratch register / R12
|
|
|
|
ip = loop_counter - (key_length * xor_seed)
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# xor_seed = (loop_counter * loop_counter) + block_number
|
|
|
|
xor_seed = (loop_counter * loop_counter) + block_number
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# shift the xor_seed left by the bits in IP.
|
|
|
|
xor_seed = xor_seed >> ip
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# load a byte into IP
|
|
|
|
ip = r0[loop_counter]
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# XOR the seed with r7
|
|
|
|
xor_seed = xor_seed ^ r7
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# and then with IP
|
|
|
|
xor_seed = xor_seed ^ ip
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
#Now store the byte back
|
|
|
|
r1[loop_counter] = xor_seed & 0xFF
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
#increment the loop_counter
|
|
|
|
loop_counter = loop_counter + 1
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
def encrypt_file(input, output_file, file_length):
|
|
|
|
input_file = bytearray(input.read())
|
|
|
|
block_size = 0x800
|
|
|
|
key_length = 0x18
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
uid_value = uuid.uuid4()
|
|
|
|
file_key = int(uid_value.hex[0:8], 16)
|
2020-06-10 22:16:22 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
xor_crc = 0xEF3D4323;
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# the input file is exepcted to be in chunks of 0x800
|
|
|
|
# so round the size
|
|
|
|
while len(input_file) % block_size != 0:
|
|
|
|
input_file.extend(b'0x0')
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# write the file header
|
|
|
|
output_file.write(struct.pack(">I", 0x443D2D3F))
|
|
|
|
# encrypt the contents using a known file header key
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# write the file_key
|
|
|
|
output_file.write(struct.pack("<I", file_key))
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
#TODO - how to enforce that the firmware aligns to block boundaries?
|
|
|
|
block_count = int(len(input_file) / block_size)
|
|
|
|
print ("Block Count is ", block_count)
|
|
|
|
for block_number in range(0, block_count):
|
|
|
|
block_offset = (block_number * block_size)
|
|
|
|
block_end = block_offset + block_size
|
|
|
|
block_array = bytearray(input_file[block_offset: block_end])
|
|
|
|
xor_block(block_array, block_array, block_number, block_size, file_key)
|
|
|
|
for n in range (0, block_size):
|
|
|
|
input_file[block_offset + n] = block_array[n]
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# update the expected CRC value.
|
|
|
|
xor_crc = calculate_crc(block_array, xor_crc)
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# write CRC
|
|
|
|
output_file.write(struct.pack("<I", xor_crc))
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# finally, append the encrypted results.
|
|
|
|
output_file.write(input_file)
|
|
|
|
return
|
2019-10-25 22:11:25 +00:00
|
|
|
|
2021-11-04 10:28:42 +00:00
|
|
|
# Encrypt ${PROGNAME}.bin and save it as 'update.cbd'
|
|
|
|
def encrypt(source, target, env):
|
2022-08-01 06:14:58 +00:00
|
|
|
from pathlib import Path
|
2022-08-06 05:09:46 +00:00
|
|
|
|
2022-08-01 06:14:58 +00:00
|
|
|
fwpath = Path(target[0].path)
|
|
|
|
fwsize = fwpath.stat().st_size
|
2022-08-06 05:09:46 +00:00
|
|
|
|
|
|
|
enname = board.get("build.crypt_chitu")
|
|
|
|
enpath = Path(target[0].dir.path)
|
|
|
|
|
2022-08-01 06:14:58 +00:00
|
|
|
fwfile = fwpath.open("rb")
|
2022-08-06 05:09:46 +00:00
|
|
|
enfile = (enpath / enname).open("wb")
|
|
|
|
|
|
|
|
print(f"Encrypting {fwpath} to {enname}")
|
|
|
|
encrypt_file(fwfile, enfile, fwsize)
|
|
|
|
fwfile.close()
|
|
|
|
enfile.close()
|
|
|
|
fwpath.unlink()
|
2022-08-01 06:14:58 +00:00
|
|
|
|
|
|
|
marlin.relocate_firmware("0x08008800")
|
2021-11-04 10:28:42 +00:00
|
|
|
marlin.add_post_action(encrypt);
|