Debian Codegraph @

src/crypto/random.c

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <assert.h>
32 #include <stddef.h>
33 #include <string.h>
34
35 #include "hash-ops.h"
36 #include "initializer.h"
37 #include "random.h"
38
39 static void generate_system_random_bytes(size_t n, void *result);
40
41 #if defined(_WIN32)
42
43 #include <windows.h>
44 #include <wincrypt.h>
45 #include <stdio.h>
46
47 static void generate_system_random_bytes(size_t n, void *result) {
48 HCRYPTPROV prov;
49 #ifdef NDEBUG
50 #define must_succeed(x) do if (!(x)) { fprintf(stderr, "Failed: " #x); _exit(1); } while (0)
51 #else
52 #define must_succeed(x) do if (!(x)) abort(); while (0)
53 #endif
54 must_succeed(CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT));
55 must_succeed(CryptGenRandom(prov, (DWORD)n, result));
56 must_succeed(CryptReleaseContext(prov, 0));
57 #undef must_succeed
58 }
59
60 #else
61
62 #include <err.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <stdlib.h>
66 #include <sys/stat.h>
67 #include <sys/types.h>
68 #include <unistd.h>
69
70 static void generate_system_random_bytes(size_t n, void *result) {
71 int fd;
72 if ((fd = open("/dev/urandom", O_RDONLY | O_NOCTTY | O_CLOEXEC)) < 0) {
73 err(EXIT_FAILURE, "open /dev/urandom");
74 }
75 for (;;) {
76 ssize_t res = read(fd, result, n);
77 if ((size_t) res == n) {
78 break;
79 }
80 if (res < 0) {
81 if (errno != EINTR) {
82 err(EXIT_FAILURE, "read /dev/urandom");
83 }
84 } else if (res == 0) {
85 errx(EXIT_FAILURE, "read /dev/urandom: end of file");
86 } else {
87 result = padd(result, (size_t) res);
88 n -= (size_t) res;
89 }
90 }
91 if (close(fd) < 0) {
92 err(EXIT_FAILURE, "close /dev/urandom");
93 }
94 }
95
96 #endif
97
98 static union hash_state state;
99
100 #if !defined(NDEBUG)
101 static volatile int curstate; /* To catch thread safety problems. */
102 #endif
103
104 FINALIZER(deinit_random) {
105 #if !defined(NDEBUG)
106 assert(curstate == 1);
107 curstate = 0;
108 #endif
109 memset(&state, 0, sizeof(union hash_state));
110 }
111
112 INITIALIZER(init_random) {
113 generate_system_random_bytes(32, &state);
114 REGISTER_FINALIZER(deinit_random);
115 #if !defined(NDEBUG)
116 assert(curstate == 0);
117 curstate = 1;
118 #endif
119 }
120
121 void generate_random_bytes_not_thread_safe(size_t n, void *result) {
122 #if !defined(NDEBUG)
123 assert(curstate == 1);
124 curstate = 2;
125 #endif
126 if (n == 0) {
127 #if !defined(NDEBUG)
128 assert(curstate == 2);
129 curstate = 1;
130 #endif
131 return;
132 }
133 for (;;) {
134 hash_permutation(&state);
135 if (n <= HASH_DATA_AREA) {
136 memcpy(result, &state, n);
137 #if !defined(NDEBUG)
138 assert(curstate == 2);
139 curstate = 1;
140 #endif
141 return;
142 } else {
143 memcpy(result, &state, HASH_DATA_AREA);
144 result = padd(result, HASH_DATA_AREA);
145 n -= HASH_DATA_AREA;
146 }
147 }
148 }
149
150 void add_extra_entropy_not_thread_safe(const void *ptr, size_t bytes)
151 {
152 size_t i;
153
154 while (bytes > 0)
155 {
156 hash_permutation(&state);
157 const size_t round_bytes = bytes > HASH_DATA_AREA ? HASH_DATA_AREA : bytes;
158 for (i = 0; i < round_bytes; ++i)
159 state.b[i] ^= ((const uint8_t*)ptr)[i];
160 bytes -= round_bytes;
161 ptr = cpadd(ptr, round_bytes);
162 }
163 }