Skip to content

Commit

Permalink
rm AES_CCM
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Jan 3, 2025
1 parent e93b1a3 commit c8cbc26
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
16 changes: 5 additions & 11 deletions hivemind_bus_client/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class SupportedCiphers(str, enum.Enum):
- CHACHA20-POLY1305 - https://datatracker.ietf.org/doc/html/rfc7539
"""
AES_GCM = "AES-GCM"
AES_CCM = "AES-CCM"
CHACHA20_POLY1305 = "CHACHA20-POLY1305" # specified in RFC7539.


Expand All @@ -99,8 +98,8 @@ def optimal_ciphers() -> List[SupportedCiphers]:
List[SupportedCiphers]: A list of optimal ciphers based on CPU support.
"""
if not cpu_supports_AES():
return [SupportedCiphers.CHACHA20_POLY1305, SupportedCiphers.AES_CCM, SupportedCiphers.AES_GCM]
return [SupportedCiphers.AES_GCM, SupportedCiphers.AES_CCM, SupportedCiphers.CHACHA20_POLY1305]
return [SupportedCiphers.CHACHA20_POLY1305, SupportedCiphers.AES_GCM]
return [SupportedCiphers.AES_GCM, SupportedCiphers.CHACHA20_POLY1305]


def _norm_cipher(cipher: Union[SupportedCiphers, str]) -> SupportedCiphers:
Expand Down Expand Up @@ -281,7 +280,6 @@ def encrypt_bin(key: Union[str, bytes], plaintext: Union[str, bytes], cipher: Un
plaintext (Union[str, bytes]): The data to encrypt. Strings will be encoded as UTF-8.
cipher (SupportedCiphers): The encryption cipher. Supported options:
- AES_GCM: AES-GCM with 128-bit/256-bit key
- AES_CCM: AES-GCM with 128-bit/256-bit key
- CHACHA20_POLY1305: ChaCha20-Poly1305 with 256-bit key
Returns:
Expand All @@ -299,8 +297,6 @@ def encrypt_bin(key: Union[str, bytes], plaintext: Union[str, bytes], cipher: Un
if cipher in BLOCK_CIPHERS:
if cipher == SupportedCiphers.AES_GCM:
mode = AES.MODE_GCM
elif cipher == SupportedCiphers.AES_CCM:
mode = AES.MODE_CCM
else:
raise ValueError("invalid block cipher mode")
ciphertext, tag, nonce = encryptor(key, plaintext, mode=mode)
Expand Down Expand Up @@ -333,7 +329,7 @@ def decrypt_bin(key: Union[str, bytes], ciphertext: Union[str, bytes], cipher: U
cipher = _norm_cipher(cipher)

# extract nonce/tag depending on cipher, sizes are different
if cipher in AES_CIPHERS:
if cipher == SupportedCiphers.AES_GCM:
nonce, ciphertext, tag = (ciphertext[:AES_NONCE_SIZE],
ciphertext[AES_NONCE_SIZE:-AES_TAG_SIZE],
ciphertext[-AES_TAG_SIZE:])
Expand All @@ -347,8 +343,6 @@ def decrypt_bin(key: Union[str, bytes], ciphertext: Union[str, bytes], cipher: U
if cipher in BLOCK_CIPHERS:
if cipher == SupportedCiphers.AES_GCM:
mode = AES.MODE_GCM
elif cipher == SupportedCiphers.AES_CCM:
mode = AES.MODE_CCM
else:
raise ValueError("invalid block cipher mode")
return decryptor(key, ciphertext, tag, nonce, mode=mode)
Expand All @@ -363,7 +357,7 @@ def decrypt_bin(key: Union[str, bytes], ciphertext: Union[str, bytes], cipher: U
# Cipher Implementations
def encrypt_AES(key: Union[str, bytes], text: Union[str, bytes],
nonce: Optional[bytes] = None,
mode: Literal[AES.MODE_GCM, AES.MODE_CCM] = AES.MODE_GCM) -> tuple[bytes, bytes, bytes]:
mode: Literal[AES.MODE_GCM] = AES.MODE_GCM) -> tuple[bytes, bytes, bytes]:
"""
Encrypts plaintext using AES-GCM-128.
Expand Down Expand Up @@ -392,7 +386,7 @@ def decrypt_AES_128(key: Union[str, bytes],
ciphertext: bytes,
tag: bytes,
nonce: bytes,
mode: Literal[AES.MODE_GCM, AES.MODE_CCM] = AES.MODE_GCM) -> bytes:
mode: Literal[AES.MODE_GCM] = AES.MODE_GCM) -> bytes:
"""
Decrypts ciphertext encrypted using AES-GCM-128.
Expand Down
2 changes: 1 addition & 1 deletion hivemind_bus_client/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def as_dict(self) -> dict:

@property
def as_json(self) -> str:
return json.dumps(self.as_dict)
return json.dumps(self.as_dict, ensure_ascii=False)

def serialize(self) -> str:
return self.as_json
Expand Down

0 comments on commit c8cbc26

Please sign in to comment.