Exemplo n.º 1
0
    def make_data_region(self, data_config, key_folder, user_data_folder, genKeyHeader, key_to_bytearray, data_region_size, ecc_region_offset):
        patch_reserved_offset = 0x1B80
        patch_reserved_lne = 0x80

        data_region = bytearray(data_region_size)
        data_region_ignore = bytearray(data_region_size)
        for i in range(0, data_region_size):
            data_region_ignore[i] = 0xff

        if data_config['ecc_region']:
            ecc_region_enable = True
        else:
            ecc_region_enable = False

        if 'key' in data_config:
            self.genKeyRegion(data_config['key'], key_folder,
                              data_region, data_region_ignore,
                              genKeyHeader, key_to_bytearray, ecc_region_enable, data_region_size, ecc_region_offset)
        if 'user_data' in data_config:
            self.genUserRegion(
                data_config['user_data'], user_data_folder, data_region, data_region_ignore, ecc_region_enable, data_region_size, ecc_region_offset)
        if 'patch' not in data_config:
            for i in data_region_ignore[patch_reserved_offset:patch_reserved_offset+patch_reserved_lne]:
                if i != 0xff:
                    raise OtpError('region {0:#08x} to {1:#08x} is reserved for patch'.format(
                        patch_reserved_offset, patch_reserved_offset+patch_reserved_lne))

        if ecc_region_enable:
            ecc_byteArray = ECC().do_ecc(data_region)
            insert_bytearray(ecc_byteArray, data_region, ecc_region_offset)

        return data_region, data_region_ignore
Exemplo n.º 2
0
    def genUserRegion(self, data_config, user_data_folder, data_region, data_region_ignore, ecc_region_enable, data_region_size, ecc_region_offset):
        for conf in data_config:
            offset = int(conf['offset'], 16)

            data_bin = self.file_to_bytearray(conf, user_data_folder)
            insert_bytearray(data_bin, data_region, offset)
            self.genDataMask(data_region_ignore, data_bin,
                             offset, ecc_region_enable, data_region_size, ecc_region_offset)
Exemplo n.º 3
0
    def key_to_bytearray_a0(self, key_config, key_folder):
        types = key_config['types']

        if types in ['rsa_pub_oem', 'rsa_pub_soc', 'rsa_pub_aes', 'rsa_priv_soc', 'rsa_priv_aes']:
            rsa_key_file = key_folder + key_config['key_pem']
            if types in ['rsa_pub_oem', 'rsa_pub_soc', 'rsa_pub_aes']:
                insert_key_bin = rsa_key_to_bin(rsa_key_file, 'public')
            else:
                insert_key_bin = rsa_key_to_bin(rsa_key_file, 'private')
        else:
            aes_key_bin = load_file(key_folder + key_config['key_bin'])
            aes_iv_bin = load_file(key_folder + key_config['iv_bin'])
            insert_key_bin = bytearray(aes_key_bin)
            insert_bytearray(bytearray(aes_iv_bin), insert_key_bin, 0x20)

        return insert_key_bin
Exemplo n.º 4
0
    def genKeyRegion(self, key_config, key_folder, data_region, data_region_ignore,
                     genKeyHeader, key_to_bytearray, ecc_region_enable, data_region_size, ecc_region_offset):

        key_header = []
        for conf in key_config:
            key_header.append(genKeyHeader(conf, key_folder))
        key_header[-1] |= 1 << 13
        header_byteArray = bytearray(array.array('I', key_header).tobytes())
        insert_bytearray(header_byteArray, data_region, 0)

        self.genDataMask(data_region_ignore, header_byteArray, 0,
                         ecc_region_enable, data_region_size, ecc_region_offset)

        for conf in key_config:
            offset = int(conf['offset'], 16)
            key_bin = key_to_bytearray(conf, key_folder)
            insert_bytearray(key_bin, data_region, offset)
            self.genDataMask(data_region_ignore, key_bin,
                             offset, ecc_region_enable, data_region_size, ecc_region_offset)
Exemplo n.º 5
0
    def key_to_bytearray_a3_big(self, key_config, key_folder):
        types = key_config['types']

        if types in ['rsa_pub_oem', 'rsa_pub_aes', 'rsa_priv_aes']:
            rsa_key_file = key_folder + key_config['key_pem']
            if types in ['rsa_pub_oem', 'rsa_pub_aes']:
                insert_key_bin = rsa_key_to_bin(
                    rsa_key_file, 'public', order='big')
            else:
                insert_key_bin = rsa_key_to_bin(
                    rsa_key_file, 'private', order='big')
        elif types in ['aes_vault']:
            aes_key_bin = load_file(key_folder + key_config['key_bin'])
            aes_key_bin2 = load_file(key_folder + key_config['key_bin2'])
            insert_key_bin = bytearray(aes_key_bin)
            insert_bytearray(bytearray(aes_key_bin2), insert_key_bin, 0x20)
        else:
            insert_key_bin = load_file(key_folder + key_config['key_bin'])

        return insert_key_bin