xref: /freebsd/contrib/expat/lib/random_getrandom.c (revision e3935639d8d8b6556cad18e1c90e419a65f26b40)
1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 2017-2026 Sebastian Pipping <sebastian@pipping.org>
10    Copyright (c) 2017      Chanho Park <chanho61.park@samsung.com>
11    Copyright (c) 2022      Sean McBride <sean@rogue-research.com>
12    Copyright (c) 2026      Matthew Fernandez <matthew.fernandez@gmail.com>
13    Licensed under the MIT license:
14 
15    Permission is  hereby granted,  free of charge,  to any  person obtaining
16    a  copy  of  this  software   and  associated  documentation  files  (the
17    "Software"),  to  deal in  the  Software  without restriction,  including
18    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
19    distribute, sublicense, and/or sell copies of the Software, and to permit
20    persons  to whom  the Software  is  furnished to  do so,  subject to  the
21    following conditions:
22 
23    The above copyright  notice and this permission notice  shall be included
24    in all copies or substantial portions of the Software.
25 
26    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
27    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
28    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
29    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
30    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
31    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
32    USE OR OTHER DEALINGS IN THE SOFTWARE.
33 */
34 
35 #include "random_getrandom.h"
36 
37 #include "expat_config.h" // for HAVE_GETRANDOM, HAVE_SYSCALL_GETRANDOM
38 
39 #if defined(HAVE_GETRANDOM)
40 #  include <sys/random.h> /* getrandom */
41 #endif
42 
43 #if defined(HAVE_SYSCALL_GETRANDOM)
44 #  if ! defined(_GNU_SOURCE)
45 #    define _GNU_SOURCE 1 /* syscall prototype */
46 #  endif
47 #  include <unistd.h>      /* syscall */
48 #  include <sys/syscall.h> /* SYS_getrandom */
49 #endif                     // defined(HAVE_SYSCALL_GETRANDOM)
50 
51 #if ! defined(GRND_NONBLOCK)
52 #  define GRND_NONBLOCK 0x0001
53 #endif /* defined(GRND_NONBLOCK) */
54 
55 #include <assert.h>
56 #include <errno.h>
57 #include <limits.h> // for INT_MAX
58 
59 /* Obtain entropy on Linux 3.17+ */
60 bool
writeRandomBytes_getrandom_nonblock(void * target,size_t count)61 writeRandomBytes_getrandom_nonblock(void *target, size_t count) {
62   int success = false; /* full count bytes written? */
63   size_t bytesWrittenTotal = 0;
64   const unsigned int getrandomFlags = GRND_NONBLOCK;
65 
66   do {
67     void *const currentTarget = (void *)((char *)target + bytesWrittenTotal);
68     const size_t bytesToWrite = count - bytesWrittenTotal;
69 
70     assert(bytesToWrite <= INT_MAX);
71 
72     errno = 0;
73 
74     const int bytesWrittenMore =
75 #if defined(HAVE_GETRANDOM)
76         (int)getrandom(currentTarget, bytesToWrite, getrandomFlags);
77 #else
78         (int)syscall(SYS_getrandom, currentTarget, bytesToWrite,
79                      getrandomFlags);
80 #endif
81 
82     if (bytesWrittenMore > 0) {
83       bytesWrittenTotal += bytesWrittenMore;
84       if (bytesWrittenTotal >= count)
85         success = true;
86     }
87   } while (! success && (errno == EINTR));
88 
89   return success;
90 }
91