2015-06-23 10:05:56 +03:00
/*
2003-08-13 05:53:07 +04:00
Unix SMB / CIFS implementation .
Functions to create reasonable random numbers for crypto use .
Copyright ( C ) Jeremy Allison 2001
2015-06-23 10:05:56 +03:00
2003-08-13 05:53:07 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2003-08-13 05:53:07 +04:00
( at your option ) any later version .
2015-06-23 10:05:56 +03:00
2003-08-13 05:53:07 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2015-06-23 10:05:56 +03:00
2003-08-13 05:53:07 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2003-08-13 05:53:07 +04:00
*/
2015-06-23 10:28:28 +03:00
# include "replace.h"
2005-02-10 08:09:35 +03:00
# include "system/filesys.h"
2015-06-23 10:28:28 +03:00
# include "lib/util/genrand.h"
2015-10-02 01:27:22 +03:00
# include "sys_rw_data.h"
2015-06-23 10:28:28 +03:00
# include "lib/util/blocking.h"
2007-10-16 03:27:15 +04:00
static int urand_fd = - 1 ;
2015-10-02 01:27:22 +03:00
static void open_urandom ( void )
2003-08-13 05:53:07 +04:00
{
2015-10-02 01:27:22 +03:00
if ( urand_fd ! = - 1 ) {
2003-08-13 05:53:07 +04:00
return ;
2013-06-13 23:55:43 +04:00
}
2015-10-02 01:27:22 +03:00
urand_fd = open ( " /dev/urandom " , O_RDONLY , 0 ) ;
if ( urand_fd = = - 1 ) {
abort ( ) ;
2003-08-13 05:53:07 +04:00
}
2015-10-02 01:27:22 +03:00
smb_set_close_on_exec ( urand_fd ) ;
2003-08-13 05:53:07 +04:00
}
2006-03-05 20:15:19 +03:00
_PUBLIC_ void generate_random_buffer ( uint8_t * out , int len )
2003-08-13 05:53:07 +04:00
{
2015-10-02 01:27:22 +03:00
ssize_t rw_ret ;
2003-08-13 05:53:07 +04:00
2015-10-02 01:27:22 +03:00
open_urandom ( ) ;
2003-08-13 05:53:07 +04:00
2015-10-02 01:27:22 +03:00
rw_ret = read_data ( urand_fd , out , len ) ;
if ( rw_ret ! = len ) {
abort ( ) ;
2003-08-13 05:53:07 +04:00
}
}
2015-10-02 01:27:22 +03:00
/*
* Keep generate_secret_buffer in case we ever want to do something
* different
*/
2007-10-16 03:27:15 +04:00
_PUBLIC_ void generate_secret_buffer ( uint8_t * out , int len )
{
generate_random_buffer ( out , len ) ;
}