From 8d1c76ec7caf247d5675e14260d20fc508977ffb Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 23 Aug 2024 03:14:03 +0000 Subject: release v0.1.8 --- frozen_deps/Cryptodome/Cipher/_mode_ocb.py | 45 +++++++++++++++++------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'frozen_deps/Cryptodome/Cipher/_mode_ocb.py') diff --git a/frozen_deps/Cryptodome/Cipher/_mode_ocb.py b/frozen_deps/Cryptodome/Cipher/_mode_ocb.py index 27c2797..1295e61 100644 --- a/frozen_deps/Cryptodome/Cipher/_mode_ocb.py +++ b/frozen_deps/Cryptodome/Cipher/_mode_ocb.py @@ -71,7 +71,7 @@ Example: import struct from binascii import unhexlify -from Cryptodome.Util.py3compat import bord, _copy_bytes +from Cryptodome.Util.py3compat import bord, _copy_bytes, bchr from Cryptodome.Util.number import long_to_bytes, bytes_to_long from Cryptodome.Util.strxor import strxor @@ -142,15 +142,22 @@ class OcbMode(object): self._cache_P = b"" # Allowed transitions after initialization - self._next = [self.update, self.encrypt, self.decrypt, - self.digest, self.verify] + self._next = ["update", "encrypt", "decrypt", + "digest", "verify"] # Compute Offset_0 params_without_key = dict(cipher_params) key = params_without_key.pop("key") - nonce = (struct.pack('B', self._mac_len << 4 & 0xFF) + - b'\x00' * (14 - len(nonce)) + - b'\x01' + self.nonce) + + taglen_mod128 = (self._mac_len * 8) % 128 + if len(self.nonce) < 15: + nonce = bchr(taglen_mod128 << 1) +\ + b'\x00' * (14 - len(nonce)) +\ + b'\x01' +\ + self.nonce + else: + nonce = bchr((taglen_mod128 << 1) | 0x01) +\ + self.nonce bottom_bits = bord(nonce[15]) & 0x3F # 6 bits, 0..63 top_bits = bord(nonce[15]) & 0xC0 # 2 bits @@ -217,12 +224,12 @@ class OcbMode(object): A piece of associated data. """ - if self.update not in self._next: + if "update" not in self._next: raise TypeError("update() can only be called" " immediately after initialization") - self._next = [self.encrypt, self.decrypt, self.digest, - self.verify, self.update] + self._next = ["encrypt", "decrypt", "digest", + "verify", "update"] if len(self._cache_A) > 0: filler = min(16 - len(self._cache_A), len(assoc_data)) @@ -316,14 +323,14 @@ class OcbMode(object): Its length may not match the length of the *plaintext*. """ - if self.encrypt not in self._next: + if "encrypt" not in self._next: raise TypeError("encrypt() can only be called after" " initialization or an update()") if plaintext is None: - self._next = [self.digest] + self._next = ["digest"] else: - self._next = [self.encrypt] + self._next = ["encrypt"] return self._transcrypt(plaintext, _raw_ocb_lib.OCB_encrypt, "encrypt") def decrypt(self, ciphertext=None): @@ -345,14 +352,14 @@ class OcbMode(object): Its length may not match the length of the *ciphertext*. """ - if self.decrypt not in self._next: + if "decrypt" not in self._next: raise TypeError("decrypt() can only be called after" " initialization or an update()") if ciphertext is None: - self._next = [self.verify] + self._next = ["verify"] else: - self._next = [self.decrypt] + self._next = ["decrypt"] return self._transcrypt(ciphertext, _raw_ocb_lib.OCB_decrypt, "decrypt") @@ -388,12 +395,12 @@ class OcbMode(object): :Return: the MAC, as a byte string. """ - if self.digest not in self._next: + if "digest" not in self._next: raise TypeError("digest() cannot be called now for this cipher") assert(len(self._cache_P) == 0) - self._next = [self.digest] + self._next = ["digest"] if self._mac_tag is None: self._compute_mac_tag() @@ -423,12 +430,12 @@ class OcbMode(object): or the key is incorrect. """ - if self.verify not in self._next: + if "verify" not in self._next: raise TypeError("verify() cannot be called now for this cipher") assert(len(self._cache_P) == 0) - self._next = [self.verify] + self._next = ["verify"] if self._mac_tag is None: self._compute_mac_tag() -- cgit v1.2.3-70-g09d2