Imported Upstream version 7.53.1
[platform/upstream/curl.git] / lib / rand.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifdef HAVE_FCNTL_H
26 #include <fcntl.h>
27 #endif
28
29 #include <curl/curl.h>
30 #include "vtls/vtls.h"
31 #include "sendf.h"
32 #include "rand.h"
33
34 /* The last 3 #include files should be in this order */
35 #include "curl_printf.h"
36 #include "curl_memory.h"
37 #include "memdebug.h"
38
39 static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
40 {
41   unsigned int r;
42   CURLcode result = CURLE_OK;
43   static unsigned int randseed;
44   static bool seeded = FALSE;
45
46 #ifdef CURLDEBUG
47   char *force_entropy = getenv("CURL_ENTROPY");
48   if(force_entropy) {
49     if(!seeded) {
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);
54       seeded = TRUE;
55     }
56     else
57       randseed++;
58     *rnd = randseed;
59     return CURLE_OK;
60   }
61 #endif
62
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 */
68     return result;
69
70   /* ---- non-cryptographic version following ---- */
71
72 #ifdef RANDOM_FILE
73   if(!seeded) {
74     /* if there's a random file to read a seed from, use it */
75     int fd = open(RANDOM_FILE, O_RDONLY);
76     if(fd > -1) {
77       /* read random data into the randseed variable */
78       ssize_t nread = read(fd, &randseed, sizeof(randseed));
79       if(nread == sizeof(randseed))
80         seeded = TRUE;
81       close(fd);
82     }
83   }
84 #endif
85
86   if(!seeded) {
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;
93     seeded = TRUE;
94   }
95
96   /* Return an unsigned 32-bit pseudo-random number. */
97   r = randseed = randseed * 1103515245 + 12345;
98   *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
99   return CURLE_OK;
100 }
101
102 /*
103  * Curl_rand() stores 'num' number of random unsigned integers in the buffer
104  * 'rndptr' points to.
105  *
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
108  * "weak" random.
109  *
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.
112  *
113  * NOTE: 'data' may be passed in as NULL when coming from external API without
114  * easy handle!
115  *
116  */
117
118 CURLcode Curl_rand(struct Curl_easy *data, unsigned int *rndptr,
119                    unsigned int num)
120 {
121   CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
122   unsigned int i;
123
124   assert(num > 0);
125
126   for(i = 0; i < num; i++) {
127     result = randit(data, rndptr++);
128     if(result)
129       return result;
130   }
131   return result;
132 }