Skip to content

Commit

Permalink
perf(AES): eliminate try-except block in a for loop (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
arunanshub committed Aug 1, 2023
1 parent 1f2d6dc commit 7460dbb
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/pyflocker/ciphers/backends/cryptography_/AES.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def __init__(self, key: bytes, nonce: bytes, mac_len: int = 16) -> None:

# create a cache since cryptography allows us to calculate tag
# only once... why...
self._omac_cache = []
self._omac_cache: list[bytes] = []
self._omac_cache.append(self._omac[0].finalize())

self._cipher = CrCipher(
Expand Down Expand Up @@ -300,11 +300,9 @@ def finalize(self) -> None:

tag = bytes(typing.cast("int", algo.AES.block_size) // 8)
for i in range(3):
try:
tag = strxor(tag, self._omac_cache[i])
except IndexError:
if i >= len(self._omac_cache):
self._omac_cache.append(self._omac[i].finalize())
tag = strxor(tag, self._omac_cache[i])
tag = strxor(tag, self._omac_cache[i])
self.__tag, self.__ctx = tag[: self._mac_len], None

def finalize_with_tag(self, tag: bytes) -> None:
Expand Down

0 comments on commit 7460dbb

Please sign in to comment.