rand.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.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 or mbedTLS), this function will use "weak" random.
  98. *
  99. * When built *with* TLS support and a backend that offers strong random, it
  100. * will return error if it cannot provide strong random values.
  101. *
  102. * NOTE: 'data' may be passed in as NULL when coming from external API without
  103. * easy handle!
  104. *
  105. */
  106. CURLcode Curl_rand(struct Curl_easy *data, unsigned char *rnd, size_t num)
  107. {
  108. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  109. DEBUGASSERT(num > 0);
  110. while(num) {
  111. unsigned int r;
  112. size_t left = num < sizeof(unsigned int) ? num : sizeof(unsigned int);
  113. result = randit(data, &r);
  114. if(result)
  115. return result;
  116. while(left) {
  117. *rnd++ = (unsigned char)(r & 0xFF);
  118. r >>= 8;
  119. --num;
  120. --left;
  121. }
  122. }
  123. return result;
  124. }
  125. /*
  126. * Curl_rand_hex() fills the 'rnd' buffer with a given 'num' size with random
  127. * hexadecimal digits PLUS a zero terminating byte. It must be an odd number
  128. * size.
  129. */
  130. CURLcode Curl_rand_hex(struct Curl_easy *data, unsigned char *rnd,
  131. size_t num)
  132. {
  133. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  134. const char *hex = "0123456789abcdef";
  135. unsigned char buffer[128];
  136. unsigned char *bufp = buffer;
  137. DEBUGASSERT(num > 1);
  138. #ifdef __clang_analyzer__
  139. /* This silences a scan-build warning about accessing this buffer with
  140. uninitialized memory. */
  141. memset(buffer, 0, sizeof(buffer));
  142. #endif
  143. if((num/2 >= sizeof(buffer)) || !(num&1))
  144. /* make sure it fits in the local buffer and that it is an odd number! */
  145. return CURLE_BAD_FUNCTION_ARGUMENT;
  146. num--; /* save one for zero termination */
  147. result = Curl_rand(data, buffer, num/2);
  148. if(result)
  149. return result;
  150. while(num) {
  151. /* clang-tidy warns on this line without this comment: */
  152. /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
  153. *rnd++ = hex[(*bufp & 0xF0)>>4];
  154. *rnd++ = hex[*bufp & 0x0F];
  155. bufp++;
  156. num -= 2;
  157. }
  158. *rnd = 0;
  159. return result;
  160. }