CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
OathStore.h
Go to the documentation of this file.
1#pragma once
2
3#include "cdc_core/IModule.h"
5#include <cstdint>
6#include <cstddef>
7#include <ctime>
8
9namespace cdc::mod_2fa {
10
14enum class OathAlgorithm : uint8_t {
15 SHA1 = 0,
16 SHA256 = 1,
18};
19
23enum class OathType : uint8_t {
24 TOTP = 0,
25 HOTP = 1,
26 CR = 2
27};
28
32namespace OathFlag {
34 constexpr uint8_t TOUCH_REQUIRED = 0x01;
36 constexpr uint8_t USB_CR_SLOT = 0x02;
37}
38
42struct OathEntry {
43 uint8_t type;
44 char name[16 + 1];
45 char issuer[32 + 1];
46 uint8_t secret[64];
47 uint8_t secretLen;
48 uint8_t algorithm;
49 uint8_t digits;
50 uint32_t period;
51 uint64_t counter;
52 uint8_t flags;
53};
54
55class OathStore {
56public:
57 static constexpr uint8_t NAME_LEN = 16;
58 static constexpr uint8_t ISSUER_LEN = 32;
59 static constexpr uint8_t SECRET_LEN = 64;
60 static constexpr uint8_t DEFAULT_DIGITS = 6;
61 static constexpr uint32_t DEFAULT_PERIOD = 30;
62
63 bool readAccount(uint16_t slot, OathEntry* out);
64 bool addAccount(uint8_t type, const char* name, const char* issuer,
65 const char* secretBase32, uint8_t digits, uint32_t period,
66 uint8_t algorithm, uint64_t counter, uint8_t flags = 0);
67 bool updateAccount(uint16_t slot, uint8_t type, const char* name, const char* issuer,
68 const char* secretBase32, uint8_t digits, uint32_t period,
69 uint8_t algorithm, uint64_t counter, uint8_t flags = 0);
70 bool deleteAccount(uint16_t slot);
71
87 int challengeResponse(const char* entryName, const uint8_t* challenge, size_t clen,
88 uint8_t* out, bool* touchRequiredOut = nullptr);
89
105 int challengeResponseUsbSlot(const uint8_t* challenge, size_t clen, uint8_t* out,
106 bool* touchRequiredOut = nullptr);
107
113 bool findUsbCrSlot(uint16_t* slotOut) const;
114
123 void clearUsbCrFlagExcept(uint16_t keepSlot);
124
131 bool findByName(const char* name, uint16_t* slotOut) const;
132
145 int8_t generateCode(uint16_t slot, char* codeOut, size_t codeOutLen);
146
147 bool isTimeValid() const;
148 uint8_t timeRemaining(uint32_t period) const;
149
150 static OathStore& instance();
151
153 uint16_t capacity() const { return slots_.capacity(); }
154 bool toPhysicalSlot(uint16_t logicalIndex, uint16_t* slotOut) const {
155 return slots_.toPhysicalSlot(logicalIndex, slotOut);
156 }
157 bool toLogicalSlot(uint16_t slot, uint16_t* logicalIndexOut) const {
158 return slots_.toLogicalSlot(slot, logicalIndexOut);
159 }
160 bool hasSlotRange() const { return slots_.hasSlotRange(); }
161 uint8_t moduleId() const { return slots_.moduleId(); }
162 uint16_t rmemStart() const { return slots_.rmemStart(); }
163 uint16_t rmemEnd() const { return slots_.rmemEnd(); }
164
165private:
166 OathStore() = default;
167
168 uint32_t generate(const uint8_t* secret, size_t secretLen, uint64_t counter,
169 uint8_t digits, OathAlgorithm algorithm) const;
170 bool hmacCompute(OathAlgorithm algo, const uint8_t* key, size_t keyLen,
171 const uint8_t* data, size_t dataLen,
172 uint8_t* output, size_t* outputLen) const;
173 bool persistCounter(uint16_t slot, const OathEntry& entry, uint64_t counter);
174
176};
177
178} // namespace cdc::mod_2fa
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]
uint8_t flags
Manages logical-to-physical RMEM slot mapping for module storage layers.
Definition SlotManager.h:19
int challengeResponseUsbSlot(const uint8_t *challenge, size_t clen, uint8_t *out, bool *touchRequiredOut=nullptr)
Computes the raw HMAC challenge-response for the USB-CR slot entry.
bool toPhysicalSlot(uint16_t logicalIndex, uint16_t *slotOut) const
Definition OathStore.h:154
bool isTimeValid() const
Returns whether system time is considered valid for TOTP.
void clearUsbCrFlagExcept(uint16_t keepSlot)
Clears the USB-CR-slot flag on every entry except keepSlot.
int challengeResponse(const char *entryName, const uint8_t *challenge, size_t clen, uint8_t *out, bool *touchRequiredOut=nullptr)
Computes the raw HMAC challenge-response for a CR entry by name.
static constexpr uint8_t NAME_LEN
Definition OathStore.h:57
static constexpr uint8_t SECRET_LEN
Definition OathStore.h:59
bool hasSlotRange() const
Definition OathStore.h:160
uint16_t rmemEnd() const
Definition OathStore.h:163
bool toLogicalSlot(uint16_t slot, uint16_t *logicalIndexOut) const
Definition OathStore.h:157
uint8_t moduleId() const
Definition OathStore.h:161
bool findUsbCrSlot(uint16_t *slotOut) const
Finds the logical slot of the CR entry flagged as the USB-CR slot.
bool addAccount(uint8_t type, const char *name, const char *issuer, const char *secretBase32, uint8_t digits, uint32_t period, uint8_t algorithm, uint64_t counter, uint8_t flags=0)
Adds a new OATH entry from a Base32 secret.
static OathStore & instance()
Returns singleton OATH store instance.
static constexpr uint8_t DEFAULT_DIGITS
Definition OathStore.h:60
int8_t generateCode(uint16_t slot, char *codeOut, size_t codeOutLen)
Renders the current code for an entry into codeOut.
uint16_t capacity() const
Definition OathStore.h:153
bool updateAccount(uint16_t slot, uint8_t type, const char *name, const char *issuer, const char *secretBase32, uint8_t digits, uint32_t period, uint8_t algorithm, uint64_t counter, uint8_t flags=0)
Updates an existing OATH entry.
uint16_t rmemStart() const
Definition OathStore.h:162
static constexpr uint32_t DEFAULT_PERIOD
Definition OathStore.h:61
uint8_t timeRemaining(uint32_t period) const
Returns seconds remaining in current TOTP time step.
void setSlotRange(const cdc::core::IModule::SlotRange &range)
Configures logical-to-physical slot mapping for OATH entries.
static constexpr uint8_t ISSUER_LEN
Definition OathStore.h:58
bool findByName(const char *name, uint16_t *slotOut) const
Finds a logical slot index by account name.
bool readAccount(uint16_t slot, OathEntry *out)
Reads one OATH entry from secure-element storage.
bool deleteAccount(uint16_t slot)
Deletes account in logical slot.
Per-entry flag bits stored in OathEntry::flags.
Definition OathStore.h:32
constexpr uint8_t USB_CR_SLOT
Designate this CR entry as the USB OTP-HID slot-2 responder.
Definition OathStore.h:36
constexpr uint8_t TOUCH_REQUIRED
Require an on-device touch confirmation before answering a CR request.
Definition OathStore.h:34
OathType
OATH entry type discriminator.
Definition OathStore.h:23
OathAlgorithm
Hash algorithm used by an OATH entry's HMAC engine.
Definition OathStore.h:14
Unified OATH credential record (TOTP, HOTP, and reserved CR).
Definition OathStore.h:42
uint8_t digits
Output digit count (TOTP/HOTP).
Definition OathStore.h:49
uint8_t secret[64]
Raw HMAC key.
Definition OathStore.h:46
uint8_t algorithm
OathAlgorithm value.
Definition OathStore.h:48
uint8_t secretLen
Valid bytes in secret.
Definition OathStore.h:47
uint64_t counter
Moving factor (HOTP only).
Definition OathStore.h:51
uint8_t type
OathType discriminator.
Definition OathStore.h:43
uint32_t period
TOTP step in seconds (TOTP only).
Definition OathStore.h:50
char issuer[32+1]
Optional issuer text.
Definition OathStore.h:45
char name[16+1]
Account label.
Definition OathStore.h:44
uint8_t flags
Reserved entry flags.
Definition OathStore.h:52