random_seed.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. uint8_t tmp[120];
  61. struct AVSHA *sha = (void*)tmp;
  62. clock_t last_t = 0;
  63. clock_t last_td = 0;
  64. static uint64_t i = 0;
  65. static uint32_t buffer[512] = { 0 };
  66. unsigned char digest[20];
  67. uint64_t last_i = i;
  68. av_assert0(sizeof(tmp) >= av_sha_size);
  69. if(TEST){
  70. memset(buffer, 0, sizeof(buffer));
  71. last_i = i = 0;
  72. }else{
  73. #ifdef AV_READ_TIME
  74. buffer[13] ^= AV_READ_TIME();
  75. buffer[41] ^= AV_READ_TIME()>>32;
  76. #endif
  77. }
  78. for (;;) {
  79. clock_t t = clock();
  80. if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) >= t) {
  81. last_td = t - last_t;
  82. buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (last_td % 3294638521U);
  83. } else {
  84. last_td = t - last_t;
  85. buffer[++i & 511] += last_td % 3294638521U;
  86. if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
  87. break;
  88. }
  89. last_t = t;
  90. }
  91. if(TEST) {
  92. buffer[0] = buffer[1] = 0;
  93. } else {
  94. #ifdef AV_READ_TIME
  95. buffer[111] += AV_READ_TIME();
  96. #endif
  97. }
  98. av_sha_init(sha, 160);
  99. av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer));
  100. av_sha_final(sha, digest);
  101. return AV_RB32(digest) + AV_RB32(digest + 16);
  102. }
  103. uint32_t av_get_random_seed(void)
  104. {
  105. uint32_t seed;
  106. #if HAVE_CRYPTGENRANDOM
  107. HCRYPTPROV provider;
  108. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
  109. CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  110. BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
  111. CryptReleaseContext(provider, 0);
  112. if (ret)
  113. return seed;
  114. }
  115. #endif
  116. #if HAVE_ARC4RANDOM
  117. return arc4random();
  118. #endif
  119. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  120. return seed;
  121. if (read_random(&seed, "/dev/random") == sizeof(seed))
  122. return seed;
  123. return get_generic_seed();
  124. }