| 1 |
"""Masking tools. |
| 2 |
|
| 3 |
This module provides functions for masking sensitive data |
| 4 |
(e.g. MAC addresses) using a deterministic masking approach. |
| 5 |
|
| 6 |
This module exists to avoid leaking sensitive information in logs |
| 7 |
and during debugging. At the same time, it does not guarantee |
| 8 |
that the masked values are unique or non-reversible. |
| 9 |
|
| 10 |
This module does not provide any security guarantees. |
| 11 |
""" |
| 12 |
|
| 13 |
from __future__ import annotations |
| 14 |
|
| 15 |
import hashlib |
| 16 |
import hmac |
| 17 |
import os |
| 18 |
import threading |
| 19 |
|
| 20 |
from asusrouter.tools.identifiers import MacAddress |
| 21 |
|
| 22 |
# Environment variable name for the mask key |
| 23 |
_ENV_KEY_NAME = "ASUSROUTER_MASK_KEY" |
| 24 |
|
| 25 |
|
| 26 |
def _read_key_from_string(key: str) -> bytes: |
| 27 |
"""Read the key from a string.""" |
| 28 |
|
| 29 |
# Try reading as hex |
| 30 |
try: |
| 31 |
return bytes.fromhex(key) |
| 32 |
# Fall back to encoding if hex conversion fails |
| 33 |
except ValueError: |
| 34 |
return key.encode() |
| 35 |
|
| 36 |
|
| 37 |
# Try loading key from the environment variable |
| 38 |
_key_env = os.environ.get(_ENV_KEY_NAME) |
| 39 |
if _key_env is not None: |
| 40 |
_key = _read_key_from_string(_key_env) |
| 41 |
# If there was no key to load, generate a new one for this runtime |
| 42 |
else: |
| 43 |
_key = os.urandom(32) |
| 44 |
|
| 45 |
# Lock to protect concurrent reads/writes of the module-level key |
| 46 |
_key_lock = threading.RLock() |
| 47 |
|
| 48 |
|
| 49 |
def configure_key(key: bytes | str | None) -> None: |
| 50 |
"""Set the key used for deterministic masking.""" |
| 51 |
|
| 52 |
global _key # noqa: PLW0603 |
| 53 |
|
| 54 |
# If it's a string, read it as bytes |
| 55 |
if isinstance(key, str): |
| 56 |
key = _read_key_from_string(key) |
| 57 |
|
| 58 |
# Write key under lock to avoid races with readers |
| 59 |
with _key_lock: |
| 60 |
if isinstance(key, bytes | bytearray): |
| 61 |
_key = bytes(key) |
| 62 |
return |
| 63 |
# Generate a new key |
| 64 |
_key = os.urandom(32) |
| 65 |
return |
| 66 |
|
| 67 |
|
| 68 |
def _hmac_digest(data: bytes, key: bytes | None = None) -> bytes: |
| 69 |
"""Compute HMAC-SHA256 digest.""" |
| 70 |
|
| 71 |
# Use the provided key or the loaded mask key |
| 72 |
if key is not None: |
| 73 |
k = key |
| 74 |
else: |
| 75 |
# Read the module key under lock to ensure a consistent view |
| 76 |
with _key_lock: |
| 77 |
k = _key |
| 78 |
|
| 79 |
return hmac.new(k, data, hashlib.sha256).digest() |
| 80 |
|
| 81 |
|
| 82 |
def mask_mac( |
| 83 |
real_mac: MacAddress | str | bytes | bytearray | int, |
| 84 |
key: bytes | None = None, |
| 85 |
) -> MacAddress: |
| 86 |
"""Deterministically map real MAC -> pseudo-MAC. |
| 87 |
|
| 88 |
- Uses HMAC-SHA256(key, real_mac) and takes the first 6 bytes. |
| 89 |
- Creates a new MacAddress instance from the masked bytes. |
| 90 |
- Sets it to a locally-administered MAC address. |
| 91 |
- Returns the masked MacAddress instance. |
| 92 |
""" |
| 93 |
|
| 94 |
# Make sure we have a MacAddress. If we get a wrong input, |
| 95 |
# it will automatically raise a ValueError |
| 96 |
mac_obj = MacAddress.from_value(real_mac) |
| 97 |
|
| 98 |
# Compute the HMAC digest |
| 99 |
digest = _hmac_digest(mac_obj.to_bytes(), key) |
| 100 |
|
| 101 |
# Use first 6 bytes to create masked MAC |
| 102 |
masked_mac = MacAddress(digest[:6]) |
| 103 |
|
| 104 |
# Set masked MAC as locally-administered |
| 105 |
masked_mac.set_locally_administered() |
| 106 |
|
| 107 |
return masked_mac |
| 108 |
|
| 109 |
|
| 110 |
def get_key_hex() -> str: |
| 111 |
"""Return current key as hex.""" |
| 112 |
|
| 113 |
return _key.hex() |