CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cdc::mod_2fa Namespace Reference

Namespaces

namespace  OathFlag
 Per-entry flag bits stored in OathEntry::flags.

Classes

struct  OathEntry
 Unified OATH credential record (TOTP, HOTP, and reserved CR). More...
class  OathStore
class  TwoFaModule
struct  PendingChallenge
 Pending challenge handed from the BLE host task to the main task. More...
struct  PendingConfirm
 Response awaiting an on-device touch confirmation. More...
struct  OathPayload
class  OathCodeView
struct  WizardState

Enumerations

enum class  OathAlgorithm : uint8_t { SHA1 = 0 , SHA256 = 1 , SHA512 = 2 }
 Hash algorithm used by an OATH entry's HMAC engine. More...
enum class  OathType : uint8_t { TOTP = 0 , HOTP = 1 , CR = 2 }
 OATH entry type discriminator. More...

Functions

bool ble_chalresp_init ()
 BLE GATT challenge-response transport.
void ble_chalresp_deinit ()
 Tears down the BLE CR subsystem and removes GATT callbacks.
void ble_chalresp_tick (uint32_t nowMs)
 Main-task tick: processes a pending challenge (confirm + notify).
static IBluetoothControllergetBle ()
 Returns shared Bluetooth controller instance.
static void notifyResponse (uint16_t connHandle, const uint8_t *data, size_t len)
 Sends a response notification to the requesting connection.
static void deliverConfirmedResponse ()
 Computes and notifies the response stashed in s_confirm.
static void onTouchConfirm (void *userData)
 Touch-confirm accepted: notify the prepared response.
static void onTouchCancel (void *userData)
 Touch-confirm declined: discard the prepared response.
static int onChallengeWrite (uint16_t connHandle, uint16_t, const uint8_t *data, uint16_t len)
 GATT challenge write callback (BLE host task).
static bool registerGattService ()
 Registers the CR GATT service and characteristics.
static void onDisconnect (uint16_t connHandle, int reason)
 Clears pending request state if the requesting peer disconnects.
static bool validateOathParams (uint8_t type, uint8_t &digits, uint32_t &period, uint8_t &algorithm)
 Validates OATH entry parameters and clamps to defaults when invalid.
static int base32CharValue (char c)
 Converts one Base32 character into 5-bit value.
static int base32Decode (const char *encoded, uint8_t *out, size_t outMax)
 Decodes Base32 secret into raw bytes.
static bool writePayload (uint16_t physSlot, const char *name, uint8_t type, const char *issuer, const uint8_t *secret, uint8_t secretLen, uint8_t digits, uint32_t period, uint64_t counter, uint8_t algorithm, uint8_t flags)
 Builds and persists an OATH payload to a physical slot.
static void formatCode (uint32_t code, uint8_t digits, char *codeOut, size_t codeOutLen)
 Formats a numeric code into codeOut with leading zeros.
static void registerStrings ()
static uint8_t parseAlgo (const char *token)
 Parses textual or numeric algorithm identifiers into store values.
static uint8_t parseType (const char *token)
 Parses a textual or numeric entry-type token.
static bool findSlotByIndex (uint16_t index, uint16_t *slotOut)
 Resolves a displayed list index to the logical OATH slot number.
static void cmd_totp_list (const char *args)
 Serial command handler printing all configured OATH entries.
static void cmd_totp_add (const char *args)
 Serial command handler adding an OATH entry from tokens.
static void cmd_totp_del (const char *args)
 Serial command handler deleting an OATH entry by index.
static void cmd_totp_get (const char *args)
 Serial command handler generating one code by index.
static int hexDecode (const char *hex, uint8_t *out, size_t outMax)
 Decodes a hex string into bytes.
static void cmd_chalresp (const char *args)
 Serial command computing a raw challenge-response for a named CR entry.
static void cmd_totp (const char *args)
static void registerCommands ()
 Registers serial commands exposed by the 2FA module.
static void wizardEdit (uint16_t slot)
 OATH code detail view implementation.
static void pushT9WizardStep (const char *title, const char *initialText, uint16_t maxLen, ui::T9InputView::SaveCallback onSave)
 Pushes a configured T9 input step for the account wizard flow.
static void freeListBuffers ()
 Frees dynamically allocated list buffers used by the account list.
static void rebuildList ()
 Rebuilds the account list view content from Tropic storage cache.
static void onListSelect (uint16_t index, void *userData)
 Handles selection from the account list.
static void wizardStart ()
 Starts the add-account wizard with default values.
static void onWizardType (uint16_t index, void *userData)
 Saves selected entry type and opens the name step.
static void onWizardName (const char *text)
 Saves wizard account name and opens secret step.
static void onWizardSecret (const char *text)
 Saves wizard secret and opens issuer step.
static void onWizardIssuer (const char *text)
 Saves wizard issuer and opens digit-selection step.
static void onWizardDigits (uint16_t index, void *userData)
 Saves selected code length and opens algorithm-selection step.
static void onWizardAlgo (uint16_t index, void *userData)
 Saves selected algorithm; opens period step (TOTP) or finishes (HOTP).
static void onWizardPeriod (uint16_t index, void *userData)
 Saves selected period and finalizes add/edit operation.
static void onWizardTouch (uint16_t index, void *userData)
 Saves the CR touch-confirm choice and finalizes the entry.
static void onWizardUsbCr (uint16_t index, void *userData)
 Saves the USB-CR-slot designation and finalizes the entry.
static void pushAlgoStep ()
 Pushes the algorithm-selection step.
static void pushTouchStep ()
 Pushes the CR touch-confirm toggle step.
static void pushUsbCrStep ()
 Pushes the CR USB-slot-2 designation step.
static void wizardFinish ()
 Validates wizard data and persists account changes.
static void base32Encode (const uint8_t *data, size_t dataLen, char *out, size_t outMax)
 Encodes binary secret bytes into unpadded Base32 text.
static bool ensureListBuffers ()
 Ensures account list backing buffers are allocated for current capacity.
static bool importOathEntry (const cJSON *entry, void *user)
 Maps and upserts one OATH entry from its JSON representation.
