rng.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * QEMU Random Number Generator Backend
  3. *
  4. * Copyright IBM, Corp. 2012
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #ifndef QEMU_RNG_H
  13. #define QEMU_RNG_H
  14. #include "qemu/queue.h"
  15. #include "qom/object.h"
  16. #define TYPE_RNG_BACKEND "rng-backend"
  17. OBJECT_DECLARE_TYPE(RngBackend, RngBackendClass,
  18. RNG_BACKEND)
  19. #define TYPE_RNG_BUILTIN "rng-builtin"
  20. typedef struct RngRequest RngRequest;
  21. typedef void (EntropyReceiveFunc)(void *opaque,
  22. const void *data,
  23. size_t size);
  24. struct RngRequest
  25. {
  26. EntropyReceiveFunc *receive_entropy;
  27. uint8_t *data;
  28. void *opaque;
  29. size_t offset;
  30. size_t size;
  31. QSIMPLEQ_ENTRY(RngRequest) next;
  32. };
  33. struct RngBackendClass
  34. {
  35. ObjectClass parent_class;
  36. void (*request_entropy)(RngBackend *s, RngRequest *req);
  37. void (*opened)(RngBackend *s, Error **errp);
  38. };
  39. struct RngBackend
  40. {
  41. Object parent;
  42. /*< protected >*/
  43. bool opened;
  44. QSIMPLEQ_HEAD(, RngRequest) requests;
  45. };
  46. /**
  47. * rng_backend_request_entropy:
  48. * @s: the backend to request entropy from
  49. * @size: the number of bytes of data to request
  50. * @receive_entropy: a function to be invoked when entropy is available
  51. * @opaque: data that should be passed to @receive_entropy
  52. *
  53. * This function is used by the front-end to request entropy from an entropy
  54. * source. This function can be called multiple times before @receive_entropy
  55. * is invoked with different values of @receive_entropy and @opaque. The
  56. * backend will queue each request and handle appropriately.
  57. *
  58. * The backend does not need to pass the full amount of data to @receive_entropy
  59. * but will pass a value greater than 0.
  60. */
  61. void rng_backend_request_entropy(RngBackend *s, size_t size,
  62. EntropyReceiveFunc *receive_entropy,
  63. void *opaque);
  64. /**
  65. * rng_backend_free_request:
  66. * @s: the backend that created the request
  67. * @req: the request to finalize
  68. *
  69. * Used by child rng backend classes to finalize requests once they've been
  70. * processed. The request is removed from the list of active requests and
  71. * deleted.
  72. */
  73. void rng_backend_finalize_request(RngBackend *s, RngRequest *req);
  74. #endif