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 135c74521dSAnthony Liguori #include "qemu/rng-random.h" 145c74521dSAnthony Liguori #include "qemu/rng.h" 15*7b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h" 165c74521dSAnthony Liguori #include "main-loop.h" 175c74521dSAnthony Liguori 185c74521dSAnthony Liguori struct RndRandom 195c74521dSAnthony Liguori { 205c74521dSAnthony Liguori RngBackend parent; 215c74521dSAnthony Liguori 225c74521dSAnthony Liguori int fd; 235c74521dSAnthony Liguori char *filename; 245c74521dSAnthony Liguori 255c74521dSAnthony Liguori EntropyReceiveFunc *receive_func; 265c74521dSAnthony Liguori void *opaque; 275c74521dSAnthony Liguori size_t size; 285c74521dSAnthony Liguori }; 295c74521dSAnthony Liguori 305c74521dSAnthony Liguori /** 315c74521dSAnthony Liguori * A simple and incomplete backend to request entropy from /dev/random. 325c74521dSAnthony Liguori * 335c74521dSAnthony Liguori * This backend exposes an additional "filename" property that can be used to 345c74521dSAnthony Liguori * set the filename to use to open the backend. 355c74521dSAnthony Liguori */ 365c74521dSAnthony Liguori 375c74521dSAnthony Liguori static void entropy_available(void *opaque) 385c74521dSAnthony Liguori { 395c74521dSAnthony Liguori RndRandom *s = RNG_RANDOM(opaque); 405c74521dSAnthony Liguori uint8_t buffer[s->size]; 415c74521dSAnthony Liguori ssize_t len; 425c74521dSAnthony Liguori 435c74521dSAnthony Liguori len = read(s->fd, buffer, s->size); 445c74521dSAnthony Liguori g_assert(len != -1); 455c74521dSAnthony Liguori 465c74521dSAnthony Liguori s->receive_func(s->opaque, buffer, len); 475c74521dSAnthony Liguori s->receive_func = NULL; 485c74521dSAnthony Liguori 495c74521dSAnthony Liguori qemu_set_fd_handler(s->fd, NULL, NULL, NULL); 505c74521dSAnthony Liguori } 515c74521dSAnthony Liguori 525c74521dSAnthony Liguori static void rng_random_request_entropy(RngBackend *b, size_t size, 535c74521dSAnthony Liguori EntropyReceiveFunc *receive_entropy, 545c74521dSAnthony Liguori void *opaque) 555c74521dSAnthony Liguori { 565c74521dSAnthony Liguori RndRandom *s = RNG_RANDOM(b); 575c74521dSAnthony Liguori 585c74521dSAnthony Liguori if (s->receive_func) { 595c74521dSAnthony Liguori s->receive_func(s->opaque, NULL, 0); 605c74521dSAnthony Liguori } 615c74521dSAnthony Liguori 625c74521dSAnthony Liguori s->receive_func = receive_entropy; 635c74521dSAnthony Liguori s->opaque = opaque; 645c74521dSAnthony Liguori s->size = size; 655c74521dSAnthony Liguori 665c74521dSAnthony Liguori qemu_set_fd_handler(s->fd, entropy_available, NULL, s); 675c74521dSAnthony Liguori } 685c74521dSAnthony Liguori 695c74521dSAnthony Liguori static void rng_random_opened(RngBackend *b, Error **errp) 705c74521dSAnthony Liguori { 715c74521dSAnthony Liguori RndRandom *s = RNG_RANDOM(b); 725c74521dSAnthony Liguori 735c74521dSAnthony Liguori if (s->filename == NULL) { 745c74521dSAnthony Liguori error_set(errp, QERR_INVALID_PARAMETER_VALUE, 755c74521dSAnthony Liguori "filename", "a valid filename"); 765c74521dSAnthony Liguori } else { 775c74521dSAnthony Liguori s->fd = open(s->filename, O_RDONLY | O_NONBLOCK); 785c74521dSAnthony Liguori 795c74521dSAnthony Liguori if (s->fd == -1) { 805c74521dSAnthony Liguori error_set(errp, QERR_OPEN_FILE_FAILED, s->filename); 815c74521dSAnthony Liguori } 825c74521dSAnthony Liguori } 835c74521dSAnthony Liguori } 845c74521dSAnthony Liguori 855c74521dSAnthony Liguori static char *rng_random_get_filename(Object *obj, Error **errp) 865c74521dSAnthony Liguori { 875c74521dSAnthony Liguori RndRandom *s = RNG_RANDOM(obj); 885c74521dSAnthony Liguori 895c74521dSAnthony Liguori if (s->filename) { 905c74521dSAnthony Liguori return g_strdup(s->filename); 915c74521dSAnthony Liguori } 925c74521dSAnthony Liguori 935c74521dSAnthony Liguori return NULL; 945c74521dSAnthony Liguori } 955c74521dSAnthony Liguori 965c74521dSAnthony Liguori static void rng_random_set_filename(Object *obj, const char *filename, 975c74521dSAnthony Liguori Error **errp) 985c74521dSAnthony Liguori { 995c74521dSAnthony Liguori RngBackend *b = RNG_BACKEND(obj); 1005c74521dSAnthony Liguori RndRandom *s = RNG_RANDOM(obj); 1015c74521dSAnthony Liguori 1025c74521dSAnthony Liguori if (b->opened) { 1035c74521dSAnthony Liguori error_set(errp, QERR_PERMISSION_DENIED); 1045c74521dSAnthony Liguori return; 1055c74521dSAnthony Liguori } 1065c74521dSAnthony Liguori 1075c74521dSAnthony Liguori if (s->filename) { 1085c74521dSAnthony Liguori g_free(s->filename); 1095c74521dSAnthony Liguori } 1105c74521dSAnthony Liguori 1115c74521dSAnthony Liguori s->filename = g_strdup(filename); 1125c74521dSAnthony Liguori } 1135c74521dSAnthony Liguori 1145c74521dSAnthony Liguori static void rng_random_init(Object *obj) 1155c74521dSAnthony Liguori { 1165c74521dSAnthony Liguori RndRandom *s = RNG_RANDOM(obj); 1175c74521dSAnthony Liguori 1185c74521dSAnthony Liguori object_property_add_str(obj, "filename", 1195c74521dSAnthony Liguori rng_random_get_filename, 1205c74521dSAnthony Liguori rng_random_set_filename, 1215c74521dSAnthony Liguori NULL); 1225c74521dSAnthony Liguori 1235c74521dSAnthony Liguori s->filename = g_strdup("/dev/random"); 1245c74521dSAnthony Liguori } 1255c74521dSAnthony Liguori 1265c74521dSAnthony Liguori static void rng_random_finalize(Object *obj) 1275c74521dSAnthony Liguori { 1285c74521dSAnthony Liguori RndRandom *s = RNG_RANDOM(obj); 1295c74521dSAnthony Liguori 1305c74521dSAnthony Liguori qemu_set_fd_handler(s->fd, NULL, NULL, NULL); 1315c74521dSAnthony Liguori 1325c74521dSAnthony Liguori if (s->fd != -1) { 1335c74521dSAnthony Liguori close(s->fd); 1345c74521dSAnthony Liguori } 1355c74521dSAnthony Liguori 1365c74521dSAnthony Liguori g_free(s->filename); 1375c74521dSAnthony Liguori } 1385c74521dSAnthony Liguori 1395c74521dSAnthony Liguori static void rng_random_class_init(ObjectClass *klass, void *data) 1405c74521dSAnthony Liguori { 1415c74521dSAnthony Liguori RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); 1425c74521dSAnthony Liguori 1435c74521dSAnthony Liguori rbc->request_entropy = rng_random_request_entropy; 1445c74521dSAnthony Liguori rbc->opened = rng_random_opened; 1455c74521dSAnthony Liguori } 1465c74521dSAnthony Liguori 1475c74521dSAnthony Liguori static TypeInfo rng_random_info = { 1485c74521dSAnthony Liguori .name = TYPE_RNG_RANDOM, 1495c74521dSAnthony Liguori .parent = TYPE_RNG_BACKEND, 1505c74521dSAnthony Liguori .instance_size = sizeof(RndRandom), 1515c74521dSAnthony Liguori .class_init = rng_random_class_init, 1525c74521dSAnthony Liguori .instance_init = rng_random_init, 1535c74521dSAnthony Liguori .instance_finalize = rng_random_finalize, 1545c74521dSAnthony Liguori }; 1555c74521dSAnthony Liguori 1565c74521dSAnthony Liguori static void register_types(void) 1575c74521dSAnthony Liguori { 1585c74521dSAnthony Liguori type_register_static(&rng_random_info); 1595c74521dSAnthony Liguori } 1605c74521dSAnthony Liguori 1615c74521dSAnthony Liguori type_init(register_types); 162