def decrypt(prx, meta, **kwargs): p = prx_header_8(prx) xorbuf = kirk.kirk7(meta['seed'], meta['key']) # calculate SHA1 of header h = SHA1.new() h.update(xorbuf[:0x14]) h.update(p.vanity_area()) h.update(p.kirk_block()) h.update(p.kirk_metadata()) h.update(p.elf_info()) if h.digest() != p.sha1_hash(): print("bad SHA1") return False # decrypt the kirk header header = xor(p.kirk_block(), xorbuf[0x14:0x84]) header = kirk.kirk7(header, meta['key']) header = xor(header, xorbuf[0x20:]) # prepare the kirk block block = header + p.kirk_metadata() + p.elf_info() + prx[0x150:] # do the decryption return kirk.kirk1(block)
def decrypt(prx, meta, **kwargs): xorbuf = expand_seed(meta['seed'], meta['key']) # check if range contains nonzero if any(x != 0 for x in prx[0xD4:0xD4 + 0x30]): return False p = prx_header_9(prx) print(meta['pubkey']) print(p.prx_ecdsa().hex()) # check ECDSA signature # kirk.kirk11(bytes.fromhex(meta['pubkey']), p.prx_ecdsa( # ), prx[4:0x104] + b'\x00'*0x28 + prx[0x12C:]) h2 = SHA1.new() h2.update(prx[4:0x104] + b'\x00' * 0x28 + prx[0x12C:]) print(h2.hexdigest()) # decrypt the header information p.decrypt_header(meta['key']) # calculate SHA1 of header h = SHA1.new() h.update(p.tag()) h.update(xorbuf[:0x10]) h.update(b'\x00' * 0x58) h.update(p.btcnf_id()) h.update(p.kirk_aes_key()) h.update(p.kirk_cmac_key()) h.update(p.kirk_cmac_header_hash()) h.update(p.kirk_cmac_data_hash()) h.update(p.kirk_metadata()) h.update(p.elf_info()) # sanity check that our SHA1 actually matches if h.digest() != p.sha1_hash(): return False # decrypt the kirk block header = xor(p.kirk_block(), xorbuf[0x10:0x50]) header = kirk.kirk7(header, meta['key']) header = xor(header, xorbuf[0x50:]) # prepare the kirk block block = header + b'\x00' * 0x30 block = set_kirk_cmd_1(block) block = block + p.kirk_metadata() + b'\x00'*0x10 + \ p.elf_info() + prx[0x150:] return kirk.kirk1(block)
def _demangle(self, block): if self._version == 5: block = xor( block, math.ceil(len(block) / 0x10) * bytes.fromhex('D869B895336B633498B9FC3CB7262BD7')[:len(block)]) block = kirk.kirk7(block, 0x55) if self._version == 5: block = xor( block, math.ceil(len(block) / 0x10) * bytes.fromhex('0DA09084AF9EB6E2D294F2AAEF996871')[:len(block)]) return block
def encrypt(prx, meta, id=None): xorbuf = expand_seed(meta['seed'], meta['key']) # encrypt as kirk1 encrypted = kirk.kirk1_encrypt_ecdsa(prx[0x150:], salt=prx[:0x80]) header = xor(encrypted[:0x40], xorbuf[0x50:]) header = kirk.kirk4(header, meta['key']) header = xor(header, xorbuf[0x10:0x50]) # calculate an id if id == None: id = Random.get_random_bytes(16) elif type(id) is str: id = '{:16.16}'.format(id).encode() id = kirk.kirk7(id, meta['key']) # create a prx header prx_header = prx_header_6() prx_header.set_elf_info(prx[:0x80]) prx_header.set_kirk_block(header) prx_header.set_kirk_ecdsa_data_sig_end(encrypted[0x40:0x60]) prx_header.set_kirk_metadata(encrypted[0x70:0x80]) prx_header.set_btcnf_id(id) prx_header.set_tag(prx[0xD0:0xD4]) # calculate SHA1 of header h = SHA1.new() h.update(prx_header.tag()) h.update(xorbuf[:0x10]) h.update(b'\x00' * 0x38) h.update(prx_header.kirk_ecdsa_data_sig_end()) h.update(prx_header.btcnf_id()) h.update(prx_header.kirk_aes_key()) h.update(prx_header.kirk_ecdsa_header_sig()) h.update(prx_header.kirk_ecdsa_data_sig_begin()) h.update(prx_header.kirk_metadata()) h.update(prx_header.elf_info()) prx_header.set_sha1_hash(h.digest()) # encrypt the header and return the complete PRX prx_header.encrypt_header(meta['key']) return prx_header.prx() + encrypted[0x90 + 0x80:]
def decrypt(prx, meta): xorbuf = expand_seed(meta['seed'], meta['key']) # check if range contains nonzero if any(x != 0 for x in prx[0xD4:0xD4 + 0x38]): return False p = prx_header_6(prx) # decrypt the header information p.decrypt_header(meta['key']) # calculate SHA1 of header h = SHA1.new() h.update(p.tag()) h.update(xorbuf[:0x10]) h.update(b'\x00' * 0x38) h.update(p.kirk_ecdsa_data_sig_end()) h.update(p.btcnf_id()) h.update(p.kirk_aes_key()) h.update(p.kirk_ecdsa_header_sig()) h.update(p.kirk_ecdsa_data_sig_begin()) h.update(p.kirk_metadata()) h.update(p.elf_info()) if h.digest() != p.sha1_hash(): print("bad SHA1") return False # decrypt the kirk header header = xor(p.kirk_block(), xorbuf[0x10:0x50]) header = kirk.kirk7(header, meta['key']) header = xor(header, xorbuf[0x50:]) # prepare the kirk block block = header + p.kirk_ecdsa_data_sig_end() + b'\x00' * 0x10 block = set_kirk_cmd_1(block) block = set_kirk_cmd_1_ecdsa(block) block = block + p.kirk_metadata() + b'\x00'*0x10 + \ p.elf_info() + prx[0x150:] # do the decryption return kirk.kirk1(block)
def encrypt(prx, meta, vanity=None, **kwargs): xorbuf = kirk.kirk7(meta['seed'], meta['key']) # encrypt as kirk1 encrypted = kirk.kirk1_encrypt_ecdsa(prx[0x150:], salt=prx[:0x80]) header = xor(encrypted[:0x70], xorbuf[0x20:]) header = kirk.kirk4(header, meta['key']) header = xor(header, xorbuf[0x14:0x84]) # calculate some vanity if vanity == None: vanity = Random.get_random_bytes(0x28) elif type(vanity) is str: vanity = '{:40.40}'.format(vanity).encode() # create a prx header prx_header = prx_header_8() prx_header.set_elf_info(prx[:0x80]) prx_header.set_kirk_block(header) prx_header.set_kirk_metadata(encrypted[0x70:0x90]) prx_header.set_vanity_area(vanity) prx_header.set_tag(prx[0xD0:0xD4]) # calculate SHA1 of header h = SHA1.new() h.update(xorbuf[:0x14]) h.update(prx_header.vanity_area()) h.update(prx_header.kirk_block()) h.update(prx_header.kirk_metadata()) h.update(prx_header.elf_info()) prx_header.set_sha1_hash(h.digest()) # encrypt the header and return the complete PRX return prx_header.prx() + encrypted[0x90 + 0x80:]
def decrypt_header(self, key): self.header = self.header[:0x5C] + kirk.kirk7( self.header[0x5C:0x5C + 0x60], key) + self.header[0x5C + 0x60:]
def expand_seed(seed, key): return kirk.kirk7(b''.join([bytes([x])+seed[1:] for x in range(9)]), key)