From: Philipp Gesang Date: Mon, 20 Mar 2017 15:42:51 +0000 (+0100) Subject: adapt tag handling in decryption X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=9d72063b0c6077febf5a726f6a70e081481870cc;p=python-delta-tar adapt tag handling in decryption --- diff --git a/deltatar/crypto.py b/deltatar/crypto.py index c6eb711..17466ad 100755 --- a/deltatar/crypto.py +++ b/deltatar/crypto.py @@ -203,13 +203,14 @@ def hdr_make (hdr): HDR_FMT = "I2n_header { version: %d, paramversion: %d, nacl: %s[%d]," \ - " iv: %s[%d], ctsize: %d }" + " iv: %s[%d], ctsize: %d, tag: %s[%d]}" def hdr_fmt (h): return HDR_FMT % (h["version"], h["paramversion"], binascii.hexlify (h["nacl"]), len(h["nacl"]), binascii.hexlify (h["iv"]), len(h["iv"]), - h["ctsize"]) + h["ctsize"], + binascii.hexlify (h["tag"]), len(h["tag"])) def hex_spaced_of_bytes (b): @@ -403,8 +404,8 @@ class Decrypt (Crypto): iv = hdr ["iv"] self.tag = hdr ["tag"] self.aes = Cipher \ - ( algorithms.AES (key) - , modes.GCM (hdr["iv"]) + ( algorithms.AES (self.key) + , modes.GCM (hdr["iv"], tag=self.tag) , backend = default_backend ()) \ . decryptor () # XXX figure out what we want for AAD. Filename (not known to stream)? @@ -422,8 +423,14 @@ class Decrypt (Crypto): return self.next(hdr) - def done (self): - return self.aes.finalize_with_tag (self.tag) + def done (self, tag=None): + try: + if tag is None: + return True, self.aes.finalize () + else: + return self.aes.finalize_with_tag (self.tag) + except crypto.cryptography.exceptions.InvalidTag as exn: + return False, repr (exn) def process (self, buf):