• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use usb_util::DescriptorType;
6 
7 // How long it takes for the security key to become inactive and time out all previously pending
8 // transactions since last activity.
9 pub const TRANSACTION_TIMEOUT_MILLIS: u64 = 120_000;
10 
11 // How long to wait before timing out and canceling a USB transfer from the guest if the host
12 // security key is unresponsive.
13 pub const USB_TRANSFER_TIMEOUT_MILLIS: u64 = 5_000;
14 
15 // 5ms is the default USB interrupt polling rate according to specs.
16 pub const USB_POLL_RATE_MILLIS: u64 = 5;
17 
18 // Some applications expect a very short RTT when handling packets between host key and guest, half
19 // a millisecond seems like a decent compromise.
20 pub const PACKET_POLL_RATE_NANOS: u64 = 50_000;
21 
22 // Total max number of transactions we can hold in our key. Any more transactions will push older
23 // transactions away from the stack.
24 pub const MAX_TRANSACTIONS: usize = 4;
25 
26 // Max number of incoming packets still to be processed by the guest
27 pub const U2FHID_MAX_IN_PENDING: usize = 32;
28 
29 pub const U2FHID_PACKET_SIZE: usize = 64;
30 pub const PACKET_INIT_HEADER_SIZE: usize = 7;
31 pub const PACKET_CONT_HEADER_SIZE: usize = 5;
32 pub const PACKET_INIT_DATA_SIZE: usize = U2FHID_PACKET_SIZE - PACKET_INIT_HEADER_SIZE;
33 pub const PACKET_CONT_DATA_SIZE: usize = U2FHID_PACKET_SIZE - PACKET_CONT_HEADER_SIZE;
34 
35 pub const CID_SIZE: usize = 4;
36 pub const BROADCAST_CID: [u8; CID_SIZE] = [0xFF, 0xFF, 0xFF, 0xFF];
37 
38 pub const NONCE_SIZE: usize = 8;
39 pub const EMPTY_NONCE: [u8; NONCE_SIZE] = [0u8; NONCE_SIZE];
40 
41 // It's a valid init packet only if the 7th bit of the cmd field is set
42 pub const PACKET_INIT_VALID_CMD: u8 = 0b1000_0000;
43 pub const U2FHID_ERROR_CMD: u8 = 0xBF;
44 
45 pub const U2FHID_CONTROL_ENDPOINT: u8 = 0x00;
46 pub const U2FHID_IN_ENDPOINT: u8 = 0x81;
47 pub const U2FHID_OUT_ENDPOINT: u8 = 0x01;
48 
49 // Generic HID commands
50 pub const HID_GET_IDLE: u8 = 0x02;
51 pub const HID_SET_IDLE: u8 = 0x0A;
52 pub const HID_GET_REPORT_DESC: u8 = 0x22;
53 
54 pub const HID_MAX_DESCRIPTOR_SIZE: usize = 4096;
55 
56 // Descriptor data taken from: https://github.com/gl-sergei/u2f-token/blob/master/src/usb-hid.c
57 // With minor modifications for our own PID and VID and other strings
58 pub const U2FHID_DEVICE_DESC: &[u8] = &[
59     18,
60     DescriptorType::Device as u8,
61     0x10,
62     0x01,
63     0x00,
64     0x00,
65     0x00,
66     0x40,
67     // Google Vendor ID
68     0xd1,
69     0x18,
70     // Unique Product ID
71     0xd0,
72     0xf1,
73     0x00,
74     0x01,
75     0,
76     0,
77     0,
78     1,
79 ];
80 
81 pub const HID_REPORT_DESC_HEADER: &[u8] = &[
82     0x06, 0xd0, 0xf1, // Usage Page (FIDO)
83     0x09, 0x01, // Usage (FIDO)
84 ];
85 
86 pub const U2FHID_CONFIG_DESC: &[u8] = &[
87     9,
88     DescriptorType::Configuration as u8,
89     /* Configuration Descriptor. */
90     41,
91     0x00, /* wTotalLength. */
92     0x01, /* bNumInterfaces. */
93     0x01, /* bConfigurationValue. */
94     0,    /* iConfiguration. */
95     0x80, /* bmAttributes. */
96     15,   /* bMaxPower (100mA). */
97     /* Interface Descriptor. */
98     9, /* bLength: Interface Descriptor size */
99     DescriptorType::Interface as u8,
100     0,    /* bInterfaceNumber: Number of Interface */
101     0x00, /* bAlternateSetting: Alternate setting */
102     0x02, /* bNumEndpoints: Two endpoints used */
103     0x03, /* bInterfaceClass: HID */
104     0x00, /* bInterfaceSubClass: no boot */
105     0x00, /* bInterfaceProtocol: 0=none */
106     0x00, /* iInterface */
107     /* HID Descriptor. */
108     9,    /* bLength: HID Descriptor size */
109     0x21, /* bDescriptorType: HID */
110     0x10,
111     0x01, /* bcdHID: HID Class Spec release number */
112     0x00, /* bCountryCode: Hardware target country */
113     0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
114     0x22, /* bDescriptorType */
115     0x22,
116     0, /* wItemLength: Total length of Report descriptor */
117     /* Endpoint IN1 Descriptor */
118     7, /* bLength: Endpoint Descriptor size */
119     DescriptorType::Endpoint as u8,
120     0x81, /* bEndpointAddress: (IN1) */
121     0x03, /* bmAttributes: Interrupt */
122     0x40,
123     0x00, /* wMaxPacketSize: 64 */
124     0x05, /* bInterval (5ms) */
125     /* Endpoint OUT1 Descriptor */
126     7, /* bLength: Endpoint Descriptor size */
127     DescriptorType::Endpoint as u8,
128     0x01, /* bEndpointAddress: (OUT1) */
129     0x03, /* bmAttributes: Interrupt */
130     0x40,
131     0x00, /* wMaxPacketSize: 64 */
132     0x05, /* bInterval (5ms) */
133 ];
134 
135 pub const HID_REPORT_DESC: &[u8] = &[
136     0x06, 0xd0, 0xf1, /* USAGE_PAGE (FIDO Alliance) */
137     0x09, 0x01, /* USAGE (Keyboard) */
138     0xa1, 0x01, /* COLLECTION (Application) */
139     0x09, 0x20, /* USAGE (Input report data) */
140     0x15, 0x00, /* LOGICAL_MINIMUM (0) */
141     0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
142     0x75, 0x08, /* REPORT_SIZE (8) */
143     0x95, 0x40, /* REPORT_COUNT (64) */
144     0x81, 0x02, /* INPUT (Data,Var,Abs); Modifier byte */
145     0x09, 0x21, /* USAGE (Output report data) */
146     0x15, 0x00, /* LOGICAL_MINIMUM (0) */
147     0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
148     0x75, 0x08, /* REPORT_SIZE (8) */
149     0x95, 0x40, /* REPORT_COUNT (64) */
150     0x91, 0x02, /* OUTPUT (Data,Var,Abs); Modifier byte */
151     0xc0, /* END_COLLECTION */
152 ];
153