Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
00062 result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
00063 if(result != CURLE_NOT_BUILT_IN)
00064
00065
00066 return result;
00067
00068
00069
00070 #ifdef RANDOM_FILE
00071 if(!seeded) {
00072
00073 int fd = open(RANDOM_FILE, O_RDONLY);
00074 if(fd > -1) {
00075
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
00095 r = randseed = randseed * 1103515245 + 12345;
00096 *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
00097 return CURLE_OK;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
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 }