u2f.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef U2F_H
  26. #define U2F_H
  27. #include "hw/qdev-core.h"
  28. #define U2FHID_PACKET_SIZE 64
  29. #define U2FHID_PENDING_IN_NUM 32
  30. typedef struct U2FKeyInfo U2FKeyInfo;
  31. #define TYPE_U2F_KEY "u2f-key"
  32. OBJECT_DECLARE_TYPE(U2FKeyState, U2FKeyClass, U2F_KEY)
  33. /*
  34. * Callbacks to be used by the U2F key base device (i.e. hw/u2f.c)
  35. * to interact with its variants (i.e. hw/u2f-*.c)
  36. */
  37. struct U2FKeyClass {
  38. /*< private >*/
  39. USBDeviceClass parent_class;
  40. /*< public >*/
  41. void (*recv_from_guest)(U2FKeyState *key,
  42. const uint8_t packet[U2FHID_PACKET_SIZE]);
  43. void (*realize)(U2FKeyState *key, Error **errp);
  44. void (*unrealize)(U2FKeyState *key);
  45. };
  46. /*
  47. * State of the U2F key base device (i.e. hw/u2f.c)
  48. */
  49. struct U2FKeyState {
  50. USBDevice dev;
  51. USBEndpoint *ep;
  52. uint8_t idle;
  53. /* Pending packets to be send to the guest */
  54. uint8_t pending_in[U2FHID_PENDING_IN_NUM][U2FHID_PACKET_SIZE];
  55. uint8_t pending_in_start;
  56. uint8_t pending_in_end;
  57. uint8_t pending_in_num;
  58. };
  59. /*
  60. * API to be used by the U2F key device variants (i.e. hw/u2f-*.c)
  61. * to interact with the U2F key base device (i.e. hw/u2f.c)
  62. */
  63. void u2f_send_to_guest(U2FKeyState *key,
  64. const uint8_t packet[U2FHID_PACKET_SIZE]);
  65. extern const VMStateDescription vmstate_u2f_key;
  66. #define VMSTATE_U2F_KEY(_field, _state) { \
  67. .name = (stringify(_field)), \
  68. .size = sizeof(U2FKeyState), \
  69. .vmsd = &vmstate_u2f_key, \
  70. .flags = VMS_STRUCT, \
  71. .offset = vmstate_offset_value(_state, _field, U2FKeyState), \
  72. }
  73. #endif /* U2F_H */