WIP: reading of picseal jpg file...

This commit is contained in:
JohnE 2016-12-03 19:49:01 -08:00
parent f114f0ed61
commit 5fe92beedb
5 changed files with 69 additions and 32 deletions

View File

@ -18,10 +18,9 @@ class Signature:
self.genKeys()
#
def genSig(self, hshh):
def genSig(self):
signer = PKCS1_v1_5.new(self.key_data)
self.sig_data = signer.sign(hshh)
return self.sig_data
self.sig_data = signer.sign(self.hh)
#
def verifySig(self, hshh, bin_sig):
@ -87,14 +86,20 @@ class Signature:
#
def _test():
#logging.basicConfig(level=logging.DEBUG)
msg = b'Hieee, this is a test =)'
sig = Signature()
sig_data = sig.genSig(msg)
print("Signature created")
print("sig_data=={}".format(sig_data))
sig.hh.update(msg)
sig.hash_data = sig.hh.digest()
sig.genSig()
print("Hash created:\n{}".format(sig.hash_data))
print("Size=={}".format(str(len(sig.hash_data))))
print("Signature created:\n{}".format(sig.sig_data))
print("Size=={}".format(str(len(sig.sig_data))))
isVerified = sig.verifySig(msg, sig_data)
isVerified = sig.verifySig(sig.hh, sig.sig_data)
print("isVerified=={}".format(str(isVerified)))

View File

@ -49,7 +49,8 @@ class JpgBin:
self.prev_mstr = "DUH!"
self.prev_imgData = False
self.jpg_fp = JpgFingerprint()
self.jpg_fp = JpgFingerprint()
self.picseal = None
#
# check for JPG file type marker
@ -146,17 +147,20 @@ class JpgBin:
#
#
def markerAppData(self, marker_hex):
seek = True
self.__addPrevMarkerData(marker_hex, "APP ", False)
if (0xffef == marker_hex):
pass
rec_len = self.__calcSeekBytes()
logging.info("length=={}".format(str(rec_len)))
if (self.__processPicSeal()):
seek = False
if (seek):
(rec_len,) = self.__calcSeekBytes()
logging.info("length=={}".format(str(rec_len)))
#
def markerComment(self, marker_hex):
self.__addPrevMarkerData(marker_hex, "COM ", False)
rec_len = self.__calcSeekBytes()
self.__calcSeekBytes()
#
# Image Data
@ -175,7 +179,7 @@ class JpgBin:
#
def markerDQT(self, marker_hex):
self.__addPrevMarkerData(marker_hex, "DQT ")
rec_len = self.__calcSeekBytes()
self.__calcSeekBytes()
#
def markerDRI(self, marker_hex):
@ -185,16 +189,16 @@ class JpgBin:
#
def markerSOF0(self, marker_hex):
self.__addPrevMarkerData(marker_hex, "SOF0")
rec_len = self.__calcSeekBytes()
self.__calcSeekBytes()
#
def markerSOF2(self, marker_hex):
self.__addPrevMarkerData(marker_hex, "SOF2")
rec_len = self.__calcSeekBytes()
self.__calcSeekBytes()
def markerDHT(self, marker_hex):
self.__addPrevMarkerData(marker_hex, "DHT ")
rec_len = self.__calcSeekBytes()
self.__calcSeekBytes()
#
# end of file marker is never added to the marker array list (same as start of image marker)
@ -235,15 +239,17 @@ class JpgBin:
# move the index 2 bytes, then read the 2 bytes to get the length
#
def __calcSeekBytes(self):
prev_buf = None
self.data_idx += 2
(rec_len,) = struct.unpack('>H', self.data_buf[self.data_idx:self.data_idx+2])
remain_bytes = self.data_len - self.data_idx
if (rec_len >= remain_bytes):
prev_buf = self.data_buf[self.data_idx:]
rec_diff = rec_len - remain_bytes
self.__seekBytes(rec_diff)
else:
self.data_idx += rec_len
return rec_len
return rec_len, prev_buf
#
def __seekBytes(self, num_bytes):
@ -252,6 +258,31 @@ class JpgBin:
logging.debug("SEEK: seek=={}, cur_loc=={}".format(num_bytes, pos))
return pos
#
def __getBuf(self, buf_size):
#
# [app_rec_header:2|len:2|picseal_header:7]
#
def __processPicSeal(self):
self.picseal = JpgPicSeal()
rec_hdr = 4
ps_hdr_size = rec_hdr+len(JpgPicSeal.picseal_marker)
remain_buf = self.data_len-(self.data_idx+ps_hdr_size)
if (remain_buf > ps_hdr_size):
if (self.picseal.isPicSeal(self.data_buf[self.data_idx+rec_hdr:self.data_idx+ps_hdr_size])):
#
# calculate size, check buffer, maybe read more bytes from file
#
print("*** *** GOT HERE MOFO *** ***")
self.picseal.deserialize(buf)
return True
return False
#
def printMarkerImg(self):
return self.jpg_fp.printImgMarkers()

View File

@ -5,8 +5,8 @@
# Has: JPG fingerprint
#
# Big-endian
# Pub: [picseal:7|type:1|hash:64|pubkey:550]
# Pvt: [picseal:7|type:1|hash:64|pvtkey:2347]
# Pub: [picseal:7|type:1|sig:512|pubkey:550]
# Pvt: [picseal:7|type:1|sig:512|pvtkey:2347]
#
import struct
import logging
@ -19,24 +19,26 @@ class JpgPicSeal:
picseal_marker = b'\x70\x69\x63\x73\x65\x61\x6C'
def __init__(self, sig):
def __init__(self, sig=None):
self.sig = sig
self.sig_data = None
#
# READ
#
def isPicSeal(self, buf):
if (buf == JpgPicSeal.picseal_marker):
return True
return False
#
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 *** ***")
# read type 0x01 is public key, 0x02 private key
buf = fhr.read(1)
pubpvt = struct.unpack('>B', buf)
buf = fhr.read(64)
self.sig_data = fhr.read(512)
if (pubpvt == b'\x01'):
#pub
buf = fhr.read(550)

View File

@ -44,9 +44,8 @@ class JpgProc:
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))
self.sig.genSig(img_hash)
return img_hash
self.sig.genSig()
#logging.info("img_hash-size=={}, img_hash=={}".format(len(img_hash), img_hash))
#
def writePicSealJpg(self, fname=None):

View File

@ -16,8 +16,8 @@ write_picseal = False
def processImage(image_fn):
jpg_proc = JpgProc()
jpg_proc.process(image_fn)
jpg_proc.hash()
if (write_picseal):
jpg_proc.hash()
print("Writing PicSeal JPG files...")
jpg_proc.writePicSealJpg()