const char * skipSpaces (const char *s)
 Advances over leading ASCII whitespace in a C string.
const char * nextToken (const char *s, char *out, size_t outSize)
 Extracts one whitespace-delimited token from a string.

Variables

static const uint8_t CR_SVC_UUID [16]
 CR service UUID: 8E2F1F30-8B5D-4D7A-9A6E-4C9D6A8B1A01 (little-endian).
static const uint8_t CR_CHALLENGE_UUID [16]
 Challenge characteristic UUID (...1F31...), write.
static const uint8_t CR_RESPONSE_UUID [16]
 Response characteristic UUID (...1F32...), read + notify.
static constexpr uint16_t INVALID_HANDLE = 0xFFFF
static constexpr size_t MAX_NAME_LEN = 16
 Max challenge payload (entry name + NUL + challenge bytes) accepted per write.
static constexpr size_t MAX_CHALLENGE_LEN = 128
static constexpr size_t MAX_FRAME_LEN = MAX_NAME_LEN + 1 + MAX_CHALLENGE_LEN
static bool s_initialized = false
static uint16_t s_challenge_handle = 0
static uint16_t s_response_handle = 0
static GattCharacteristic s_gattChars [2] = {}
static GattServiceDef s_gattSvcDef = {}
static IBluetoothController::ListenerToken s_tokDisconn = IBluetoothController::INVALID_LISTENER
static SemaphoreHandle_t s_mutex = nullptr
static uint8_t s_challengeBuf [MAX_CHALLENGE_LEN] = {}
static PendingChallenge s_pending = {}
static PendingConfirm s_confirm = {}
static constexpr uint8_t OATH_DIGITS_MIN = 6
 Allowed OATH digit count range per RFC 4226 / RFC 6238.
static constexpr uint8_t OATH_DIGITS_MAX = 8
static constexpr uint32_t TOTP_PERIOD_MIN = 15
 Allowed TOTP period range in seconds.
static constexpr uint32_t TOTP_PERIOD_MAX = 300
static const uint32_t POWERS_10 []
constexpr ui::I18nEntry kStrings []
static constexpr const char * CMD_MODULE = "totp"
 Serial command handlers for 2FA module.
static constexpr size_t SECRET_B32_LEN = 128
static bool s_commandsRegistered = false
static const cdc::serial::SubCommand kTotpSubs []
 Sub-command table for the TOTP serial command group.
static ui::ListView s_listView
 2FA module UI state.
static ui::T9InputView s_t9Input
static ui::ListView s_typeMenu
static ui::ListView s_digitsMenu
static ui::ListView s_algoMenu
static ui::ListView s_periodMenu
static ui::ListView s_touchMenu
static ui::ListView s_usbCrMenu
static OathCodeView s_codeView
static bool s_viewsInitialized = false
static ui::ListItems_listItems = nullptr
 Dynamic list buffers released by freeListBuffers.
static char(* s_listLabels )[24] = nullptr
static uint16_t * s_listSlots = nullptr
static uint16_t s_accountCount = 0
static uint16_t s_capacity = 0
static WizardState s_wizard = {}
static constexpr int kSchemaVer = 1
 Schema version written to and expected from the 2FA backup section.
static constexpr size_t kBase32BufLen = 103 + 1
 Maximum Base32 string length for the longest supported secret (64 bytes raw).

Enumeration Type Documentation

◆ OathAlgorithm

enum class cdc::mod_2fa::OathAlgorithm : uint8_t
strong

Hash algorithm used by an OATH entry's HMAC engine.

Enumerator
SHA1 
SHA256 
SHA512 

Definition at line 14 of file OathStore.h.

◆ OathType

enum class cdc::mod_2fa::OathType : uint8_t
strong

OATH entry type discriminator.

Enumerator
TOTP 
HOTP 
CR 

Definition at line 23 of file OathStore.h.

Function Documentation

◆ base32CharValue()

int cdc::mod_2fa::base32CharValue ( char c)
static

Converts one Base32 character into 5-bit value.

Parameters
cInput character.
Returns
Value in range 0..31, or -1 if invalid.

Definition at line 98 of file OathStore.cpp.

Referenced by base32Decode().

◆ base32Decode()

int cdc::mod_2fa::base32Decode ( const char * encoded,
uint8_t * out,
size_t outMax )
static

Decodes Base32 secret into raw bytes.

Parameters
encodedBase32 input string.
outOutput byte buffer.
outMaxOutput capacity.
Returns
Number of decoded bytes, or -1 on error.

Definition at line 112 of file OathStore.cpp.

References base32CharValue().

Referenced by cdc::mod_2fa::OathStore::addAccount(), and cdc::mod_2fa::OathStore::updateAccount().

◆ base32Encode()

void cdc::mod_2fa::base32Encode ( const uint8_t * data,
size_t dataLen,
char * out,
size_t outMax )
static

Encodes binary secret bytes into unpadded Base32 text.

Parameters
dataInput binary payload.
dataLenInput length in bytes.
outOutput Base32 buffer.
outMaxOutput buffer size.

Definition at line 809 of file TwoFaModule.cpp.

Referenced by cdc::mod_2fa::TwoFaModule::exportBackup(), and wizardEdit().

◆ ble_chalresp_deinit()

void cdc::mod_2fa::ble_chalresp_deinit ( )

Tears down the BLE CR subsystem and removes GATT callbacks.

Definition at line 280 of file ble_chalresp.cpp.

References CR_SVC_UUID, BleUuid::from128(), getBle(), IBluetoothController::INVALID_LISTENER, s_confirm, s_initialized, s_mutex, s_pending, and s_tokDisconn.

Referenced by cdc::mod_2fa::TwoFaModule::stop().

◆ ble_chalresp_init()

bool cdc::mod_2fa::ble_chalresp_init ( )

BLE GATT challenge-response transport.

Initializes the BLE CR subsystem and registers the GATT service.

Exposes a small GATT service with a write characteristic (challenge) and a notify characteristic (response). A connected host writes a challenge frame, the badge optionally requires an on-device touch confirmation, computes the raw HMAC via the registered IChallengeResponder, and notifies the result.

