]> git.sur5r.net Git - openldap/blob - libraries/liblutil/entropy.c
Suck in HEAD changes since 2.1alpha
[openldap] / libraries / liblutil / entropy.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <ac/string.h>
10 #include <ac/time.h>
11 #include <ac/unistd.h>
12
13 #ifdef HAVE_PROCESS_H
14 #include <process.h>
15 #endif
16
17 #include <fcntl.h>
18
19 #include <lutil.h>
20 #include <lutil_md5.h>
21
22 /*
23  * lutil_entropy() provides nbytes of entropy in buf.
24  * Quality offerred is suitable for one-time uses, such as "once" keys.
25  * Values may not be suitable for multi-time uses.
26  *
27  * Note:  Callers are encouraged to provide additional bytes of
28  * of entropy in the buf argument.  This information is used in
29  * fallback mode to improve the quality of bytes returned.
30  *
31  * This routinue should be extended to support additional sources
32  * of entropy.
33  */
34 int lutil_entropy( unsigned char *buf, ber_len_t nbytes )
35 {
36         if( nbytes == 0 ) return 0;
37
38 #ifdef URANDOM_DEVICE
39         /* Linux and *BSD offer a urandom device */
40         {
41                 int rc, fd;
42
43                 fd = open( URANDOM_DEVICE, O_RDONLY );
44
45                 if( fd < 0 ) return -1;
46
47                 rc = read( fd, buf, nbytes );
48                 close(fd);
49
50                 /* should return nbytes */
51                 if( rc != nbytes ) return -1;
52
53                 return 0;
54         }
55 #elif PROV_RSA_FULL
56         {
57                 /* Not used since _WIN32_WINNT not set... */
58                 HCRYPTPROV hProv = 0;
59
60                 /* Get handle to user default provider */
61                 if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
62                    return -1;
63                 }
64
65                 /* Generate random initialization vector */
66                 if(!CryptGenRandom(hProv, (DWORD) nbytes, (BYTE *) buf)) {
67                    return -1;
68                 }
69
70                 /* Release provider handle */
71                 if(hProv != 0) CryptReleaseContext(hProv, 0);
72
73                 return 0;
74         }
75 #else
76         {
77                 /* based upon Phil Karn's "practical randomness" idea
78                  * but implementation 100% OpenLDAP.  So don't blame Phil.
79                  *
80                  * Worse case is that this is a MD5 hash of a counter, if
81                  * MD5 is a strong cryptographic hash, this should be fairly
82                  * resistant to attack
83                  */
84
85                 /*
86                  * the caller may need to provide external synchronization OR
87                  * provide entropy (in buf) to ensure quality results as
88                  * access to this counter may not be atomic.
89                  */
90                 static int counter = 0;
91                 ber_len_t n;
92
93                 struct rdata_s {
94                         int counter;
95
96                         unsigned char *buf;
97                         struct rdata_s *stack;
98
99                         pid_t   pid;
100
101 #ifdef HAVE_GETTIMEOFDAY
102                         struct timeval tv;
103 #else
104                         time_t  time;
105 #endif
106
107                         unsigned long   junk;   /* purposely not initialized */
108                 } rdata;
109
110                 /* make sure rdata differs for each process */
111                 rdata.pid = getpid();
112
113                 /* make sure rdata differs for each program */
114                 rdata.buf = buf;
115                 rdata.stack = &rdata;
116
117                 for( n = 0; n < nbytes; n += 16 ) {
118                         struct lutil_MD5Context ctx;
119                         char digest[16];
120
121                         /* poor resolution */
122 #ifdef HAVE_GETTIMEOFDAY
123                         (void) gettimeofday( &rdata.tv, NULL );
124 #else
125                         (void) time( &rdata.time );
126 #endif
127
128                         /* make sure rdata differs */
129                         rdata.counter = ++counter;
130                         rdata.pid++;
131                         rdata.junk++;
132
133                         lutil_MD5Init( &ctx );
134                         lutil_MD5Update( &ctx, (unsigned char *) &rdata, sizeof( rdata ) );
135
136                         /* allow caller to provided additional entropy */
137                         lutil_MD5Update( &ctx, buf, nbytes );
138
139                         lutil_MD5Final( digest, &ctx );
140
141                         AC_MEMCPY( &buf[n], digest,
142                                 nbytes - n >= 16 ? 16 : nbytes - n );
143                 }
144
145                 return 0;
146         }
147 #endif
148         return -1;
149 }