secrets.h.example (3042B)
1 #pragma once 2 // ───────────────────────────────────────────────────────────────────────── 3 // secrets.h — the GROUP KEY (HMAC key for beacon auth) 4 // 5 // THIS FILE (secrets.h.example) IS A TEMPLATE, TRACKED IN GIT. It holds no key. 6 // Badges only see badges built with the same key. 7 // 8 // 1. cp firmware/src/secrets.h.example firmware/src/secrets.h 9 // (secrets.h is gitignored — it must never be committed) 10 // 2. In secrets.h, paste the key between the quotes on the GROUP_PSK_HEX line: 11 // • JOINING AN EXISTING GROUP (e.g. updating your badge): paste the key you 12 // were given. Do NOT generate one — a new key can't see anyone else's 13 // badge, and that shows up as "nobody in range", not as an error. 14 // • STARTING A NEW GROUP: generate one with openssl rand -hex 32 and keep 15 // it in a password manager. It is unrecoverable, and every badge in the 16 // group must be built with the IDENTICAL key. 17 // 3. Build with env oled_v3_prod. 18 // 19 // The key is 64 hex characters (0-9, a-f), no spaces or 0x. It's converted to 20 // bytes at compile time below, and a bad paste stops the build with a message. 21 // ───────────────────────────────────────────────────────────────────────── 22 #include <stdint.h> 23 #include <stddef.h> 24 25 #define GROUP_PSK_HEX "" 26 27 // ── nothing to edit below this line ────────────────────────────────────── 28 29 namespace group_psk { 30 constexpr int nibble(char c) { 31 return (c >= '0' && c <= '9') ? c - '0' 32 : (c >= 'a' && c <= 'f') ? c - 'a' + 10 33 : (c >= 'A' && c <= 'F') ? c - 'A' + 10 : -1; 34 } 35 template <size_t N> constexpr bool valid(const char (&s)[N]) { 36 if (N != 65) return false; // 64 hex chars + the string's NUL 37 for (size_t i = 0; i < 64; i++) if (nibble(s[i]) < 0) return false; 38 return true; 39 } 40 struct Key { uint8_t b[32]; }; 41 template <size_t N> constexpr Key parse(const char (&s)[N]) { 42 Key k{}; 43 if (valid(s)) 44 for (size_t i = 0; i < 32; i++) k.b[i] = uint8_t(nibble(s[2 * i]) << 4 | nibble(s[2 * i + 1])); 45 return k; 46 } 47 } // namespace group_psk 48 49 static_assert(sizeof(GROUP_PSK_HEX) == 65, 50 "secrets.h: GROUP_PSK_HEX must be exactly 64 hex characters - paste the whole key between the quotes"); 51 static_assert(group_psk::valid(GROUP_PSK_HEX), 52 "secrets.h: GROUP_PSK_HEX may only contain 0-9 and a-f (no spaces, colons or 0x)"); 53 54 static constexpr group_psk::Key GROUP_PSK_KEY = group_psk::parse(GROUP_PSK_HEX); 55 static const uint8_t* const GROUP_PSK = GROUP_PSK_KEY.b; 56 static const size_t GROUP_PSK_LEN = sizeof(GROUP_PSK_KEY.b); 57 58 #define GROUP_PSK_SET 1 // tells config.h a key was provided here