Implemented purely through IBluetoothController; this file never touches NimBLE directly. The write callback runs on the BLE host task and only records the pending request; ble_chalresp_tick() (main task) performs the confirmation and notification so the E-Paper UI is touched only there.

Initializes the BLE CR subsystem and registers the GATT service.

Returns
true on success.
true on success.

Definition at line 238 of file ble_chalresp.cpp.

References getBle(), LOG_E, LOG_I, onDisconnect(), registerGattService(), s_initialized, s_mutex, s_tokDisconn, and TAG.

Referenced by cdc::mod_2fa::TwoFaModule::init().

◆ ble_chalresp_tick()

◆ cmd_chalresp()

void cdc::mod_2fa::cmd_chalresp ( const char * args)
static

Serial command computing a raw challenge-response for a named CR entry.

Looks up the CR entry by name, computes HMAC(secret, challenge) using the entry's algorithm (SHA1 or SHA256), and prints the hex response. The serial path is trusted (AUTH-gated) and does not require a touch confirmation.

Parameters
argsCommand arguments (<name> <hex-challenge>).

Definition at line 365 of file TwoFaModule.cpp.

References cdc::mod_2fa::OathStore::challengeResponse(), hexDecode(), cdc::mod_2fa::OathStore::instance(), cdc::core::IChallengeResponder::MAX_RESPONSE_LEN, name, cdc::mod_2fa::OathStore::NAME_LEN, nextToken(), and cdc::serial::Console::printf().

Referenced by registerCommands().

◆ cmd_totp()

void cdc::mod_2fa::cmd_totp ( const char * args)
static

Definition at line 421 of file TwoFaModule.cpp.

References cdc::serial::dispatchSubCommand(), and kTotpSubs.

Referenced by registerCommands().

◆ cmd_totp_add()

void cdc::mod_2fa::cmd_totp_add ( const char * args)
static

Serial command handler adding an OATH entry from tokens.

Parameters
argsCommand arguments (<type> <name> <secret> [issuer] [digits] [period] [algo] [counter]).

Definition at line 207 of file TwoFaModule.cpp.

References cdc::mod_2fa::OathStore::addAccount(), cdc::mod_2fa::OathStore::DEFAULT_DIGITS, cdc::mod_2fa::OathStore::DEFAULT_PERIOD, cdc::mod_2fa::OathStore::instance(), cdc::mod_2fa::OathStore::ISSUER_LEN, name, cdc::mod_2fa::OathStore::NAME_LEN, nextToken(), parseAlgo(), parseType(), cdc::serial::Console::printf(), and SECRET_B32_LEN.

◆ cmd_totp_del()

void cdc::mod_2fa::cmd_totp_del ( const char * args)
static

Serial command handler deleting an OATH entry by index.

Parameters
argsCommand arguments (<index>).

Definition at line 263 of file TwoFaModule.cpp.

References cdc::mod_2fa::OathStore::deleteAccount(), findSlotByIndex(), cdc::mod_2fa::OathStore::instance(), and cdc::serial::Console::printf().

◆ cmd_totp_get()

void cdc::mod_2fa::cmd_totp_get ( const char * args)
static

Serial command handler generating one code by index.

For HOTP entries this advances and persists the moving counter.

Parameters
argsCommand arguments (<index>).

Definition at line 285 of file TwoFaModule.cpp.

References cdc::mod_2fa::OathEntry::counter, findSlotByIndex(), cdc::mod_2fa::OathStore::generateCode(), HOTP, cdc::mod_2fa::OathStore::instance(), cdc::mod_2fa::OathEntry::issuer, cdc::serial::Console::printf(), and cdc::mod_2fa::OathEntry::type.

◆ cmd_totp_list()

void cdc::mod_2fa::cmd_totp_list ( const char * args)
static

Serial command handler printing all configured OATH entries.

Parameters
argsUnused command arguments.

Definition at line 165 of file TwoFaModule.cpp.

References CR, cdc::core::TropicStorage::forEachSlot(), HOTP, cdc::core::TropicStorage::instance(), cdc::mod_2fa::OathStore::instance(), moduleId, cdc::serial::Console::printf(), and cdc::mod_2fa::OathEntry::type.

◆ deliverConfirmedResponse()

void cdc::mod_2fa::deliverConfirmedResponse ( )
static

Computes and notifies the response stashed in s_confirm.

Runs on the main task (touch-confirm callback or direct tick path).

Definition at line 115 of file ble_chalresp.cpp.

References LOG_I, notifyResponse(), s_confirm, s_mutex, and TAG.

Referenced by ble_chalresp_tick(), and onTouchConfirm().

◆ ensureListBuffers()

bool cdc::mod_2fa::ensureListBuffers ( )
static

Ensures account list backing buffers are allocated for current capacity.

Returns
true if buffers are ready for use.

Definition at line 843 of file TwoFaModule.cpp.

References cdc::mod_2fa::OathStore::capacity(), cdc::mod_2fa::OathStore::instance(), s_capacity, s_listItems, s_listLabels, and s_listSlots.

Referenced by rebuildList().

◆ findSlotByIndex()

bool cdc::mod_2fa::findSlotByIndex ( uint16_t index,
uint16_t * slotOut )
static

Resolves a displayed list index to the logical OATH slot number.

Parameters
indexUI list index.
slotOutOutput logical slot.
Returns
true if a matching slot was found.

Definition at line 126 of file TwoFaModule.cpp.

References cdc::core::TropicStorage::forEachSlot(), cdc::core::TropicStorage::instance(), and cdc::mod_2fa::OathStore::instance().

Referenced by cmd_totp_del(), and cmd_totp_get().

◆ formatCode()

void cdc::mod_2fa::formatCode ( uint32_t code,
uint8_t digits,
char * codeOut,
size_t codeOutLen )
static

Formats a numeric code into codeOut with leading zeros.

Parameters
codeNumeric code value.
digitsNumber of digits to pad to.
codeOutOutput text buffer.
codeOutLenOutput buffer size.

Definition at line 505 of file OathStore.cpp.

