CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
t1_state.h File Reference

Go to the source code of this file.

Classes

struct  t1_state_t
 Card-side T=1 state. Embedded entirely in caller-provided storage. More...

Macros

#define T1_MAX_APDU_LEN   4096
 ISO 7816-3 T=1 transport state machine (card side).
#define T1_MAX_RESPONSE_LEN   4096
 Maximum response size that can be chunked back.

Enumerations

enum  t1_feed_t { T1_FEED_NEED_OUT , T1_FEED_APDU_READY , T1_FEED_WAIT_MORE , T1_FEED_IGNORED }
 Outcome of feeding a block to the state machine. More...

Functions

void t1_state_init (t1_state_t *state, bool use_crc)
 Initialise the state machine. Sets sequence numbers to 0, IFSC/IFSD to defaults.
t1_feed_t t1_state_feed (t1_state_t *state, const t1_block_t *block)
 Feed an incoming decoded block to the state machine.
bool t1_state_queue_response (t1_state_t *state, const uint8_t *resp, size_t resp_len)
 Register the application's APDU response so the state machine can chunk it into I-blocks.
size_t t1_state_next_outbound (t1_state_t *state, uint8_t *out, size_t out_cap)
 Emit the next outbound block (control reply or response chunk).

Macro Definition Documentation

◆ T1_MAX_APDU_LEN

#define T1_MAX_APDU_LEN   4096

ISO 7816-3 T=1 transport state machine (card side).

Layered on top of t1_block.h. Owns:

  • sequence-number bookkeeping (N(S), N(R))
  • IFSC negotiation
  • reassembly of chained reader-to-card APDUs (chaining bit set in I-blocks)
  • chunking of card-to-reader responses according to the negotiated IFSD
  • response generation for S-blocks (IFS, ABORT, WTX, RESYNCH)
  • R-block emission on EDC error / unexpected sequence number

The CCID layer (ccid.cpp) drives this state machine through three calls:

  1. t1_state_feed() on every incoming block
  2. t1_state_queue_response() when the APDU dispatcher returns
  3. t1_state_next_outbound() until it returns 0

Maximum APDU size we accept across chained I-blocks (extended-length cap).

Definition at line 27 of file t1_state.h.

◆ T1_MAX_RESPONSE_LEN

#define T1_MAX_RESPONSE_LEN   4096

Maximum response size that can be chunked back.

Definition at line 32 of file t1_state.h.

Enumeration Type Documentation

◆ t1_feed_t

enum t1_feed_t

Outcome of feeding a block to the state machine.

Enumerator
T1_FEED_NEED_OUT 

A response block (R / S / ACK) is queued; drain via t1_state_next_outbound.

T1_FEED_APDU_READY 

A complete APDU is in state->apdu_buf (length state->apdu_len).

T1_FEED_WAIT_MORE 

Reader chained more data; we ACKed with an R-block.

T1_FEED_IGNORED 

Block ignored (decoder error already surfaced via outbound).

Definition at line 36 of file t1_state.h.

Function Documentation

◆ t1_state_feed()

t1_feed_t t1_state_feed ( t1_state_t * state,
const t1_block_t * block )

Feed an incoming decoded block to the state machine.

On EDC errors or unexpected sequence numbers the function queues an R-block (control_block). Mainline I-blocks either complete an APDU (T1_FEED_APDU_READY) or ACK chained pieces (T1_FEED_WAIT_MORE). S-blocks are handled and their responses queued.

Definition at line 142 of file t1_state.cpp.

References handle_iblock(), handle_rblock(), handle_sblock(), t1_block_t::kind, T1_BLOCK_I, T1_BLOCK_R, T1_BLOCK_S, and T1_FEED_IGNORED.

◆ t1_state_init()

void t1_state_init ( t1_state_t * state,
bool use_crc )

Initialise the state machine. Sets sequence numbers to 0, IFSC/IFSD to defaults.

Parameters
stateState storage (caller-owned, will be zeroed).
use_crcEDC choice; must match what's negotiated with the reader via ATR.

Definition at line 36 of file t1_state.cpp.

References t1_state_t::ifsc, t1_state_t::ifsd, t1_state_t::ns_expected, t1_state_t::ns_send, T1_DEFAULT_IFSC, and t1_state_t::use_crc.

◆ t1_state_next_outbound()

size_t t1_state_next_outbound ( t1_state_t * state,
uint8_t * out,
size_t out_cap )

Emit the next outbound block (control reply or response chunk).

Parameters
stateState machine.
outOutput buffer.
out_capOutput buffer capacity.
Returns
Number of bytes written. Zero means nothing to send.

Definition at line 167 of file t1_state.cpp.

References t1_state_t::control_block, t1_state_t::control_len, t1_state_t::ifsd, t1_state_t::ns_send, t1_state_t::resp_buf, t1_state_t::resp_len, t1_state_t::resp_pending, t1_state_t::resp_sent, t1_block_encode_i(), T1_DEFAULT_IFSC, T1_OK, and t1_state_t::use_crc.

◆ t1_state_queue_response()

bool t1_state_queue_response ( t1_state_t * state,
const uint8_t * resp,
size_t resp_len )

Register the application's APDU response so the state machine can chunk it into I-blocks.

Must only be called after t1_state_feed returned T1_FEED_APDU_READY.

Returns
false if resp_len > T1_MAX_RESPONSE_LEN.

Definition at line 155 of file t1_state.cpp.

References t1_state_t::resp_buf, t1_state_t::resp_len, t1_state_t::resp_pending, and t1_state_t::resp_sent.