# -*- coding: UTF-8 -*- import sys import binascii import struct import hashlib from ecdsa import SigningKey, VerifyingKey, BadSignatureError def zigbee_ota_signing(bin_file, priv_pem): # check if valid with open(bin_file, 'rb+') as fp: filedata = fp.read(2*1024*1024) bytesdata = bytearray(filedata) print(len(bytesdata)) # check magic word magicwd = struct.unpack_from("I", bytesdata, 0) if magicwd[0] != 0x0BEEF11E: print("Magic word of OTA header is invalid") fp.close() return headerlen = struct.unpack_from("= totallen: break # update header, add a new tag to fill the signature. tag(2) + length(4) + signature(64) totallen = totallen + 70 struct.pack_into(" \n" \ " zigbee-ota-sign verify \n" if len(sys.argv) < 4: print(helperTexts) else: bin_file=sys.argv[2] priv_pem=sys.argv[3] if sys.argv[1] == "sign": zigbee_ota_signing(bin_file, priv_pem) elif sys.argv[1] == "verify": zigbee_ota_sig_verify(bin_file, priv_pem) else: print(helperTexts)