rand.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #include <fcntl.h>
00026 
00027 #include <curl/curl.h>
00028 #include "vtls/vtls.h"
00029 #include "sendf.h"
00030 #include "rand.h"
00031 
00032 /* The last 3 #include files should be in this order */
00033 #include "curl_printf.h"
00034 #include "curl_memory.h"
00035 #include "memdebug.h"
00036 
00037 static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
00038 {
00039   unsigned int r;
00040   CURLcode result = CURLE_OK;
00041   static unsigned int randseed;
00042   static bool seeded = FALSE;
00043 
00044 #ifdef CURLDEBUG
00045   char *force_entropy = getenv("CURL_ENTROPY");
00046   if(force_entropy) {
00047     if(!seeded) {
00048       size_t elen = strlen(force_entropy);
00049       size_t clen = sizeof(randseed);
00050       size_t min = elen < clen ? elen : clen;
00051       memcpy((char *)&randseed, force_entropy, min);
00052       seeded = TRUE;
00053     }
00054     else
00055       randseed++;
00056     *rnd = randseed;
00057     return CURLE_OK;
00058   }
00059 #endif
00060 
00061   /* data may be NULL! */
00062   result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
00063   if(result != CURLE_NOT_BUILT_IN)
00064     /* only if there is no random funtion in the TLS backend do the non crypto
00065        version, otherwise return result */
00066     return result;
00067 
00068   /* ---- non-cryptographic version following ---- */
00069 
00070 #ifdef RANDOM_FILE
00071   if(!seeded) {
00072     /* if there's a random file to read a seed from, use it */
00073     int fd = open(RANDOM_FILE, O_RDONLY);
00074     if(fd > -1) {
00075       /* read random data into the randseed variable */
00076       ssize_t nread = read(fd, &randseed, sizeof(randseed));
00077       if(nread == sizeof(randseed))
00078         seeded = TRUE;
00079       close(fd);
00080     }
00081   }
00082 #endif
00083 
00084   if(!seeded) {
00085     struct timeval now = curlx_tvnow();
00086     infof(data, "WARNING: Using weak random seed\n");
00087     randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
00088     randseed = randseed * 1103515245 + 12345;
00089     randseed = randseed * 1103515245 + 12345;
00090     randseed = randseed * 1103515245 + 12345;
00091     seeded = TRUE;
00092   }
00093 
00094   /* Return an unsigned 32-bit pseudo-random number. */
00095   r = randseed = randseed * 1103515245 + 12345;
00096   *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
00097   return CURLE_OK;
00098 }
00099 
00100 /*
00101  * Curl_rand() stores 'num' number of random unsigned integers in the buffer
00102  * 'rndptr' points to.
00103  *
00104  * If libcurl is built without TLS support or with a TLS backend that lacks a
00105  * proper random API (Gskit, PolarSSL or mbedTLS), this function will use
00106  * "weak" random.
00107  *
00108  * When built *with* TLS support and a backend that offers strong random, it
00109  * will return error if it cannot provide strong random values.
00110  *
00111  * NOTE: 'data' may be passed in as NULL when coming from external API without
00112  * easy handle!
00113  *
00114  */
00115 
00116 CURLcode Curl_rand(struct Curl_easy *data, unsigned int *rndptr,
00117                    unsigned int num)
00118 {
00119   CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
00120   unsigned int i;
00121 
00122   assert(num > 0);
00123 
00124   for(i = 0; i < num; i++) {
00125     result = randit(data, rndptr++);
00126     if(result)
00127       return result;
00128   }
00129   return result;
00130 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06