xref: /openbmc/qemu/backends/rng-random.c (revision cde63615343fb241f4044e6471e6e25be78574c8)
15c74521dSAnthony Liguori /*
25c74521dSAnthony Liguori  * QEMU Random Number Generator Backend
35c74521dSAnthony Liguori  *
45c74521dSAnthony Liguori  * Copyright IBM, Corp. 2012
55c74521dSAnthony Liguori  *
65c74521dSAnthony Liguori  * Authors:
75c74521dSAnthony Liguori  *  Anthony Liguori   <aliguori@us.ibm.com>
85c74521dSAnthony Liguori  *
95c74521dSAnthony Liguori  * This work is licensed under the terms of the GNU GPL, version 2 or later.
105c74521dSAnthony Liguori  * See the COPYING file in the top-level directory.
115c74521dSAnthony Liguori  */
125c74521dSAnthony Liguori 
139c058332SPeter Maydell #include "qemu/osdep.h"
14dccfcd0eSPaolo Bonzini #include "sysemu/rng-random.h"
15dccfcd0eSPaolo Bonzini #include "sysemu/rng.h"
16da34e65cSMarkus Armbruster #include "qapi/error.h"
177b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h"
181de7afc9SPaolo Bonzini #include "qemu/main-loop.h"
195c74521dSAnthony Liguori 
20*cde63615SWei Jiangang struct RngRandom
215c74521dSAnthony Liguori {
225c74521dSAnthony Liguori     RngBackend parent;
235c74521dSAnthony Liguori 
245c74521dSAnthony Liguori     int fd;
255c74521dSAnthony Liguori     char *filename;
265c74521dSAnthony Liguori };
275c74521dSAnthony Liguori 
285c74521dSAnthony Liguori /**
295c74521dSAnthony Liguori  * A simple and incomplete backend to request entropy from /dev/random.
305c74521dSAnthony Liguori  *
315c74521dSAnthony Liguori  * This backend exposes an additional "filename" property that can be used to
325c74521dSAnthony Liguori  * set the filename to use to open the backend.
335c74521dSAnthony Liguori  */
345c74521dSAnthony Liguori 
355c74521dSAnthony Liguori static void entropy_available(void *opaque)
365c74521dSAnthony Liguori {
37*cde63615SWei Jiangang     RngRandom *s = RNG_RANDOM(opaque);
3860253ed1SLadi Prosek 
39443590c2SLadi Prosek     while (!QSIMPLEQ_EMPTY(&s->parent.requests)) {
40443590c2SLadi Prosek         RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
415c74521dSAnthony Liguori         ssize_t len;
425c74521dSAnthony Liguori 
4360253ed1SLadi Prosek         len = read(s->fd, req->data, req->size);
44acbbc036SAmit Shah         if (len < 0 && errno == EAGAIN) {
45acbbc036SAmit Shah             return;
46acbbc036SAmit Shah         }
475c74521dSAnthony Liguori         g_assert(len != -1);
485c74521dSAnthony Liguori 
4960253ed1SLadi Prosek         req->receive_entropy(req->opaque, req->data, len);
505c74521dSAnthony Liguori 
5160253ed1SLadi Prosek         rng_backend_finalize_request(&s->parent, req);
5260253ed1SLadi Prosek     }
5360253ed1SLadi Prosek 
5460253ed1SLadi Prosek     /* We've drained all requests, the fd handler can be reset. */
555c74521dSAnthony Liguori     qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
565c74521dSAnthony Liguori }
575c74521dSAnthony Liguori 
5860253ed1SLadi Prosek static void rng_random_request_entropy(RngBackend *b, RngRequest *req)
595c74521dSAnthony Liguori {
60*cde63615SWei Jiangang     RngRandom *s = RNG_RANDOM(b);
615c74521dSAnthony Liguori 
62443590c2SLadi Prosek     if (QSIMPLEQ_EMPTY(&s->parent.requests)) {
6360253ed1SLadi Prosek         /* If there are no pending requests yet, we need to
6460253ed1SLadi Prosek          * install our fd handler. */
655c74521dSAnthony Liguori         qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
665c74521dSAnthony Liguori     }
6760253ed1SLadi Prosek }
685c74521dSAnthony Liguori 
695c74521dSAnthony Liguori static void rng_random_opened(RngBackend *b, Error **errp)
705c74521dSAnthony Liguori {
71*cde63615SWei Jiangang     RngRandom *s = RNG_RANDOM(b);
725c74521dSAnthony Liguori 
735c74521dSAnthony Liguori     if (s->filename == NULL) {
74c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
755c74521dSAnthony Liguori                    "filename", "a valid filename");
765c74521dSAnthony Liguori     } else {
777f9c9d12SStefan Berger         s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
785c74521dSAnthony Liguori         if (s->fd == -1) {
79bc5741adSLuiz Capitulino             error_setg_file_open(errp, errno, s->filename);
805c74521dSAnthony Liguori         }
815c74521dSAnthony Liguori     }
825c74521dSAnthony Liguori }
835c74521dSAnthony Liguori 
845c74521dSAnthony Liguori static char *rng_random_get_filename(Object *obj, Error **errp)
855c74521dSAnthony Liguori {
86*cde63615SWei Jiangang     RngRandom *s = RNG_RANDOM(obj);
875c74521dSAnthony Liguori 
885c74521dSAnthony Liguori     return g_strdup(s->filename);
895c74521dSAnthony Liguori }
905c74521dSAnthony Liguori 
915c74521dSAnthony Liguori static void rng_random_set_filename(Object *obj, const char *filename,
925c74521dSAnthony Liguori                                  Error **errp)
935c74521dSAnthony Liguori {
945c74521dSAnthony Liguori     RngBackend *b = RNG_BACKEND(obj);
95*cde63615SWei Jiangang     RngRandom *s = RNG_RANDOM(obj);
965c74521dSAnthony Liguori 
975c74521dSAnthony Liguori     if (b->opened) {
98c6bd8c70SMarkus Armbruster         error_setg(errp, QERR_PERMISSION_DENIED);
995c74521dSAnthony Liguori         return;
1005c74521dSAnthony Liguori     }
1015c74521dSAnthony Liguori 
1025c74521dSAnthony Liguori     g_free(s->filename);
1035c74521dSAnthony Liguori     s->filename = g_strdup(filename);
1045c74521dSAnthony Liguori }
1055c74521dSAnthony Liguori 
1065c74521dSAnthony Liguori static void rng_random_init(Object *obj)
1075c74521dSAnthony Liguori {
108*cde63615SWei Jiangang     RngRandom *s = RNG_RANDOM(obj);
1095c74521dSAnthony Liguori 
1105c74521dSAnthony Liguori     object_property_add_str(obj, "filename",
1115c74521dSAnthony Liguori                             rng_random_get_filename,
1125c74521dSAnthony Liguori                             rng_random_set_filename,
1135c74521dSAnthony Liguori                             NULL);
1145c74521dSAnthony Liguori 
1155c74521dSAnthony Liguori     s->filename = g_strdup("/dev/random");
116513b8c74SPaolo Bonzini     s->fd = -1;
1175c74521dSAnthony Liguori }
1185c74521dSAnthony Liguori 
1195c74521dSAnthony Liguori static void rng_random_finalize(Object *obj)
1205c74521dSAnthony Liguori {
121*cde63615SWei Jiangang     RngRandom *s = RNG_RANDOM(obj);
1225c74521dSAnthony Liguori 
1235c74521dSAnthony Liguori     if (s->fd != -1) {
124513b8c74SPaolo Bonzini         qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1257f9c9d12SStefan Berger         qemu_close(s->fd);
1265c74521dSAnthony Liguori     }
1275c74521dSAnthony Liguori 
1285c74521dSAnthony Liguori     g_free(s->filename);
1295c74521dSAnthony Liguori }
1305c74521dSAnthony Liguori 
1315c74521dSAnthony Liguori static void rng_random_class_init(ObjectClass *klass, void *data)
1325c74521dSAnthony Liguori {
1335c74521dSAnthony Liguori     RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
1345c74521dSAnthony Liguori 
1355c74521dSAnthony Liguori     rbc->request_entropy = rng_random_request_entropy;
1365c74521dSAnthony Liguori     rbc->opened = rng_random_opened;
1375c74521dSAnthony Liguori }
1385c74521dSAnthony Liguori 
1398c43a6f0SAndreas Färber static const TypeInfo rng_random_info = {
1405c74521dSAnthony Liguori     .name = TYPE_RNG_RANDOM,
1415c74521dSAnthony Liguori     .parent = TYPE_RNG_BACKEND,
142*cde63615SWei Jiangang     .instance_size = sizeof(RngRandom),
1435c74521dSAnthony Liguori     .class_init = rng_random_class_init,
1445c74521dSAnthony Liguori     .instance_init = rng_random_init,
1455c74521dSAnthony Liguori     .instance_finalize = rng_random_finalize,
1465c74521dSAnthony Liguori };
1475c74521dSAnthony Liguori 
1485c74521dSAnthony Liguori static void register_types(void)
1495c74521dSAnthony Liguori {
1505c74521dSAnthony Liguori     type_register_static(&rng_random_info);
1515c74521dSAnthony Liguori }
1525c74521dSAnthony Liguori 
1535c74521dSAnthony Liguori type_init(register_types);
154