u2f.h 3.2 KB

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