r19604: This is a massive commit, and I appologise in advance for it's size.
[Samba.git] / source / heimdal / lib / des / rand-unix.c
bloba51c6c0c0dd9279fca49bfd200b0d9b825f23c00
1 /*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 RCSID("$Id: rand-unix.c,v 1.2 2006/10/21 21:09:14 lha Exp $");
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <rand.h>
44 #include <roken.h>
47 * Unix /dev/random
50 static int
51 get_device_fd(int flags)
53 static const char *rnd_devices[] = {
54 "/dev/urandom",
55 "/dev/random",
56 "/dev/srandom",
57 "/dev/arandom",
58 NULL
60 const char **p;
62 for(p = rnd_devices; *p; p++) {
63 int fd = open(*p, flags | O_NDELAY);
64 if(fd >= 0)
65 return fd;
67 return -1;
70 static void
71 unix_seed(const void *indata, int size)
73 int fd;
75 if (size <= 0)
76 return;
78 fd = get_device_fd(O_WRONLY);
79 if (fd < 0)
80 return;
82 write(fd, indata, size);
83 close(fd);
87 static int
88 unix_bytes(unsigned char *outdata, int size)
90 ssize_t count;
91 int fd;
93 if (size <= 0)
94 return 0;
96 fd = get_device_fd(O_RDONLY);
97 if (fd < 0)
98 return 0;
100 while (size > 0) {
101 count = read (fd, outdata, size);
102 if (count < 0 && errno == EINTR)
103 continue;
104 else if (count <= 0) {
105 close(fd);
106 return 0;
108 outdata += count;
109 size -= count;
111 close(fd);
113 return 1;
116 static void
117 unix_cleanup(void)
121 static void
122 unix_add(const void *indata, int size, double entropi)
124 unix_seed(indata, size);
127 static int
128 unix_pseudorand(unsigned char *outdata, int size)
130 return unix_bytes(outdata, size);
133 static int
134 unix_status(void)
136 int fd;
138 fd = get_device_fd(O_RDONLY);
139 if (fd < 0)
140 return 0;
141 close(fd);
143 return 1;
146 const RAND_METHOD hc_rand_unix_method = {
147 unix_seed,
148 unix_bytes,
149 unix_cleanup,
150 unix_add,
151 unix_pseudorand,
152 unix_status