Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / firmware / findfamily-tracker / src / tracker_epoch.c

tracker_epoch.c102 lines · 2.8 KiBRaw
1/*
2 * Rotating beacon id and the tracker's notion of wall-clock time.
3 *
4 * The id must be byte-identical to TrackerProtocol.epochId (Kotlin) and
5 * tracker_epoch_id (Rust), or the owner's recomputed ids never match what finders
6 * upload and a sighting silently resolves to nothing. Hence PSA HMAC-SHA256 here
7 * rather than any hand-rolled hashing.
8 */
9#include "ff_tracker.h"
10
11#include <zephyr/kernel.h>
12#include <zephyr/logging/log.h>
13#include <psa/crypto.h>
14
15#include <string.h>
16
17LOG_MODULE_REGISTER(ff_epoch, LOG_LEVEL_INF);
18
19/* TrackerProtocol.EPOCH_DOMAIN */
20static const uint8_t epoch_domain[6] = { 'f', 'f', 't', 'r', 'k', '1' };
21
22uint64_t ff_now_unix(const struct ff_tracker_state *st)
23{
24 int64_t elapsed_ms;
25
26 if (st->unix_at_base == 0U) {
27 return 0U;
28 }
29 /*
30 * k_uptime_get() is LFCLK-backed and keeps counting through sleep, so this
31 * holds across the idle periods between beacons. It does reset to 0 on reboot,
32 * which is why the caller re-persists the time base once per epoch.
33 */
34 elapsed_ms = k_uptime_get() - st->uptime_at_base_ms;
35 if (elapsed_ms < 0) {
36 elapsed_ms = 0;
37 }
38 return st->unix_at_base + (uint64_t)(elapsed_ms / 1000);
39}
40
41uint64_t ff_current_epoch(const struct ff_tracker_state *st)
42{
43 uint64_t now = ff_now_unix(st);
44
45 return now == 0U ? 0U : now / FF_EPOCH_SECONDS;
46}
47
48int ff_epoch_id(const uint8_t *secret, uint64_t epoch, uint8_t *out)
49{
50 psa_status_t status;
51 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
52 psa_key_id_t key = PSA_KEY_ID_NULL;
53 psa_mac_operation_t op = PSA_MAC_OPERATION_INIT;
54 uint8_t mac[32];
55 uint8_t epoch_be[8];
56 size_t mac_len = 0;
57
58 for (int i = 0; i < 8; i++) {
59 epoch_be[i] = (uint8_t)(epoch >> (56 - i * 8));
60 }
61
62 psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_SIGN_MESSAGE);
63 psa_set_key_algorithm(&attr, PSA_ALG_HMAC(PSA_ALG_SHA_256));
64 psa_set_key_type(&attr, PSA_KEY_TYPE_HMAC);
65 psa_set_key_bits(&attr, FF_SECRET_LEN * 8);
66
67 status = psa_import_key(&attr, secret, FF_SECRET_LEN, &key);
68 if (status != PSA_SUCCESS) {
69 LOG_ERR("psa_import_key: %d", (int)status);
70 return -EIO;
71 }
72
73 status = psa_mac_sign_setup(&op, key, PSA_ALG_HMAC(PSA_ALG_SHA_256));
74 if (status != PSA_SUCCESS) {
75 goto out;
76 }
77 status = psa_mac_update(&op, epoch_domain, sizeof(epoch_domain));
78 if (status != PSA_SUCCESS) {
79 goto out;
80 }
81 status = psa_mac_update(&op, epoch_be, sizeof(epoch_be));
82 if (status != PSA_SUCCESS) {
83 goto out;
84 }
85 status = psa_mac_sign_finish(&op, mac, sizeof(mac), &mac_len);
86
87out:
88 if (status != PSA_SUCCESS) {
89 LOG_ERR("HMAC failed: %d", (int)status);
90 (void)psa_mac_abort(&op);
91 (void)psa_destroy_key(key);
92 return -EIO;
93 }
94 (void)psa_destroy_key(key);
95
96 if (mac_len < FF_EPOCH_ID_LEN) {
97 return -EIO;
98 }
99 /* Truncate to 16 bytes, exactly as the Kotlin and Rust sides do. */
100 memcpy(out, mac, FF_EPOCH_ID_LEN);
101 return 0;
102}