chitu_crypt.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #
  2. # chitu_crypt.py
  3. # Customizations for Chitu boards
  4. #
  5. import pioutil
  6. if pioutil.is_pio_build():
  7. import struct, uuid, marlin
  8. board = pioutil.env.BoardConfig()
  9. def calculate_crc(contents, seed):
  10. accumulating_xor_value = seed
  11. for i in range(0, len(contents), 4):
  12. value = struct.unpack('<I', contents[ i : i + 4])[0]
  13. accumulating_xor_value = accumulating_xor_value ^ value
  14. return accumulating_xor_value
  15. def xor_block(r0, r1, block_number, block_size, file_key):
  16. # This is the loop counter
  17. loop_counter = 0x0
  18. # This is the key length
  19. key_length = 0x18
  20. # This is an initial seed
  21. xor_seed = 0x4BAD
  22. # This is the block counter
  23. block_number = xor_seed * block_number
  24. #load the xor key from the file
  25. r7 = file_key
  26. for loop_counter in range(0, block_size):
  27. # meant to make sure different bits of the key are used.
  28. xor_seed = loop_counter // key_length
  29. # IP is a scratch register / R12
  30. ip = loop_counter - (key_length * xor_seed)
  31. # xor_seed = (loop_counter * loop_counter) + block_number
  32. xor_seed = (loop_counter * loop_counter) + block_number
  33. # shift the xor_seed left by the bits in IP.
  34. xor_seed = xor_seed >> ip
  35. # load a byte into IP
  36. ip = r0[loop_counter]
  37. # XOR the seed with r7
  38. xor_seed = xor_seed ^ r7
  39. # and then with IP
  40. xor_seed = xor_seed ^ ip
  41. #Now store the byte back
  42. r1[loop_counter] = xor_seed & 0xFF
  43. #increment the loop_counter
  44. loop_counter = loop_counter + 1
  45. def encrypt_file(input, output_file, file_length):
  46. input_file = bytearray(input.read())
  47. block_size = 0x800
  48. key_length = 0x18
  49. uid_value = uuid.uuid4()
  50. file_key = int(uid_value.hex[0:8], 16)
  51. xor_crc = 0xEF3D4323
  52. # the input file is exepcted to be in chunks of 0x800
  53. # so round the size
  54. while len(input_file) % block_size != 0:
  55. input_file.extend(b'0x0')
  56. # write the file header
  57. output_file.write(struct.pack(">I", 0x443D2D3F))
  58. # encrypt the contents using a known file header key
  59. # write the file_key
  60. output_file.write(struct.pack("<I", file_key))
  61. #TODO - how to enforce that the firmware aligns to block boundaries?
  62. block_count = len(input_file) // block_size
  63. print ("Block Count is ", block_count)
  64. for block_number in range(0, block_count):
  65. block_offset = (block_number * block_size)
  66. block_end = block_offset + block_size
  67. block_array = bytearray(input_file[block_offset: block_end])
  68. xor_block(block_array, block_array, block_number, block_size, file_key)
  69. for n in range (0, block_size):
  70. input_file[block_offset + n] = block_array[n]
  71. # update the expected CRC value.
  72. xor_crc = calculate_crc(block_array, xor_crc)
  73. # write CRC
  74. output_file.write(struct.pack("<I", xor_crc))
  75. # finally, append the encrypted results.
  76. output_file.write(input_file)
  77. return
  78. # Encrypt ${PROGNAME}.bin and save it as 'update.cbd'
  79. def encrypt(source, target, env):
  80. from pathlib import Path
  81. fwpath = Path(target[0].path)
  82. fwsize = fwpath.stat().st_size
  83. enname = board.get("build.crypt_chitu")
  84. enpath = Path(target[0].dir.path)
  85. fwfile = fwpath.open("rb")
  86. enfile = (enpath / enname).open("wb")
  87. print(f"Encrypting {fwpath} to {enname}")
  88. encrypt_file(fwfile, enfile, fwsize)
  89. fwfile.close()
  90. enfile.close()
  91. fwpath.unlink()
  92. marlin.relocate_firmware("0x08008800")
  93. marlin.add_post_action(encrypt)