Browse Source

crypto: add IV overflow check

will never happen, but better play safe.
Thomas Waldmann 3 years ago
parent
commit
e4b65dea76
1 changed files with 4 additions and 0 deletions
  1. 4 0
      src/borg/crypto/key.py

+ 4 - 0
src/borg/crypto/key.py

@@ -732,11 +732,15 @@ class AEADKeyBase(KeyBase):
 
     logically_encrypted = True
 
+    MAX_IV = 2 ** 48 - 1
+
     def encrypt(self, id, data):
         # to encrypt new data in this session we use always self.cipher and self.sessionid
         data = self.compressor.compress(data)
         reserved = b'\0'
         iv = self.cipher.next_iv()
+        if iv > self.MAX_IV:  # see the data-structures docs about why the IV range is enough
+            raise IntegrityError("IV overflow, should never happen.")
         iv_48bit = iv.to_bytes(6, 'big')
         header = self.TYPE_STR + reserved + iv_48bit + self.sessionid
         return self.cipher.encrypt(data, header=header, iv=iv, aad=id)