From 47e2792648fab2fe82ea19b10ba9a1b9f3b591ab Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 20 Mar 2017 15:02:03 +0100 Subject: [PATCH] fix encrypted read logic for begin/end at entry boundaries --- deltatar/crypto.py | 26 +++++++++++++++----------- 1 files changed, 15 insertions(+), 11 deletions(-) diff --git a/deltatar/crypto.py b/deltatar/crypto.py index 9342c2a..c6eb711 100755 --- a/deltatar/crypto.py +++ b/deltatar/crypto.py @@ -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 ############################################################################### -- 1.7.1