rand.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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. size_t elen = strlen(force_entropy);
  45. size_t clen = sizeof(randseed);
  46. size_t min = elen < clen ? elen : clen;
  47. memcpy((char *)&randseed, force_entropy, min);
  48. seeded = TRUE;
  49. }
  50. else
  51. randseed++;
  52. *rnd = randseed;
  53. return CURLE_OK;
  54. }
  55. #endif
  56. /* data may be NULL! */
  57. result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
  58. if(result != CURLE_NOT_BUILT_IN)
  59. /* only if there is no random funtion in the TLS backend do the non crypto
  60. version, otherwise return result */
  61. return result;
  62. /* ---- non-cryptographic version following ---- */
  63. #ifdef RANDOM_FILE
  64. if(!seeded) {
  65. /* if there's a random file to read a seed from, use it */
  66. int fd = open(RANDOM_FILE, O_RDONLY);
  67. if(fd > -1) {
  68. /* read random data into the randseed variable */
  69. ssize_t nread = read(fd, &randseed, sizeof(randseed));
  70. if(nread == sizeof(randseed))
  71. seeded = TRUE;
  72. close(fd);
  73. }
  74. }
  75. #endif
  76. if(!seeded) {
  77. struct timeval now = curlx_tvnow();
  78. infof(data, "WARNING: Using weak random seed\n");
  79. randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
  80. randseed = randseed * 1103515245 + 12345;
  81. randseed = randseed * 1103515245 + 12345;
  82. randseed = randseed * 1103515245 + 12345;
  83. seeded = TRUE;
  84. }
  85. /* Return an unsigned 32-bit pseudo-random number. */
  86. r = randseed = randseed * 1103515245 + 12345;
  87. *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
  88. return CURLE_OK;
  89. }
  90. /*
  91. * Curl_rand() stores 'num' number of random unsigned integers in the buffer
  92. * 'rndptr' points to.
  93. *
  94. * If libcurl is built without TLS support or with a TLS backend that lacks a
  95. * proper random API (Gskit, PolarSSL or mbedTLS), this function will use
  96. * "weak" random.
  97. *
  98. * When built *with* TLS support and a backend that offers strong random, it
  99. * will return error if it cannot provide strong random values.
  100. *
  101. * NOTE: 'data' may be passed in as NULL when coming from external API without
  102. * easy handle!
  103. *
  104. */
  105. CURLcode Curl_rand(struct Curl_easy *data, unsigned int *rndptr,
  106. unsigned int num)
  107. {
  108. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  109. unsigned int i;
  110. assert(num > 0);
  111. for(i = 0; i < num; i++) {
  112. result = randit(data, rndptr++);
  113. if(result)
  114. return result;
  115. }
  116. return result;
  117. }