Skip to content

Commit

Permalink
crypto: add IV overflow check
Browse files Browse the repository at this point in the history
will never happen, but better play safe.
  • Loading branch information
ThomasWaldmann committed Mar 22, 2022
1 parent ff5e3dd commit 2e20162
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/borg/crypto/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2e20162

Please sign in to comment.