Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / findfamily-tracker / src / tracker_sts.c
tracker_sts.c103 lines · 3.3 KiBRaw
| 1 | /* |
| 2 | * HKDF-SHA256 derivation of the FiRa session material, mirroring |
| 3 | * findfamily/src/main/java/com/vayunmathur/findfamily/tracker/TrackerUwbKeys.kt. |
| 4 | * |
| 5 | * The phone never sends the STS key or our UWB address: the GATT link is unbonded and |
| 6 | * unencrypted, so anything on it could be supplied by a stranger standing nearby. Both |
| 7 | * sides derive from the bind-time beacon secret instead, and the per-find write carries |
| 8 | * only channel/slot params. |
| 9 | * |
| 10 | * A mismatch of a single byte against the Kotlin side shows up as a FiRa session that |
| 11 | * never converges, with nothing in any log to say why — TrackerUwbKeysTest holds the |
| 12 | * fixed vectors this must reproduce. |
| 13 | */ |
| 14 | #include "ff_tracker.h" |
| 15 | |
| 16 | #include <zephyr/kernel.h> |
| 17 | #include <zephyr/logging/log.h> |
| 18 | #include <psa/crypto.h> |
| 19 | |
| 20 | #include <string.h> |
| 21 | |
| 22 | LOG_MODULE_REGISTER(ff_sts, LOG_LEVEL_INF); |
| 23 | |
| 24 | #define STS_KEY_LEN 8 |
| 25 | #define UWB_ADDR_LEN 2 |
| 26 | |
| 27 | static const char sts_info[] = "com.vayunmathur.findfamily/uwb-sts"; |
| 28 | static const char addr_info[] = "com.vayunmathur.findfamily/uwb-addr"; |
| 29 | |
| 30 | /* |
| 31 | * Hkdf.kt defaults the salt to 32 zero bytes rather than omitting it. RFC 5869 makes an |
| 32 | * absent salt equivalent to a zero-filled one of HashLen, so this matches, but PSA needs |
| 33 | * it passed explicitly. |
| 34 | */ |
| 35 | static const uint8_t zero_salt[32]; |
| 36 | |
| 37 | static int hkdf_sha256(const uint8_t *ikm, size_t ikm_len, const uint8_t *info, |
| 38 | size_t info_len, uint8_t *out, size_t out_len) |
| 39 | { |
| 40 | psa_status_t status; |
| 41 | psa_key_derivation_operation_t op = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 42 | |
| 43 | status = psa_key_derivation_setup(&op, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 44 | if (status != PSA_SUCCESS) { |
| 45 | goto err; |
| 46 | } |
| 47 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_SALT, |
| 48 | zero_salt, sizeof(zero_salt)); |
| 49 | if (status != PSA_SUCCESS) { |
| 50 | goto err; |
| 51 | } |
| 52 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 53 | ikm, ikm_len); |
| 54 | if (status != PSA_SUCCESS) { |
| 55 | goto err; |
| 56 | } |
| 57 | status = psa_key_derivation_input_bytes(&op, PSA_KEY_DERIVATION_INPUT_INFO, |
| 58 | info, info_len); |
| 59 | if (status != PSA_SUCCESS) { |
| 60 | goto err; |
| 61 | } |
| 62 | status = psa_key_derivation_output_bytes(&op, out, out_len); |
| 63 | if (status != PSA_SUCCESS) { |
| 64 | goto err; |
| 65 | } |
| 66 | (void)psa_key_derivation_abort(&op); |
| 67 | return 0; |
| 68 | |
| 69 | err: |
| 70 | LOG_ERR("HKDF failed: %d", (int)status); |
| 71 | (void)psa_key_derivation_abort(&op); |
| 72 | return -EIO; |
| 73 | } |
| 74 | |
| 75 | int ff_sts_key(const uint8_t *secret, uint32_t session_id, uint8_t *out) |
| 76 | { |
| 77 | /* info = "…/uwb-sts" || u32_be(sessionId); the trailing NUL is not part of it. */ |
| 78 | uint8_t info[sizeof(sts_info) - 1 + 4]; |
| 79 | |
| 80 | memcpy(info, sts_info, sizeof(sts_info) - 1); |
| 81 | info[sizeof(sts_info) - 1 + 0] = (uint8_t)(session_id >> 24); |
| 82 | info[sizeof(sts_info) - 1 + 1] = (uint8_t)(session_id >> 16); |
| 83 | info[sizeof(sts_info) - 1 + 2] = (uint8_t)(session_id >> 8); |
| 84 | info[sizeof(sts_info) - 1 + 3] = (uint8_t)session_id; |
| 85 | |
| 86 | return hkdf_sha256(secret, FF_SECRET_LEN, info, sizeof(info), out, STS_KEY_LEN); |
| 87 | } |
| 88 | |
| 89 | int ff_uwb_address(const uint8_t *secret, uint8_t *out) |
| 90 | { |
| 91 | int rc = hkdf_sha256(secret, FF_SECRET_LEN, (const uint8_t *)addr_info, |
| 92 | sizeof(addr_info) - 1, out, UWB_ADDR_LEN); |
| 93 | |
| 94 | if (rc) { |
| 95 | return rc; |
| 96 | } |
| 97 | /* 0x0000 and 0xFFFF are reserved by FiRa. Nudge exactly as TrackerUwbKeys does, |
| 98 | * or the two sides would disagree on the address for those secrets. */ |
| 99 | if ((out[0] == 0x00 && out[1] == 0x00) || (out[0] == 0xFF && out[1] == 0xFF)) { |
| 100 | out[1] = 0x01; |
| 101 | } |
| 102 | return 0; |
| 103 | } |