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