random_seed.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_BCRYPT
  28. #include <windows.h>
  29. #include <bcrypt.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_BCRYPT
  111. BCRYPT_ALG_HANDLE algo_handle;
  112. NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
  113. MS_PRIMITIVE_PROVIDER, 0);
  114. if (BCRYPT_SUCCESS(ret)) {
  115. NTSTATUS ret = BCryptGenRandom(algo_handle, (UCHAR*)&seed, sizeof(seed), 0);
  116. BCryptCloseAlgorithmProvider(algo_handle, 0);
  117. if (BCRYPT_SUCCESS(ret))
  118. return seed;
  119. }
  120. #endif
  121. #if HAVE_ARC4RANDOM
  122. return arc4random();
  123. #endif
  124. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  125. return seed;
  126. if (read_random(&seed, "/dev/random") == sizeof(seed))
  127. return seed;
  128. return get_generic_seed();
  129. }