aboutsummaryrefslogtreecommitdiff
path: root/frozen_deps/Cryptodome/Cipher/_mode_ocb.py
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2024-08-23 03:14:03 +0000
committerDeterminant <[email protected]>2024-08-22 20:34:57 -0700
commit8d1c76ec7caf247d5675e14260d20fc508977ffb (patch)
tree8fa7c8ce3b7e3f4ece150a6da5922b5eb2dc7772 /frozen_deps/Cryptodome/Cipher/_mode_ocb.py
parent258780284151d49cba1d9c0d2ce33f9a19bb058b (diff)
release v0.1.8
Diffstat (limited to 'frozen_deps/Cryptodome/Cipher/_mode_ocb.py')
-rw-r--r--frozen_deps/Cryptodome/Cipher/_mode_ocb.py45
1 files changed, 26 insertions, 19 deletions
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()