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