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"
2019-08-04 15:10:53 +03:00
# include "lib/util/fault.h"
2015-06-23 10:28:28 +03:00
# include "lib/util/genrand.h"
2007-10-16 03:27:15 +04:00
2019-03-18 19:03:30 +03:00
# include <gnutls/gnutls.h>
# include <gnutls/crypto.h>
2007-10-16 03:27:15 +04:00
2019-07-31 16:38:50 +03:00
/*
* Details about the GnuTLS CSPRNG :
*
* https : //nikmav.blogspot.com/2017/03/improving-by-simplifying-gnutls-prng.html
*/
2019-08-04 15:10:53 +03:00
_NORETURN_ static void genrand_panic ( int err ,
const char * location ,
const char * func )
{
char buf [ 200 ] ;
snprintf ( buf , sizeof ( buf ) ,
" %s:%s: GnuTLS could not generate a random buffer: %s [%d] \n " ,
location , func , gnutls_strerror_name ( err ) , err ) ;
smb_panic ( buf ) ;
}
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
{
2019-08-12 17:10:20 +03:00
/* Random number generator for temporary keys. */
2019-08-04 15:10:53 +03:00
int ret = gnutls_rnd ( GNUTLS_RND_RANDOM , out , len ) ;
if ( ret ! = 0 ) {
genrand_panic ( ret , __location__ , __func__ ) ;
}
2003-08-13 05:53:07 +04:00
}
2007-10-16 03:27:15 +04:00
_PUBLIC_ void generate_secret_buffer ( uint8_t * out , int len )
{
2019-08-12 17:10:20 +03:00
/*
* Random number generator for long term keys .
*
* The key generator , will re - seed after a fixed amount of bytes is
2019-07-31 16:38:50 +03:00
* generated ( typically less than the nonce ) , and will also re - seed
* based on time , i . e . , after few hours of operation without reaching
* the limit for a re - seed . For its re - seed it mixes mixes data obtained
* from the OS random device with the previous key .
*/
2019-08-04 15:10:53 +03:00
int ret = gnutls_rnd ( GNUTLS_RND_KEY , out , len ) ;
if ( ret ! = 0 ) {
genrand_panic ( ret , __location__ , __func__ ) ;
}
2007-10-16 03:27:15 +04:00
}
2019-07-31 16:16:37 +03:00
_PUBLIC_ void generate_nonce_buffer ( uint8_t * out , int len )
{
/*
2019-08-12 17:10:20 +03:00
* Random number generator for nonce and initialization vectors .
*
2019-07-31 16:16:37 +03:00
* The nonce generator will reseed after outputting a fixed amount of
* bytes ( typically few megabytes ) , or after few hours of operation
* without reaching the limit has passed .
*/
2019-08-04 15:10:53 +03:00
int ret = gnutls_rnd ( GNUTLS_RND_NONCE , out , len ) ;
if ( ret ! = 0 ) {
genrand_panic ( ret , __location__ , __func__ ) ;
}
2019-07-31 16:16:37 +03:00
}