openldap/libraries/liblutil/entropy.c
1999-10-26 23:04:44 +00:00

42 lines
741 B
C

/* $OpenLDAP$ */
/*
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "portable.h"
#include <fcntl.h>
#include <lutil.h>
/*
* lutil_entropy() provides nbytes 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);
/* should return nbytes */
if( rc < nbytes ) return -1;
return 0;
}
#endif
return -1;
}