3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <lutil_md5.h>
15 * lutil_entropy() provides nbytes of entropy in buf.
16 * Quality offerred is suitable for one-time uses, such as "once" keys.
18 int lutil_entropy( char *buf, int nbytes )
20 if( nbytes < 0 ) return -1;
21 if( nbytes == 0 ) return 0;
25 /* Linux and *BSD offer a urandom device */
29 fd = open( URANDOM_DEVICE, O_RDONLY );
31 if( fd < 0 ) return -1;
33 rc = read( fd, buf, nbytes );
36 /* should return nbytes */
37 if( rc < nbytes ) return -1;
43 /* based upon Phil Karn's "practical randomness" idea
44 * but implementation 100% OpenLDAP. So don't blame Phil. */
45 /* worse case is this is a MD5 hash of a counter, if
46 * MD5 is a strong cryptographic hash, this should
47 * be fairly resisant to attack
49 static int initialized = 0;
55 #ifdef HAVE_GETTIMEOFDAY
69 for( n = 0; n < nbytes; n += 16 ) {
70 struct lutil_MD5Context ctx;
73 #ifdef HAVE_GETTIMEOFDAY
74 (void) gettimeofday( &rdata.tv, sizeof( rdata.tv ) );
76 (void) time( &rdata.time );
82 lutil_MD5Init( &ctx );
83 lutil_MD5Update( &ctx, (char *) &rdata, sizeof( rdata ) );
84 lutil_MD5Final( digest, &ctx );
86 memcpy( &buf[n], digest,
87 nbytes - n > 16 ? 16 : nbytes - n );