From 2388a3f4fdcd807ff3857c962f828232c22a6f16 Mon Sep 17 00:00:00 2001 From: David Buchanan Date: Thu, 21 Feb 2019 15:52:42 +0000 Subject: [PATCH] sept_sign: Python 3 compat (#356) --- sept/sept-secondary/KEYS_template.py | 14 ++++----- sept/sept-secondary/sept_sign.py | 46 +++++++++++++++++----------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/sept/sept-secondary/KEYS_template.py b/sept/sept-secondary/KEYS_template.py index 0b3a4ad9a..38cdcb0c4 100644 --- a/sept/sept-secondary/KEYS_template.py +++ b/sept/sept-secondary/KEYS_template.py @@ -1,7 +1,7 @@ -HOVI_ENC_KEY_PRD = '00000000000000000000000000000000'.decode('hex') -HOVI_ENC_KEY_DEV = '00000000000000000000000000000000'.decode('hex') -HOVI_SIG_KEY_PRD = '00000000000000000000000000000000'.decode('hex') -HOVI_SIG_KEY_DEV = '00000000000000000000000000000000'.decode('hex') -HOVI_KEK_KEY_PRD = '00000000000000000000000000000000'.decode('hex') -HOVI_KEK_KEY_DEV = '00000000000000000000000000000000'.decode('hex') -IV = '00000000000000000000000000000000'.decode('hex') +HOVI_ENC_KEY_PRD = bytearray.fromhex('00000000000000000000000000000000') +HOVI_ENC_KEY_DEV = bytearray.fromhex('00000000000000000000000000000000') +HOVI_SIG_KEY_PRD = bytearray.fromhex('00000000000000000000000000000000') +HOVI_SIG_KEY_DEV = bytearray.fromhex('00000000000000000000000000000000') +HOVI_KEK_KEY_PRD = bytearray.fromhex('00000000000000000000000000000000') +HOVI_KEK_KEY_DEV = bytearray.fromhex('00000000000000000000000000000000') +IV = bytearray.fromhex('00000000000000000000000000000000') diff --git a/sept/sept-secondary/sept_sign.py b/sept/sept-secondary/sept_sign.py index 17d515433..a4dd83a87 100644 --- a/sept/sept-secondary/sept_sign.py +++ b/sept/sept-secondary/sept_sign.py @@ -9,48 +9,57 @@ except ImportError: import KEYS_template as KEYS print('Warning: output will not work on 7.0.0+!') + 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): N = ((N << 1) ^ 0x87) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF else: N = ((N << 1) ^ 0x00) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF - return ('%032x' % N).decode('hex') + return bytearray.fromhex('%032x' % N) + 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): 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: k1 = shift_left_xor_rb(k1) - data += '\x80' - data += '\x00' * ((0x10 - (len(data) & 0xF)) & 0xF) + data += b'\x80' + data += bytearray((0x10 - (len(data) & 0xF)) & 0xF) 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: - 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 + def sign_encrypt_code(code, sig_key, enc_key, iv, desired_mac): # Pad with 0x20 of zeroes. - code += '\x00' * 0x20 + code += bytearray(0x20) code_len = len(code) code_len += 0xFFF code_len &= ~0xFFF - code += '\x00' * (code_len - len(code)) - + code += bytearray(code_len - len(code)) + # Add empty trustzone, warmboot segments. - code += '\x00'* (0x1FE0 - 0x10) - pk11_hdr = 'PK11' + pk('