Skip to content

Commit 7eaa316

Browse files
authored
Refactor session.py with import changes and validations
Refactor imports and add validation for plaintext size in encrypt method.
1 parent aeffabf commit 7eaa316

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/cyphersyntax/session.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
)
2121
from .identity import Identity
2222
from .kdf import derive_key_confirmation, derive_message_key, derive_session_root
23-
from .protocol import MAX_MESSAGE_SEQUENCE, PROTOCOL_VERSION, validate_message_sequence
23+
from .protocol import (
24+
MAX_MESSAGE_SEQUENCE,
25+
MAX_PLAINTEXT_BYTES,
26+
PROTOCOL_VERSION,
27+
validate_message_sequence,
28+
)
2429
from .replay import ReplayWindow
2530
from .wire import MessageEnvelope
2631

@@ -49,6 +54,10 @@ def _build_aead(self, key: bytes):
4954
raise ValueError(f"unsupported suite: {self.suite}")
5055

5156
def encrypt(self, plaintext: bytes) -> bytes:
57+
if type(plaintext) is not bytes:
58+
raise TypeError("plaintext must be bytes")
59+
if len(plaintext) > MAX_PLAINTEXT_BYTES:
60+
raise ValueError("plaintext exceeds the maximum message size")
5261
if self.send_sequence > MAX_MESSAGE_SEQUENCE:
5362
raise OverflowError("message sequence exhausted")
5463
validate_message_sequence(self.send_sequence)

0 commit comments

Comments
 (0)