1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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.haxx.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 size_t elen = strlen(force_entropy);
51 size_t clen = sizeof(randseed);
52 size_t min = elen < clen ? elen : clen;
53 memcpy((char *)&randseed, force_entropy, min);
63 /* data may be NULL! */
64 result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
65 if(result != CURLE_NOT_BUILT_IN)
66 /* only if there is no random funtion in the TLS backend do the non crypto
67 version, otherwise return result */
70 /* ---- non-cryptographic version following ---- */
74 /* if there's a random file to read a seed from, use it */
75 int fd = open(RANDOM_FILE, O_RDONLY);
77 /* read random data into the randseed variable */
78 ssize_t nread = read(fd, &randseed, sizeof(randseed));
79 if(nread == sizeof(randseed))
87 struct timeval now = curlx_tvnow();
88 infof(data, "WARNING: Using weak random seed\n");
89 randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
90 randseed = randseed * 1103515245 + 12345;
91 randseed = randseed * 1103515245 + 12345;
92 randseed = randseed * 1103515245 + 12345;
96 /* Return an unsigned 32-bit pseudo-random number. */
97 r = randseed = randseed * 1103515245 + 12345;
98 *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
103 * Curl_rand() stores 'num' number of random unsigned integers in the buffer
104 * 'rndptr' points to.
106 * If libcurl is built without TLS support or with a TLS backend that lacks a
107 * proper random API (Gskit, PolarSSL or mbedTLS), this function will use
110 * When built *with* TLS support and a backend that offers strong random, it
111 * will return error if it cannot provide strong random values.
113 * NOTE: 'data' may be passed in as NULL when coming from external API without
118 CURLcode Curl_rand(struct Curl_easy *data, unsigned int *rndptr,
121 CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
126 for(i = 0; i < num; i++) {
127 result = randit(data, rndptr++);