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

Update encryption code to Decoder 1.6.2 #176

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions TheengsGateway/ble_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ def decode_advertisement(
self.hass_presence(decoded_json)

# Handle encrypted payload
if decoded_json.get("encr", False):
self.handle_encrypted_advertisement(
if decoded_json.get("encr", 0) > 0:
decoded_json = self.handle_encrypted_advertisement(
data_json,
decoded_json,
)
Expand Down Expand Up @@ -512,14 +512,14 @@ def handle_encrypted_advertisement(
self,
data_json: DataJSONType,
decoded_json: DataJSONType,
) -> None:
) -> DataJSONType:
"""Handle encrypted advertisement."""
try:
bindkey = bytes.fromhex(
self.configuration["bindkeys"][get_address(decoded_json)],
)
decryptor = create_decryptor(
decoded_json["model_id"], # type: ignore[arg-type]
decoded_json["encr"], # type: ignore[arg-type]
)
decrypted_data = decryptor.decrypt(
bindkey,
Expand Down Expand Up @@ -564,6 +564,8 @@ def handle_encrypted_advertisement(
)
except ValueError:
logger.exception("Decryption failed")
finally:
return decoded_json # noqa: B012


def run(configuration: dict, config_path: Path) -> None:
Expand Down
16 changes: 7 additions & 9 deletions TheengsGateway/decryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,21 @@ def replace_encrypted_data(
data_json["servicedata"] = bthome_service_data.hex()


_DECRYPTOR_MODELS = {
"LYWSD03MMC/MJWSD05MMC_PVVX_ENCR": LYWSD03MMC_PVVXDecryptor,
"SBBT_002C_ENCR": BTHomeV2Decryptor,
"SBDW_002C_ENCR": BTHomeV2Decryptor,
"SBMO_003Z_ENCR": BTHomeV2Decryptor,
_DECRYPTORS = {
1: LYWSD03MMC_PVVXDecryptor,
2: BTHomeV2Decryptor,
}


class UnsupportedEncryptionError(Exception):
"""Exception raised when trying to decrypt an unsupported device."""


def create_decryptor(model_id: str) -> AdvertisementDecryptor:
"""Return the decryptor class for the given model ID."""
if model_id not in _DECRYPTOR_MODELS:
def create_decryptor(mode: int) -> AdvertisementDecryptor:
"""Return the decryptor class for the given encryption mode."""
if mode not in _DECRYPTORS:
raise UnsupportedEncryptionError
return _DECRYPTOR_MODELS[model_id]() # type: ignore[abstract]
return _DECRYPTORS[mode]() # type: ignore[abstract]


def reverse_address(address: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"importlib-metadata",
"paho-mqtt>=1.6.1",
"pycryptodomex>=3.18.0",
"TheengsDecoder>=1.5.5",
"TheengsDecoder>=1.6.2",
],
use_scm_version={"version_scheme": "no-guess-dev"},
setup_requires=["setuptools_scm"],
Expand Down