Skip to content

Commit

Permalink
split encoding and cipher + add AES_CCM
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Jan 2, 2025
1 parent 4c218a8 commit 9118883
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 101 deletions.
17 changes: 10 additions & 7 deletions hivemind_bus_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from hivemind_bus_client.serialization import HiveMindBinaryPayloadType
from hivemind_bus_client.serialization import get_bitstring, decode_bitstring
from hivemind_bus_client.util import serialize_message
from hivemind_bus_client.encryption import encrypt_as_json, decrypt_from_json, encrypt_bin, decrypt_bin, JsonCiphers, BinaryCiphers
from hivemind_bus_client.encryption import (encrypt_as_json, decrypt_from_json, encrypt_bin, decrypt_bin,
SupportedEncodings, SupportedCiphers)
from poorman_handshake.asymmetric.utils import encrypt_RSA, load_RSA_key, sign_RSA


Expand Down Expand Up @@ -104,8 +105,8 @@ def __init__(self, key: Optional[str] = None,
internal_bus: Optional[OVOSBusClient] = None,
bin_callbacks: BinaryDataCallbacks = BinaryDataCallbacks()):
self.bin_callbacks = bin_callbacks
self.json_cipher = JsonCiphers.JSON_HEX_AES_GCM_128 # server defaults before it was made configurable
self.bin_cipher = BinaryCiphers.BINARY_AES_GCM_128 # server defaults before it was made configurable
self.json_encoding = SupportedEncodings.JSON_HEX # server defaults before it was made configurable
self.cipher = SupportedCiphers.AES_GCM # server defaults before it was made configurable

self.identity = identity or None
self._password = password
Expand Down Expand Up @@ -273,11 +274,12 @@ def on_message(self, *args):
if self.crypto_key:
# handle binary encryption
if isinstance(message, bytes):
message = decrypt_bin(self.crypto_key, message, cipher=self.bin_cipher)
message = decrypt_bin(self.crypto_key, message, cipher=self.cipher)
# handle json encryption
elif "ciphertext" in message:
# LOG.debug(f"got encrypted message: {len(message)}")
message = decrypt_from_json(self.crypto_key, message, cipher=self.json_cipher)
message = decrypt_from_json(self.crypto_key, message,
cipher=self.cipher, encoding=self.json_encoding)
else:
LOG.debug("Message was unencrypted")

Expand Down Expand Up @@ -369,14 +371,15 @@ def emit(self, message: Union[MycroftMessage, HiveMessage],
binary_type=binary_type,
hivemeta=message.metadata)
if self.crypto_key:
ws_payload = encrypt_bin(self.crypto_key, bitstr.bytes, cipher=self.bin_cipher)
ws_payload = encrypt_bin(self.crypto_key, bitstr.bytes, cipher=self.cipher)
else:
ws_payload = bitstr.bytes
self.client.send(ws_payload, ABNF.OPCODE_BINARY)
else:
ws_payload = serialize_message(message)
if self.crypto_key:
ws_payload = encrypt_as_json(self.crypto_key, ws_payload, cipher=self.json_cipher)
ws_payload = encrypt_as_json(self.crypto_key, ws_payload,
cipher=self.cipher, encoding=self.json_encoding)
self.client.send(ws_payload)

except WebSocketConnectionClosedException:
Expand Down
Loading

0 comments on commit 9118883

Please sign in to comment.