rand.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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. #include "curl_setup.h"
  23. #ifdef HAVE_FCNTL_H
  24. #include <fcntl.h>
  25. #endif
  26. #include <curl/curl.h>
  27. #include "vtls/vtls.h"
  28. #include "sendf.h"
  29. #include "rand.h"
  30. /* The last 3 #include files should be in this order */
  31. #include "curl_printf.h"
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
  35. {
  36. unsigned int r;
  37. CURLcode result = CURLE_OK;
  38. static unsigned int randseed;
  39. static bool seeded = FALSE;
  40. #ifdef CURLDEBUG
  41. char *force_entropy = getenv("CURL_ENTROPY");
  42. if(force_entropy) {
  43. if(!seeded) {
  44. unsigned int seed = 0;
  45. size_t elen = strlen(force_entropy);
  46. size_t clen = sizeof(seed);
  47. size_t min = elen < clen ? elen : clen;
  48. memcpy((char *)&seed, force_entropy, min);
  49. randseed = ntohl(seed);
  50. seeded = TRUE;
  51. }
  52. else
  53. randseed++;
  54. *rnd = randseed;
  55. return CURLE_OK;
  56. }
  57. #endif
  58. /* data may be NULL! */
  59. result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
  60. if(result != CURLE_NOT_BUILT_IN)
  61. /* only if there is no random function in the TLS backend do the non crypto
  62. version, otherwise return result */
  63. return result;
  64. /* ---- non-cryptographic version following ---- */
  65. #ifdef RANDOM_FILE
  66. if(!seeded) {
  67. /* if there's a random file to read a seed from, use it */
  68. int fd = open(RANDOM_FILE, O_RDONLY);
  69. if(fd > -1) {
  70. /* read random data into the randseed variable */
  71. ssize_t nread = read(fd, &randseed, sizeof(randseed));
  72. if(nread == sizeof(randseed))
  73. seeded = TRUE;
  74. close(fd);
  75. }
  76. }
  77. #endif
  78. if(!seeded) {
  79. struct curltime now = Curl_now();
  80. infof(data, "WARNING: Using weak random seed\n");
  81. randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
  82. randseed = randseed * 1103515245 + 12345;
  83. randseed = randseed * 1103515245 + 12345;
  84. randseed = randseed * 1103515245 + 12345;
  85. seeded = TRUE;
  86. }
  87. /* Return an unsigned 32-bit pseudo-random number. */
  88. r = randseed = randseed * 1103515245 + 12345;
  89. *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
  90. return CURLE_OK;
  91. }
  92. /*
  93. * Curl_rand() stores 'num' number of random unsigned integers in the buffer
  94. * 'rndptr' points to.
  95. *
  96. * If libcurl is built without TLS support or with a TLS backend that lacks a
  97. * proper random API (Gskit, PolarSSL or mbedTLS), this function will use
  98. * "weak" random.
  99. *
  100. * When built *with* TLS support and a backend that offers strong random, it
  101. * will return error if it cannot provide strong random values.
  102. *
  103. * NOTE: 'data' may be passed in as NULL when coming from external API without
  104. * easy handle!
  105. *
  106. */
  107. CURLcode Curl_rand(struct Curl_easy *data, unsigned char *rnd, size_t num)
  108. {
  109. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  110. DEBUGASSERT(num > 0);
  111. while(num) {
  112. unsigned int r;
  113. size_t left = num < sizeof(unsigned int) ? num : sizeof(unsigned int);
  114. result = randit(data, &r);
  115. if(result)
  116. return result;
  117. while(left) {
  118. *rnd++ = (unsigned char)(r & 0xFF);
  119. r >>= 8;
  120. --num;
  121. --left;
  122. }
  123. }
  124. return result;
  125. }
  126. /*
  127. * Curl_rand_hex() fills the 'rnd' buffer with a given 'num' size with random
  128. * hexadecimal digits PLUS a zero terminating byte. It must be an odd number
  129. * size.
  130. */
  131. CURLcode Curl_rand_hex(struct Curl_easy *data, unsigned char *rnd,
  132. size_t num)
  133. {
  134. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  135. const char *hex = "0123456789abcdef";
  136. unsigned char buffer[128];
  137. unsigned char *bufp = buffer;
  138. DEBUGASSERT(num > 1);
  139. if((num/2 >= sizeof(buffer)) || !(num&1))
  140. /* make sure it fits in the local buffer and that it is an odd number! */
  141. return CURLE_BAD_FUNCTION_ARGUMENT;
  142. num--; /* save one for zero termination */
  143. result = Curl_rand(data, buffer, num/2);
  144. if(result)
  145. return result;
  146. while(num) {
  147. *rnd++ = hex[(*bufp & 0xF0)>>4];
  148. *rnd++ = hex[*bufp & 0x0F];
  149. bufp++;
  150. num -= 2;
  151. }
  152. *rnd = 0;
  153. return result;
  154. }