blob: e2a49b0571d79eab335d5a74841d92c50a727b6a [file] [log] [blame]
Anthony Liguori5c745212012-06-25 10:34:09 -05001/*
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
Peter Maydell9c058332016-01-29 17:49:54 +000013#include "qemu/osdep.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020014#include "sysemu/rng-random.h"
15#include "sysemu/rng.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010016#include "qapi/error.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Anthony Liguori5c745212012-06-25 10:34:09 -050019
Wei Jiangangcde63612016-04-15 09:36:08 +080020struct RngRandom
Anthony Liguori5c745212012-06-25 10:34:09 -050021{
22 RngBackend parent;
23
24 int fd;
25 char *filename;
Anthony Liguori5c745212012-06-25 10:34:09 -050026};
27
28/**
29 * A simple and incomplete backend to request entropy from /dev/random.
30 *
31 * This backend exposes an additional "filename" property that can be used to
32 * set the filename to use to open the backend.
33 */
34
35static void entropy_available(void *opaque)
36{
Wei Jiangangcde63612016-04-15 09:36:08 +080037 RngRandom *s = RNG_RANDOM(opaque);
Anthony Liguori5c745212012-06-25 10:34:09 -050038
Ladi Prosek443590c2016-03-03 14:16:11 +010039 while (!QSIMPLEQ_EMPTY(&s->parent.requests)) {
40 RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
Ladi Prosek60253ed2016-03-03 09:37:18 +010041 ssize_t len;
42
43 len = read(s->fd, req->data, req->size);
44 if (len < 0 && errno == EAGAIN) {
45 return;
46 }
47 g_assert(len != -1);
48
49 req->receive_entropy(req->opaque, req->data, len);
50
51 rng_backend_finalize_request(&s->parent, req);
Amit Shahacbbc032013-04-16 15:58:16 +053052 }
Anthony Liguori5c745212012-06-25 10:34:09 -050053
Ladi Prosek60253ed2016-03-03 09:37:18 +010054 /* We've drained all requests, the fd handler can be reset. */
Anthony Liguori5c745212012-06-25 10:34:09 -050055 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
56}
57
Ladi Prosek60253ed2016-03-03 09:37:18 +010058static void rng_random_request_entropy(RngBackend *b, RngRequest *req)
Anthony Liguori5c745212012-06-25 10:34:09 -050059{
Wei Jiangangcde63612016-04-15 09:36:08 +080060 RngRandom *s = RNG_RANDOM(b);
Anthony Liguori5c745212012-06-25 10:34:09 -050061
Ladi Prosek443590c2016-03-03 14:16:11 +010062 if (QSIMPLEQ_EMPTY(&s->parent.requests)) {
Ladi Prosek60253ed2016-03-03 09:37:18 +010063 /* If there are no pending requests yet, we need to
64 * install our fd handler. */
65 qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
Anthony Liguori5c745212012-06-25 10:34:09 -050066 }
Anthony Liguori5c745212012-06-25 10:34:09 -050067}
68
69static void rng_random_opened(RngBackend *b, Error **errp)
70{
Wei Jiangangcde63612016-04-15 09:36:08 +080071 RngRandom *s = RNG_RANDOM(b);
Anthony Liguori5c745212012-06-25 10:34:09 -050072
73 if (s->filename == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +010074 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
75 "filename", "a valid filename");
Anthony Liguori5c745212012-06-25 10:34:09 -050076 } else {
Stefan Berger7f9c9d12013-03-01 07:53:55 -050077 s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
Anthony Liguori5c745212012-06-25 10:34:09 -050078 if (s->fd == -1) {
Luiz Capitulinobc5741a2013-06-07 14:28:02 -040079 error_setg_file_open(errp, errno, s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -050080 }
81 }
82}
83
84static char *rng_random_get_filename(Object *obj, Error **errp)
85{
Wei Jiangangcde63612016-04-15 09:36:08 +080086 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -050087
Markus Armbruster24588102014-12-04 10:26:55 +010088 return g_strdup(s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -050089}
90
91static void rng_random_set_filename(Object *obj, const char *filename,
92 Error **errp)
93{
94 RngBackend *b = RNG_BACKEND(obj);
Wei Jiangangcde63612016-04-15 09:36:08 +080095 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -050096
97 if (b->opened) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +010098 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori5c745212012-06-25 10:34:09 -050099 return;
100 }
101
Eduardo Habkostfeced892014-05-30 17:02:18 -0300102 g_free(s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -0500103 s->filename = g_strdup(filename);
104}
105
106static void rng_random_init(Object *obj)
107{
Wei Jiangangcde63612016-04-15 09:36:08 +0800108 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -0500109
110 object_property_add_str(obj, "filename",
111 rng_random_get_filename,
112 rng_random_set_filename,
113 NULL);
114
115 s->filename = g_strdup("/dev/random");
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100116 s->fd = -1;
Anthony Liguori5c745212012-06-25 10:34:09 -0500117}
118
119static void rng_random_finalize(Object *obj)
120{
Wei Jiangangcde63612016-04-15 09:36:08 +0800121 RngRandom *s = RNG_RANDOM(obj);
Anthony Liguori5c745212012-06-25 10:34:09 -0500122
Anthony Liguori5c745212012-06-25 10:34:09 -0500123 if (s->fd != -1) {
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100124 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
Stefan Berger7f9c9d12013-03-01 07:53:55 -0500125 qemu_close(s->fd);
Anthony Liguori5c745212012-06-25 10:34:09 -0500126 }
127
128 g_free(s->filename);
129}
130
131static void rng_random_class_init(ObjectClass *klass, void *data)
132{
133 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
134
135 rbc->request_entropy = rng_random_request_entropy;
136 rbc->opened = rng_random_opened;
137}
138
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100139static const TypeInfo rng_random_info = {
Anthony Liguori5c745212012-06-25 10:34:09 -0500140 .name = TYPE_RNG_RANDOM,
141 .parent = TYPE_RNG_BACKEND,
Wei Jiangangcde63612016-04-15 09:36:08 +0800142 .instance_size = sizeof(RngRandom),
Anthony Liguori5c745212012-06-25 10:34:09 -0500143 .class_init = rng_random_class_init,
144 .instance_init = rng_random_init,
145 .instance_finalize = rng_random_finalize,
146};
147
148static void register_types(void)
149{
150 type_register_static(&rng_random_info);
151}
152
153type_init(register_types);