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