CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
PasswordStore.h
Go to the documentation of this file.
1#pragma once
2
3#include "cdc_core/IModule.h"
6#include <cstdint>
7#include <cstddef>
8
9namespace cdc::mod_password {
10
11constexpr uint8_t PASSWORD_TITLE_LEN = 24;
12constexpr uint8_t PASSWORD_USERNAME_LEN = 64;
13constexpr uint8_t PASSWORD_PASSWORD_LEN = 64;
14constexpr uint8_t PASSWORD_URL_LEN = 96;
15
16constexpr size_t PASSWORD_PAYLOAD_MAX =
17 cdc::hal::ISecureElement::RMEM_SLOT_SIZE - sizeof(cdc::hal::ISecureElement::RMemHeader);
18constexpr size_t PASSWORD_FIXED_PAYLOAD =
21static_assert(PASSWORD_NOTES_LEN > 0, "Password notes length must be positive");
22
31
32class PasswordStore {
33public:
34 static constexpr uint8_t TITLE_LEN = PASSWORD_TITLE_LEN;
35 static constexpr uint8_t USERNAME_LEN = PASSWORD_USERNAME_LEN;
36 static constexpr uint8_t PASSWORD_LEN = PASSWORD_PASSWORD_LEN;
37 static constexpr uint8_t URL_LEN = PASSWORD_URL_LEN;
38 static constexpr uint8_t TOTP_SLOT_NONE = 0xFF;
39
40 static constexpr size_t PAYLOAD_MAX = PASSWORD_PAYLOAD_MAX;
41 static constexpr size_t FIXED_PAYLOAD = PASSWORD_FIXED_PAYLOAD;
42 static constexpr size_t NOTES_LEN = PASSWORD_NOTES_LEN;
43
44 struct EntryIndex {
45 char title[TITLE_LEN + 1];
46 uint16_t slot;
47 };
48
49 static PasswordStore& instance();
50
51 bool readEntry(uint16_t slot, PasswordEntry* out) const;
52 bool addEntry(const PasswordEntry& entry);
53 bool updateEntry(uint16_t slot, const PasswordEntry& entry);
54 bool deleteEntry(uint16_t slot);
55
56 bool listEntriesSorted(EntryIndex* entries, uint16_t maxEntries, uint16_t* countOut) const;
57 bool findFreeLogicalSlot(uint16_t* logicalSlotOut) const;
58 bool findByTitle(const char* title, uint16_t* logicalSlotOut) const;
59
61 uint16_t capacity() const { return slots_.capacity(); }
62 bool toPhysicalSlot(uint16_t logicalIndex, uint16_t* slotOut) const {
63 return slots_.toPhysicalSlot(logicalIndex, slotOut);
64 }
65 bool toLogicalSlot(uint16_t slot, uint16_t* logicalIndexOut) const {
66 return slots_.toLogicalSlot(slot, logicalIndexOut);
67 }
68 bool hasSlotRange() const { return slots_.hasSlotRange(); }
69 uint8_t moduleId() const { return slots_.moduleId(); }
70 uint16_t rmemStart() const { return slots_.rmemStart(); }
71 uint16_t rmemEnd() const { return slots_.rmemEnd(); }
72
73private:
74 PasswordStore() = default;
75
76 static int compareTitles(const char* a, const char* b);
77
79};
80
81} // namespace cdc::mod_password
Manages logical-to-physical RMEM slot mapping for module storage layers.
Definition SlotManager.h:19
static constexpr uint16_t RMEM_SLOT_SIZE
bool toPhysicalSlot(uint16_t logicalIndex, uint16_t *slotOut) const
static constexpr uint8_t PASSWORD_LEN
bool updateEntry(uint16_t slot, const PasswordEntry &entry)
Updates existing password entry.
static constexpr uint8_t TITLE_LEN
bool addEntry(const PasswordEntry &entry)
Adds a new password entry into first free slot.
static constexpr size_t NOTES_LEN
static constexpr uint8_t TOTP_SLOT_NONE
static constexpr size_t FIXED_PAYLOAD
bool findFreeLogicalSlot(uint16_t *logicalSlotOut) const
Finds first free logical slot in this module's range.
static constexpr uint8_t USERNAME_LEN
static PasswordStore & instance()
Returns singleton password store instance.
bool listEntriesSorted(EntryIndex *entries, uint16_t maxEntries, uint16_t *countOut) const
Lists entries sorted alphabetically by title.
bool deleteEntry(uint16_t slot)
Deletes entry at logical slot index.
static constexpr uint8_t URL_LEN
bool toLogicalSlot(uint16_t slot, uint16_t *logicalIndexOut) const
static constexpr size_t PAYLOAD_MAX
bool findByTitle(const char *title, uint16_t *logicalSlotOut) const
Finds the logical slot of an entry with a matching title.
bool readEntry(uint16_t slot, PasswordEntry *out) const
Reads one password entry from secure-element storage.
void setSlotRange(const cdc::core::IModule::SlotRange &range)
Configures logical-to-physical slot mapping for password entries.
constexpr uint8_t PASSWORD_URL_LEN
constexpr uint8_t PASSWORD_USERNAME_LEN
constexpr size_t PASSWORD_FIXED_PAYLOAD
constexpr size_t PASSWORD_PAYLOAD_MAX
constexpr size_t PASSWORD_NOTES_LEN
constexpr uint8_t PASSWORD_PASSWORD_LEN
constexpr uint8_t PASSWORD_TITLE_LEN
char password[PASSWORD_PASSWORD_LEN+1]
char url[PASSWORD_URL_LEN+1]
char title[PASSWORD_TITLE_LEN+1]
char notes[PASSWORD_NOTES_LEN+1]
char username[PASSWORD_USERNAME_LEN+1]