1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-15 10:58:09 +00:00
trezor-firmware/legacy/firmware/u2f/u2f_hid.h

134 lines
4.9 KiB
C
Raw Normal View History

// clang-format off
/**
* Copyright FIDO Alliance, 2017
*
* Licensed under CC-BY:
* https://creativecommons.org/licenses/by/4.0/legalcode
*
* Editor: Jakob Ehrensvard, Yubico, jakob@yubico.com
*/
2015-11-02 23:08:18 +00:00
#ifndef __U2FHID_H_INCLUDED__
#define __U2FHID_H_INCLUDED__
#ifdef _MSC_VER // Windows
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
2015-11-02 23:08:18 +00:00
typedef unsigned long int uint64_t;
#else
#include <stdint.h>
#endif
#ifdef __cplusplus
extern "C" {
2015-11-02 23:08:18 +00:00
#endif
// Size of HID reports
2015-11-02 23:08:18 +00:00
#define HID_RPT_SIZE 64 // Default size of raw HID report
2015-11-02 23:08:18 +00:00
// Frame layout - command- and continuation frames
#define CID_BROADCAST 0xffffffff // Broadcast channel id
#define TYPE_MASK 0x80 // Frame type mask
#define TYPE_INIT 0x80 // Initial frame identifier
#define TYPE_CONT 0x00 // Continuation frame identifier
typedef struct __attribute__((packed)) {
uint32_t cid; // Channel identifier
union __attribute__((packed)) {
uint8_t type; // Frame type - b7 defines type
struct __attribute__((packed)) {
uint8_t cmd; // Command - b7 set
uint8_t bcnth; // Message byte count - high part
uint8_t bcntl; // Message byte count - low part
uint8_t data[HID_RPT_SIZE - 7]; // Data payload
} init;
struct __attribute__((packed)) {
uint8_t seq; // Sequence number - b7 cleared
uint8_t data[HID_RPT_SIZE - 5]; // Data payload
} cont;
};
} U2FHID_FRAME;
2015-11-02 23:08:18 +00:00
#define FRAME_TYPE(f) ((f).type & TYPE_MASK)
#define FRAME_CMD(f) ((f).init.cmd & ~TYPE_MASK)
#define MSG_LEN(f) ((f).init.bcnth*256 + (f).init.bcntl)
2015-11-02 23:08:18 +00:00
#define FRAME_SEQ(f) ((f).cont.seq & ~TYPE_MASK)
// HID usage- and usage-page definitions
#define FIDO_USAGE_PAGE 0xf1d0 // FIDO alliance HID usage page
#define FIDO_USAGE_U2FHID 0x01 // U2FHID usage for top-level collection
#define FIDO_USAGE_DATA_IN 0x20 // Raw IN data report
#define FIDO_USAGE_DATA_OUT 0x21 // Raw OUT data report
2015-11-02 23:08:18 +00:00
// General constants
2015-11-02 23:08:18 +00:00
#define U2FHID_IF_VERSION 2 // Current interface implementation version
#define U2FHID_TRANS_TIMEOUT 3000 // Default message timeout in ms
2015-11-02 23:08:18 +00:00
// U2FHID native commands
#define U2FHID_PING (TYPE_INIT | 0x01) // Echo data through local processor only
#define U2FHID_MSG (TYPE_INIT | 0x03) // Send U2F message frame
#define U2FHID_LOCK (TYPE_INIT | 0x04) // Send lock channel command
#define U2FHID_INIT (TYPE_INIT | 0x06) // Channel initialization
#define U2FHID_WINK (TYPE_INIT | 0x08) // Send device identification wink
#define U2FHID_SYNC (TYPE_INIT | 0x3c) // Protocol resync command
#define U2FHID_ERROR (TYPE_INIT | 0x3f) // Error response
2015-11-02 23:08:18 +00:00
#define U2FHID_VENDOR_FIRST (TYPE_INIT | 0x40) // First vendor defined command
#define U2FHID_VENDOR_LAST (TYPE_INIT | 0x7f) // Last vendor defined command
2015-11-02 23:08:18 +00:00
// U2FHID_INIT command defines
#define INIT_NONCE_SIZE 8 // Size of channel initialization challenge
#define CAPFLAG_WINK 0x01 // Device supports WINK command
#define CAPFLAG_LOCK 0x02 // Device supports LOCK command
typedef struct __attribute__((packed)) {
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
} U2FHID_INIT_REQ;
typedef struct __attribute__((packed)) {
uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
uint32_t cid; // Channel identifier
uint8_t versionInterface; // Interface version
uint8_t versionMajor; // Major version number
uint8_t versionMinor; // Minor version number
uint8_t versionBuild; // Build version number
uint8_t capFlags; // Capabilities flags
} U2FHID_INIT_RESP;
2015-11-02 23:08:18 +00:00
// U2FHID_SYNC command defines
2015-11-02 23:08:18 +00:00
typedef struct __attribute__((packed)) {
uint8_t nonce; // Client application nonce
} U2FHID_SYNC_REQ;
2015-11-02 23:08:18 +00:00
typedef struct __attribute__((packed)) {
uint8_t nonce; // Client application nonce
} U2FHID_SYNC_RESP;
2015-11-02 23:08:18 +00:00
// Low-level error codes. Return as negatives.
#define ERR_NONE 0x00 // No error
#define ERR_INVALID_CMD 0x01 // Invalid command
#define ERR_INVALID_PAR 0x02 // Invalid parameter
#define ERR_INVALID_LEN 0x03 // Invalid message length
#define ERR_INVALID_SEQ 0x04 // Invalid message sequencing
#define ERR_MSG_TIMEOUT 0x05 // Message has timed out
#define ERR_CHANNEL_BUSY 0x06 // Channel busy
#define ERR_LOCK_REQUIRED 0x0a // Command requires channel lock
#define ERR_INVALID_CID 0x0b // Message on CID 0
#define ERR_OTHER 0x7f // Other unspecified error
2015-11-02 23:08:18 +00:00
#ifdef __cplusplus
}
#endif
#endif // __U2FHID_H_INCLUDED__