CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
GpgRecvStore.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstddef>
5
6namespace cdc::mod_gpg {
7
8constexpr uint8_t kGpgRecvFlagVerified = 0x01;
9
23#pragma pack(push, 1)
25 uint8_t curve;
26 char user_id[64];
27 uint8_t pubkey[64];
28 uint8_t pubkey_len;
29 uint32_t created_at;
30 uint8_t fingerprint_v4[20];
31 uint8_t fingerprint_v5[32];
32 uint32_t received_at;
33 uint8_t my_signature[64];
34 uint8_t sig_len;
36 // DEC (RFC 6637 ECDH P-256) encryption subkey and the two signatures the
37 // peer produced over it. Cannot be forged locally; replayed verbatim into
38 // the armored export. All-zero when absent.
39 uint8_t pubkey_dec[64];
41 uint8_t owner_self_sig[64];
42 uint8_t dec_binding_sig[64];
43 uint8_t flags;
44};
45#pragma pack(pop)
46
47static_assert(sizeof(gpg_recv_key_t) ==
48 (1 + 64 + 64 + 1 + 4 + 20 + 32 + 4 + 64 + 1 + 4 +
49 64 + 4 + 64 + 64 + 1),
50 "gpg_recv_key_t layout drift");
51
60 char nvs_key[16];
61 uint32_t received_at;
62 uint8_t flags;
63};
64
72class GpgRecvStore {
73public:
75 static constexpr uint8_t kMaxKeys = 128;
76
77 static GpgRecvStore& instance();
78
80 bool addKey(const gpg_recv_key_t& key);
81
83 uint8_t count();
84
91 uint8_t listIndex(gpg_recv_index_entry_t* out, uint8_t max);
92
94 bool getKey(uint8_t index, gpg_recv_key_t* out);
95
97 bool deleteKey(uint8_t index);
98
100 bool setSignature(uint8_t index,
101 const uint8_t* sig, uint8_t sig_len,
102 uint32_t sig_created_at,
103 uint8_t flags);
104
105private:
106 GpgRecvStore() = default;
107 GpgRecvStore(const GpgRecvStore&) = delete;
108 GpgRecvStore& operator=(const GpgRecvStore&) = delete;
109
111 static void deriveKeyName(const uint8_t fp_v4[20], char out[16]);
112
114 bool readByName(const char* nvs_key, gpg_recv_key_t* out);
115
117 bool writeByName(const char* nvs_key, const gpg_recv_key_t& key);
118
120 bool resolveKeyName(uint8_t index, char out[16]);
121};
122
123} // namespace cdc::mod_gpg
uint8_t flags
bool addKey(const gpg_recv_key_t &key)
Persist a new key. Replaces an existing entry if the fingerprint matches.
bool deleteKey(uint8_t index)
Remove one key by sorted index. No-op if index is out of range.
bool getKey(uint8_t index, gpg_recv_key_t *out)
Load one key by sorted index (0..count()-1).
static GpgRecvStore & instance()
bool setSignature(uint8_t index, const uint8_t *sig, uint8_t sig_len, uint32_t sig_created_at, uint8_t flags)
Attach a cross-signature, its creation time and flag bits to an entry.
uint8_t listIndex(gpg_recv_index_entry_t *out, uint8_t max)
Build the sorted index (oldest first).
uint8_t count()
Number of stored keys.
static constexpr uint8_t kMaxKeys
Hard ceiling. Past this addKey rejects further inserts.
constexpr uint8_t kGpgRecvFlagVerified
Definition GpgRecvStore.h:8
Sort entry used to expose a stable ordered index over NVS keys.
char nvs_key[16]
uint8_t flags
uint32_t received_at
One GPG public key received from another badge.