xref: /openbmc/qemu/backends/rng-random.c (revision e1fe50dc)
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 #include "sysemu/rng-random.h"
14 #include "sysemu/rng.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qemu/main-loop.h"
17 
18 struct RndRandom
19 {
20     RngBackend parent;
21 
22     int fd;
23     char *filename;
24 
25     EntropyReceiveFunc *receive_func;
26     void *opaque;
27     size_t size;
28 };
29 
30 /**
31  * A simple and incomplete backend to request entropy from /dev/random.
32  *
33  * This backend exposes an additional "filename" property that can be used to
34  * set the filename to use to open the backend.
35  */
36 
37 static void entropy_available(void *opaque)
38 {
39     RndRandom *s = RNG_RANDOM(opaque);
40     uint8_t buffer[s->size];
41     ssize_t len;
42 
43     len = read(s->fd, buffer, s->size);
44     if (len < 0 && errno == EAGAIN) {
45         return;
46     }
47     g_assert(len != -1);
48 
49     s->receive_func(s->opaque, buffer, len);
50     s->receive_func = NULL;
51 
52     qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
53 }
54 
55 static void rng_random_request_entropy(RngBackend *b, size_t size,
56                                         EntropyReceiveFunc *receive_entropy,
57                                         void *opaque)
58 {
59     RndRandom *s = RNG_RANDOM(b);
60 
61     if (s->receive_func) {
62         s->receive_func(s->opaque, NULL, 0);
63     }
64 
65     s->receive_func = receive_entropy;
66     s->opaque = opaque;
67     s->size = size;
68 
69     qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
70 }
71 
72 static void rng_random_opened(RngBackend *b, Error **errp)
73 {
74     RndRandom *s = RNG_RANDOM(b);
75 
76     if (s->filename == NULL) {
77         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
78                   "filename", "a valid filename");
79     } else {
80         s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
81 
82         if (s->fd == -1) {
83             error_set(errp, QERR_OPEN_FILE_FAILED, s->filename);
84         }
85     }
86 }
87 
88 static char *rng_random_get_filename(Object *obj, Error **errp)
89 {
90     RndRandom *s = RNG_RANDOM(obj);
91 
92     if (s->filename) {
93         return g_strdup(s->filename);
94     }
95 
96     return NULL;
97 }
98 
99 static void rng_random_set_filename(Object *obj, const char *filename,
100                                  Error **errp)
101 {
102     RngBackend *b = RNG_BACKEND(obj);
103     RndRandom *s = RNG_RANDOM(obj);
104 
105     if (b->opened) {
106         error_set(errp, QERR_PERMISSION_DENIED);
107         return;
108     }
109 
110     if (s->filename) {
111         g_free(s->filename);
112     }
113 
114     s->filename = g_strdup(filename);
115 }
116 
117 static void rng_random_init(Object *obj)
118 {
119     RndRandom *s = RNG_RANDOM(obj);
120 
121     object_property_add_str(obj, "filename",
122                             rng_random_get_filename,
123                             rng_random_set_filename,
124                             NULL);
125 
126     s->filename = g_strdup("/dev/random");
127 }
128 
129 static void rng_random_finalize(Object *obj)
130 {
131     RndRandom *s = RNG_RANDOM(obj);
132 
133     qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
134 
135     if (s->fd != -1) {
136         qemu_close(s->fd);
137     }
138 
139     g_free(s->filename);
140 }
141 
142 static void rng_random_class_init(ObjectClass *klass, void *data)
143 {
144     RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
145 
146     rbc->request_entropy = rng_random_request_entropy;
147     rbc->opened = rng_random_opened;
148 }
149 
150 static const TypeInfo rng_random_info = {
151     .name = TYPE_RNG_RANDOM,
152     .parent = TYPE_RNG_BACKEND,
153     .instance_size = sizeof(RndRandom),
154     .class_init = rng_random_class_init,
155     .instance_init = rng_random_init,
156     .instance_finalize = rng_random_finalize,
157 };
158 
159 static void register_types(void)
160 {
161     type_register_static(&rng_random_info);
162 }
163 
164 type_init(register_types);
165