References OATH_DIGITS_MAX, and OATH_DIGITS_MIN.

Referenced by cdc::mod_2fa::OathStore::generateCode().

◆ freeListBuffers()

void cdc::mod_2fa::freeListBuffers ( )
static

Frees dynamically allocated list buffers used by the account list.

Definition at line 773 of file TwoFaModule.cpp.

References s_accountCount, s_capacity, s_listItems, s_listLabels, and s_listSlots.

Referenced by cdc::mod_2fa::TwoFaModule::stop().

◆ getBle()

IBluetoothController * cdc::mod_2fa::getBle ( )
static

Returns shared Bluetooth controller instance.

Returns
Controller pointer or nullptr.

Definition at line 94 of file ble_chalresp.cpp.

References cdc::hal::getBluetoothControllerInstance().

Referenced by ble_chalresp_deinit(), ble_chalresp_init(), notifyResponse(), and registerGattService().

◆ hexDecode()

int cdc::mod_2fa::hexDecode ( const char * hex,
uint8_t * out,
size_t outMax )
static

Decodes a hex string into bytes.

Parameters
hexNull-terminated hex text (even number of nibbles, no separators).
outOutput byte buffer.
outMaxOutput capacity.
Returns
Number of decoded bytes, or -1 on malformed input or overflow.

Definition at line 334 of file TwoFaModule.cpp.

Referenced by cmd_chalresp().

◆ importOathEntry()

bool cdc::mod_2fa::importOathEntry ( const cJSON * entry,
void * user )
static

Maps and upserts one OATH entry from its JSON representation.

If an entry with the same name already exists it is overwritten (backup wins); otherwise a new slot is allocated. Malformed or unstorable entries return false so the caller can tally them as failed.

Parameters
entryJSON array element.
userUnused.
Returns
true if the entry was stored.

Definition at line 1450 of file TwoFaModule.cpp.

References cdc::mod_2fa::OathStore::addAccount(), cdc::mod_2fa::OathStore::findByName(), flags, cdc::mod_2fa::OathStore::instance(), LOG_W, name, TAG, and cdc::mod_2fa::OathStore::updateAccount().

Referenced by cdc::mod_2fa::TwoFaModule::importBackup().

◆ nextToken()

const char * cdc::core::nextToken ( const char * s,
char * out,
size_t outSize )
inline

Extracts one whitespace-delimited token from a string.

Supports \ (backslash-space) as an escaped literal space inside a token, so tokens may contain space characters when escaped.

Parameters
sInput cursor position.
outOutput token buffer (will be null-terminated on success).
outSizeOutput buffer capacity.
Returns
Pointer to the next unread input position, or nullptr when no token exists.

Definition at line 31 of file StringUtils.h.

Referenced by cmd_chalresp(), and cmd_totp_add().

◆ notifyResponse()

void cdc::mod_2fa::notifyResponse ( uint16_t connHandle,
const uint8_t * data,
size_t len )
static

Sends a response notification to the requesting connection.

Parameters
connHandleConnection handle.
dataResponse bytes.
lenResponse length.

Definition at line 104 of file ble_chalresp.cpp.

References getBle(), INVALID_HANDLE, and s_response_handle.

Referenced by deliverConfirmedResponse().

◆ onChallengeWrite()

int cdc::mod_2fa::onChallengeWrite ( uint16_t connHandle,
uint16_t ,
const uint8_t * data,
uint16_t len )
static

GATT challenge write callback (BLE host task).

Records the request only; the response is computed on the main task in ble_chalresp_tick so the touch-confirm UI is never driven from here.

Parameters
connHandleConnection handle.
dataFrame bytes: NUL-terminated entry name then challenge.
lenFrame length.
Returns
GATT status (0 = success).

Definition at line 154 of file ble_chalresp.cpp.

References LOG_W, MAX_CHALLENGE_LEN, MAX_FRAME_LEN, MAX_NAME_LEN, s_challengeBuf, s_confirm, s_mutex, s_pending, and TAG.

Referenced by registerGattService().

◆ onDisconnect()

void cdc::mod_2fa::onDisconnect ( uint16_t connHandle,
int reason )
static

Clears pending request state if the requesting peer disconnects.

Parameters
connHandleDisconnected handle.
reasonBLE disconnect reason (unused).

Definition at line 223 of file ble_chalresp.cpp.

References s_confirm, s_mutex, and s_pending.

Referenced by ble_chalresp_init().

◆ onListSelect()

void cdc::mod_2fa::onListSelect ( uint16_t index,
void * userData )
static

Handles selection from the account list.

Parameters
indexSelected row index.
userDataOptional user pointer (unused).

Definition at line 915 of file TwoFaModule.cpp.

References cdc::ui::ViewStack::instance(), name, cdc::ui::ViewStack::push(), s_accountCount, s_codeView, s_listLabels, s_listSlots, and wizardStart().

Referenced by cdc::mod_2fa::TwoFaModule::getMenuItems().

◆ onTouchCancel()

void cdc::mod_2fa::onTouchCancel ( void * userData)
static

Touch-confirm declined: discard the prepared response.

Parameters
userDataUnused.

Definition at line 136 of file ble_chalresp.cpp.

References LOG_W, s_confirm, s_mutex, and TAG.

Referenced by ble_chalresp_tick().

◆ onTouchConfirm()

void cdc::mod_2fa::onTouchConfirm ( void * userData)
static

Touch-confirm accepted: notify the prepared response.

Parameters
userDataUnused.

Definition at line 127 of file ble_chalresp.cpp.

References deliverConfirmedResponse().

Referenced by ble_chalresp_tick().

◆ onWizardAlgo()

void cdc::mod_2fa::onWizardAlgo ( uint16_t index,
void * userData )
static

Saves selected algorithm; opens period step (TOTP) or finishes (HOTP).

Parameters
indexSelected list index.
userDataOptional user pointer (unused).

Definition at line 1072 of file TwoFaModule.cpp.

References CR, HOTP, cdc::ui::ViewStack::instance(), onWizardPeriod(), cdc::ui::ViewStack::push(), pushTouchStep(), s_periodMenu, s_wizard, cdc::ui::tr(), and wizardFinish().

