fix encrypted read logic for begin/end at entry boundaries
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 20 Mar 2017 14:02:03 +0000 (15:02 +0100)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 20 Mar 2017 14:02:08 +0000 (15:02 +0100)
deltatar/crypto.py

index 9342c2a..c6eb711 100755 (executable)
@@ -165,10 +165,10 @@ def hdr_read (data):
 
 def hdr_read_stream (instr):
     data = instr.read(I2N_HDR_SIZE)
-    if len (data) != FMT_I2N_HDR:
-        return False, "error reading from [%r]: expected %d B, received %d" \
+    if len (data) != I2N_HDR_SIZE:
+        return False, "error reading from [%r]: expected %d B, received %d B" \
                       % (instr, I2N_HDR_SIZE, len (data))
-    return True, hdr_read (data)
+    return hdr_read (data)
 
 
 def hdr_from_params (version, paramversion, nacl, iv, ctsize, tag):
@@ -316,13 +316,6 @@ class Crypto (object):
             self.pfx = os.urandom(8)
 
 
-    def set_parameters_from_header (self, hdr):
-        self.password     = password
-        self.nacl         = nacl
-        self.paramversion = paramversion
-        self.pfx          = pfx
-
-
     def process (self, buf):
         if self.aes is not None:
             return self.aes.update (buf)
@@ -390,6 +383,8 @@ class Decrypt (Crypto):
     pfx          = None
     password     = None
     tag          = None # GCM tag, part of header
+    ctsize       = -1
+    ptsize       = -1
 
     def __init__ (self, password, paramversion=None, nacl=None):
         if      paramversion is not None \
@@ -415,6 +410,8 @@ class Decrypt (Crypto):
         # XXX figure out what we want for AAD. Filename (not known to stream)?
         #     Size?
         #self.aes.authenticate_additional_data (str.encode (aad))
+        ctsize = 0
+        ptsize = 0
 
 
     def next_in_source (self, tarinfo, source):
@@ -425,10 +422,17 @@ class Decrypt (Crypto):
         return self.next(hdr)
 
 
-    def done (self, tag):
+    def done (self):
         return self.aes.finalize_with_tag (self.tag)
 
 
+    def process (self, buf):
+        self.ctsize += len (buf)
+        data = super().process (buf)
+        self.ptsize += len (data)
+        return data
+
+
 ###############################################################################
 ## freestanding invocation
 ###############################################################################