Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add chacha20 cipher + z85 encoding #50

Merged
merged 12 commits into from
Jan 3, 2025
17 changes: 11 additions & 6 deletions hivemind_bus_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
from hivemind_bus_client.message import HiveMessage, HiveMessageType
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, \
encrypt_as_json, decrypt_from_json, encrypt_bin, decrypt_bin
from hivemind_bus_client.util import serialize_message
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,6 +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_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 @@ -271,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)
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)
message = decrypt_from_json(self.crypto_key, message,
cipher=self.cipher, encoding=self.json_encoding)
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved
else:
LOG.debug("Message was unencrypted")

Expand Down Expand Up @@ -367,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)
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)
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
Loading