From 93bdbc26a2610b4d5b3249f980d2a7b9b932def7 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Fri, 7 Nov 2025 04:33:04 +0000 Subject: [PATCH] Remove Encryptor.final_detached(), as final() handles both cases identically (only returns MAC). --- pyaegis/aegis128l.py | 47 +------------------------------------------ pyaegis/aegis128x2.py | 47 +------------------------------------------ pyaegis/aegis128x4.py | 47 +------------------------------------------ pyaegis/aegis256.py | 47 +------------------------------------------ pyaegis/aegis256x2.py | 47 +------------------------------------------ pyaegis/aegis256x4.py | 47 +------------------------------------------ 6 files changed, 6 insertions(+), 276 deletions(-) diff --git a/pyaegis/aegis128l.py b/pyaegis/aegis128l.py index fbf3075..9510863 100644 --- a/pyaegis/aegis128l.py +++ b/pyaegis/aegis128l.py @@ -598,7 +598,7 @@ class Encryptor: def bytes_out(self) -> int: """Total ciphertext bytes produced so far. - Includes update() and final()/final_detached() output, also MAC tag. + Includes update() and final() output. """ return self._bytes_out @@ -682,51 +682,6 @@ class Encryptor: self._bytes_out += w return out[:w] - def final_detached( - self, - ct_into: bytearray | None = None, - mac_into: bytearray | None = None, - maclen: int = ABYTES_MIN, - ) -> tuple[bytearray, bytearray]: - """Finalize encryption, producing detached tail bytes and tag. - - Args: - ct_into: Optional destination for the remaining ciphertext tail. - mac_into: Optional destination for the tag. - maclen: Tag length (16 or 32). Defaults to 16. - - Returns: - A tuple of (tail_bytes, mac). When destination buffers are provided, - the first element is a slice of ``ct_into`` up to the number of bytes - written, and the second is ``mac_into``. - - Raises: - TypeError: If maclen is invalid or mac_into has the wrong length. - RuntimeError: If the C final call fails. - """ - if maclen not in (16, 32): - raise TypeError("maclen must be 16 or 32") - out = ct_into if ct_into is not None else bytearray(TAILBYTES_MAX) - mac = mac_into if mac_into is not None else bytearray(maclen) - if len(mac) != maclen: - raise TypeError("mac_into length must equal maclen") - written = ffi.new("size_t *") - rc = _lib.aegis128l_state_encrypt_detached_final( - self._st, - ffi.from_buffer(out), - len(out), - written, - ffi.from_buffer(mac), - maclen, - ) - if rc != 0: - err_num = ffi.errno - err_name = errno.errorcode.get(err_num, f"errno_{err_num}") - raise RuntimeError(f"state encrypt detached final failed: {err_name}") - w = int(written[0]) - self._bytes_out += w + maclen - return out[:w], mac - class Decryptor: """Incremental decryptor. diff --git a/pyaegis/aegis128x2.py b/pyaegis/aegis128x2.py index 19342be..0c70f70 100644 --- a/pyaegis/aegis128x2.py +++ b/pyaegis/aegis128x2.py @@ -598,7 +598,7 @@ class Encryptor: def bytes_out(self) -> int: """Total ciphertext bytes produced so far. - Includes update() and final()/final_detached() output, also MAC tag. + Includes update() and final() output. """ return self._bytes_out @@ -682,51 +682,6 @@ class Encryptor: self._bytes_out += w return out[:w] - def final_detached( - self, - ct_into: bytearray | None = None, - mac_into: bytearray | None = None, - maclen: int = ABYTES_MIN, - ) -> tuple[bytearray, bytearray]: - """Finalize encryption, producing detached tail bytes and tag. - - Args: - ct_into: Optional destination for the remaining ciphertext tail. - mac_into: Optional destination for the tag. - maclen: Tag length (16 or 32). Defaults to 16. - - Returns: - A tuple of (tail_bytes, mac). When destination buffers are provided, - the first element is a slice of ``ct_into`` up to the number of bytes - written, and the second is ``mac_into``. - - Raises: - TypeError: If maclen is invalid or mac_into has the wrong length. - RuntimeError: If the C final call fails. - """ - if maclen not in (16, 32): - raise TypeError("maclen must be 16 or 32") - out = ct_into if ct_into is not None else bytearray(TAILBYTES_MAX) - mac = mac_into if mac_into is not None else bytearray(maclen) - if len(mac) != maclen: - raise TypeError("mac_into length must equal maclen") - written = ffi.new("size_t *") - rc = _lib.aegis128x2_state_encrypt_detached_final( - self._st, - ffi.from_buffer(out), - len(out), - written, - ffi.from_buffer(mac), - maclen, - ) - if rc != 0: - err_num = ffi.errno - err_name = errno.errorcode.get(err_num, f"errno_{err_num}") - raise RuntimeError(f"state encrypt detached final failed: {err_name}") - w = int(written[0]) - self._bytes_out += w + maclen - return out[:w], mac - class Decryptor: """Incremental decryptor. diff --git a/pyaegis/aegis128x4.py b/pyaegis/aegis128x4.py index 0a1cc89..e3ebfa6 100644 --- a/pyaegis/aegis128x4.py +++ b/pyaegis/aegis128x4.py @@ -598,7 +598,7 @@ class Encryptor: def bytes_out(self) -> int: """Total ciphertext bytes produced so far. - Includes update() and final()/final_detached() output, also MAC tag. + Includes update() and final() output. """ return self._bytes_out @@ -682,51 +682,6 @@ class Encryptor: self._bytes_out += w return out[:w] - def final_detached( - self, - ct_into: bytearray | None = None, - mac_into: bytearray | None = None, - maclen: int = ABYTES_MIN, - ) -> tuple[bytearray, bytearray]: - """Finalize encryption, producing detached tail bytes and tag. - - Args: - ct_into: Optional destination for the remaining ciphertext tail. - mac_into: Optional destination for the tag. - maclen: Tag length (16 or 32). Defaults to 16. - - Returns: - A tuple of (tail_bytes, mac). When destination buffers are provided, - the first element is a slice of ``ct_into`` up to the number of bytes - written, and the second is ``mac_into``. - - Raises: - TypeError: If maclen is invalid or mac_into has the wrong length. - RuntimeError: If the C final call fails. - """ - if maclen not in (16, 32): - raise TypeError("maclen must be 16 or 32") - out = ct_into if ct_into is not None else bytearray(TAILBYTES_MAX) - mac = mac_into if mac_into is not None else bytearray(maclen) - if len(mac) != maclen: - raise TypeError("mac_into length must equal maclen") - written = ffi.new("size_t *") - rc = _lib.aegis128x4_state_encrypt_detached_final( - self._st, - ffi.from_buffer(out), - len(out), - written, - ffi.from_buffer(mac), - maclen, - ) - if rc != 0: - err_num = ffi.errno - err_name = errno.errorcode.get(err_num, f"errno_{err_num}") - raise RuntimeError(f"state encrypt detached final failed: {err_name}") - w = int(written[0]) - self._bytes_out += w + maclen - return out[:w], mac - class Decryptor: """Incremental decryptor. diff --git a/pyaegis/aegis256.py b/pyaegis/aegis256.py index b3a385b..47bb2d2 100644 --- a/pyaegis/aegis256.py +++ b/pyaegis/aegis256.py @@ -598,7 +598,7 @@ class Encryptor: def bytes_out(self) -> int: """Total ciphertext bytes produced so far. - Includes update() and final()/final_detached() output, also MAC tag. + Includes update() and final() output. """ return self._bytes_out @@ -682,51 +682,6 @@ class Encryptor: self._bytes_out += w return out[:w] - def final_detached( - self, - ct_into: bytearray | None = None, - mac_into: bytearray | None = None, - maclen: int = ABYTES_MIN, - ) -> tuple[bytearray, bytearray]: - """Finalize encryption, producing detached tail bytes and tag. - - Args: - ct_into: Optional destination for the remaining ciphertext tail. - mac_into: Optional destination for the tag. - maclen: Tag length (16 or 32). Defaults to 16. - - Returns: - A tuple of (tail_bytes, mac). When destination buffers are provided, - the first element is a slice of ``ct_into`` up to the number of bytes - written, and the second is ``mac_into``. - - Raises: - TypeError: If maclen is invalid or mac_into has the wrong length. - RuntimeError: If the C final call fails. - """ - if maclen not in (16, 32): - raise TypeError("maclen must be 16 or 32") - out = ct_into if ct_into is not None else bytearray(TAILBYTES_MAX) - mac = mac_into if mac_into is not None else bytearray(maclen) - if len(mac) != maclen: - raise TypeError("mac_into length must equal maclen") - written = ffi.new("size_t *") - rc = _lib.aegis256_state_encrypt_detached_final( - self._st, - ffi.from_buffer(out), - len(out), - written, - ffi.from_buffer(mac), - maclen, - ) - if rc != 0: - err_num = ffi.errno - err_name = errno.errorcode.get(err_num, f"errno_{err_num}") - raise RuntimeError(f"state encrypt detached final failed: {err_name}") - w = int(written[0]) - self._bytes_out += w + maclen - return out[:w], mac - class Decryptor: """Incremental decryptor. diff --git a/pyaegis/aegis256x2.py b/pyaegis/aegis256x2.py index ee22ac6..3659f41 100644 --- a/pyaegis/aegis256x2.py +++ b/pyaegis/aegis256x2.py @@ -598,7 +598,7 @@ class Encryptor: def bytes_out(self) -> int: """Total ciphertext bytes produced so far. - Includes update() and final()/final_detached() output, also MAC tag. + Includes update() and final() output. """ return self._bytes_out @@ -682,51 +682,6 @@ class Encryptor: self._bytes_out += w return out[:w] - def final_detached( - self, - ct_into: bytearray | None = None, - mac_into: bytearray | None = None, - maclen: int = ABYTES_MIN, - ) -> tuple[bytearray, bytearray]: - """Finalize encryption, producing detached tail bytes and tag. - - Args: - ct_into: Optional destination for the remaining ciphertext tail. - mac_into: Optional destination for the tag. - maclen: Tag length (16 or 32). Defaults to 16. - - Returns: - A tuple of (tail_bytes, mac). When destination buffers are provided, - the first element is a slice of ``ct_into`` up to the number of bytes - written, and the second is ``mac_into``. - - Raises: - TypeError: If maclen is invalid or mac_into has the wrong length. - RuntimeError: If the C final call fails. - """ - if maclen not in (16, 32): - raise TypeError("maclen must be 16 or 32") - out = ct_into if ct_into is not None else bytearray(TAILBYTES_MAX) - mac = mac_into if mac_into is not None else bytearray(maclen) - if len(mac) != maclen: - raise TypeError("mac_into length must equal maclen") - written = ffi.new("size_t *") - rc = _lib.aegis256x2_state_encrypt_detached_final( - self._st, - ffi.from_buffer(out), - len(out), - written, - ffi.from_buffer(mac), - maclen, - ) - if rc != 0: - err_num = ffi.errno - err_name = errno.errorcode.get(err_num, f"errno_{err_num}") - raise RuntimeError(f"state encrypt detached final failed: {err_name}") - w = int(written[0]) - self._bytes_out += w + maclen - return out[:w], mac - class Decryptor: """Incremental decryptor. diff --git a/pyaegis/aegis256x4.py b/pyaegis/aegis256x4.py index bd4f781..c0fba41 100644 --- a/pyaegis/aegis256x4.py +++ b/pyaegis/aegis256x4.py @@ -598,7 +598,7 @@ class Encryptor: def bytes_out(self) -> int: """Total ciphertext bytes produced so far. - Includes update() and final()/final_detached() output, also MAC tag. + Includes update() and final() output. """ return self._bytes_out @@ -682,51 +682,6 @@ class Encryptor: self._bytes_out += w return out[:w] - def final_detached( - self, - ct_into: bytearray | None = None, - mac_into: bytearray | None = None, - maclen: int = ABYTES_MIN, - ) -> tuple[bytearray, bytearray]: - """Finalize encryption, producing detached tail bytes and tag. - - Args: - ct_into: Optional destination for the remaining ciphertext tail. - mac_into: Optional destination for the tag. - maclen: Tag length (16 or 32). Defaults to 16. - - Returns: - A tuple of (tail_bytes, mac). When destination buffers are provided, - the first element is a slice of ``ct_into`` up to the number of bytes - written, and the second is ``mac_into``. - - Raises: - TypeError: If maclen is invalid or mac_into has the wrong length. - RuntimeError: If the C final call fails. - """ - if maclen not in (16, 32): - raise TypeError("maclen must be 16 or 32") - out = ct_into if ct_into is not None else bytearray(TAILBYTES_MAX) - mac = mac_into if mac_into is not None else bytearray(maclen) - if len(mac) != maclen: - raise TypeError("mac_into length must equal maclen") - written = ffi.new("size_t *") - rc = _lib.aegis256x4_state_encrypt_detached_final( - self._st, - ffi.from_buffer(out), - len(out), - written, - ffi.from_buffer(mac), - maclen, - ) - if rc != 0: - err_num = ffi.errno - err_name = errno.errorcode.get(err_num, f"errno_{err_num}") - raise RuntimeError(f"state encrypt detached final failed: {err_name}") - w = int(written[0]) - self._bytes_out += w + maclen - return out[:w], mac - class Decryptor: """Incremental decryptor.