You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.6KB

  1. /*
  2. * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "config.h"
  21. #if HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. #if HAVE_IO_H
  25. #include <io.h>
  26. #endif
  27. #if HAVE_CRYPTGENRANDOM
  28. #include <windows.h>
  29. #include <wincrypt.h>
  30. #endif
  31. #include <fcntl.h>
  32. #include <math.h>
  33. #include <time.h>
  34. #include <string.h>
  35. #include "avassert.h"
  36. #include "internal.h"
  37. #include "intreadwrite.h"
  38. #include "timer.h"
  39. #include "random_seed.h"
  40. #include "sha.h"
  41. #ifndef TEST
  42. #define TEST 0
  43. #endif
  44. static int read_random(uint32_t *dst, const char *file)
  45. {
  46. #if HAVE_UNISTD_H
  47. int fd = avpriv_open(file, O_RDONLY);
  48. int err = -1;
  49. if (fd == -1)
  50. return -1;
  51. err = read(fd, dst, sizeof(*dst));
  52. close(fd);
  53. return err;
  54. #else
  55. return -1;
  56. #endif
  57. }
  58. static uint32_t get_generic_seed(void)
  59. {
  60. uint64_t tmp[120/8];
  61. struct AVSHA *sha = (void*)tmp;
  62. clock_t last_t = 0;
  63. clock_t last_td = 0;
  64. clock_t init_t = 0;
  65. static uint64_t i = 0;
  66. static uint32_t buffer[512] = { 0 };
  67. unsigned char digest[20];
  68. uint64_t last_i = i;
  69. av_assert0(sizeof(tmp) >= av_sha_size);
  70. if(TEST){
  71. memset(buffer, 0, sizeof(buffer));
  72. last_i = i = 0;
  73. }else{
  74. #ifdef AV_READ_TIME
  75. buffer[13] ^= AV_READ_TIME();
  76. buffer[41] ^= AV_READ_TIME()>>32;
  77. #endif
  78. }
  79. for (;;) {
  80. clock_t t = clock();
  81. if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) >= t) {
  82. last_td = t - last_t;
  83. buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (last_td % 3294638521U);
  84. } else {
  85. last_td = t - last_t;
  86. buffer[++i & 511] += last_td % 3294638521U;
  87. if ((t - init_t) >= CLOCKS_PER_SEC>>5)
  88. if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
  89. break;
  90. }
  91. last_t = t;
  92. if (!init_t)
  93. init_t = t;
  94. }
  95. if(TEST) {
  96. buffer[0] = buffer[1] = 0;
  97. } else {
  98. #ifdef AV_READ_TIME
  99. buffer[111] += AV_READ_TIME();
  100. #endif
  101. }
  102. av_sha_init(sha, 160);
  103. av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer));
  104. av_sha_final(sha, digest);
  105. return AV_RB32(digest) + AV_RB32(digest + 16);
  106. }
  107. uint32_t av_get_random_seed(void)
  108. {
  109. uint32_t seed;
  110. #if HAVE_CRYPTGENRANDOM
  111. HCRYPTPROV provider;
  112. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
  113. CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  114. BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
  115. CryptReleaseContext(provider, 0);
  116. if (ret)
  117. return seed;
  118. }
  119. #endif
  120. #if HAVE_ARC4RANDOM
  121. return arc4random();
  122. #endif
  123. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  124. return seed;
  125. if (read_random(&seed, "/dev/random") == sizeof(seed))
  126. return seed;
  127. return get_generic_seed();
  128. }