sept_sign: Python 3 compat (#356)

This commit is contained in:
David Buchanan 2019-02-21 15:52:42 +00:00 committed by SciresM
parent cfc9576eaf
commit 2388a3f4fd
2 changed files with 35 additions and 25 deletions

View file

@ -1,7 +1,7 @@
HOVI_ENC_KEY_PRD = '00000000000000000000000000000000'.decode('hex') HOVI_ENC_KEY_PRD = bytearray.fromhex('00000000000000000000000000000000')
HOVI_ENC_KEY_DEV = '00000000000000000000000000000000'.decode('hex') HOVI_ENC_KEY_DEV = bytearray.fromhex('00000000000000000000000000000000')
HOVI_SIG_KEY_PRD = '00000000000000000000000000000000'.decode('hex') HOVI_SIG_KEY_PRD = bytearray.fromhex('00000000000000000000000000000000')
HOVI_SIG_KEY_DEV = '00000000000000000000000000000000'.decode('hex') HOVI_SIG_KEY_DEV = bytearray.fromhex('00000000000000000000000000000000')
HOVI_KEK_KEY_PRD = '00000000000000000000000000000000'.decode('hex') HOVI_KEK_KEY_PRD = bytearray.fromhex('00000000000000000000000000000000')
HOVI_KEK_KEY_DEV = '00000000000000000000000000000000'.decode('hex') HOVI_KEK_KEY_DEV = bytearray.fromhex('00000000000000000000000000000000')
IV = '00000000000000000000000000000000'.decode('hex') IV = bytearray.fromhex('00000000000000000000000000000000')

View file

@ -9,48 +9,57 @@ except ImportError:
import KEYS_template as KEYS import KEYS_template as KEYS
print('Warning: output will not work on 7.0.0+!') print('Warning: output will not work on 7.0.0+!')
def shift_left_xor_rb(s): def shift_left_xor_rb(s):
N = int(s.encode('hex'), 16) if hasattr(int, "from_bytes"):
N = int.from_bytes(s, byteorder="big")
else:
N = int(s.encode('hex'), 16)
if N & (1 << 127): if N & (1 << 127):
N = ((N << 1) ^ 0x87) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF N = ((N << 1) ^ 0x87) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
else: else:
N = ((N << 1) ^ 0x00) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF N = ((N << 1) ^ 0x00) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
return ('%032x' % N).decode('hex') return bytearray.fromhex('%032x' % N)
def sxor(x, y): def sxor(x, y):
return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(x, y)) return bytearray(a^b for a,b in zip(x, y))
def get_last_block_for_desired_mac(key, data, desired_mac): def get_last_block_for_desired_mac(key, data, desired_mac):
assert len(desired_mac) == 0x10 assert len(desired_mac) == 0x10
k1 = shift_left_xor_rb(AES.new(key, AES.MODE_ECB).encrypt('\x00'*0x10)) k1 = shift_left_xor_rb(AES.new(key, AES.MODE_ECB).encrypt(bytearray(0x10)))
if len(data) & 0xF: if len(data) & 0xF:
k1 = shift_left_xor_rb(k1) k1 = shift_left_xor_rb(k1)
data += '\x80' data += b'\x80'
data += '\x00' * ((0x10 - (len(data) & 0xF)) & 0xF) data += bytearray((0x10 - (len(data) & 0xF)) & 0xF)
num_blocks = (len(data) + 0xF) >> 4 num_blocks = (len(data) + 0xF) >> 4
last_block = sxor(AES.new(key, AES.MODE_ECB).decrypt(desired_mac), k1) last_block = sxor(bytearray(AES.new(key, AES.MODE_ECB).decrypt(desired_mac)), bytearray(k1))
if len(data) > 0x0: if len(data) > 0x0:
last_block = sxor(last_block, AES.new(key, AES.MODE_CBC, '\x00'*0x10).encrypt(data)[-0x10:]) last_block = sxor(last_block, bytearray(AES.new(key, AES.MODE_CBC, bytearray(0x10)).encrypt(data)[-0x10:]))
return last_block return last_block
def sign_encrypt_code(code, sig_key, enc_key, iv, desired_mac): def sign_encrypt_code(code, sig_key, enc_key, iv, desired_mac):
# Pad with 0x20 of zeroes. # Pad with 0x20 of zeroes.
code += '\x00' * 0x20 code += bytearray(0x20)
code_len = len(code) code_len = len(code)
code_len += 0xFFF code_len += 0xFFF
code_len &= ~0xFFF code_len &= ~0xFFF
code += '\x00' * (code_len - len(code)) code += bytearray(code_len - len(code))
# Add empty trustzone, warmboot segments. # Add empty trustzone, warmboot segments.
code += '\x00'* (0x1FE0 - 0x10) code += bytearray(0x1FE0 - 0x10)
pk11_hdr = 'PK11' + pk('<IIIIIII', 0x1000, 0, 0, code_len - 0x20, 0, 0x1000, 0) pk11_hdr = b'PK11' + pk('<IIIIIII', 0x1000, 0, 0, code_len - 0x20, 0, 0x1000, 0)
pk11 = pk11_hdr + code pk11 = pk11_hdr + code
enc_pk11 = AES.new(enc_key, AES.MODE_CBC, iv).encrypt(pk11) enc_pk11 = AES.new(enc_key, AES.MODE_CBC, iv).encrypt(pk11)
enc_pk11 = pk('<IIII', len(pk11) + 0x10, 0, 0, 0) + iv + enc_pk11 enc_pk11 = pk('<IIII', len(pk11) + 0x10, 0, 0, 0) + iv + enc_pk11
enc_pk11 += get_last_block_for_desired_mac(sig_key, enc_pk11, desired_mac) enc_pk11 += get_last_block_for_desired_mac(sig_key, enc_pk11, desired_mac)
enc_pk11 += CMAC.new(sig_key, enc_pk11, AES).digest() enc_pk11 += CMAC.new(sig_key, enc_pk11, AES).digest()
return enc_pk11 return enc_pk11
def main(argc, argv): def main(argc, argv):
if argc != 3: if argc != 3:
print('Usage: %s input output' % argv[0]) print('Usage: %s input output' % argv[0])
@ -58,11 +67,12 @@ def main(argc, argv):
with open(argv[1], 'rb') as f: with open(argv[1], 'rb') as f:
code = f.read() code = f.read()
if len(code) & 0xF: if len(code) & 0xF:
code += '\x00'*(0x10 - (len(code) & 0xF)) code += bytearray(0x10 - (len(code) & 0xF))
# TODO: Support dev unit crypto # TODO: Support dev unit crypto
with open(argv[2], 'wb') as f: with open(argv[2], 'wb') as f:
f.write(sign_encrypt_code(code, KEYS.HOVI_SIG_KEY_PRD, KEYS.HOVI_ENC_KEY_PRD, KEYS.IV, 'THANKS_NVIDIA_<3')) f.write(sign_encrypt_code(code, KEYS.HOVI_SIG_KEY_PRD, KEYS.HOVI_ENC_KEY_PRD, KEYS.IV, b'THANKS_NVIDIA_<3'))
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main(len(sys.argv), sys.argv)) sys.exit(main(len(sys.argv), sys.argv))