Zig API Reference: zig-ctap2¶
cbor.zig¶
Minimal CBOR encoder/decoder for CTAP2.
Types¶
Value (union)¶
A decoded CBOR value.
MapEntry (struct)¶
Encoder (struct)¶
Header (struct)¶
A decoded CBOR header: major type and argument.
Decoder (struct)¶
Functions¶
init¶
written¶
encodeUint¶
Encode an unsigned integer.
encodeNegInt¶
Encode a negative integer (CBOR stores as -1 - n).
encodeByteString¶
Encode a byte string.
encodeTextString¶
Encode a text string.
beginArray¶
Begin an array of known length.
beginMap¶
Begin a map of known length.
encodeBool¶
Encode a boolean.
encodeNull¶
Encode null.
init¶
remaining¶
decodeUint¶
Decode a single unsigned integer.
decodeByteString¶
Decode a byte string, returning a slice into the source data.
decodeTextString¶
Decode a text string, returning a slice into the source data.
decodeArrayHeader¶
Decode an array header, returning the element count.
decodeMapHeader¶
Decode a map header, returning the entry count.
peekMajorType¶
Peek at the major type of the next value without consuming it.
skipValue¶
Skip a single CBOR value (including nested structures).
decodeRawHeader¶
Decode a header and return the raw major type + arg for flexible handling.
Constants¶
Error
ctap2.zig¶
CTAP2 command encoding and response parsing.
Types¶
CommandCode (enum)¶
StatusCode (enum)¶
MakeCredentialResult (struct)¶
Parsed result from authenticatorMakeCredential.
GetAssertionResult (struct)¶
Parsed result from authenticatorGetAssertion.
Functions¶
statusMessage¶
Map a CTAP2 status byte to a human-readable message string.
encodeMakeCredential¶
Encode a makeCredential request into CBOR.
pub fn encodeMakeCredential( buf: []u8, client_data_hash: []const u8, rp_id: []const u8, rp_name: []const u8, user_id: []const u8, user_name: []const u8, user_display_name: []const u8, algorithms: []const i32, resident_key: bool, ) cbor.Error![]const u8
encodeGetAssertion¶
Encode a getAssertion request into CBOR.
pub fn encodeGetAssertion( buf: []u8, rp_id: []const u8, client_data_hash: []const u8, allow_list_ids: []const []const u8, ) cbor.Error![]const u8
encodeGetInfo¶
Encode a getInfo request.
parseMakeCredentialResponse¶
Parse a raw CTAP2 authenticatorMakeCredential response. The response format is: status_byte(1) + CBOR_map The CBOR map has integer keys: 1 = fmt (text string) 2 = authData (byte string) 3 = attStmt (map) From authData we extract the credential ID: rpIdHash(32) + flags(1) + signCount(4) + [aaguid(16) + credIdLen(2) + credentialId(credIdLen) + ...]
parseGetAssertionResponse¶
Parse a raw CTAP2 authenticatorGetAssertion response. The response format is: status_byte(1) + CBOR_map The CBOR map has integer keys: 1 = credential (map with "id" byte string) — optional per spec 2 = authData (byte string) 3 = signature (byte string) 4 = user (map with "id" byte string) — optional Per CTAP2 spec: key 1 (credential) is omitted when the allowList in the request had exactly one entry. In that case, use the fallback credential ID.
pub fn parseGetAssertionResponse( response_data: []const u8, fallback_cred_id: ?[]const u8, ) cbor.Error!GetAssertionResult
ctaphid.zig¶
CTAPHID transport framing for FIDO2 USB HID communication.
Types¶
Command (enum)¶
CTAPHID command codes.
KeepaliveStatus (enum)¶
Keepalive status codes.
InitHeader (struct)¶
Parse an init packet header.
InitResponse (struct)¶
CTAPHID_INIT response structure.
Functions¶
buildInitPacket¶
Build an initialization packet.
buildContPacket¶
Build a continuation packet.
fragmentMessage¶
Fragment a message into CTAPHID packets. Returns the number of packets written to out.
parseInitPacket¶
reassembleMessage¶
Reassemble a complete message from init + continuation packets. read_fn is called to get each subsequent packet.
parseInitResponse¶
Parse a CTAPHID_INIT response payload.
Constants¶
ErrorPacket-- A 64-byte HID packet ready to send.
ffi.zig¶
C FFI exports for libctap2.
Constants¶
KeepaliveCallback-- Keepalive callback type: receives status byte (1=processing, 2=user presence needed).
hid.zig¶
Platform-selected USB HID transport for FIDO2 devices.
Constants¶
platformDeviceErrorenumerateopenFirst
hid_linux.zig¶
Linux USB HID transport via hidraw.
Types¶
Device (struct)¶
A handle to an open FIDO2 HID device.
Functions¶
write¶
Write a 64-byte packet to the device.
read¶
Read a 64-byte packet from the device with timeout.
close¶
Close the device.
enumerate¶
Enumerate connected FIDO2 USB HID devices. Scans /sys/class/hidraw/ to discover all hidraw devices (not limited to a fixed range). Returns DevicesNotAccessible if FIDO devices are found but none could be opened (likely a permissions issue).
openFirst¶
Find and open the first available FIDO2 device.
Constants¶
Error
hid_macos.zig¶
macOS USB HID transport via IOKit.
Types¶
Device (struct)¶
A handle to an open FIDO2 HID device. Owns both the device ref and the manager that created it.
Functions¶
write¶
read¶
close¶
enumerate¶
openFirst¶
Constants¶
Error
pin.zig¶
CTAP2 Client PIN protocol v2 (authenticatorClientPIN comm...
Types¶
SubCommand (enum)¶
authenticatorClientPIN subcommands.
PINRetriesResult (struct)¶
Result from getPINRetries.
CoseKey (struct)¶
A COSE_Key for EC2 P-256 (used in key agreement).
EphemeralKeyPair (struct)¶
Ephemeral key pair for ECDH key agreement.
SharedSecret (struct)¶
Shared secret derived from ECDH.
PINTokenResult (struct)¶
Result from getPINToken.
Functions¶
generateKeyPair¶
Generate an ephemeral ECDH P-256 key pair for key agreement.
deriveSharedSecret¶
Perform ECDH: multiply their public point by our private scalar. Returns SHA-256 of the x-coordinate of the shared point.
computeHmac¶
Compute HMAC-SHA-256(key, message).
computePinAuth¶
Compute pinAuth: first 16 bytes of HMAC-SHA-256(pinToken, message). Used for authenticating commands with a PIN token.
aes256CbcEncrypt¶
AES-256-CBC encrypt (with zero IV, per CTAP2 PIN protocol v2 spec). Input must be a multiple of 16 bytes. Returns the ciphertext (same length as input).
aes256CbcDecrypt¶
AES-256-CBC decrypt (with zero IV, per CTAP2 PIN protocol v2 spec). Input must be a multiple of 16 bytes. Returns the plaintext (same length as input).
encodeGetPINRetries¶
Encode a getPINRetries request. Request: {1: pinUvAuthProtocol(2), 2: subCommand(1)}
encodeGetKeyAgreement¶
Encode a getKeyAgreement request. Request: {1: pinUvAuthProtocol(2), 2: subCommand(2)}
encodeGetPINToken¶
Encode a getPinUvAuthTokenUsingPinWithPermissions request (subCommand 0x09). Request: {1: protocol, 2: subCommand(9), 3: keyAgreement(COSE_Key), 6: pinHashEnc}
pub fn encodeGetPINToken( buf: []u8, our_public_key: CoseKey, pin_hash_enc: []const u8, ) cbor.Error![]const u8
parsePINRetriesResponse¶
Parse a getPINRetries response. Response CBOR (after status byte): {3: pinRetries, 4: powerCycleState(optional)}
parseKeyAgreementResponse¶
Parse a getKeyAgreement response. Response CBOR (after status byte): {1: keyAgreement(COSE_Key)} COSE_Key: {1: kty(2), 3: alg(-25), -1: crv(1), -2: x(32 bytes), -3: y(32 bytes)}
parsePINTokenResponse¶
Parse a getPINToken response. Response CBOR (after status byte): {2: pinUvAuthToken(encrypted bytes)}
pub fn parsePINTokenResponse( response_data: []const u8, shared_secret: SharedSecret, ) !PINTokenResult
encryptPINHash¶
Prepare the encrypted PIN hash for a getPINToken request. Takes a UTF-8 PIN string, hashes it with SHA-256, takes the first 16 bytes, pads to 64 bytes, and encrypts with AES-256-CBC using the shared secret. Returns the 64-byte encrypted PIN hash.
encodeMakeCredentialWithPIN¶
Encode a makeCredential command with pinAuth and pinUvAuthProtocol. This adds parameters 8 (pinUvAuthProtocol) and 9 (pinAuth) to the command. pinAuth = LEFT(HMAC-SHA-256(pinToken, clientDataHash), 16)
pub fn encodeMakeCredentialWithPIN( buf: []u8, client_data_hash: []const u8, rp_id: []const u8, rp_name: []const u8, user_id: []const u8, user_name: []const u8, user_display_name: []const u8, algorithms: []const i32, resident_key: bool, pin_token: [32]u8, ) cbor.Error![]const u8
encodeGetAssertionWithPIN¶
Encode a getAssertion command with pinAuth and pinUvAuthProtocol.