rumpuser_random.c revision 1.3 1 1.3 pooka /* $NetBSD: rumpuser_random.c,v 1.3 2014/08/24 14:37:31 pooka Exp $ */
2 1.3 pooka
3 1.1 justin /*
4 1.1 justin * Copyright (c) 2014 Justin Cormack. All Rights Reserved.
5 1.1 justin *
6 1.1 justin * Redistribution and use in source and binary forms, with or without
7 1.1 justin * modification, are permitted provided that the following conditions
8 1.1 justin * are met:
9 1.1 justin * 1. Redistributions of source code must retain the above copyright
10 1.1 justin * notice, this list of conditions and the following disclaimer.
11 1.1 justin * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 justin * notice, this list of conditions and the following disclaimer in the
13 1.1 justin * documentation and/or other materials provided with the distribution.
14 1.1 justin *
15 1.1 justin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 justin * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 justin * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 justin * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 justin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 justin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 justin * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 justin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 justin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 justin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 justin * SUCH DAMAGE.
26 1.1 justin */
27 1.1 justin
28 1.1 justin #include "rumpuser_port.h"
29 1.1 justin
30 1.1 justin #if !defined(lint)
31 1.3 pooka __RCSID("$NetBSD: rumpuser_random.c,v 1.3 2014/08/24 14:37:31 pooka Exp $");
32 1.1 justin #endif /* !lint */
33 1.1 justin
34 1.1 justin #include <sys/types.h>
35 1.1 justin
36 1.1 justin #include <assert.h>
37 1.1 justin #include <errno.h>
38 1.1 justin #include <fcntl.h>
39 1.1 justin #include <stdint.h>
40 1.1 justin #include <stdio.h>
41 1.1 justin #include <stdlib.h>
42 1.1 justin #include <string.h>
43 1.1 justin #include <unistd.h>
44 1.1 justin
45 1.1 justin #include <rump/rumpuser.h>
46 1.1 justin
47 1.1 justin #include "rumpuser_int.h"
48 1.1 justin
49 1.1 justin static const size_t random_maxread = 32;
50 1.1 justin
51 1.1 justin #ifdef PLATFORM_HAS_ARC4RANDOM_BUF
52 1.1 justin int
53 1.1 justin rumpuser__random_init(void)
54 1.1 justin {
55 1.1 justin
56 1.1 justin return 0;
57 1.1 justin }
58 1.1 justin #else
59 1.1 justin static const char *random_device = "/dev/urandom";
60 1.1 justin static int random_fd = -1;
61 1.1 justin
62 1.1 justin int
63 1.1 justin rumpuser__random_init(void)
64 1.1 justin {
65 1.1 justin
66 1.1 justin random_fd = open(random_device, O_RDONLY);
67 1.1 justin if (random_fd < 0) {
68 1.1 justin fprintf(stderr, "random init open failed\n");
69 1.2 justin return errno;
70 1.1 justin }
71 1.1 justin return 0;
72 1.1 justin }
73 1.1 justin #endif
74 1.1 justin
75 1.1 justin int
76 1.1 justin rumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp)
77 1.1 justin {
78 1.1 justin #ifndef PLATFORM_HAS_ARC4RANDOM_BUF
79 1.1 justin ssize_t rv;
80 1.1 justin
81 1.1 justin rv = read(random_fd, buf, buflen > random_maxread ? random_maxread : buflen);
82 1.1 justin if (rv < 0) {
83 1.1 justin ET(errno);
84 1.1 justin }
85 1.1 justin *retp = rv;
86 1.1 justin #else
87 1.1 justin buflen = buflen > random_maxread ? random_maxread : buflen;
88 1.1 justin arc4random_buf(buf, buflen);
89 1.1 justin *retp = buflen;
90 1.1 justin #endif
91 1.1 justin
92 1.2 justin return 0;
93 1.1 justin }
94