diff --git a/libs/crypto_sig.py b/libs/crypto_sig.py index d6b2932..d4987f2 100644 --- a/libs/crypto_sig.py +++ b/libs/crypto_sig.py @@ -10,12 +10,11 @@ class Signature: # def __init__(self): - key_data = None - pub_key = None - sig_data = None - hash_data = None - hh = None - self.hh = SHA512.new() + self.key_data = None + self.pub_key = None + self.sig_data = None + self.hash_data = None + self.hh = SHA512.new() self.genKeys() # @@ -37,20 +36,43 @@ class Signature: logging.debug("public key==\n{}".format(self.pub_key.exportKey('PEM'))) logging.debug("private key (and pub, other info)==\n{}".format(self.key_data.exportKey('PEM'))) + # + def importPubKey(self, bin_pubkey): + self.pub_key = RSA.importKey(bin_pubkey) + + + # + def importPvtKey(self, bin_pvtkey): + self.key_data = RSA.importKey(bin_pvtkey) + + # # Private key also includes public key data...SO ANNOYING! # - def getPrivKeyPEM(self): + def getPvtKeyPEM(self): return self.key_data.exportKey('PEM') # def getPubKeyPEM(self): return self.pub_key.exportKey('PEM') + # + def getPvtKeyDER(self): + return self.key_data.exportKey('DER') + + # + def getPubKeyDER(self): + return self.pub_key.exportKey('DER') + # def getSignature(self): return sig_data + # + def getNewHashHandle(self): + self.hh = SHA512.new() + return self.hh + # def hashBin(self, bin_data): hshh = SHA512.new() diff --git a/libs/jpg_bin_w.py b/libs/jpg_bin_w.py index 77686be..9ef88c3 100644 --- a/libs/jpg_bin_w.py +++ b/libs/jpg_bin_w.py @@ -5,7 +5,8 @@ class JpgBinWrite: soi_marker = b'\xff\xd8' eof_marker = b'\xff\xd9' - picseal_marker = b'\xff\xe0' + picseal_marker = b'\xff\xef' + # picseal_marker = b'\xff\xe0' # def __init__(self, jpg_in, jpg_out): @@ -17,8 +18,8 @@ class JpgBinWrite: # def writeJpgPicSealPub(self, crypto_sig, fp): self.__writeJpgHeader() - - # write picseal serialize data + + self.__writeJpgPicSeal(crypto_sig, crypto_sig.getPubKeyDER()) self.__writeJpgImg(fp) @@ -28,10 +29,26 @@ class JpgBinWrite: def writeJpgPicSealPvt(self, crypto_sig, fp): self.__writeJpgHeader() - # write picseal serialize data + self.__writeJpgPicSeal(crypto_sig, crypto_sig.getPvtKeyDER()) self.__writeJpgImg(fp) - + + + # + def __writeJpgPicSeal(self, crypto_sig, keyder): + # must include 2 bytes of length too + size = 2 + size += len(crypto_sig.hash_data) + size += len(keyder) + + # write picseal marker + self.fhw.write(JpgBinWrite.picseal_marker) + # write the size of picseal record + self.fhw.write(size.to_bytes(2, byteorder='big')) + # write hash of image + self.fhw.write(crypto_sig.hash_data) + # write the public key + self.fhw.write(keyder) # def __writeJpgImg(self, fp): diff --git a/libs/jpg_proc.py b/libs/jpg_proc.py index 8e7613e..f133906 100644 --- a/libs/jpg_proc.py +++ b/libs/jpg_proc.py @@ -43,6 +43,7 @@ class JpgProc: # hash the jpg image data self.sig = Signature() img_hash = self.jpg.genHash(self.sig.hh) + self.sig.hash_data = img_hash logging.info("img_hash-size=={}, img_hash=={}".format(len(img_hash), img_hash)) return img_hash