crandom.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 /** @file
00022  *
00023  * Cryptographically strong random number generator
00024  *
00025  * Currently the cryptographic part is not implemented, and this just
00026  * uses random().
00027  */
00028 
00029 #include <gpxe/crypto.h>
00030 #include <stdlib.h>
00031 
00032 /**
00033  * Get cryptographically strong random bytes
00034  *
00035  * @v buf       Buffer in which to store random bytes
00036  * @v len       Number of random bytes to generate
00037  *
00038  * @b WARNING: This function is currently underimplemented, and does
00039  * not give numbers any stronger than random()!
00040  */
00041 void get_random_bytes ( void *buf, size_t len )
00042 {
00043         u8 *bufp = buf;
00044 
00045         /*
00046          * Somewhat arbitrarily, choose the 0x00FF0000-masked byte
00047          * returned by random() as having good entropy. PRNGs often
00048          * don't provide good entropy in lower bits, and the top byte
00049          * might show a pattern because of sign issues.
00050          */
00051 
00052         while ( len-- ) {
00053                 *bufp++ = ( random() >> 16 ) & 0xFF;
00054         }
00055 }

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1