picseal_pub/libs/jpg_picseal.py

119 lines
2.5 KiB
Python

#
# Purpose: Write 2 new JPG image files => PicSeal JPG Files
#
# Data: public key, private key, image hash, image signature
# Has: JPG fingerprint
#
# Big-endian
# Pub: [app15:2|size:2|'picseal':7|type:1|sig:512|pubkey:550]
# Pvt: [app15:2|size:2|'picseal':7|type:1|sig:512|pvtkey:2347]
#
import struct
import logging
from Crypto.PublicKey import RSA
from libs.crypto_sig import Signature
class JpgPicSeal:
app15_marker = b'\xff\xef'
# ascii code for "picseal"
picseal_marker = b'\x70\x69\x63\x73\x65\x61\x6C'
pub_marker = b'\x01'
pvt_marker = b'\x02'
def __init__(self):
pass
#
# READ
#
def isPicSeal(buf):
if (buf == JpgPicSeal.picseal_marker):
return True
return False
#
# [size:2|picseal:7|type:1|sig:512|key:550] == 1072
#
def deserialize(buf):
sig = Signature()
retval = False
# read type 0x01 is public key, 0x02 private key
try:
if (buf[9] == ord(JpgPicSeal.pub_marker)):
sig.importPubKey(buf[522:])
print("*** *** ***")
print("*** Public Key Import Sucessful")
print("*** *** ***")
else:
sig.importPvtKey(buf[522:])
print("*** *** ***")
print("*** Private Key Import Sucessful")
print("*** *** ***")
return sig
except Exception as ex:
logging.debug(ex)
return None
#
def readPub(self):
pass
def readPvt(self):
pass
#
# WRITE
#
#
def serilize(fname):
pass
#
# input is the Crypto Sig class
#
def writePub(fhw, sig):
JpgPicSeal.__writeData(fhw, sig.sig_data, sig.getPubKeyDER(), JpgPicSeal.pub_marker)
#
# input is the Crypto Sig class
#
def writePvt(fhw, sig):
JpgPicSeal.__writeData(fhw, sig.sig_data, sig.getPvtKeyDER(), JpgPicSeal.pvt_marker)
#
def __writeData(fhw, sig_data, keyder, pubpvt):
# must include 2 bytes for length too, plus 1 for the key type
size = 3
size += len(JpgPicSeal.picseal_marker)
size += len(sig_data)
size += len(keyder)
logging.debug("3+{}+{}+{}=={} | picseal, sig, key".format(str(len(JpgPicSeal.picseal_marker)), str(len(sig_data)), str(len(keyder)), str(size) ))
#
# 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 signature of image
fhw.write(sig_data)
# write the key data
fhw.write(keyder)