Referenced by pushAlgoStep().

◆ onWizardDigits()

void cdc::mod_2fa::onWizardDigits ( uint16_t index,
void * userData )
static

Saves selected code length and opens algorithm-selection step.

Parameters
indexSelected list index.
userDataOptional user pointer (unused).

Definition at line 1041 of file TwoFaModule.cpp.

References pushAlgoStep(), and s_wizard.

Referenced by onWizardIssuer().

◆ onWizardIssuer()

void cdc::mod_2fa::onWizardIssuer ( const char * text)
static

Saves wizard issuer and opens digit-selection step.

Parameters
textEntered issuer string.

Definition at line 1023 of file TwoFaModule.cpp.

References cdc::ui::ViewStack::instance(), onWizardDigits(), cdc::ui::ViewStack::push(), s_digitsMenu, s_wizard, and cdc::ui::tr().

Referenced by onWizardSecret().

◆ onWizardName()

void cdc::mod_2fa::onWizardName ( const char * text)
static

Saves wizard account name and opens secret step.

Parameters
textEntered account name.

Definition at line 1000 of file TwoFaModule.cpp.

References onWizardSecret(), pushT9WizardStep(), s_wizard, SECRET_B32_LEN, and cdc::ui::tr().

Referenced by onWizardType(), and wizardEdit().

◆ onWizardPeriod()

void cdc::mod_2fa::onWizardPeriod ( uint16_t index,
void * userData )
static

Saves selected period and finalizes add/edit operation.

Parameters
indexSelected list index.
userDataOptional user pointer (unused).

Definition at line 1102 of file TwoFaModule.cpp.

References s_wizard, and wizardFinish().

Referenced by onWizardAlgo().

◆ onWizardSecret()

void cdc::mod_2fa::onWizardSecret ( const char * text)
static

Saves wizard secret and opens issuer step.

Parameters
textEntered Base32 secret.

Definition at line 1009 of file TwoFaModule.cpp.

References CR, cdc::mod_2fa::OathStore::ISSUER_LEN, onWizardIssuer(), pushAlgoStep(), pushT9WizardStep(), s_wizard, and cdc::ui::tr().

Referenced by onWizardName().

◆ onWizardTouch()

void cdc::mod_2fa::onWizardTouch ( uint16_t index,
void * userData )
static

Saves the CR touch-confirm choice and finalizes the entry.

Parameters
indexSelected list index (0 = required, 1 = not required).
userDataUnused.

Definition at line 1128 of file TwoFaModule.cpp.

References pushUsbCrStep(), s_wizard, and cdc::mod_2fa::OathFlag::TOUCH_REQUIRED.

Referenced by pushTouchStep().

◆ onWizardType()

void cdc::mod_2fa::onWizardType ( uint16_t index,
void * userData )
static

Saves selected entry type and opens the name step.

Parameters
indexSelected list index.
userDataOptional user pointer (unused).

Definition at line 986 of file TwoFaModule.cpp.

References CR, HOTP, cdc::mod_2fa::OathStore::NAME_LEN, onWizardName(), pushT9WizardStep(), s_wizard, TOTP, and cdc::ui::tr().

Referenced by wizardStart().

◆ onWizardUsbCr()

void cdc::mod_2fa::onWizardUsbCr ( uint16_t index,
void * userData )
static

Saves the USB-CR-slot designation and finalizes the entry.

Parameters
indexSelected list index (0 = designate, 1 = leave undesignated).
userDataUnused.

Definition at line 1158 of file TwoFaModule.cpp.

References s_wizard, cdc::mod_2fa::OathFlag::USB_CR_SLOT, and wizardFinish().

Referenced by pushUsbCrStep().

◆ parseAlgo()

uint8_t cdc::mod_2fa::parseAlgo ( const char * token)
static

Parses textual or numeric algorithm identifiers into store values.

Parameters
tokenAlgorithm token (sha1, sha256, sha512, or numeric).
Returns
Encoded algorithm value used by OathStore.

Definition at line 79 of file TwoFaModule.cpp.

References SHA1, SHA256, and SHA512.

Referenced by cmd_totp_add().

◆ parseType()

uint8_t cdc::mod_2fa::parseType ( const char * token)
static

Parses a textual or numeric entry-type token.

Parameters
tokenType token (totp, hotp, or numeric).
Returns
Encoded OathType value (defaults to TOTP).

Definition at line 103 of file TwoFaModule.cpp.

References CR, HOTP, and TOTP.

Referenced by cmd_totp_add().

◆ pushAlgoStep()

void cdc::mod_2fa::pushAlgoStep ( )
static

Pushes the algorithm-selection step.

CR offers only SHA1/SHA256 (the algorithms the transports carry); TOTP/HOTP additionally offer SHA512. The selected list index maps 1:1 to the OathAlgorithm enum value.

Definition at line 1055 of file TwoFaModule.cpp.

References CR, cdc::ui::ViewStack::instance(), onWizardAlgo(), cdc::ui::ViewStack::push(), s_algoMenu, s_wizard, and cdc::ui::tr().

Referenced by onWizardDigits(), and onWizardSecret().

◆ pushT9WizardStep()

void cdc::mod_2fa::pushT9WizardStep ( const char * title,
const char * initialText,
uint16_t maxLen,
ui::T9InputView::SaveCallback onSave )
static

Pushes a configured T9 input step for the account wizard flow.

Parameters
titleStep title.
initialTextInitial input text.
maxLenMaximum accepted text length.
onSaveSave callback for the step.

Definition at line 763 of file TwoFaModule.cpp.

References cdc::ui::ViewStack::instance(), cdc::ui::ViewStack::push(), and s_t9Input.

Referenced by onWizardName(), onWizardSecret(), onWizardType(), and wizardEdit().

◆ pushTouchStep()

void cdc::mod_2fa::pushTouchStep ( )
static

Pushes the CR touch-confirm toggle step.

Touch confirmation defaults to on; the first list entry (index 0) sets the touch flag, the second clears it.

