diff options
author | Diego Elio Pettenò <flameeyes@flameeyes.com> | 2023-08-03 00:05:35 +0200 |
---|---|---|
committer | mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> | 2023-08-03 10:59:53 +0200 |
commit | 2c77347d56edf36c2e8d16338c7e9e348fc44c24 (patch) | |
tree | 11ae88c4e16506f164d3cd39b4e4dd7699b2e7f1 | |
parent | Reformat to pass pre-commit checks. (diff) | |
download | freestyle-hid-2c77347d56edf36c2e8d16338c7e9e348fc44c24.tar freestyle-hid-2c77347d56edf36c2e8d16338c7e9e348fc44c24.tar.gz freestyle-hid-2c77347d56edf36c2e8d16338c7e9e348fc44c24.tar.bz2 freestyle-hid-2c77347d56edf36c2e8d16338c7e9e348fc44c24.tar.lz freestyle-hid-2c77347d56edf36c2e8d16338c7e9e348fc44c24.tar.xz freestyle-hid-2c77347d56edf36c2e8d16338c7e9e348fc44c24.tar.zst freestyle-hid-2c77347d56edf36c2e8d16338c7e9e348fc44c24.zip |
-rw-r--r-- | freestyle_hid/_exceptions.py | 11 | ||||
-rw-r--r-- | freestyle_hid/_session.py | 25 | ||||
-rw-r--r-- | setup.cfg | 2 |
3 files changed, 28 insertions, 10 deletions
diff --git a/freestyle_hid/_exceptions.py b/freestyle_hid/_exceptions.py index 38a822d..2b803f8 100644 --- a/freestyle_hid/_exceptions.py +++ b/freestyle_hid/_exceptions.py @@ -16,3 +16,14 @@ class ChecksumError(Exception): class CommandError(Exception): """Errors related to the command stream.""" + + +class MissingFreeStyleKeys(Exception): + """The freestyle-hid-keys package is missing.""" + + def __init__(self): + super().__init__( + "The freestyle-hid-keys package is missing, please install it from PyPi." + " You can install freestyle-hid[encryption] to select the encryption keys" + " package as an extra dependency." + ) diff --git a/freestyle_hid/_session.py b/freestyle_hid/_session.py index cdfca42..0debaed 100644 --- a/freestyle_hid/_session.py +++ b/freestyle_hid/_session.py @@ -10,16 +10,18 @@ from typing import AnyStr, Callable, Iterator, Optional, Sequence, Tuple import construct -from ._exceptions import ChecksumError, CommandError +from ._exceptions import ChecksumError, CommandError, MissingFreeStyleKeys from ._freestyle_encryption import SpeckCMAC, SpeckEncrypt from ._hidwrapper import HidWrapper -ABBOTT_VENDOR_ID = 0x1A61 +try: + from freestyle_keys import libre2 as libre2_keys + + _HAS_LIBRE2_KEYS = True +except ImportError: + _HAS_LIBRE2_KEYS = False -_AUTH_ENC_MASTER_KEY = 0xDEADBEEF -_AUTH_MAC_MASTER_KEY = 0xDEADBEEF -_SESS_ENC_MASTER_KEY = 0xDEADBEEF -_SESS_MAC_MASTER_KEY = 0xDEADBEEF +ABBOTT_VENDOR_ID = 0x1A61 _INIT_COMMAND = 0x01 _INIT_RESPONSE = 0x71 @@ -125,15 +127,18 @@ class Session: self._encrypted_protocol = product_id in [0x3950] def encryption_handshake(self): + if not _HAS_LIBRE2_KEYS: + raise MissingFreeStyleKeys() + self.send_command(0x05, b"") response = self.read_response() assert response[0] == 0x06 serial = response[1][:13] - crypt = SpeckCMAC(_AUTH_ENC_MASTER_KEY) + crypt = SpeckCMAC(libre2_keys.AUTHORIZATION_ENCRYPTION_KEY) auth_enc_key = crypt.derive("AuthrEnc".encode(), serial) auth_enc = SpeckEncrypt(auth_enc_key) - crypt = SpeckCMAC(_AUTH_MAC_MASTER_KEY) + crypt = SpeckCMAC(libre2_keys.AUTHORIZATION_MAC_KEY) auth_mac_key = crypt.derive("AuthrMAC".encode(), serial) auth_mac = SpeckCMAC(auth_mac_key) @@ -161,11 +166,11 @@ class Session: assert resp_dec[:8] == driver_rand assert resp_dec[8:] == reader_rand - crypt = SpeckCMAC(_SESS_ENC_MASTER_KEY) + crypt = SpeckCMAC(libre2_keys.SESSION_ENCRYPTION_KEY) ses_enc_key = crypt.derive( "SessnEnc".encode(), serial + reader_rand + driver_rand ) - crypt = SpeckCMAC(_SESS_MAC_MASTER_KEY) + crypt = SpeckCMAC(libre2_keys.SESSION_MAC_KEY) ses_mac_key = crypt.derive( "SessnMAC".encode(), serial + reader_rand + driver_rand ) @@ -40,6 +40,8 @@ python_requires = ~= 3.7 [options.extras_require] hidapi = hidapi +encryption = + freestyle-keys tools = click click_log |