/src/httrack/src/htsrandom.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 2026 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* System CSPRNG. See htsrandom.h. */ |
30 | | /* ------------------------------------------------------------ */ |
31 | | |
32 | | #ifdef HAVE_CONFIG_H |
33 | | #include "config.h" |
34 | | #endif |
35 | | |
36 | | #include "htsrandom.h" |
37 | | |
38 | | #ifdef _WIN32 |
39 | | #include "htswin32.h" |
40 | | #else |
41 | | #include <errno.h> |
42 | | #include <stdio.h> |
43 | | /* One macro gates both, because only <sys/random.h> declares getrandom(). */ |
44 | | #if defined(HAVE_SYS_RANDOM_H) && defined(HAVE_GETRANDOM) |
45 | | #define HTS_USE_GETRANDOM |
46 | | #include <sys/random.h> |
47 | | #endif |
48 | | #endif |
49 | | |
50 | | #ifdef _WIN32 |
51 | | /* RtlGenRandom, resolved at runtime so no import library is needed. */ |
52 | | typedef BOOLEAN(WINAPI *hts_rtlgenrandom_t)(PVOID buffer, ULONG length); |
53 | | #endif |
54 | | |
55 | 0 | hts_boolean hts_random_bytes(void *buf, size_t len) { |
56 | 0 | if (len == 0) /* nothing to ask any source for, on every platform alike */ |
57 | 0 | return HTS_TRUE; |
58 | | #ifdef _WIN32 |
59 | | hts_boolean ok = HTS_FALSE; |
60 | | HMODULE dll = LoadLibraryA("advapi32.dll"); |
61 | | |
62 | | if (dll != NULL) { |
63 | | hts_rtlgenrandom_t gen = |
64 | | (hts_rtlgenrandom_t) GetProcAddress(dll, "SystemFunction036"); |
65 | | |
66 | | /* the ULONG cast must not truncate; callers ask for a few dozen bytes */ |
67 | | if (gen != NULL && len <= 0x10000 && gen(buf, (ULONG) len)) { |
68 | | ok = HTS_TRUE; |
69 | | } |
70 | | FreeLibrary(dll); |
71 | | } |
72 | | return ok; |
73 | | #else |
74 | 0 | unsigned char *const p = (unsigned char *) buf; |
75 | 0 | size_t got = 0; |
76 | 0 | FILE *fp; |
77 | |
|
78 | 0 | #ifdef HTS_USE_GETRANDOM |
79 | | /* Breaks the build if the two guards ever split again: an implicit |
80 | | declaration would return int, not ssize_t. */ |
81 | 0 | enum { |
82 | 0 | getrandom_is_prototyped = |
83 | 0 | 1 / (sizeof(getrandom(p, len, 0)) == sizeof(ssize_t)) |
84 | 0 | }; |
85 | |
|
86 | 0 | while (got < len) { |
87 | 0 | const ssize_t n = getrandom(p + got, len - got, 0); |
88 | |
|
89 | 0 | if (n < 0) { |
90 | 0 | if (errno == EINTR) |
91 | 0 | continue; |
92 | 0 | break; /* pre-3.17 kernel or a seccomp filter: try /dev/urandom */ |
93 | 0 | } |
94 | 0 | got += (size_t) n; |
95 | 0 | } |
96 | 0 | if (got == len) |
97 | 0 | return HTS_TRUE; |
98 | 0 | #endif |
99 | 0 | fp = fopen("/dev/urandom", "rb"); |
100 | 0 | if (fp == NULL) |
101 | 0 | return HTS_FALSE; |
102 | | /* a short read is a hard failure, not partial credit */ |
103 | 0 | got += fread(p + got, 1, len - got, fp); |
104 | 0 | fclose(fp); |
105 | 0 | return got == len ? HTS_TRUE : HTS_FALSE; |
106 | 0 | #endif |
107 | 0 | } |