Definition at line 1114 of file TwoFaModule.cpp.

References cdc::ui::ViewStack::instance(), onWizardTouch(), cdc::ui::ViewStack::push(), s_touchMenu, and cdc::ui::tr().

Referenced by onWizardAlgo().

◆ pushUsbCrStep()

void cdc::mod_2fa::pushUsbCrStep ( )
static

Pushes the CR USB-slot-2 designation step.

Defaults to off; the first entry (index 0) marks this CR entry as the single USB OTP-HID slot-2 responder, the second leaves it undesignated.

Definition at line 1144 of file TwoFaModule.cpp.

References cdc::ui::ViewStack::instance(), onWizardUsbCr(), cdc::ui::ViewStack::push(), s_usbCrMenu, and cdc::ui::tr().

Referenced by onWizardTouch().

◆ rebuildList()

◆ registerCommands()

void cdc::mod_2fa::registerCommands ( )
static

Registers serial commands exposed by the 2FA module.

Definition at line 428 of file TwoFaModule.cpp.

References cmd_chalresp(), CMD_MODULE, cmd_totp(), cdc::serial::getCommandRegistry(), kTotpSubs, and s_commandsRegistered.

Referenced by cdc::mod_2fa::TwoFaModule::init().

◆ registerGattService()

bool cdc::mod_2fa::registerGattService ( )
static

◆ registerStrings()

void cdc::mod_2fa::registerStrings ( )
static

◆ skipSpaces()

const char * cdc::core::skipSpaces ( const char * s)
inline

Advances over leading ASCII whitespace in a C string.

Parameters
sInput string pointer.
Returns
Pointer to first non-whitespace character, or nullptr when input is null.

Definition at line 13 of file StringUtils.h.

◆ validateOathParams()

bool cdc::mod_2fa::validateOathParams ( uint8_t type,
uint8_t & digits,
uint32_t & period,
uint8_t & algorithm )
static

Validates OATH entry parameters and clamps to defaults when invalid.

Parameters
typeIn/out entry type, rejected if out of the supported range.
digitsIn/out digit count, replaced by default if zero.
periodIn/out period seconds (TOTP only), replaced by default if zero.
algorithmIn/out algorithm code.
Returns
true if parameters are valid (after clamping).

Definition at line 34 of file OathStore.cpp.

References CR, cdc::mod_2fa::OathStore::DEFAULT_DIGITS, cdc::mod_2fa::OathStore::DEFAULT_PERIOD, LOG_W, OATH_DIGITS_MAX, OATH_DIGITS_MIN, SHA1, SHA256, SHA512, TAG, TOTP, TOTP_PERIOD_MAX, and TOTP_PERIOD_MIN.

Referenced by cdc::mod_2fa::OathStore::addAccount(), and cdc::mod_2fa::OathStore::updateAccount().

◆ wizardEdit()

◆ wizardFinish()

◆ wizardStart()

void cdc::mod_2fa::wizardStart ( )
static

◆ writePayload()

bool cdc::mod_2fa::writePayload ( uint16_t physSlot,
const char * name,
uint8_t type,
const char * issuer,
const uint8_t * secret,
uint8_t secretLen,
uint8_t digits,
uint32_t period,
uint64_t counter,
uint8_t algorithm,
uint8_t flags )
static

Builds and persists an OATH payload to a physical slot.

Parameters
physSlotPhysical R-Memory slot.
nameAccount label.
typeEntry type discriminator.
issuerOptional issuer text.
secretRaw secret bytes.
secretLenSecret length.
digitsOutput digit count.
periodTOTP period seconds.
counterHOTP moving factor.
algorithmHash algorithm identifier.
flagsEntry flags.
Returns
true on successful write.

Definition at line 237 of file OathStore.cpp.

References cdc::mod_2fa::OathPayload::algorithm, cdc::mod_2fa::OathPayload::counter, cdc::mod_2fa::OathStore::DEFAULT_DIGITS, cdc::mod_2fa::OathStore::DEFAULT_PERIOD, cdc::mod_2fa::OathPayload::digits, cdc::mod_2fa::OathPayload::flags, flags, cdc::hal::getSecureElementInstance(), cdc::core::TropicStorage::instance(), cdc::mod_2fa::OathStore::instance(), cdc::mod_2fa::OathPayload::issuer, LOG_E, cdc::mod_2fa::OathStore::moduleId(), moduleId, name, cdc::hal::OK, cdc::mod_2fa::OathPayload::period, cdc::mod_2fa::OathPayload::secret, cdc::mod_2fa::OathPayload::secretLen, TAG, cdc::mod_2fa::OathPayload::type, and cdc::core::TropicStorage::writeSlot().

Referenced by cdc::mod_2fa::OathStore::addAccount(), cdc::mod_2fa::OathStore::clearUsbCrFlagExcept(), and cdc::mod_2fa::OathStore::updateAccount().

Variable Documentation

◆ CMD_MODULE

const char* cdc::mod_2fa::CMD_MODULE = "totp"
staticconstexpr

Serial command handlers for 2FA module.

Definition at line 67 of file TwoFaModule.cpp.

Referenced by registerCommands().

◆ CR_CHALLENGE_UUID

const uint8_t cdc::mod_2fa::CR_CHALLENGE_UUID[16]
static
Initial value:
= {
0x01, 0x1A, 0x8B, 0x6A, 0x9D, 0x4C, 0x6E, 0x9A,
0x7A, 0x4D, 0x5D, 0x8B, 0x31, 0x1F, 0x2F, 0x8E
}

Challenge characteristic UUID (...1F31...), write.

Definition at line 32 of file ble_chalresp.cpp.

Referenced by registerGattService().

◆ CR_RESPONSE_UUID

const uint8_t cdc::mod_2fa::CR_RESPONSE_UUID[16]
static
Initial value:
= {
0x01, 0x1A, 0x8B, 0x6A, 0x9D, 0x4C, 0x6E, 0x9A,
0x7A, 0x4D, 0x5D, 0x8B, 0x32, 0x1F, 0x2F, 0x8E
}

Response characteristic UUID (...1F32...), read + notify.

