Backport some DRBG renamings and typo fixes
[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 #ifndef _GNU_SOURCE
11 # define _GNU_SOURCE
12 #endif
13 #include "e_os.h"
14 #include <stdio.h>
15 #include "internal/cryptlib.h"
16 #include <openssl/rand.h>
17 #include "rand_lcl.h"
18 #include "internal/rand_int.h"
19 #include <stdio.h>
20 #include "internal/dso.h"
21 #if defined(__linux)
22 # include <sys/syscall.h>
23 #endif
24 #if defined(__FreeBSD__)
25 # include <sys/types.h>
26 # include <sys/sysctl.h>
27 # include <sys/param.h>
28 #endif
29 #if defined(__OpenBSD__) || defined(__NetBSD__)
30 # include <sys/param.h>
31 #endif
32
33 #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
34 # include <sys/types.h>
35 # include <sys/stat.h>
36 # include <fcntl.h>
37 # include <unistd.h>
38 # include <sys/time.h>
39
40 static uint64_t get_time_stamp(void);
41 static uint64_t get_timer_bits(void);
42
43 /* Macro to convert two thirty two bit values into a sixty four bit one */
44 # define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
45
46 /*
47  * Check for the existence and support of POSIX timers.  The standard
48  * says that the _POSIX_TIMERS macro will have a positive value if they
49  * are available.
50  *
51  * However, we want an additional constraint: that the timer support does
52  * not require an extra library dependency.  Early versions of glibc
53  * require -lrt to be specified on the link line to access the timers,
54  * so this needs to be checked for.
55  *
56  * It is worse because some libraries define __GLIBC__ but don't
57  * support the version testing macro (e.g. uClibc).  This means
58  * an extra check is needed.
59  *
60  * The final condition is:
61  *      "have posix timers and either not glibc or glibc without -lrt"
62  *
63  * The nested #if sequences are required to avoid using a parameterised
64  * macro that might be undefined.
65  */
66 # undef OSSL_POSIX_TIMER_OKAY
67 # if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0
68 #  if defined(__GLIBC__)
69 #   if defined(__GLIBC_PREREQ)
70 #    if __GLIBC_PREREQ(2, 17)
71 #     define OSSL_POSIX_TIMER_OKAY
72 #    endif
73 #   endif
74 #  else
75 #   define OSSL_POSIX_TIMER_OKAY
76 #  endif
77 # endif
78 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */
79
80 #if defined(OPENSSL_RAND_SEED_NONE)
81 /* none means none. this simplifies the following logic */
82 # undef OPENSSL_RAND_SEED_OS
83 # undef OPENSSL_RAND_SEED_GETRANDOM
84 # undef OPENSSL_RAND_SEED_LIBRANDOM
85 # undef OPENSSL_RAND_SEED_DEVRANDOM
86 # undef OPENSSL_RAND_SEED_RDTSC
87 # undef OPENSSL_RAND_SEED_RDCPU
88 # undef OPENSSL_RAND_SEED_EGD
89 #endif
90
91 #if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
92         !defined(OPENSSL_RAND_SEED_NONE)
93 # error "UEFI and VXWorks only support seeding NONE"
94 #endif
95
96 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
97     || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
98     || defined(OPENSSL_SYS_UEFI))
99
100 # if defined(OPENSSL_SYS_VOS)
101
102 #  ifndef OPENSSL_RAND_SEED_OS
103 #   error "Unsupported seeding method configured; must be os"
104 #  endif
105
106 #  if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
107 #   error "Unsupported HP-PA and IA32 at the same time."
108 #  endif
109 #  if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
110 #   error "Must have one of HP-PA or IA32"
111 #  endif
112
113 /*
114  * The following algorithm repeatedly samples the real-time clock (RTC) to
115  * generate a sequence of unpredictable data.  The algorithm relies upon the
116  * uneven execution speed of the code (due to factors such as cache misses,
117  * interrupts, bus activity, and scheduling) and upon the rather large
118  * relative difference between the speed of the clock and the rate at which
119  * it can be read.  If it is ported to an environment where execution speed
120  * is more constant or where the RTC ticks at a much slower rate, or the
121  * clock can be read with fewer instructions, it is likely that the results
122  * would be far more predictable.  This should only be used for legacy
123  * platforms.
124  *
125  * As a precaution, we assume only 2 bits of entropy per byte.
126  */
127 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
128 {
129     short int code;
130     int i, k;
131     size_t bytes_needed;
132     struct timespec ts;
133     unsigned char v;
134 #  ifdef OPENSSL_SYS_VOS_HPPA
135     long duration;
136     extern void s$sleep(long *_duration, short int *_code);
137 #  else
138     long long duration;
139     extern void s$sleep2(long long *_duration, short int *_code);
140 #  endif
141
142     bytes_needed = rand_pool_bytes_needed(pool, 4 /*entropy_factor*/);
143
144     for (i = 0; i < bytes_needed; i++) {
145         /*
146          * burn some cpu; hope for interrupts, cache collisions, bus
147          * interference, etc.
148          */
149         for (k = 0; k < 99; k++)
150             ts.tv_nsec = random();
151
152 #  ifdef OPENSSL_SYS_VOS_HPPA
153         /* sleep for 1/1024 of a second (976 us).  */
154         duration = 1;
155         s$sleep(&duration, &code);
156 #  else
157         /* sleep for 1/65536 of a second (15 us).  */
158         duration = 1;
159         s$sleep2(&duration, &code);
160 #  endif
161
162         /* Get wall clock time, take 8 bits. */
163         clock_gettime(CLOCK_REALTIME, &ts);
164         v = (unsigned char)(ts.tv_nsec & 0xFF);
165         rand_pool_add(pool, arg, &v, sizeof(v) , 2);
166     }
167     return rand_pool_entropy_available(pool);
168 }
169
170 void rand_pool_cleanup(void)
171 {
172 }
173
174 void rand_pool_keep_random_devices_open(int keep)
175 {
176 }
177
178 # else
179
180 #  if defined(OPENSSL_RAND_SEED_EGD) && \
181         (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
182 #   error "Seeding uses EGD but EGD is turned off or no device given"
183 #  endif
184
185 #  if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
186 #   error "Seeding uses urandom but DEVRANDOM is not configured"
187 #  endif
188
189 #  if defined(OPENSSL_RAND_SEED_OS)
190 #   if !defined(DEVRANDOM)
191 #    error "OS seeding requires DEVRANDOM to be configured"
192 #   endif
193 #   define OPENSSL_RAND_SEED_GETRANDOM
194 #   define OPENSSL_RAND_SEED_DEVRANDOM
195 #  endif
196
197 #  if defined(OPENSSL_RAND_SEED_LIBRANDOM)
198 #   error "librandom not (yet) supported"
199 #  endif
200
201 #  if (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
202 /*
203  * sysctl_random(): Use sysctl() to read a random number from the kernel
204  * Returns the number of bytes returned in buf on success, -1 on failure.
205  */
206 static ssize_t sysctl_random(char *buf, size_t buflen)
207 {
208     int mib[2];
209     size_t done = 0;
210     size_t len;
211
212     /*
213      * Note: sign conversion between size_t and ssize_t is safe even
214      * without a range check, see comment in syscall_random()
215      */
216
217     /*
218      * On FreeBSD old implementations returned longs, newer versions support
219      * variable sizes up to 256 byte. The code below would not work properly
220      * when the sysctl returns long and we want to request something not a
221      * multiple of longs, which should never be the case.
222      */
223     if (!ossl_assert(buflen % sizeof(long) == 0)) {
224         errno = EINVAL;
225         return -1;
226     }
227
228     /*
229      * On NetBSD before 4.0 KERN_ARND was an alias for KERN_URND, and only
230      * filled in an int, leaving the rest uninitialized. Since NetBSD 4.0
231      * it returns a variable number of bytes with the current version supporting
232      * up to 256 bytes.
233      * Just return an error on older NetBSD versions.
234      */
235 #if   defined(__NetBSD__) && __NetBSD_Version__ < 400000000
236     errno = ENOSYS;
237     return -1;
238 #endif
239
240     mib[0] = CTL_KERN;
241     mib[1] = KERN_ARND;
242
243     do {
244         len = buflen;
245         if (sysctl(mib, 2, buf, &len, NULL, 0) == -1)
246             return done > 0 ? done : -1;
247         done += len;
248         buf += len;
249         buflen -= len;
250     } while (buflen > 0);
251
252     return done;
253 }
254 #  endif
255
256 #  if defined(OPENSSL_RAND_SEED_GETRANDOM)
257 /*
258  * syscall_random(): Try to get random data using a system call
259  * returns the number of bytes returned in buf, or < 0 on error.
260  */
261 static ssize_t syscall_random(void *buf, size_t buflen)
262 {
263     /*
264      * Note: 'buflen' equals the size of the buffer which is used by the
265      * get_entropy() callback of the RAND_DRBG. It is roughly bounded by
266      *
267      *   2 * RAND_POOL_FACTOR * (RAND_DRBG_STRENGTH / 8) = 2^14
268      *
269      * which is way below the OSSL_SSIZE_MAX limit. Therefore sign conversion
270      * between size_t and ssize_t is safe even without a range check.
271      */
272
273     /*
274      * Do runtime detection to find getentropy().
275      *
276      * Known OSs that should support this:
277      * - Darwin since 16 (OSX 10.12, IOS 10.0).
278      * - Solaris since 11.3
279      * - OpenBSD since 5.6
280      * - Linux since 3.17 with glibc 2.25
281      * - FreeBSD since 12.0 (1200061)
282      */
283 #  if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__) && !defined(__hpux)
284     extern int getentropy(void *buffer, size_t length) __attribute__((weak));
285
286     if (getentropy != NULL)
287         return getentropy(buf, buflen) == 0 ? (ssize_t)buflen : -1;
288 #  else
289     union {
290         void *p;
291         int (*f)(void *buffer, size_t length);
292     } p_getentropy;
293
294     /*
295      * We could cache the result of the lookup, but we normally don't
296      * call this function often.
297      */
298     ERR_set_mark();
299     p_getentropy.p = DSO_global_lookup("getentropy");
300     ERR_pop_to_mark();
301     if (p_getentropy.p != NULL)
302         return p_getentropy.f(buf, buflen) == 0 ? (ssize_t)buflen : -1;
303 #  endif
304
305     /* Linux supports this since version 3.17 */
306 #  if defined(__linux) && defined(SYS_getrandom)
307     return syscall(SYS_getrandom, buf, buflen, 0);
308 #  elif (defined(__FreeBSD__) || defined(__NetBSD__)) && defined(KERN_ARND)
309     return sysctl_random(buf, buflen);
310 #  else
311     errno = ENOSYS;
312     return -1;
313 #  endif
314 }
315 #  endif    /* defined(OPENSSL_RAND_SEED_GETRANDOM) */
316
317 #  if defined(OPENSSL_RAND_SEED_DEVRANDOM)
318 static const char *random_device_paths[] = { DEVRANDOM };
319 static struct random_device {
320     int fd;
321     dev_t dev;
322     ino_t ino;
323     mode_t mode;
324     dev_t rdev;
325 } random_devices[OSSL_NELEM(random_device_paths)];
326 static int keep_random_devices_open = 1;
327
328 /*
329  * Verify that the file descriptor associated with the random source is
330  * still valid. The rationale for doing this is the fact that it is not
331  * uncommon for daemons to close all open file handles when daemonizing.
332  * So the handle might have been closed or even reused for opening
333  * another file.
334  */
335 static int check_random_device(struct random_device * rd)
336 {
337     struct stat st;
338
339     return rd->fd != -1
340            && fstat(rd->fd, &st) != -1
341            && rd->dev == st.st_dev
342            && rd->ino == st.st_ino
343            && ((rd->mode ^ st.st_mode) & ~(S_IRWXU | S_IRWXG | S_IRWXO)) == 0
344            && rd->rdev == st.st_rdev;
345 }
346
347 /*
348  * Open a random device if required and return its file descriptor or -1 on error
349  */
350 static int get_random_device(size_t n)
351 {
352     struct stat st;
353     struct random_device * rd = &random_devices[n];
354
355     /* reuse existing file descriptor if it is (still) valid */
356     if (check_random_device(rd))
357         return rd->fd;
358
359     /* open the random device ... */
360     if ((rd->fd = open(random_device_paths[n], O_RDONLY)) == -1)
361         return rd->fd;
362
363     /* ... and cache its relevant stat(2) data */
364     if (fstat(rd->fd, &st) != -1) {
365         rd->dev = st.st_dev;
366         rd->ino = st.st_ino;
367         rd->mode = st.st_mode;
368         rd->rdev = st.st_rdev;
369     } else {
370         close(rd->fd);
371         rd->fd = -1;
372     }
373
374     return rd->fd;
375 }
376
377 /*
378  * Close a random device making sure it is a random device
379  */
380 static void close_random_device(size_t n)
381 {
382     struct random_device * rd = &random_devices[n];
383
384     if (check_random_device(rd))
385         close(rd->fd);
386     rd->fd = -1;
387 }
388
389 static void open_random_devices(void)
390 {
391     size_t i;
392
393     for (i = 0; i < OSSL_NELEM(random_devices); i++)
394         (void)get_random_device(i);
395 }
396
397 int rand_pool_init(void)
398 {
399     size_t i;
400
401     for (i = 0; i < OSSL_NELEM(random_devices); i++)
402         random_devices[i].fd = -1;
403     open_random_devices();
404     return 1;
405 }
406
407 void rand_pool_cleanup(void)
408 {
409     size_t i;
410
411     for (i = 0; i < OSSL_NELEM(random_devices); i++)
412         close_random_device(i);
413 }
414
415 void rand_pool_keep_random_devices_open(int keep)
416 {
417     if (keep)
418         open_random_devices();
419     else
420         rand_pool_cleanup();
421     keep_random_devices_open = keep;
422 }
423
424 #  else     /* !defined(OPENSSL_RAND_SEED_DEVRANDOM) */
425
426 int rand_pool_init(void)
427 {
428     return 1;
429 }
430
431 void rand_pool_cleanup(void)
432 {
433 }
434
435 void rand_pool_keep_random_devices_open(int keep)
436 {
437 }
438
439 #  endif    /* defined(OPENSSL_RAND_SEED_DEVRANDOM) */
440
441 /*
442  * Try the various seeding methods in turn, exit when successful.
443  *
444  * TODO(DRBG): If more than one entropy source is available, is it
445  * preferable to stop as soon as enough entropy has been collected
446  * (as favored by @rsalz) or should one rather be defensive and add
447  * more entropy than requested and/or from different sources?
448  *
449  * Currently, the user can select multiple entropy sources in the
450  * configure step, yet in practice only the first available source
451  * will be used. A more flexible solution has been requested, but
452  * currently it is not clear how this can be achieved without
453  * overengineering the problem. There are many parameters which
454  * could be taken into account when selecting the order and amount
455  * of input from the different entropy sources (trust, quality,
456  * possibility of blocking).
457  */
458 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
459 {
460 #  if defined(OPENSSL_RAND_SEED_NONE)
461     return rand_pool_entropy_available(pool);
462 #  else
463     size_t bytes_needed;
464     size_t entropy_available = 0;
465     unsigned char *buffer;
466
467 #   if defined(OPENSSL_RAND_SEED_GETRANDOM)
468     {
469         ssize_t bytes;
470         /* Maximum allowed number of consecutive unsuccessful attempts */
471         int attempts = 3;
472
473         bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
474         while (bytes_needed != 0 && attempts-- > 0) {
475             buffer = rand_pool_add_begin(pool, bytes_needed);
476             bytes = syscall_random(buffer, bytes_needed);
477             if (bytes > 0) {
478                 rand_pool_add_end(pool, bytes, 8 * bytes);
479                 bytes_needed -= bytes;
480                 attempts = 3; /* reset counter after successful attempt */
481             } else if (bytes < 0 && errno != EINTR) {
482                 break;
483             }
484         }
485     }
486     entropy_available = rand_pool_entropy_available(pool);
487     if (entropy_available > 0)
488         return entropy_available;
489 #   endif
490
491 #   if defined(OPENSSL_RAND_SEED_LIBRANDOM)
492     {
493         /* Not yet implemented. */
494     }
495 #   endif
496
497 #   if defined(OPENSSL_RAND_SEED_DEVRANDOM)
498     bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
499     {
500         size_t i;
501
502         for (i = 0; bytes_needed > 0 && i < OSSL_NELEM(random_device_paths); i++) {
503             ssize_t bytes = 0;
504             /* Maximum allowed number of consecutive unsuccessful attempts */
505             int attempts = 3;
506             const int fd = get_random_device(i);
507
508             if (fd == -1)
509                 continue;
510
511             while (bytes_needed != 0 && attempts-- > 0) {
512                 buffer = rand_pool_add_begin(pool, bytes_needed);
513                 bytes = read(fd, buffer, bytes_needed);
514
515                 if (bytes > 0) {
516                     rand_pool_add_end(pool, bytes, 8 * bytes);
517                     bytes_needed -= bytes;
518                     attempts = 3; /* reset counter after successful attempt */
519                 } else if (bytes < 0 && errno != EINTR) {
520                     break;
521                 }
522             }
523             if (bytes < 0 || !keep_random_devices_open)
524                 close_random_device(i);
525
526             bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
527         }
528         entropy_available = rand_pool_entropy_available(pool);
529         if (entropy_available > 0)
530             return entropy_available;
531     }
532 #   endif
533
534 #   if defined(OPENSSL_RAND_SEED_RDTSC)
535     entropy_available = rand_acquire_entropy_from_tsc(pool);
536     if (entropy_available > 0)
537         return entropy_available;
538 #   endif
539
540 #   if defined(OPENSSL_RAND_SEED_RDCPU)
541     entropy_available = rand_acquire_entropy_from_cpu(pool);
542     if (entropy_available > 0)
543         return entropy_available;
544 #   endif
545
546 #   if defined(OPENSSL_RAND_SEED_EGD)
547     bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
548     if (bytes_needed > 0) {
549         static const char *paths[] = { DEVRANDOM_EGD, NULL };
550         int i;
551
552         for (i = 0; paths[i] != NULL; i++) {
553             buffer = rand_pool_add_begin(pool, bytes_needed);
554             if (buffer != NULL) {
555                 size_t bytes = 0;
556                 int num = RAND_query_egd_bytes(paths[i],
557                                                buffer, (int)bytes_needed);
558                 if (num == (int)bytes_needed)
559                     bytes = bytes_needed;
560
561                 rand_pool_add_end(pool, bytes, 8 * bytes);
562                 entropy_available = rand_pool_entropy_available(pool);
563             }
564             if (entropy_available > 0)
565                 return entropy_available;
566         }
567     }
568 #   endif
569
570     return rand_pool_entropy_available(pool);
571 #  endif
572 }
573 # endif
574 #endif
575
576 #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
577 int rand_pool_add_nonce_data(RAND_POOL *pool)
578 {
579     struct {
580         pid_t pid;
581         CRYPTO_THREAD_ID tid;
582         uint64_t time;
583     } data = { 0 };
584
585     /*
586      * Add process id, thread id, and a high resolution timestamp to
587      * ensure that the nonce is unique with high probability for
588      * different process instances.
589      */
590     data.pid = getpid();
591     data.tid = CRYPTO_THREAD_get_current_id();
592     data.time = get_time_stamp();
593
594     return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
595 }
596
597 int rand_pool_add_additional_data(RAND_POOL *pool)
598 {
599     struct {
600         CRYPTO_THREAD_ID tid;
601         uint64_t time;
602     } data = { 0 };
603
604     /*
605      * Add some noise from the thread id and a high resolution timer.
606      * The thread id adds a little randomness if the drbg is accessed
607      * concurrently (which is the case for the <master> drbg).
608      */
609     data.tid = CRYPTO_THREAD_get_current_id();
610     data.time = get_timer_bits();
611
612     return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
613 }
614
615
616 /*
617  * Get the current time with the highest possible resolution
618  *
619  * The time stamp is added to the nonce, so it is optimized for not repeating.
620  * The current time is ideal for this purpose, provided the computer's clock
621  * is synchronized.
622  */
623 static uint64_t get_time_stamp(void)
624 {
625 # if defined(OSSL_POSIX_TIMER_OKAY)
626     {
627         struct timespec ts;
628
629         if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
630             return TWO32TO64(ts.tv_sec, ts.tv_nsec);
631     }
632 # endif
633 # if defined(__unix__) \
634      || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
635     {
636         struct timeval tv;
637
638         if (gettimeofday(&tv, NULL) == 0)
639             return TWO32TO64(tv.tv_sec, tv.tv_usec);
640     }
641 # endif
642     return time(NULL);
643 }
644
645 /*
646  * Get an arbitrary timer value of the highest possible resolution
647  *
648  * The timer value is added as random noise to the additional data,
649  * which is not considered a trusted entropy sourec, so any result
650  * is acceptable.
651  */
652 static uint64_t get_timer_bits(void)
653 {
654     uint64_t res = OPENSSL_rdtsc();
655
656     if (res != 0)
657         return res;
658
659 # if defined(__sun) || defined(__hpux)
660     return gethrtime();
661 # elif defined(_AIX)
662     {
663         timebasestruct_t t;
664
665         read_wall_time(&t, TIMEBASE_SZ);
666         return TWO32TO64(t.tb_high, t.tb_low);
667     }
668 # elif defined(OSSL_POSIX_TIMER_OKAY)
669     {
670         struct timespec ts;
671
672 #  ifdef CLOCK_BOOTTIME
673 #   define CLOCK_TYPE CLOCK_BOOTTIME
674 #  elif defined(_POSIX_MONOTONIC_CLOCK)
675 #   define CLOCK_TYPE CLOCK_MONOTONIC
676 #  else
677 #   define CLOCK_TYPE CLOCK_REALTIME
678 #  endif
679
680         if (clock_gettime(CLOCK_TYPE, &ts) == 0)
681             return TWO32TO64(ts.tv_sec, ts.tv_nsec);
682     }
683 # endif
684 # if defined(__unix__) \
685      || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
686     {
687         struct timeval tv;
688
689         if (gettimeofday(&tv, NULL) == 0)
690             return TWO32TO64(tv.tv_sec, tv.tv_usec);
691     }
692 # endif
693     return time(NULL);
694 }
695 #endif /* defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__) */