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