return collected fixed iv parts from .close() when encrypting
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 28 Mar 2017 12:28:06 +0000 (14:28 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
Provisional implementation for dumping the IVs in the info file
that will be superseded once the crypto context can be provided
to tarfile by the user.

deltatar/crypto.py
deltatar/tarfile.py

index 8c826b3..fa8a2cf 100755 (executable)
@@ -466,7 +466,7 @@ class Encrypt (Crypto):
                                    self.ctsize, self.enc.tag)
         if ok is False:
             raise Exception ("XXX error constructing header: %r" % hdr) ## we need to converge on a sensible error handling strategy
-        return data, hdr
+        return data, hdr, self.pfx
 
 
     def process (self, buf):
index 1d84365..0431d5f 100644 (file)
@@ -117,8 +117,9 @@ GNU_FORMAT = 1                  # GNU tar format
 PAX_FORMAT = 2                  # POSIX.1-2001 (pax) format
 DEFAULT_FORMAT = GNU_FORMAT
 
-DELTATAR_HEADER_VERSION    = 1
-DELTATAR_PARAMETER_VERSION = 1
+DELTATAR_HEADER_VERSION     = 1
+DELTATAR_PARAMETER_VERSION  = 1
+DELTATAR_PDTCRYPT_EXTENSION = "pdtcrypt"
 
 GZ_FMT_HEADER        = b"<BBBBLBB"
 GZ_MAGIC             = (0x1f, 0x8b) # 0o37, 0o213
@@ -163,8 +164,6 @@ PAX_NUMBER_FIELDS = {
     "size": int
 }
 
-VALID_ENCRYPTION_MODES = [ "aes" ]
-
 #---------------------------------------------------------
 # initialization
 #---------------------------------------------------------
@@ -603,7 +602,10 @@ class _Stream:
         Seek back to header position, read dummy bytes, finalize crypto
         obtaining the actual header, write header, seek back to current
         position.
+
+        Returns the list of IV fixed parts as used during encryption.
         """
+        fixed = None
         if      self.encryption is not None \
             and self.lasthdr    is not None :
             self.__sync ()
@@ -614,10 +616,11 @@ class _Stream:
             dpos = pos1 - self.lasthdr
             assert dpos == crypto.I2N_HDR_SIZE
             self.fileobj.seek_set (pos0)
-            data, hdr = self.encryption.done (dummy)
+            data, hdr, fixed = self.encryption.done (dummy)
             self.__write_to_file(hdr, pos=self.lasthdr)
             self.__write_to_file(data) # append remainder of data
             self.lasthdr = -1
+        return fixed
 
 
     def _finalize_write_gz (self):
@@ -759,27 +762,31 @@ class _Stream:
         """Close the _Stream object. No operation should be
            done on it afterwards.
         """
+        fixed = None
+
         if self.closed:
             return
 
         if self.mode == "w":
             self._finalize_write_gz ()
             self.__enc_write(self.buf)
-            self._finalize_write_encrypt ()
-
-        if close_fileobj and not self._extfileobj:
-            self.fileobj.close()
-
-        # read the zlib crc and length and check them
-        if not close_fileobj and self.mode == "r" and self.comptype == "gz":
-            read_crc = self.__read(4)
-            read_length = self.__read(4)
-            calculated_crc = self.crc
-            if struct.unpack("<L", read_crc)[0] != calculated_crc:
-                raise CompressionError("bad gzip crc")
+            fixed = self._finalize_write_encrypt ()
 
+        if close_fileobj is True:
+            if not self._extfileobj:
+                self.fileobj.close()
+        else:
+            # read the zlib crc and length and check them
+            if self.mode == "r" and self.comptype == "gz":
+                read_crc = self.__read(4)
+                read_length = self.__read(4)
+                calculated_crc = self.crc
+                if struct.unpack("<L", read_crc)[0] != calculated_crc:
+                    raise CompressionError("bad gzip crc")
         self.closed = True
 
+        return fixed
+
     def _init_read_gz(self):
         """Initialize for reading a gzip compressed fileobj.
         """