CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ctaphid.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cstddef>
5
6// CTAPHID Transport Layer
7// Handles USB HID framing for CTAP2 messages
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13#define CTAPHID_PACKET_SIZE 64
14#define CTAPHID_INIT_DATA 57
15#define CTAPHID_CONT_DATA 59
16#define CTAPHID_MAX_MSG_SIZE 2048
17
18#define CTAPHID_BROADCAST_CID 0xFFFFFFFF
19
20// CTAPHID Commands
21#define CTAPHID_PING 0x01
22#define CTAPHID_MSG 0x03
23#define CTAPHID_LOCK 0x04
24#define CTAPHID_INIT 0x06
25#define CTAPHID_WINK 0x08
26#define CTAPHID_CBOR 0x10
27#define CTAPHID_CANCEL 0x11
28#define CTAPHID_KEEPALIVE 0x3B
29#define CTAPHID_ERROR 0x3F
30
31// CTAPHID Errors
32#define CTAPHID_ERR_INVALID_CMD 0x01
33#define CTAPHID_ERR_INVALID_PAR 0x02
34#define CTAPHID_ERR_INVALID_LEN 0x03
35#define CTAPHID_ERR_INVALID_SEQ 0x04
36#define CTAPHID_ERR_MSG_TIMEOUT 0x05
37#define CTAPHID_ERR_CHANNEL_BUSY 0x06
38#define CTAPHID_ERR_LOCK_REQUIRED 0x0A
39#define CTAPHID_ERR_INVALID_CHANNEL 0x0B
40#define CTAPHID_ERR_OTHER 0x7F
41
42// Vendor command range
43#define CTAPHID_VENDOR_FIRST 0x40
44#define CTAPHID_VENDOR_LAST 0x7F
45
46// CTAPHID Status
47#define CTAPHID_STATUS_PROCESSING 0x01
48#define CTAPHID_STATUS_UPNEEDED 0x02
49
50// Capabilities
51#define CTAPHID_CAP_WINK 0x01
52#define CTAPHID_CAP_CBOR 0x04
53
54#ifdef __DOXYGEN__
55namespace cdc::mod_fido2 {
56#endif
57
58typedef struct {
59 uint32_t cid;
60 uint8_t cmd;
61 uint16_t bcnt;
62 uint8_t seq;
63 uint16_t offset;
64 uint8_t* buffer;
65 uint16_t buffer_size;
66 bool active;
67 uint32_t last_activity;
68
69 // Per-channel response state.
72 uint16_t response_len;
74 uint32_t response_cid;
75 uint8_t response_cmd;
78
79#ifdef __DOXYGEN__
80} // namespace cdc::mod_fido2
81#endif
82
83bool ctaphid_init(void);
84bool ctaphid_process_packet(const uint8_t* packet);
85bool ctaphid_has_response(void);
86bool ctaphid_get_response_packet(uint8_t* packet);
87void ctaphid_send_keepalive(uint32_t cid, uint8_t status);
88void ctaphid_send_error(uint32_t cid, uint8_t error);
89void ctaphid_check_timeout(void);
90uint32_t ctaphid_get_current_cid(void);
91bool ctaphid_is_busy(void);
92
93void ctaphid_get_cmd_counts(uint32_t* cbor_count, uint32_t* msg_count);
95
96#ifdef __cplusplus
97}
98#endif
void ctaphid_get_cmd_counts(uint32_t *cbor_count, uint32_t *msg_count)
Returns cumulative counters for CTAPHID CBOR and MSG commands.
Definition ctaphid.cpp:456
bool ctaphid_init(void)
Initializes CTAPHID transport state and synchronization primitives.
Definition ctaphid.cpp:434
void ctaphid_check_timeout(void)
Expires active channels whose message assembly timeout elapsed.
Definition ctaphid.cpp:730
bool ctaphid_is_busy(void)
Reports whether any CTAPHID channel currently has an active transaction.
Definition ctaphid.cpp:761
uint32_t ctaphid_get_current_cid(void)
Returns the channel identifier of the currently processed request.
Definition ctaphid.cpp:753
bool ctaphid_has_response(void)
Indicates whether any channel has a response queued for host retrieval.
Definition ctaphid.cpp:607
void ctaphid_reset_cmd_counts(void)
Resets CTAPHID command counters.
Definition ctaphid.cpp:464
bool ctaphid_get_response_packet(uint8_t *packet)
Retrieves the next response HID packet from a per-channel response queue.
Definition ctaphid.cpp:634
void ctaphid_send_error(uint32_t cid, uint8_t error)
Queues a CTAPHID ERROR response for the given channel.
Definition ctaphid.cpp:703
bool ctaphid_process_packet(const uint8_t *packet)
Processes one incoming 64-byte CTAPHID packet.
Definition ctaphid.cpp:474
void ctaphid_send_keepalive(uint32_t cid, uint8_t status)
Sends a CTAPHID KEEPALIVE packet immediately over USB.
Definition ctaphid.cpp:689