__all__ = [ "hdr_make", "hdr_read", "hdr_fmt", "hdr_fmt_pretty"
- , "I2N_HDR_SIZE" , "AES_GCM_IV_CNT_DATA", "AES_GCM_IV_CNT_INFOFILE"
+ , "PDTCRYPT_HDR_SIZE" , "AES_GCM_IV_CNT_DATA", "AES_GCM_IV_CNT_INFOFILE"
]
## constants
###############################################################################
-I2N_HDR_MAGIC = b"PDTCRYPT"
+PDTCRYPT_HDR_MAGIC = b"PDTCRYPT"
-I2N_HDR_SIZE_MAGIC = 8 # 8
-I2N_HDR_SIZE_VERSION = 2 # 10
-I2N_HDR_SIZE_PARAMVERSION = 2 # 12
-I2N_HDR_SIZE_NACL = 16 # 28
-I2N_HDR_SIZE_IV = 12 # 40
-I2N_HDR_SIZE_CTSIZE = 8 # 48
-I2N_HDR_SIZE_TAG = 16 # 64 GCM auth tag
+PDTCRYPT_HDR_SIZE_MAGIC = 8 # 8
+PDTCRYPT_HDR_SIZE_VERSION = 2 # 10
+PDTCRYPT_HDR_SIZE_PARAMVERSION = 2 # 12
+PDTCRYPT_HDR_SIZE_NACL = 16 # 28
+PDTCRYPT_HDR_SIZE_IV = 12 # 40
+PDTCRYPT_HDR_SIZE_CTSIZE = 8 # 48
+PDTCRYPT_HDR_SIZE_TAG = 16 # 64 GCM auth tag
-I2N_HDR_SIZE = I2N_HDR_SIZE_MAGIC + I2N_HDR_SIZE_VERSION \
- + I2N_HDR_SIZE_PARAMVERSION + I2N_HDR_SIZE_NACL \
- + I2N_HDR_SIZE_IV + I2N_HDR_SIZE_CTSIZE \
- + I2N_HDR_SIZE_TAG # = 64
+PDTCRYPT_HDR_SIZE = PDTCRYPT_HDR_SIZE_MAGIC + PDTCRYPT_HDR_SIZE_VERSION \
+ + PDTCRYPT_HDR_SIZE_PARAMVERSION + PDTCRYPT_HDR_SIZE_NACL \
+ + PDTCRYPT_HDR_SIZE_IV + PDTCRYPT_HDR_SIZE_CTSIZE \
+ + PDTCRYPT_HDR_SIZE_TAG # = 64
# precalculate offsets since Python can’t do constant folding over names
-HDR_OFF_VERSION = I2N_HDR_SIZE_MAGIC
-HDR_OFF_PARAMVERSION = HDR_OFF_VERSION + I2N_HDR_SIZE_VERSION
-HDR_OFF_NACL = HDR_OFF_PARAMVERSION + I2N_HDR_SIZE_PARAMVERSION
-HDR_OFF_IV = HDR_OFF_NACL + I2N_HDR_SIZE_NACL
-HDR_OFF_CTSIZE = HDR_OFF_IV + I2N_HDR_SIZE_IV
-HDR_OFF_TAG = HDR_OFF_CTSIZE + I2N_HDR_SIZE_CTSIZE
+HDR_OFF_VERSION = PDTCRYPT_HDR_SIZE_MAGIC
+HDR_OFF_PARAMVERSION = HDR_OFF_VERSION + PDTCRYPT_HDR_SIZE_VERSION
+HDR_OFF_NACL = HDR_OFF_PARAMVERSION + PDTCRYPT_HDR_SIZE_PARAMVERSION
+HDR_OFF_IV = HDR_OFF_NACL + PDTCRYPT_HDR_SIZE_NACL
+HDR_OFF_CTSIZE = HDR_OFF_IV + PDTCRYPT_HDR_SIZE_IV
+HDR_OFF_TAG = HDR_OFF_CTSIZE + PDTCRYPT_HDR_SIZE_CTSIZE
FMT_UINT16_LE = "<H"
FMT_UINT64_LE = "<Q"
raise InvalidHeader ("error unpacking header from [%r]: %s"
% (binascii.hexlify (data), str (exn)))
- if mag != I2N_HDR_MAGIC:
+ if mag != PDTCRYPT_HDR_MAGIC:
raise InvalidHeader ("bad magic in header: expected [%s], got [%s]"
- % (I2N_HDR_MAGIC, mag))
+ % (PDTCRYPT_HDR_MAGIC, mag))
return \
{ "version" : version
def hdr_read_stream (instr):
- data = instr.read(I2N_HDR_SIZE)
- if len (data) != I2N_HDR_SIZE:
+ data = instr.read(PDTCRYPT_HDR_SIZE)
+ if len (data) != PDTCRYPT_HDR_SIZE:
raise EndOfFile ("read: expected %d B, received %d B"
- % (I2N_HDR_SIZE, len (data)))
+ % (PDTCRYPT_HDR_SIZE, len (data)))
return hdr_read (data)
def hdr_from_params (version, paramversion, nacl, iv, ctsize, tag):
- buf = bytearray (I2N_HDR_SIZE)
+ buf = bytearray (PDTCRYPT_HDR_SIZE)
bufv = memoryview (buf)
try:
struct.pack_into (FMT_I2N_HDR, bufv, 0,
- I2N_HDR_MAGIC,
+ PDTCRYPT_HDR_MAGIC,
version, paramversion, nacl, iv, ctsize, tag)
except Exception as exn:
return False, "error writing header: %s" % str (exn)
of the object header.
"""
c = reduce (lambda a, c: a + ord(c), s, 0) % 0xFF
- return bytes (bytearray (struct.pack ("B", c)) * I2N_HDR_SIZE)
+ return bytes (bytearray (struct.pack ("B", c)) * PDTCRYPT_HDR_SIZE)
def hdr_make (hdr):
def tag_read_stream (source):
- data = source.read (I2N_TLR_SIZE_TAG)
+ data = source.read (PDTCRYPT_TLR_SIZE_TAG)
return tag_read (data)
return self.stats ["obj"], self.stats ["in"], self.stats ["out"]
-
class Encrypt (Crypto):
curobj = None
noise ("PDT: %d hdr" % tell (ins))
try:
hdr = hdr_read_stream (ins)
- total_read += I2N_HDR_SIZE
+ total_read += PDTCRYPT_HDR_SIZE
except EndOfFile:
- if total_ct + total_obj * I2N_HDR_SIZE != total_read:
+ if total_ct + total_obj * PDTCRYPT_HDR_SIZE != total_read:
raise PDTDecryptionError ("ciphertext processed (%d B) plus "
"overhead (%d × %d B) does not match "
"the number of bytes read (%d )"
- % (total_ct, total_obj, I2N_HDR_SIZE,
+ % (total_ct, total_obj, PDTCRYPT_HDR_SIZE,
total_read))
# the single good exit
return total_read, total_obj, total_ct, total_pt