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" 190b8fa32fSMarkus Armbruster #include "qemu/module.h" 205c74521dSAnthony Liguori 21cde63615SWei Jiangang struct RngRandom 225c74521dSAnthony Liguori { 235c74521dSAnthony Liguori RngBackend parent; 245c74521dSAnthony Liguori 255c74521dSAnthony Liguori int fd; 265c74521dSAnthony Liguori char *filename; 275c74521dSAnthony Liguori }; 285c74521dSAnthony Liguori 295c74521dSAnthony Liguori /** 305c74521dSAnthony Liguori * A simple and incomplete backend to request entropy from /dev/random. 315c74521dSAnthony Liguori * 325c74521dSAnthony Liguori * This backend exposes an additional "filename" property that can be used to 335c74521dSAnthony Liguori * set the filename to use to open the backend. 345c74521dSAnthony Liguori */ 355c74521dSAnthony Liguori 365c74521dSAnthony Liguori static void entropy_available(void *opaque) 375c74521dSAnthony Liguori { 38cde63615SWei Jiangang RngRandom *s = RNG_RANDOM(opaque); 3960253ed1SLadi Prosek 40443590c2SLadi Prosek while (!QSIMPLEQ_EMPTY(&s->parent.requests)) { 41443590c2SLadi Prosek RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests); 425c74521dSAnthony Liguori ssize_t len; 435c74521dSAnthony Liguori 4460253ed1SLadi Prosek len = read(s->fd, req->data, req->size); 45acbbc036SAmit Shah if (len < 0 && errno == EAGAIN) { 46acbbc036SAmit Shah return; 47acbbc036SAmit Shah } 485c74521dSAnthony Liguori g_assert(len != -1); 495c74521dSAnthony Liguori 5060253ed1SLadi Prosek req->receive_entropy(req->opaque, req->data, len); 515c74521dSAnthony Liguori 5260253ed1SLadi Prosek rng_backend_finalize_request(&s->parent, req); 5360253ed1SLadi Prosek } 5460253ed1SLadi Prosek 5560253ed1SLadi Prosek /* We've drained all requests, the fd handler can be reset. */ 565c74521dSAnthony Liguori qemu_set_fd_handler(s->fd, NULL, NULL, NULL); 575c74521dSAnthony Liguori } 585c74521dSAnthony Liguori 5960253ed1SLadi Prosek static void rng_random_request_entropy(RngBackend *b, RngRequest *req) 605c74521dSAnthony Liguori { 61cde63615SWei Jiangang RngRandom *s = RNG_RANDOM(b); 625c74521dSAnthony Liguori 63443590c2SLadi Prosek if (QSIMPLEQ_EMPTY(&s->parent.requests)) { 6460253ed1SLadi Prosek /* If there are no pending requests yet, we need to 6560253ed1SLadi Prosek * install our fd handler. */ 665c74521dSAnthony Liguori qemu_set_fd_handler(s->fd, entropy_available, NULL, s); 675c74521dSAnthony Liguori } 6860253ed1SLadi Prosek } 695c74521dSAnthony Liguori 705c74521dSAnthony Liguori static void rng_random_opened(RngBackend *b, Error **errp) 715c74521dSAnthony Liguori { 72cde63615SWei Jiangang RngRandom *s = RNG_RANDOM(b); 735c74521dSAnthony Liguori 745c74521dSAnthony Liguori if (s->filename == NULL) { 75c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 765c74521dSAnthony Liguori "filename", "a valid filename"); 775c74521dSAnthony Liguori } else { 78*18f3a07bSZhao Liu s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK, errp); 795c74521dSAnthony Liguori } 805c74521dSAnthony Liguori } 815c74521dSAnthony Liguori 825c74521dSAnthony Liguori static char *rng_random_get_filename(Object *obj, Error **errp) 835c74521dSAnthony Liguori { 84cde63615SWei Jiangang RngRandom *s = RNG_RANDOM(obj); 855c74521dSAnthony Liguori 865c74521dSAnthony Liguori return g_strdup(s->filename); 875c74521dSAnthony Liguori } 885c74521dSAnthony Liguori 895c74521dSAnthony Liguori static void rng_random_set_filename(Object *obj, const char *filename, 905c74521dSAnthony Liguori Error **errp) 915c74521dSAnthony Liguori { 925c74521dSAnthony Liguori RngBackend *b = RNG_BACKEND(obj); 93cde63615SWei Jiangang RngRandom *s = RNG_RANDOM(obj); 945c74521dSAnthony Liguori 955c74521dSAnthony Liguori if (b->opened) { 96ff924448SMarkus Armbruster error_setg(errp, "Property 'filename' can no longer be set"); 975c74521dSAnthony Liguori return; 985c74521dSAnthony Liguori } 995c74521dSAnthony Liguori 1005c74521dSAnthony Liguori g_free(s->filename); 1015c74521dSAnthony Liguori s->filename = g_strdup(filename); 1025c74521dSAnthony Liguori } 1035c74521dSAnthony Liguori 1045c74521dSAnthony Liguori static void rng_random_init(Object *obj) 1055c74521dSAnthony Liguori { 106cde63615SWei Jiangang RngRandom *s = RNG_RANDOM(obj); 1075c74521dSAnthony Liguori 108a2230bd7SKashyap Chamarthy s->filename = g_strdup("/dev/urandom"); 109513b8c74SPaolo Bonzini s->fd = -1; 1105c74521dSAnthony Liguori } 1115c74521dSAnthony Liguori 1125c74521dSAnthony Liguori static void rng_random_finalize(Object *obj) 1135c74521dSAnthony Liguori { 114cde63615SWei Jiangang RngRandom *s = RNG_RANDOM(obj); 1155c74521dSAnthony Liguori 1165c74521dSAnthony Liguori if (s->fd != -1) { 117513b8c74SPaolo Bonzini qemu_set_fd_handler(s->fd, NULL, NULL, NULL); 1187f9c9d12SStefan Berger qemu_close(s->fd); 1195c74521dSAnthony Liguori } 1205c74521dSAnthony Liguori 1215c74521dSAnthony Liguori g_free(s->filename); 1225c74521dSAnthony Liguori } 1235c74521dSAnthony Liguori 1245c74521dSAnthony Liguori static void rng_random_class_init(ObjectClass *klass, void *data) 1255c74521dSAnthony Liguori { 1265c74521dSAnthony Liguori RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); 1275c74521dSAnthony Liguori 1285c74521dSAnthony Liguori rbc->request_entropy = rng_random_request_entropy; 1295c74521dSAnthony Liguori rbc->opened = rng_random_opened; 130a3d3ee6cSEduardo Habkost object_class_property_add_str(klass, "filename", 131a3d3ee6cSEduardo Habkost rng_random_get_filename, 132a3d3ee6cSEduardo Habkost rng_random_set_filename); 133a3d3ee6cSEduardo Habkost 1345c74521dSAnthony Liguori } 1355c74521dSAnthony Liguori 1368c43a6f0SAndreas Färber static const TypeInfo rng_random_info = { 1375c74521dSAnthony Liguori .name = TYPE_RNG_RANDOM, 1385c74521dSAnthony Liguori .parent = TYPE_RNG_BACKEND, 139cde63615SWei Jiangang .instance_size = sizeof(RngRandom), 1405c74521dSAnthony Liguori .class_init = rng_random_class_init, 1415c74521dSAnthony Liguori .instance_init = rng_random_init, 1425c74521dSAnthony Liguori .instance_finalize = rng_random_finalize, 1435c74521dSAnthony Liguori }; 1445c74521dSAnthony Liguori 1455c74521dSAnthony Liguori static void register_types(void) 1465c74521dSAnthony Liguori { 1475c74521dSAnthony Liguori type_register_static(&rng_random_info); 1485c74521dSAnthony Liguori } 1495c74521dSAnthony Liguori 1505c74521dSAnthony Liguori type_init(register_types); 151