DRBG: make locking api truly private
[openssl.git] / crypto / rand / rand_unix.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "e_os.h"
11 #include <stdio.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/rand.h>
14 #include "rand_lcl.h"
15 #include <stdio.h>
16
17 #if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
18         !defined(OPENSSL_RAND_SEED_NONE)
19 # error "UEFI and VXWorks only support seeding NONE"
20 #endif
21
22 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
23     || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
24     || defined(OPENSSL_SYS_UEFI))
25
26 # if defined(OPENSSL_SYS_VOS)
27
28 #  ifndef OPENSSL_RAND_SEED_OS
29 #   error "Unsupported seeding method configured; must be os"
30 #  endif
31
32 #  if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
33 #   error "Unsupported HP-PA and IA32 at the same time."
34 #  endif
35 #  if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
36 #   error "Must have one of HP-PA or IA32"
37 #  endif
38
39 /*
40  * The following algorithm repeatedly samples the real-time clock (RTC) to
41  * generate a sequence of unpredictable data.  The algorithm relies upon the
42  * uneven execution speed of the code (due to factors such as cache misses,
43  * interrupts, bus activity, and scheduling) and upon the rather large
44  * relative difference between the speed of the clock and the rate at which
45  * it can be read.  If it is ported to an environment where execution speed
46  * is more constant or where the RTC ticks at a much slower rate, or the
47  * clock can be read with fewer instructions, it is likely that the results
48  * would be far more predictable.  This should only be used for legacy
49  * platforms.
50  *
51  * As a precaution, we assume only 2 bits of entropy per byte.
52  */
53 size_t RAND_POOL_acquire_entropy(RAND_POOL *pool)
54 {
55     short int code;
56     gid_t curr_gid;
57     pid_t curr_pid;
58     uid_t curr_uid;
59     int i, k;
60     size_t bytes_needed;
61     struct timespec ts;
62     unsigned char v;
63 #  ifdef OPENSSL_SYS_VOS_HPPA
64     long duration;
65     extern void s$sleep(long *_duration, short int *_code);
66 #  else
67     long long duration;
68     extern void s$sleep2(long long *_duration, short int *_code);
69 #  endif
70
71     /*
72      * Seed with the gid, pid, and uid, to ensure *some* variation between
73      * different processes.
74      */
75     curr_gid = getgid();
76     RAND_POOL_add(pool, &curr_gid, sizeof(curr_gid), 0);
77     curr_pid = getpid();
78     RAND_POOL_add(pool, &curr_pid, sizeof(curr_pid), 0);
79     curr_uid = getuid();
80     RAND_POOL_add(pool, &curr_uid, sizeof(curr_uid), 0);
81
82     bytes_needed = RAND_POOL_bytes_needed(pool, 2 /*entropy_per_byte*/);
83
84     for (i = 0; i < bytes_needed; i++) {
85         /*
86          * burn some cpu; hope for interrupts, cache collisions, bus
87          * interference, etc.
88          */
89         for (k = 0; k < 99; k++)
90             ts.tv_nsec = random();
91
92 #  ifdef OPENSSL_SYS_VOS_HPPA
93         /* sleep for 1/1024 of a second (976 us).  */
94         duration = 1;
95         s$sleep(&duration, &code);
96 #  else
97         /* sleep for 1/65536 of a second (15 us).  */
98         duration = 1;
99         s$sleep2(&duration, &code);
100 #  endif
101
102         /* Get wall clock time, take 8 bits. */
103         clock_gettime(CLOCK_REALTIME, &ts);
104         v = (unsigned char)(ts.tv_nsec & 0xFF);
105         RAND_POOL_add(pool, arg, &v, sizeof(v) , 2);
106     }
107     return RAND_POOL_entropy_available(pool);
108 }
109
110 # else
111
112 #  if defined(OPENSSL_RAND_SEED_EGD) && \
113         (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
114 #   error "Seeding uses EGD but EGD is turned off or no device given"
115 #  endif
116
117 #  if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
118 #   error "Seeding uses urandom but DEVRANDOM is not configured"
119 #  endif
120
121 #  if defined(OPENSSL_RAND_SEED_OS)
122 #   if !defined(DEVRANDOM)
123 #    error "OS seeding requires DEVRANDOM to be configured"
124 #   endif
125 #   define OPENSSL_RAND_SEED_DEVRANDOM
126 #   if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
127 #    if __GLIBC_PREREQ(2, 25)
128 #     define OPENSSL_RAND_SEED_GETRANDOM
129 #    endif
130 #   endif
131 #  endif
132
133 #  ifdef OPENSSL_RAND_SEED_GETRANDOM
134 #   include <sys/random.h>
135 #  endif
136
137 #  if defined(OPENSSL_RAND_SEED_LIBRANDOM)
138 #   error "librandom not (yet) supported"
139 #  endif
140
141 /*
142  * Try the various seeding methods in turn, exit when successful.
143  *
144  * TODO(DRBG): If more than one entropy source is available, is it
145  * preferable to stop as soon as enough entropy has been collected
146  * (as favored by @rsalz) or should one rather be defensive and add
147  * more entropy than requested and/or from different sources?
148  *
149  * Currently, the user can select multiple entropy sources in the
150  * configure step, yet in practice only the first available source
151  * will be used. A more flexible solution has been requested, but
152  * currently it is not clear how this can be achieved without
153  * overengineering the problem. There are many parameters which
154  * could be taken into account when selecting the order and amount
155  * of input from the different entropy sources (trust, quality,
156  * possibility of blocking).
157  */
158 size_t RAND_POOL_acquire_entropy(RAND_POOL *pool)
159 {
160 #  ifdef OPENSSL_RAND_SEED_NONE
161     return RAND_POOL_entropy_available(pool);
162 #  else
163     size_t bytes_needed;
164     size_t entropy_available = 0;
165     unsigned char *buffer;
166
167 #   ifdef OPENSSL_RAND_SEED_GETRANDOM
168     bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
169     buffer = RAND_POOL_add_begin(pool, bytes_needed);
170     if (buffer != NULL) {
171         size_t bytes = 0;
172
173         if (getrandom(buffer, bytes_needed, 0) == (int)bytes_needed)
174             bytes = bytes_needed;
175
176         entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
177     }
178     if (entropy_available > 0)
179         return entropy_available;
180 #   endif
181
182 #   if defined(OPENSSL_RAND_SEED_LIBRANDOM)
183     {
184         /* Not yet implemented. */
185     }
186 #   endif
187
188 #   ifdef OPENSSL_RAND_SEED_DEVRANDOM
189     bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
190     if (bytes_needed > 0) {
191         static const char *paths[] = { DEVRANDOM, NULL };
192         FILE *fp;
193         int i;
194
195         for (i = 0; paths[i] != NULL; i++) {
196             if ((fp = fopen(paths[i], "rb")) == NULL)
197                 continue;
198             setbuf(fp, NULL);
199             buffer = RAND_POOL_add_begin(pool, bytes_needed);
200             if (buffer != NULL) {
201                 size_t bytes = 0;
202                 if (fread(buffer, 1, bytes_needed, fp) == bytes_needed)
203                     bytes = bytes_needed;
204
205                 entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
206             }
207             fclose(fp);
208             if (entropy_available > 0)
209                 return entropy_available;
210
211             bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
212         }
213     }
214 #   endif
215
216 #   ifdef OPENSSL_RAND_SEED_RDTSC
217     entropy_available = rand_acquire_entropy_from_tsc(pool);
218     if (entropy_available > 0)
219         return entropy_available;
220 #   endif
221
222 #   ifdef OPENSSL_RAND_SEED_RDCPU
223     entropy_available = rand_acquire_entropy_from_cpu(pool);
224     if (entropy_available > 0)
225         return entropy_available;
226 #   endif
227
228 #   ifdef OPENSSL_RAND_SEED_EGD
229     bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
230     if (bytes_needed > 0) {
231         static const char *paths[] = { DEVRANDOM_EGD, NULL };
232         int i;
233
234         for (i = 0; paths[i] != NULL; i++) {
235             buffer = RAND_POOL_add_begin(pool, bytes_needed);
236             if (buffer != NULL) {
237                 size_t bytes = 0;
238                 int num = RAND_query_egd_bytes(paths[i],
239                                                buffer, (int)bytes_needed);
240                 if (num == (int)bytes_needed)
241                     bytes = bytes_needed;
242
243                 entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
244             }
245             if (entropy_available > 0)
246                 return entropy_available;
247         }
248     }
249 #   endif
250
251     return RAND_POOL_entropy_available(pool);
252 #  endif
253 }
254 # endif
255
256 #endif