#!/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 #cpath=os.getcwd() #cpath="/home/hesheng.lv/projects/8850/Bin/InsertSimlocKey/" KeyType='RSA2048' MagicNum='SIMLOCK_MAGIC_S' KeyUpDir = "/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": #print "Find key: " + i 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) return secpos def InsertKey(kpath,kid,img): f = open(kpath, 'rb') if f < 0: print ("Can\'t find key: \""+kpath+"\"") return 0 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('%s' % struct.pack("