Definition at line 40 of file ble_chalresp.cpp.

Referenced by registerGattService().

◆ CR_SVC_UUID

const uint8_t cdc::mod_2fa::CR_SVC_UUID[16]
static
Initial value:
= {
0x01, 0x1A, 0x8B, 0x6A, 0x9D, 0x4C, 0x6E, 0x9A,
0x7A, 0x4D, 0x5D, 0x8B, 0x30, 0x1F, 0x2F, 0x8E
}

CR service UUID: 8E2F1F30-8B5D-4D7A-9A6E-4C9D6A8B1A01 (little-endian).

Definition at line 24 of file ble_chalresp.cpp.

Referenced by ble_chalresp_deinit(), and registerGattService().

◆ INVALID_HANDLE

uint16_t cdc::mod_2fa::INVALID_HANDLE = 0xFFFF
staticconstexpr

Definition at line 45 of file ble_chalresp.cpp.

Referenced by ble_chalresp_tick(), and notifyResponse().

◆ kBase32BufLen

size_t cdc::mod_2fa::kBase32BufLen = 103 + 1
staticconstexpr

Maximum Base32 string length for the longest supported secret (64 bytes raw).

Definition at line 1370 of file TwoFaModule.cpp.

Referenced by cdc::mod_2fa::TwoFaModule::exportBackup().

◆ kSchemaVer

int cdc::mod_2fa::kSchemaVer = 1
staticconstexpr

Schema version written to and expected from the 2FA backup section.

Definition at line 1367 of file TwoFaModule.cpp.

Referenced by cdc::mod_2fa::TwoFaModule::exportBackup(), and cdc::mod_2fa::TwoFaModule::importBackup().

◆ kStrings

ui::I18nEntry cdc::mod_2fa::kStrings[]
constexpr
Initial value:
= {
{"mod_2fa.title", "2FA"},
{"mod_2fa.add_account", "Add Account"},
{"mod_2fa.account_name", "Account Name"},
{"mod_2fa.secret", "Secret (Base32)"},
{"mod_2fa.issuer", "Issuer (optional)"},
{"mod_2fa.digits", "Digits"},
{"mod_2fa.algorithm", "Algorithm"},
{"mod_2fa.period", "Period"},
{"mod_2fa.code", "Code"},
{"mod_2fa.time_invalid", "Time not set"},
{"mod_2fa.invalid_input", "Invalid input"},
{"mod_2fa.hint_edit", "[3] Edit [N] Back"},
{"mod_2fa.hint_type", "[Y] Type [3] Edit [N] Back"},
{"mod_2fa.hint_hotp", "[5] Next [Y] Type [3] Edit [N] Back"},
{"mod_2fa.no_keyboard", "No keyboard connected"},
{"mod_2fa.type", "Type"},
{"mod_2fa.counter", "Counter"},
{"mod_2fa.touch", "Touch confirm"},
{"mod_2fa.touch_on", "Required"},
{"mod_2fa.touch_off", "Not required"},
{"mod_2fa.cr_confirm", "Allow challenge-response?"},
{"mod_2fa.cr_entry", "Challenge-Response"},
{"mod_2fa.usb_cr", "USB slot 2"},
{"mod_2fa.usb_cr_on", "Designate"},
{"mod_2fa.usb_cr_off", "No"},
}

Definition at line 33 of file TwoFaModule.cpp.

Referenced by registerStrings().

◆ kTotpSubs

const cdc::serial::SubCommand cdc::mod_2fa::kTotpSubs[]
static
Initial value:
= {
{"LIST", "", "List all 2FA entries", cmd_totp_list},
{"ADD", "<type> <name> <secret> [issuer] [digits] [period] [algo] [counter]","Add 2FA entry", cmd_totp_add},
{"DEL", "<index>", "Delete 2FA entry by index", cmd_totp_del},
{"GET", "<index>", "Generate code by index", cmd_totp_get},
{nullptr, nullptr, nullptr, nullptr},
}
static void cmd_totp_list(const char *args)
Serial command handler printing all configured OATH entries.
static void cmd_totp_add(const char *args)
Serial command handler adding an OATH entry from tokens.
static void cmd_totp_get(const char *args)
Serial command handler generating one code by index.
static void cmd_totp_del(const char *args)
Serial command handler deleting an OATH entry by index.

Sub-command table for the TOTP serial command group.

Definition at line 413 of file TwoFaModule.cpp.

Referenced by cmd_totp(), and registerCommands().

◆ MAX_CHALLENGE_LEN

size_t cdc::mod_2fa::MAX_CHALLENGE_LEN = 128
staticconstexpr

Definition at line 49 of file ble_chalresp.cpp.

Referenced by ble_chalresp_tick(), and onChallengeWrite().

◆ MAX_FRAME_LEN

size_t cdc::mod_2fa::MAX_FRAME_LEN = MAX_NAME_LEN + 1 + MAX_CHALLENGE_LEN
staticconstexpr

Definition at line 50 of file ble_chalresp.cpp.

Referenced by onChallengeWrite().

◆ MAX_NAME_LEN

size_t cdc::mod_2fa::MAX_NAME_LEN = 16
staticconstexpr

Max challenge payload (entry name + NUL + challenge bytes) accepted per write.

Definition at line 48 of file ble_chalresp.cpp.

Referenced by ble_chalresp_tick(), and onChallengeWrite().

◆ OATH_DIGITS_MAX

uint8_t cdc::mod_2fa::OATH_DIGITS_MAX = 8
staticconstexpr

Definition at line 18 of file OathStore.cpp.

Referenced by formatCode(), and validateOathParams().

◆ OATH_DIGITS_MIN

uint8_t cdc::mod_2fa::OATH_DIGITS_MIN = 6
staticconstexpr

Allowed OATH digit count range per RFC 4226 / RFC 6238.

Definition at line 17 of file OathStore.cpp.

Referenced by formatCode(), and validateOathParams().

◆ POWERS_10

const uint32_t cdc::mod_2fa::POWERS_10[]
static
Initial value:
= {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000
}

Definition at line 141 of file OathStore.cpp.

