#!/usr/bin/env python from struct import pack, unpack import subprocess,os,sys,struct,array from Crypto.PublicKey import RSA from Crypto.Util.number import long_to_bytes KeyType='RSA2048' MagicNum=b'SIMLOCK_MAGIC_S' KeyPath = "SimlocKeys/" KeyIDTable = [0x1011, 0x1012, 0x1013, 0x1014, 0x1015] KeysDirs = ["authstartkey", "authendkey", "simlockey", "imeikey", "reskey"] def GetKeyFiles(fdir): kf = "" fls=os.listdir(fdir) j = 0 for i in fls: if os.path.splitext(i)[1] == ".pem": if kf == "": kf = i j = j + 1 if j > 1: kf = "" return j, kf def get_head_offset(tos_img,tos_len): secpos = tos_img.find(MagicNum) if secpos < 0: print("there is no head!!!") return 0 else: print("Find magic num: " + MagicNum.decode()) return secpos def InsertKey(kpath,kid,img): try: f = open(kpath, 'rb') except IOError: print("Can\'t find key: \""+kpath+"\"") else: k = RSA.importKey(f.read()) f.close Mlen = (k.publickey().size()+7)//8 Nlen = len(long_to_bytes(k.publickey().n)) Elen = len(long_to_bytes(k.publickey().e)) img.write(struct.pack("