diff --git a/libs/jpg_bin.py b/libs/jpg_bin.py index e071c30..befb82c 100644 --- a/libs/jpg_bin.py +++ b/libs/jpg_bin.py @@ -21,6 +21,7 @@ import re import struct import logging from libs.jpg_fp import JpgFingerprint +from libs.jpg_picseal import JpgPicSeal # class JpgBin: @@ -29,9 +30,6 @@ class JpgBin: soi_marker = b'\xff\xd8' eof_marker = b'\xff\xd9' - app15_marker = b'\xff\xef' - # ascii code for "picseal" - picseal_marker = b'\x70\x69\x63\x73\x65\x61\x6C' def __init__(self): @@ -149,6 +147,9 @@ class JpgBin: # def markerAppData(self, marker_hex): self.__addPrevMarkerData(marker_hex, "APP ", False) + if (0xffef == marker_hex): + pass + rec_len = self.__calcSeekBytes() logging.info("length=={}".format(str(rec_len))) diff --git a/libs/jpg_bin_w.py b/libs/jpg_bin_w.py index f62b17e..440fb0d 100644 --- a/libs/jpg_bin_w.py +++ b/libs/jpg_bin_w.py @@ -2,6 +2,7 @@ # # from libs.jpg_bin import JpgBin +from libs.jpg_picseal import JpgPicSeal class JpgBinWrite: @@ -18,7 +19,8 @@ class JpgBinWrite: def writeJpgPicSealPub(self, crypto_sig, fp): self.__writeJpgHeader() - self.__writeJpgPicSeal(crypto_sig, crypto_sig.getPubKeyDER()) + ps = JpgPicSeal(crypto_sig) + ps.writePub(self.fhw) self.__writeJpgImg(fp) @@ -28,29 +30,12 @@ class JpgBinWrite: def writeJpgPicSealPvt(self, crypto_sig, fp): self.__writeJpgHeader() - self.__writeJpgPicSeal(crypto_sig, crypto_sig.getPvtKeyDER()) + ps = JpgPicSeal(crypto_sig) + ps.writePvt(self.fhw) 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 app marker - self.fhw.write(JpgBin.app15_marker) - # write picseal marker - self.fhw.write(JpgBin.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): self.__writeJpgMetadata(fp.markers_meta) diff --git a/libs/jpg_picseal.py b/libs/jpg_picseal.py index 10fa893..bac099d 100644 --- a/libs/jpg_picseal.py +++ b/libs/jpg_picseal.py @@ -4,19 +4,104 @@ # Data: public key, private key, image hash, image signature # Has: JPG fingerprint # +# Big-endian +# Pub: [picseal:7|type:1|hash:64|pubkey:550] +# Pvt: [picseal:7|type:1|hash:64|pvtkey:2347] # +import struct +import logging +from Crypto.PublicKey import RSA + class JpgPicSeal: - def __init_(self, sig): - self.sig = None - pass + app15_marker = b'\xff\xef' + # ascii code for "picseal" + picseal_marker = b'\x70\x69\x63\x73\x65\x61\x6C' + + + def __init__(self, sig): + self.sig = sig + + + # + # READ + # + + # + def deserialize(self, fhr): + buf = fhr.read(7) + ps_marker = struct.unpack('>HHHB', buf) + if (JpgPicSeal.picseal_marker == ps_marker): + logging.info("*** *** matched picseal marker *** ***") + buf = fhr.read(1) + pubpvt = struct.unpack('>B', buf) + buf = fhr.read(64) + + if (pubpvt == b'\x01'): + #pub + buf = fhr.read(550) + self.sig.importPubKey(buf) + else: + #pvt + buf = fhr.read(2347) + self.sig.importPvtKey(buf) + + + + def readPub(self): + pass + + def readPvt(self): + pass + + + + # + # WRITE + # # def serilize(self, fname): - pass + pass + # + # input is the Crypto Sig class + # + def writePub(self, fhw): + self.__writeData(fhw, self.sig.sig_data, self.sig.getPubKeyDER(), b'\x01') + + # + # input is the Crypto Sig class + # + def writePvt(self, fhw): + self.__writeData(fhw, self.sig.sig_data, self.sig.getPvtKeyDER(), b'\x02') # - def deserialize(self, fname): - pass + def __writeData(self, fhw, sig_data, keyder, pubpvt): + # must include 2 bytes of length too + size = 2 + size += len(JpgPicSeal.picseal_marker) + size += len(sig_data) + size += len(keyder) + + logging.debug("picseal marker length: {}".format(str(len(JpgPicSeal.picseal_marker)))) + logging.debug("hash size: {}".format(str(len(sig_data)))) + logging.debug("key size: {}".format(str(len(keyder)))) + + # + # write header + # write app marker + fhw.write(JpgPicSeal.app15_marker) + # write the size of picseal record + fhw.write(size.to_bytes(2, byteorder='big')) + + # + # write picseal data + # write picseal marker + fhw.write(JpgPicSeal.picseal_marker) + fhw.write(pubpvt) + # write hash of image + fhw.write(sig_data) + # write the public key data + fhw.write(keyder) diff --git a/libs/jpg_proc.py b/libs/jpg_proc.py index f133906..7606440 100644 --- a/libs/jpg_proc.py +++ b/libs/jpg_proc.py @@ -45,6 +45,7 @@ class JpgProc: 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)) + self.sig.genSig(img_hash) return img_hash # diff --git a/picseal.py b/picseal.py index 2bdab4c..87f29bb 100644 --- a/picseal.py +++ b/picseal.py @@ -3,19 +3,13 @@ # import argparse import logging -from shutil import copyfile -from libs.toolbox import Toolbox from libs.jpg_proc import JpgProc -printall = False -printmeta = False -printimage = False +printall = False +printmeta = False +printimage = False write_picseal = False -def main(): - parseArgs() - - # hash the image binary data only (not metadata) # create new pub keys, sign hash # export signature & public key to a new image file @@ -41,7 +35,12 @@ def printImageInfo(jpg_bin): print( jpg_bin.printMarkerMeta()) + +def main(): + parseArgs() + def parseArgs(): + print print("***** ***** ***** *****") print(" ** Pic * Seal ** ") print("***** ***** ***** *****\n") diff --git a/test/signature_test.py b/test/signature_test.py index 502bd47..1d50fc8 100644 --- a/test/signature_test.py +++ b/test/signature_test.py @@ -2,7 +2,7 @@ # Test Signature Class # # run from root of project -from libs.crypto_pub import Signature +from libs.crypto_sig import Signature msg = b'Hieee, this is a test =)'