1 /* cvm/random.c - CVM random number generation
2 * Copyright (C) 2010 Bruce Guenter <bruce@untroubled.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* Derived from dns_random.c from djbdns-1.05, which was made public
20 * domain as per http://cr.yp.to/distributors.html on 2007-12-28. */
21
22 #include <string.h>
23 #include <bglibs/systime.h>
24 #include <unistd.h>
25 #include <bglibs/uint32.h>
26 #include <bglibs/surfrand.h>
27 #include "random.h"
28
29 static struct surfrand state;
30
cvm_random_init(void)31 void cvm_random_init(void)
32 {
33 struct timeval tv;
34 uint32 data[32];
35
36 gettimeofday(&tv, 0);
37 data[0] += tv.tv_sec;
38 data[1] += tv.tv_usec;
39 data[2] = getpid();
40 data[3] = getppid();
41
42 surfrand_init(&state, data, 32);
43 }
44
cvm_random_fill(unsigned char * buf,unsigned len)45 void cvm_random_fill(unsigned char* buf, unsigned len)
46 {
47 surfrand_fill(&state, buf, len);
48 }
49