Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 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 | |
| 13 | #ifndef QEMU_RNG_H |
| 14 | #define QEMU_RNG_H |
| 15 | |
Paolo Bonzini | 14cccb6 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 16 | #include "qom/object.h" |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 17 | #include "qemu-common.h" |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 18 | |
| 19 | #define TYPE_RNG_BACKEND "rng-backend" |
| 20 | #define RNG_BACKEND(obj) \ |
| 21 | OBJECT_CHECK(RngBackend, (obj), TYPE_RNG_BACKEND) |
| 22 | #define RNG_BACKEND_GET_CLASS(obj) \ |
| 23 | OBJECT_GET_CLASS(RngBackendClass, (obj), TYPE_RNG_BACKEND) |
| 24 | #define RNG_BACKEND_CLASS(klass) \ |
| 25 | OBJECT_CLASS_CHECK(RngBackendClass, (klass), TYPE_RNG_BACKEND) |
| 26 | |
Ladi Prosek | 74074e8 | 2016-03-03 09:37:16 +0100 | [diff] [blame] | 27 | typedef struct RngRequest RngRequest; |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 28 | typedef struct RngBackendClass RngBackendClass; |
| 29 | typedef struct RngBackend RngBackend; |
| 30 | |
| 31 | typedef void (EntropyReceiveFunc)(void *opaque, |
| 32 | const void *data, |
| 33 | size_t size); |
| 34 | |
Ladi Prosek | 74074e8 | 2016-03-03 09:37:16 +0100 | [diff] [blame] | 35 | struct RngRequest |
| 36 | { |
| 37 | EntropyReceiveFunc *receive_entropy; |
| 38 | uint8_t *data; |
| 39 | void *opaque; |
| 40 | size_t offset; |
| 41 | size_t size; |
Ladi Prosek | 443590c | 2016-03-03 14:16:11 +0100 | [diff] [blame] | 42 | QSIMPLEQ_ENTRY(RngRequest) next; |
Ladi Prosek | 74074e8 | 2016-03-03 09:37:16 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 45 | struct RngBackendClass |
| 46 | { |
| 47 | ObjectClass parent_class; |
| 48 | |
Ladi Prosek | 60253ed | 2016-03-03 09:37:18 +0100 | [diff] [blame] | 49 | void (*request_entropy)(RngBackend *s, RngRequest *req); |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 50 | |
| 51 | void (*opened)(RngBackend *s, Error **errp); |
| 52 | }; |
| 53 | |
| 54 | struct RngBackend |
| 55 | { |
| 56 | Object parent; |
| 57 | |
| 58 | /*< protected >*/ |
| 59 | bool opened; |
Ladi Prosek | 443590c | 2016-03-03 14:16:11 +0100 | [diff] [blame] | 60 | QSIMPLEQ_HEAD(requests, RngRequest) requests; |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Ladi Prosek | 9f14b0a | 2016-03-03 09:37:17 +0100 | [diff] [blame] | 63 | |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 64 | /** |
| 65 | * rng_backend_request_entropy: |
| 66 | * @s: the backend to request entropy from |
| 67 | * @size: the number of bytes of data to request |
| 68 | * @receive_entropy: a function to be invoked when entropy is available |
| 69 | * @opaque: data that should be passed to @receive_entropy |
| 70 | * |
| 71 | * This function is used by the front-end to request entropy from an entropy |
| 72 | * source. This function can be called multiple times before @receive_entropy |
| 73 | * is invoked with different values of @receive_entropy and @opaque. The |
Amit Shah | 42015c9 | 2012-11-21 11:21:21 +0530 | [diff] [blame] | 74 | * backend will queue each request and handle appropriately. |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 75 | * |
| 76 | * The backend does not need to pass the full amount of data to @receive_entropy |
Amit Shah | 42015c9 | 2012-11-21 11:21:21 +0530 | [diff] [blame] | 77 | * but will pass a value greater than 0. |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 78 | */ |
| 79 | void rng_backend_request_entropy(RngBackend *s, size_t size, |
| 80 | EntropyReceiveFunc *receive_entropy, |
| 81 | void *opaque); |
Ladi Prosek | 9f14b0a | 2016-03-03 09:37:17 +0100 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * rng_backend_free_request: |
| 85 | * @s: the backend that created the request |
| 86 | * @req: the request to finalize |
| 87 | * |
| 88 | * Used by child rng backend classes to finalize requests once they've been |
| 89 | * processed. The request is removed from the list of active requests and |
| 90 | * deleted. |
| 91 | */ |
| 92 | void rng_backend_finalize_request(RngBackend *s, RngRequest *req); |
Anthony Liguori | a9b7b2a | 2012-06-25 10:03:47 -0500 | [diff] [blame] | 93 | #endif |