[tcp] Enable AF_INET6 transport for tcp connections
[gpxe.git] / src / crypto / crandom.c
blob9828482e4fee016f74ac1e2b53e0d5152cb4478d
1 /*
2 * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 /** @file
23 * Cryptographically strong random number generator
25 * Currently the cryptographic part is not implemented, and this just
26 * uses random().
29 #include <gpxe/crypto.h>
30 #include <stdlib.h>
32 /**
33 * Get cryptographically strong random bytes
35 * @v buf Buffer in which to store random bytes
36 * @v len Number of random bytes to generate
38 * @b WARNING: This function is currently underimplemented, and does
39 * not give numbers any stronger than random()!
41 void get_random_bytes ( void *buf, size_t len )
43 u8 *bufp = buf;
46 * Somewhat arbitrarily, choose the 0x00FF0000-masked byte
47 * returned by random() as having good entropy. PRNGs often
48 * don't provide good entropy in lower bits, and the top byte
49 * might show a pattern because of sign issues.
52 while ( len-- ) {
53 *bufp++ = ( random() >> 16 ) & 0xFF;