unify constant naming I2N_→PDTCRYPT_
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Wed, 5 Apr 2017 06:49:50 +0000 (08:49 +0200)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 7 Aug 2017 12:02:40 +0000 (14:02 +0200)
deltatar/crypto.py
deltatar/tarfile.py

index 398ae9f..4ca4e10 100755 (executable)
@@ -55,7 +55,7 @@ import cryptography
 
 
 __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"
           ]
 
 
@@ -102,28 +102,28 @@ ENCRYPTION_PARAMETERS = \
 ## 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"
@@ -176,9 +176,9 @@ def hdr_read (data):
         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
@@ -191,20 +191,20 @@ def hdr_read (data):
 
 
 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)
@@ -219,7 +219,7 @@ def hdr_make_dummy (s):
     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):
@@ -283,7 +283,7 @@ def tag_read (data):
 
 
 def tag_read_stream (source):
-    data = source.read (I2N_TLR_SIZE_TAG)
+    data = source.read (PDTCRYPT_TLR_SIZE_TAG)
     return tag_read (data)
 
 
@@ -441,7 +441,6 @@ class Crypto (object):
         return self.stats ["obj"], self.stats ["in"], self.stats ["out"]
 
 
-
 class Encrypt (Crypto):
 
     curobj = None
@@ -647,13 +646,13 @@ def depdtcrypt (pw, ins, outs):
                 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
index 730f378..400fc6e 100644 (file)
@@ -29,8 +29,6 @@
 """Read from and write to tar format archives.
 """
 
-I2N_XXX_ENCRYPTION_VERSION = 1
-
 __version__ = "$Revision: 85213 $"
 # $Source$
 
@@ -557,10 +555,10 @@ class _Stream:
             self.__sync ()
             pos0 = self.fileobj.tell ()
             self.fileobj.seek_set (self.lasthdr)
-            dummy = self.fileobj.read (crypto.I2N_HDR_SIZE)
+            dummy = self.fileobj.read (crypto.PDTCRYPT_HDR_SIZE)
             pos1 = self.fileobj.tell ()
             dpos = pos1 - self.lasthdr
-            assert dpos == crypto.I2N_HDR_SIZE
+            assert dpos == crypto.PDTCRYPT_HDR_SIZE
             self.fileobj.seek_set (pos0)
             data, hdr, fixed = self.encryption.done (dummy)
             self.__write_to_file(hdr, pos=self.lasthdr)