2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
38 __RCSID("$Heimdal: rand.c 22199 2007-12-07 13:43:25Z lha $"
53 const static RAND_METHOD
*selected_meth
= NULL
;
58 if (selected_meth
!= NULL
)
60 selected_meth
= &hc_rand_fortuna_method
;
64 RAND_seed(const void *indata
, size_t size
)
67 (*selected_meth
->seed
)(indata
, size
);
71 RAND_bytes(void *outdata
, size_t size
)
74 return (*selected_meth
->bytes
)(outdata
, size
);
81 (*selected_meth
->cleanup
)();
85 RAND_add(const void *indata
, size_t size
, double entropi
)
88 (*selected_meth
->add
)(indata
, size
, entropi
);
92 RAND_pseudo_bytes(void *outdata
, size_t size
)
95 return (*selected_meth
->pseudorand
)(outdata
, size
);
102 return (*selected_meth
->status
)();
106 RAND_set_rand_method(const RAND_METHOD
*meth
)
108 selected_meth
= meth
;
113 RAND_get_rand_method(void)
115 return selected_meth
;
119 RAND_set_rand_engine(ENGINE
*engine
)
124 #define RAND_FILE_SIZE 1024
127 RAND_load_file(const char *filename
, size_t size
)
129 unsigned char buf
[128];
134 fd
= open(filename
, O_RDONLY
| O_BINARY
, 0600);
140 slen
= read(fd
, buf
, sizeof(buf
));
143 RAND_seed(buf
, slen
);
152 RAND_write_file(const char *filename
)
154 unsigned char buf
[128];
158 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_BINARY
, 0600);
163 while(len
< RAND_FILE_SIZE
) {
164 res
= RAND_bytes(buf
, sizeof(buf
));
167 if (write(fd
, buf
, sizeof(buf
)) != sizeof(buf
)) {
180 RAND_file_name(char *filename
, size_t size
)
182 const char *e
= NULL
;
186 e
= getenv("RANDFILE");
194 * Here we really want to call getpwuid(getuid()) but this will
195 * cause recursive lookups if the nss library uses
196 * gssapi/krb5/hcrypto to authenticate to the ldap servers.
203 ret
= snprintf(filename
, size
, "%s/.rnd", e
);
205 ret
= snprintf(filename
, size
, "%s", e
);
207 if (ret
<= 0 || ret
>= size
)