blob: 601d9dc0c249860991a959a8abef814f266cdc5e [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
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020013#include "sysemu/rng-random.h"
14#include "sysemu/rng.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/main-loop.h"
Anthony Liguori5c745212012-06-25 10:34:09 -050017
18struct 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
37static 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);
Amit Shahacbbc032013-04-16 15:58:16 +053044 if (len < 0 && errno == EAGAIN) {
45 return;
46 }
Anthony Liguori5c745212012-06-25 10:34:09 -050047 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
55static 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
72static 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 {
Stefan Berger7f9c9d12013-03-01 07:53:55 -050080 s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
Anthony Liguori5c745212012-06-25 10:34:09 -050081 if (s->fd == -1) {
Luiz Capitulinobc5741a2013-06-07 14:28:02 -040082 error_setg_file_open(errp, errno, s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -050083 }
84 }
85}
86
87static char *rng_random_get_filename(Object *obj, Error **errp)
88{
89 RndRandom *s = RNG_RANDOM(obj);
90
91 if (s->filename) {
92 return g_strdup(s->filename);
93 }
94
95 return NULL;
96}
97
98static void rng_random_set_filename(Object *obj, const char *filename,
99 Error **errp)
100{
101 RngBackend *b = RNG_BACKEND(obj);
102 RndRandom *s = RNG_RANDOM(obj);
103
104 if (b->opened) {
105 error_set(errp, QERR_PERMISSION_DENIED);
106 return;
107 }
108
Eduardo Habkostfeced892014-05-30 17:02:18 -0300109 g_free(s->filename);
Anthony Liguori5c745212012-06-25 10:34:09 -0500110 s->filename = g_strdup(filename);
111}
112
113static void rng_random_init(Object *obj)
114{
115 RndRandom *s = RNG_RANDOM(obj);
116
117 object_property_add_str(obj, "filename",
118 rng_random_get_filename,
119 rng_random_set_filename,
120 NULL);
121
122 s->filename = g_strdup("/dev/random");
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100123 s->fd = -1;
Anthony Liguori5c745212012-06-25 10:34:09 -0500124}
125
126static void rng_random_finalize(Object *obj)
127{
128 RndRandom *s = RNG_RANDOM(obj);
129
Anthony Liguori5c745212012-06-25 10:34:09 -0500130 if (s->fd != -1) {
Paolo Bonzini513b8c72013-12-20 23:21:06 +0100131 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
Stefan Berger7f9c9d12013-03-01 07:53:55 -0500132 qemu_close(s->fd);
Anthony Liguori5c745212012-06-25 10:34:09 -0500133 }
134
135 g_free(s->filename);
136}
137
138static void rng_random_class_init(ObjectClass *klass, void *data)
139{
140 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
141
142 rbc->request_entropy = rng_random_request_entropy;
143 rbc->opened = rng_random_opened;
144}
145
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100146static const TypeInfo rng_random_info = {
Anthony Liguori5c745212012-06-25 10:34:09 -0500147 .name = TYPE_RNG_RANDOM,
148 .parent = TYPE_RNG_BACKEND,
149 .instance_size = sizeof(RndRandom),
150 .class_init = rng_random_class_init,
151 .instance_init = rng_random_init,
152 .instance_finalize = rng_random_finalize,
153};
154
155static void register_types(void)
156{
157 type_register_static(&rng_random_info);
158}
159
160type_init(register_types);