xref: /freebsd/contrib/expat/lib/random_rand_s.c (revision e3935639d8d8b6556cad18e1c90e419a65f26b40)
1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 2019      David Loffredo <loffredo@steptools.com>
10    Copyright (c) 2019-2026 Sebastian Pipping <sebastian@pipping.org>
11    Copyright (c) 2019      Ben Wagner <bungeman@chromium.org>
12    Copyright (c) 2019      Vadim Zeitlin <vadim@zeitlins.org>
13    Copyright (c) 2026      Matthew Fernandez <matthew.fernandez@gmail.com>
14    Licensed under the MIT license:
15 
16    Permission is  hereby granted,  free of charge,  to any  person obtaining
17    a  copy  of  this  software   and  associated  documentation  files  (the
18    "Software"),  to  deal in  the  Software  without restriction,  including
19    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
20    distribute, sublicense, and/or sell copies of the Software, and to permit
21    persons  to whom  the Software  is  furnished to  do so,  subject to  the
22    following conditions:
23 
24    The above copyright  notice and this permission notice  shall be included
25    in all copies or substantial portions of the Software.
26 
27    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
28    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
29    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
30    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
31    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
32    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
33    USE OR OTHER DEALINGS IN THE SOFTWARE.
34 */
35 
36 #include "random_rand_s.h"
37 
38 /* force stdlib to define rand_s() */
39 #if ! defined(_CRT_RAND_S)
40 #  define _CRT_RAND_S
41 #endif
42 
43 // Workaround MinGW GCC trouble with recognizing `rand_s`, likely related
44 // to return type `error_t`; the symptom was:
45 // > error: implicit declaration of function ‘rand_s’
46 #if defined(__MINGW32__)
47 #  include <errno.h>
48 #endif
49 
50 #include <stdlib.h> // for rand_s
51 #include <string.h> // for memcpy
52 
53 // Help clang-tidy out with prototype of function `rand_s`
54 #if defined(XML_CLANG_TIDY)
55 int rand_s(unsigned int *);
56 #endif
57 
58 /* Provide declaration of rand_s() for MinGW-32 (not 64, which has it),
59    as it didn't declare it in its header prior to version 5.3.0 of its
60    runtime package (mingwrt, containing stdlib.h).  The upstream fix
61    was introduced at https://osdn.net/projects/mingw/ticket/39658 . */
62 #if defined(__MINGW32__) && defined(__MINGW32_VERSION)                         \
63     && __MINGW32_VERSION < 5003000L && ! defined(__MINGW64_VERSION_MAJOR)
64 __declspec(dllimport) int rand_s(unsigned int *);
65 #endif
66 
67 /* Obtain entropy on Windows using the rand_s() function which
68  * generates cryptographically secure random numbers.  Internally it
69  * uses RtlGenRandom API which is present in Windows XP and later.
70  */
71 bool
writeRandomBytes_rand_s(void * target,size_t count)72 writeRandomBytes_rand_s(void *target, size_t count) {
73   size_t bytesWrittenTotal = 0;
74 
75   while (bytesWrittenTotal < count) {
76     unsigned int random32 = 0;
77 
78     if (rand_s(&random32))
79       return false; /* failure */
80 
81     size_t toUse = count - bytesWrittenTotal;
82     if (toUse > sizeof(random32))
83       toUse = sizeof(random32);
84     memcpy((char *)target + bytesWrittenTotal, &random32, toUse);
85     bytesWrittenTotal += toUse;
86   }
87   return true; /* success */
88 }
89