1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
29 #include <curl/curl.h>
30 #include "vtls/vtls.h"
34 /* The last 3 #include files should be in this order */
35 #include "curl_printf.h"
36 #include "curl_memory.h"
39 static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
42 CURLcode result = CURLE_OK;
43 static unsigned int randseed;
44 static bool seeded = FALSE;
47 char *force_entropy = getenv("CURL_ENTROPY");
50 unsigned int seed = 0;
51 size_t elen = strlen(force_entropy);
52 size_t clen = sizeof(seed);
53 size_t min = elen < clen ? elen : clen;
54 memcpy((char *)&seed, force_entropy, min);
55 randseed = ntohl(seed);
65 /* data may be NULL! */
66 result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
67 if(result != CURLE_NOT_BUILT_IN)
68 /* only if there is no random function in the TLS backend do the non crypto
69 version, otherwise return result */
72 /* ---- non-cryptographic version following ---- */
76 /* if there's a random file to read a seed from, use it */
77 int fd = open(RANDOM_FILE, O_RDONLY);
79 /* read random data into the randseed variable */
80 ssize_t nread = read(fd, &randseed, sizeof(randseed));
81 if(nread == sizeof(randseed))
89 struct curltime now = Curl_now();
90 infof(data, "WARNING: using weak random seed");
91 randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
92 randseed = randseed * 1103515245 + 12345;
93 randseed = randseed * 1103515245 + 12345;
94 randseed = randseed * 1103515245 + 12345;
98 /* Return an unsigned 32-bit pseudo-random number. */
99 r = randseed = randseed * 1103515245 + 12345;
100 *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
105 * Curl_rand() stores 'num' number of random unsigned integers in the buffer
106 * 'rndptr' points to.
108 * If libcurl is built without TLS support or with a TLS backend that lacks a
109 * proper random API (Gskit or mbedTLS), this function will use "weak" random.
111 * When built *with* TLS support and a backend that offers strong random, it
112 * will return error if it cannot provide strong random values.
114 * NOTE: 'data' may be passed in as NULL when coming from external API without
119 CURLcode Curl_rand(struct Curl_easy *data, unsigned char *rnd, size_t num)
121 CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
123 DEBUGASSERT(num > 0);
127 size_t left = num < sizeof(unsigned int) ? num : sizeof(unsigned int);
129 result = randit(data, &r);
134 *rnd++ = (unsigned char)(r & 0xFF);
145 * Curl_rand_hex() fills the 'rnd' buffer with a given 'num' size with random
146 * hexadecimal digits PLUS a zero terminating byte. It must be an odd number
150 CURLcode Curl_rand_hex(struct Curl_easy *data, unsigned char *rnd,
153 CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
154 const char *hex = "0123456789abcdef";
155 unsigned char buffer[128];
156 unsigned char *bufp = buffer;
157 DEBUGASSERT(num > 1);
159 #ifdef __clang_analyzer__
160 /* This silences a scan-build warning about accessing this buffer with
161 uninitialized memory. */
162 memset(buffer, 0, sizeof(buffer));
165 if((num/2 >= sizeof(buffer)) || !(num&1))
166 /* make sure it fits in the local buffer and that it is an odd number! */
167 return CURLE_BAD_FUNCTION_ARGUMENT;
169 num--; /* save one for zero termination */
171 result = Curl_rand(data, buffer, num/2);
176 /* clang-tidy warns on this line without this comment: */
177 /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
178 *rnd++ = hex[(*bufp & 0xF0)>>4];
179 *rnd++ = hex[*bufp & 0x0F];