rand.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <fcntl.h>
  24. #include <curl/curl.h>
  25. #include "vtls/vtls.h"
  26. #include "sendf.h"
  27. #include "rand.h"
  28. /* The last 3 #include files should be in this order */
  29. #include "curl_printf.h"
  30. #include "curl_memory.h"
  31. #include "memdebug.h"
  32. static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
  33. {
  34. unsigned int r;
  35. CURLcode result = CURLE_OK;
  36. static unsigned int randseed;
  37. static bool seeded = FALSE;
  38. #ifdef CURLDEBUG
  39. char *force_entropy = getenv("CURL_ENTROPY");
  40. if(force_entropy) {
  41. if(!seeded) {
  42. size_t elen = strlen(force_entropy);
  43. size_t clen = sizeof(randseed);
  44. size_t min = elen < clen ? elen : clen;
  45. memcpy((char *)&randseed, force_entropy, min);
  46. seeded = TRUE;
  47. }
  48. else
  49. randseed++;
  50. *rnd = randseed;
  51. return CURLE_OK;
  52. }
  53. #endif
  54. /* data may be NULL! */
  55. result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
  56. if(result != CURLE_NOT_BUILT_IN)
  57. /* only if there is no random funtion in the TLS backend do the non crypto
  58. version, otherwise return result */
  59. return result;
  60. /* ---- non-cryptographic version following ---- */
  61. #ifdef RANDOM_FILE
  62. if(!seeded) {
  63. /* if there's a random file to read a seed from, use it */
  64. int fd = open(RANDOM_FILE, O_RDONLY);
  65. if(fd > -1) {
  66. /* read random data into the randseed variable */
  67. ssize_t nread = read(fd, &randseed, sizeof(randseed));
  68. if(nread == sizeof(randseed))
  69. seeded = TRUE;
  70. close(fd);
  71. }
  72. }
  73. #endif
  74. if(!seeded) {
  75. struct timeval now = curlx_tvnow();
  76. infof(data, "WARNING: Using weak random seed\n");
  77. randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
  78. randseed = randseed * 1103515245 + 12345;
  79. randseed = randseed * 1103515245 + 12345;
  80. randseed = randseed * 1103515245 + 12345;
  81. seeded = TRUE;
  82. }
  83. /* Return an unsigned 32-bit pseudo-random number. */
  84. r = randseed = randseed * 1103515245 + 12345;
  85. *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
  86. return CURLE_OK;
  87. }
  88. /*
  89. * Curl_rand() stores 'num' number of random unsigned integers in the buffer
  90. * 'rndptr' points to.
  91. *
  92. * If libcurl is built without TLS support or with a TLS backend that lacks a
  93. * proper random API (Gskit, PolarSSL or mbedTLS), this function will use
  94. * "weak" random.
  95. *
  96. * When built *with* TLS support and a backend that offers strong random, it
  97. * will return error if it cannot provide strong random values.
  98. *
  99. * NOTE: 'data' may be passed in as NULL when coming from external API without
  100. * easy handle!
  101. *
  102. */
  103. CURLcode Curl_rand(struct Curl_easy *data, unsigned int *rndptr,
  104. unsigned int num)
  105. {
  106. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  107. unsigned int i;
  108. assert(num > 0);
  109. for(i = 0; i < num; i++) {
  110. result = randit(data, rndptr++);
  111. if(result)
  112. return result;
  113. }
  114. return result;
  115. }