openldap/libraries/liblutil/entropy.c
Kurt Zeilenga bdca662249 Add lutil_entropy(). Currently only supports /dev/urandom or
Linux equivs.  Should be extended to support other quality
sources of entropy.  Should be extended to support a reasonable fallback.
1999-10-26 22:57:39 +00:00

35 lines
650 B
C

/* $OpenLDAP$ */
/*
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
/*
* lutil_entropy() provides nbyptes of entropy in buf.
* Quality offerred is suitable for one-time uses, such as "once" keys.
*/
int lutil_entropy( char *buf, int nbytes )
{
if( nbytes < 0 ) return -1;
if( nbytes == 0 ) return 0;
#ifdef URANDOM_DEVICE
/* Linux and *BSD offer a urandom device */
{
int rc, fd;
fd = open( URANDOM_DEVICE, O_RDONLY );
if( fd < 0 ) return -1;
rc = read( fd, buf, nbytes );
close(fd);
if( rc < nbytes ) return -1;
return 0;
}
#endif
return -1;
}