◆ s_accountCount

uint16_t cdc::mod_2fa::s_accountCount = 0
static

Definition at line 737 of file TwoFaModule.cpp.

Referenced by freeListBuffers(), onListSelect(), and rebuildList().

◆ s_algoMenu

ui::ListView cdc::mod_2fa::s_algoMenu
static

Definition at line 726 of file TwoFaModule.cpp.

Referenced by pushAlgoStep().

◆ s_capacity

uint16_t cdc::mod_2fa::s_capacity = 0
static

Definition at line 738 of file TwoFaModule.cpp.

Referenced by ensureListBuffers(), freeListBuffers(), and rebuildList().

◆ s_challenge_handle

uint16_t cdc::mod_2fa::s_challenge_handle = 0
static

Definition at line 54 of file ble_chalresp.cpp.

Referenced by registerGattService().

◆ s_challengeBuf

uint8_t cdc::mod_2fa::s_challengeBuf[MAX_CHALLENGE_LEN] = {}
static

Definition at line 76 of file ble_chalresp.cpp.

Referenced by ble_chalresp_tick(), and onChallengeWrite().

◆ s_codeView

OathCodeView cdc::mod_2fa::s_codeView
static

Definition at line 730 of file TwoFaModule.cpp.

Referenced by onListSelect().

◆ s_commandsRegistered

bool cdc::mod_2fa::s_commandsRegistered = false
static

Definition at line 69 of file TwoFaModule.cpp.

Referenced by registerCommands().

◆ s_confirm

PendingConfirm cdc::mod_2fa::s_confirm = {}
static

◆ s_digitsMenu

ui::ListView cdc::mod_2fa::s_digitsMenu
static

Definition at line 725 of file TwoFaModule.cpp.

Referenced by onWizardIssuer().

◆ s_gattChars

GattCharacteristic cdc::mod_2fa::s_gattChars[2] = {}
static

Definition at line 57 of file ble_chalresp.cpp.

Referenced by registerGattService().

◆ s_gattSvcDef

GattServiceDef cdc::mod_2fa::s_gattSvcDef = {}
static

Definition at line 58 of file ble_chalresp.cpp.

Referenced by registerGattService().

◆ s_initialized

bool cdc::mod_2fa::s_initialized = false
static

Definition at line 52 of file ble_chalresp.cpp.

Referenced by ble_chalresp_deinit(), ble_chalresp_init(), and ble_chalresp_tick().

◆ s_listItems

ui::ListItem* cdc::mod_2fa::s_listItems = nullptr
static

Dynamic list buffers released by freeListBuffers.

Definition at line 734 of file TwoFaModule.cpp.

Referenced by ensureListBuffers(), freeListBuffers(), and rebuildList().

◆ s_listLabels

char(* cdc::mod_2fa::s_listLabels)[24] = nullptr
static

Definition at line 735 of file TwoFaModule.cpp.

Referenced by ensureListBuffers(), freeListBuffers(), onListSelect(), and rebuildList().

◆ s_listSlots

uint16_t* cdc::mod_2fa::s_listSlots = nullptr
static

Definition at line 736 of file TwoFaModule.cpp.

Referenced by ensureListBuffers(), freeListBuffers(), onListSelect(), and rebuildList().

◆ s_listView

ui::ListView cdc::mod_2fa::s_listView
static

2FA module UI state.

Static view instances (no dynamic allocation, no leaks).

Definition at line 722 of file TwoFaModule.cpp.

Referenced by cdc::mod_2fa::TwoFaModule::getMenuItems(), rebuildList(), and wizardFinish().

◆ s_mutex

SemaphoreHandle_t cdc::mod_2fa::s_mutex = nullptr
static

◆ s_pending

PendingChallenge cdc::mod_2fa::s_pending = {}
static

◆ s_periodMenu

ui::ListView cdc::mod_2fa::s_periodMenu
static

Definition at line 727 of file TwoFaModule.cpp.

Referenced by onWizardAlgo().

◆ s_response_handle

uint16_t cdc::mod_2fa::s_response_handle = 0
static

Definition at line 55 of file ble_chalresp.cpp.

Referenced by notifyResponse(), and registerGattService().

◆ s_t9Input

ui::T9InputView cdc::mod_2fa::s_t9Input
static

Definition at line 723 of file TwoFaModule.cpp.

Referenced by pushT9WizardStep().

◆ s_tokDisconn

Definition at line 60 of file ble_chalresp.cpp.

Referenced by ble_chalresp_deinit(), and ble_chalresp_init().

◆ s_touchMenu

ui::ListView cdc::mod_2fa::s_touchMenu
static

Definition at line 728 of file TwoFaModule.cpp.

Referenced by pushTouchStep().

◆ s_typeMenu

ui::ListView cdc::mod_2fa::s_typeMenu
static

Definition at line 724 of file TwoFaModule.cpp.

Referenced by wizardStart().

◆ s_usbCrMenu

ui::ListView cdc::mod_2fa::s_usbCrMenu
static

Definition at line 729 of file TwoFaModule.cpp.

Referenced by pushUsbCrStep().

◆ s_viewsInitialized

bool cdc::mod_2fa::s_viewsInitialized = false
static

Definition at line 731 of file TwoFaModule.cpp.

Referenced by cdc::mod_2fa::TwoFaModule::getMenuItems().

◆ s_wizard

◆ SECRET_B32_LEN

size_t cdc::mod_2fa::SECRET_B32_LEN = 128
staticconstexpr

Definition at line 68 of file TwoFaModule.cpp.

Referenced by cmd_totp_add(), and onWizardName().

◆ TOTP_PERIOD_MAX

uint32_t cdc::mod_2fa::TOTP_PERIOD_MAX = 300
staticconstexpr

Definition at line 24 of file OathStore.cpp.

Referenced by validateOathParams().

◆ TOTP_PERIOD_MIN

uint32_t cdc::mod_2fa::TOTP_PERIOD_MIN = 15
staticconstexpr

Allowed TOTP period range in seconds.

Definition at line 23 of file OathStore.cpp.

Referenced by validateOathParams().