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