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