xref: /qemu/hw/usb/u2f.h (revision 83ecdb18)
1 /*
2  * U2F USB device.
3  *
4  * Copyright (c) 2020 César Belley <cesar.belley@lse.epita.fr>
5  * Written by César Belley <cesar.belley@lse.epita.fr>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #ifndef U2F_H
27 #define U2F_H
28 
29 #include "hw/qdev-core.h"
30 
31 #define U2FHID_PACKET_SIZE 64
32 #define U2FHID_PENDING_IN_NUM 32
33 
34 typedef struct U2FKeyInfo U2FKeyInfo;
35 
36 #define TYPE_U2F_KEY "u2f-key"
37 OBJECT_DECLARE_TYPE(U2FKeyState, U2FKeyClass, U2F_KEY)
38 
39 /*
40  * Callbacks to be used by the U2F key base device (i.e. hw/u2f.c)
41  * to interact with its variants (i.e. hw/u2f-*.c)
42  */
43 struct U2FKeyClass {
44     /*< private >*/
45     USBDeviceClass parent_class;
46 
47     /*< public >*/
48     void (*recv_from_guest)(U2FKeyState *key,
49                             const uint8_t packet[U2FHID_PACKET_SIZE]);
50     void (*realize)(U2FKeyState *key, Error **errp);
51     void (*unrealize)(U2FKeyState *key);
52 };
53 
54 /*
55  * State of the U2F key base device (i.e. hw/u2f.c)
56  */
57 struct U2FKeyState {
58     USBDevice dev;
59     USBEndpoint *ep;
60     uint8_t idle;
61 
62     /* Pending packets to be send to the guest */
63     uint8_t pending_in[U2FHID_PENDING_IN_NUM][U2FHID_PACKET_SIZE];
64     uint8_t pending_in_start;
65     uint8_t pending_in_end;
66     uint8_t pending_in_num;
67 };
68 
69 /*
70  * API to be used by the U2F key device variants (i.e. hw/u2f-*.c)
71  * to interact with the U2F key base device (i.e. hw/u2f.c)
72  */
73 void u2f_send_to_guest(U2FKeyState *key,
74                        const uint8_t packet[U2FHID_PACKET_SIZE]);
75 
76 extern const VMStateDescription vmstate_u2f_key;
77 
78 #define VMSTATE_U2F_KEY(_field, _state) {                            \
79     .name       = (stringify(_field)),                               \
80     .size       = sizeof(U2FKeyState),                               \
81     .vmsd       = &vmstate_u2f_key,                                  \
82     .flags      = VMS_STRUCT,                                        \
83     .offset     = vmstate_offset_value(_state, _field, U2FKeyState), \
84 }
85 
86 #endif /* U2F_H */
87