CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
t1_block.cpp File Reference
#include "mod_gpg/openpgp/t1/t1_block.h"
#include <string.h>

Go to the source code of this file.

Functions

uint8_t t1_lrc (const uint8_t *buf, size_t len)
 Compute the longitudinal redundancy check (LRC) of a buffer.
uint16_t t1_crc (const uint8_t *buf, size_t len)
 Compute the ISO/IEC 13239 CRC-16 of a buffer (polynomial 0x1021, initial value 0xFFFF, no final XOR, MSB first). Used as the alternative EDC when CRC is negotiated.
static t1_status_t append_edc (uint8_t *out, size_t prologue_and_inf_len, bool use_crc, size_t out_cap, size_t *out_len)
t1_status_t t1_block_encode_i (uint8_t nad, uint8_t ns, bool more, const uint8_t *inf, uint8_t inf_len, bool use_crc, uint8_t *out, size_t out_cap, size_t *out_len)
 Encode an I-block.
t1_status_t t1_block_encode_r (uint8_t nad, uint8_t nr, t1_r_error_t err, bool use_crc, uint8_t *out, size_t out_cap, size_t *out_len)
 Encode an R-block.
t1_status_t t1_block_encode_s (uint8_t nad, t1_s_subtype_t subtype, const uint8_t *inf, uint8_t inf_len, bool use_crc, uint8_t *out, size_t out_cap, size_t *out_len)
 Encode an S-block.
t1_status_t t1_block_decode (const uint8_t *buf, size_t buf_len, bool use_crc, t1_block_t *out)
 Decode a T=1 block.

Function Documentation

◆ append_edc()

t1_status_t append_edc ( uint8_t * out,
size_t prologue_and_inf_len,
bool use_crc,
size_t out_cap,
size_t * out_len )
static

Definition at line 58 of file t1_block.cpp.

References t1_crc(), T1_ERR_OUT_BUF, t1_lrc(), and T1_OK.

Referenced by t1_block_encode_i(), t1_block_encode_r(), and t1_block_encode_s().

◆ t1_block_decode()

t1_status_t t1_block_decode ( const uint8_t * buf,
size_t buf_len,
bool use_crc,
t1_block_t * out )

Decode a T=1 block.

The decoder validates LEN, EDC, and PCB encoding rules. Reserved bits in I- and R-blocks must be zero; otherwise T1_ERR_PCB is returned.

Parameters
bufInput bytes.
buf_lenNumber of bytes available.
use_crcIf true, expect 2-byte CRC; otherwise LRC.
outDecoded block (INF pointer aliases into buf).

Definition at line 147 of file t1_block.cpp.

References t1_block_t::edc_ok, t1_block_t::i_more, t1_block_t::i_ns, t1_block_t::inf, t1_block_t::inf_len, t1_block_t::kind, t1_block_t::nad, t1_block_t::pcb, t1_block_t::r_err, t1_block_t::r_nr, t1_block_t::s_subtype, T1_BLOCK_I, T1_BLOCK_R, T1_BLOCK_S, t1_crc(), T1_ERR_LEN, T1_ERR_NULL, T1_ERR_PCB, T1_ERR_SHORT, T1_LEN_RESERVED, t1_lrc(), T1_OK, and t1_block_t::use_crc.

◆ t1_block_encode_i()

t1_status_t t1_block_encode_i ( uint8_t nad,
uint8_t ns,
bool more,
const uint8_t * inf,
uint8_t inf_len,
bool use_crc,
uint8_t * out,
size_t out_cap,
size_t * out_len )

Encode an I-block.

Parameters
nadNode address.
nsSequence number (0 or 1).
moreMore-data flag (chaining).
infInformation field payload (may be NULL when inf_len == 0).
inf_lenPayload length (<= T1_INF_MAX).
use_crcUse 2-byte CRC instead of LRC.
outOutput buffer (caller-allocated, at least 4 + inf_len + (use_crc ? 1 : 0) bytes).
out_capOutput capacity in bytes.
out_lenReceives total bytes written.

Definition at line 75 of file t1_block.cpp.

References append_edc(), T1_ERR_INF_TOO_BIG, T1_ERR_NULL, T1_ERR_OUT_BUF, T1_ERR_PCB, and T1_INF_MAX.

Referenced by t1_state_next_outbound().

◆ t1_block_encode_r()

t1_status_t t1_block_encode_r ( uint8_t nad,
uint8_t nr,
t1_r_error_t err,
bool use_crc,
uint8_t * out,
size_t out_cap,
size_t * out_len )

Encode an R-block.

Parameters
nadNode address.
nrExpected sequence number (0 or 1).
errError indicator.
use_crcEDC algorithm select.
outOutput buffer.
out_capOutput buffer capacity.
out_lenReceives bytes written.

Definition at line 103 of file t1_block.cpp.

References append_edc(), T1_ERR_NULL, T1_ERR_OUT_BUF, and T1_ERR_PCB.

Referenced by encode_rblock().

◆ t1_block_encode_s()

t1_status_t t1_block_encode_s ( uint8_t nad,
t1_s_subtype_t subtype,
const uint8_t * inf,
uint8_t inf_len,
bool use_crc,
uint8_t * out,
size_t out_cap,
size_t * out_len )

Encode an S-block.

Parameters
nadNode address.
subtypeS-block subtype (request or response).
infOptional info payload (e.g. IFS new value).
inf_lenLength of the info payload (0..254).
use_crcEDC algorithm select.
outOutput buffer.
out_capOutput buffer capacity.
out_lenReceives bytes written.

Definition at line 123 of file t1_block.cpp.

References append_edc(), T1_ERR_INF_TOO_BIG, T1_ERR_NULL, T1_ERR_OUT_BUF, T1_ERR_PCB, and T1_INF_MAX.

Referenced by encode_sblock_response().

◆ t1_crc()

uint16_t t1_crc ( const uint8_t * buf,
size_t len )

Compute the ISO/IEC 13239 CRC-16 of a buffer (polynomial 0x1021, initial value 0xFFFF, no final XOR, MSB first). Used as the alternative EDC when CRC is negotiated.

Definition at line 41 of file t1_block.cpp.

Referenced by append_edc(), and t1_block_decode().

◆ t1_lrc()

uint8_t t1_lrc ( const uint8_t * buf,
size_t len )

Compute the longitudinal redundancy check (LRC) of a buffer.

LRC is the bytewise XOR of all bytes in buf. Used as the default EDC byte when the T=1 protocol parameters do not select CRC.

Parameters
bufBuffer pointer.
lenNumber of bytes to fold into the LRC.
Returns
XOR of all bytes.

Definition at line 33 of file t1_block.cpp.

Referenced by append_edc(), and t1_block_decode().