CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ccid.h
Go to the documentation of this file.
1/*
2 * USB CCID (Chip Card Interface Device) for CDC Badge
3 *
4 * Based on pico-openpgp (https://github.com/polhenarejos/pico-openpgp)
5 * Original: Copyright (c) 2022 Pol Henarejos, AGPLv3
6 *
7 * CCID Class: 0x0B (Smart Card)
8 * Uses Gemalto VID/PID for libccid whitelist compatibility
9 */
10
11#pragma once
12#include <stdint.h>
13#include <stdbool.h>
14#include <stddef.h>
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20// CCID USB Class
21#define USB_CLASS_CCID 0x0B
22
23// VID/PID for libccid whitelist compatibility (Gemalto GemPC433)
24#define CCID_USB_VID 0x08e6
25#define CCID_USB_PID 0x4433
26
27// CCID Message Types (PC to Reader)
28#define CCID_PC_TO_RDR_ICC_POWER_ON 0x62
29#define CCID_PC_TO_RDR_ICC_POWER_OFF 0x63
30#define CCID_PC_TO_RDR_GET_SLOT_STATUS 0x65
31#define CCID_PC_TO_RDR_XFR_BLOCK 0x6F
32#define CCID_PC_TO_RDR_GET_PARAMETERS 0x6C
33#define CCID_PC_TO_RDR_RESET_PARAMETERS 0x6D
34#define CCID_PC_TO_RDR_SET_PARAMETERS 0x61
35#define CCID_PC_TO_RDR_SECURE 0x69
36
37// CCID Message Types (Reader to PC)
38#define CCID_RDR_TO_PC_DATA_BLOCK 0x80
39#define CCID_RDR_TO_PC_SLOT_STATUS 0x81
40#define CCID_RDR_TO_PC_PARAMETERS 0x82
41
42// CCID Slot Status
43#define CCID_ICC_PRESENT_ACTIVE 0x00
44#define CCID_ICC_PRESENT_INACTIVE 0x01
45#define CCID_ICC_NOT_PRESENT 0x02
46
47// CCID Command Status
48#define CCID_CMD_STATUS_OK 0x00
49#define CCID_CMD_STATUS_FAILED 0x40
50#define CCID_CMD_STATUS_TIME_EXT 0x80
51
52// CCID Error Codes
53#define CCID_ERROR_CMD_ABORTED 0xFF
54#define CCID_ERROR_ICC_MUTE 0xFE
55#define CCID_ERROR_XFR_PARITY_ERROR 0xFD
56#define CCID_ERROR_XFR_OVERRUN 0xFC
57#define CCID_ERROR_HW_ERROR 0xFB
58#define CCID_ERROR_CMD_NOT_SUPPORTED 0x00
59
60// Buffer sizes
61#define CCID_MAX_MSG_SIZE 2048
62#define CCID_HEADER_SIZE 10
63
64// CCID Message Header (10 bytes)
65typedef struct __attribute__((packed)) {
66 uint8_t bMessageType;
67 uint32_t dwLength;
68 uint8_t bSlot;
69 uint8_t bSeq;
70 uint8_t bSpecific[3];
72
73// CCID Functional Descriptor (54 bytes for OpenPGP)
74extern const uint8_t CCID_DESCRIPTOR[];
75extern const size_t CCID_DESCRIPTOR_LEN;
76
77// Initialize CCID interface
78bool ccid_init(void);
79
80// Process incoming CCID message
81// Returns response length
82int ccid_process_message(const uint8_t *msg, size_t msg_len,
83 uint8_t *resp, size_t resp_max);
84
85// Get ATR (Answer To Reset)
86const uint8_t* ccid_get_atr(size_t *len);
87
88// Card present status
89bool ccid_card_present(void);
90
91#ifdef __cplusplus
92}
93#endif
94
int ccid_process_message(const uint8_t *msg, size_t msg_len, uint8_t *resp, size_t resp_max)
Processes one incoming CCID message and writes corresponding response.
Definition ccid.cpp:148
bool ccid_card_present(void)
Returns whether virtual CCID card is available.
Definition ccid.cpp:112
ccid_header_t
Definition ccid.h:71
const uint8_t CCID_DESCRIPTOR[]
CCID functional descriptor (54 bytes) per OpenPGP 3.4.1 profile.
Definition ccid.cpp:20
bool ccid_init(void)
Definition ccid.cpp:83
struct __attribute__((packed))
Definition ccid.h:65
const uint8_t * ccid_get_atr(size_t *len)
Returns pointer and length of ATR bytes.
Definition ccid.cpp:101
const size_t CCID_DESCRIPTOR_LEN
Definition ccid.cpp:50