random_seed.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. static uint64_t i = 0;
  64. static uint32_t buffer[512] = { 0 };
  65. unsigned char digest[20];
  66. uint64_t last_i = i;
  67. av_assert0(sizeof(tmp) >= av_sha_size);
  68. if(TEST){
  69. memset(buffer, 0, sizeof(buffer));
  70. last_i = i = 0;
  71. }else{
  72. #ifdef AV_READ_TIME
  73. buffer[13] ^= AV_READ_TIME();
  74. buffer[41] ^= AV_READ_TIME()>>32;
  75. #endif
  76. }
  77. for (;;) {
  78. clock_t t = clock();
  79. if (last_t == t) {
  80. buffer[i & 511]++;
  81. } else {
  82. buffer[++i & 511] += (t - last_t) % 3294638521U;
  83. if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
  84. break;
  85. }
  86. last_t = t;
  87. }
  88. if(TEST) {
  89. buffer[0] = buffer[1] = 0;
  90. } else {
  91. #ifdef AV_READ_TIME
  92. buffer[111] += AV_READ_TIME();
  93. #endif
  94. }
  95. av_sha_init(sha, 160);
  96. av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer));
  97. av_sha_final(sha, digest);
  98. return AV_RB32(digest) + AV_RB32(digest + 16);
  99. }
  100. uint32_t av_get_random_seed(void)
  101. {
  102. uint32_t seed;
  103. #if HAVE_CRYPTGENRANDOM
  104. HCRYPTPROV provider;
  105. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
  106. CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  107. BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
  108. CryptReleaseContext(provider, 0);
  109. if (ret)
  110. return seed;
  111. }
  112. #endif
  113. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  114. return seed;
  115. if (read_random(&seed, "/dev/random") == sizeof(seed))
  116. return seed;
  117. return get_generic_seed();
  118. }
  119. #if TEST
  120. #undef printf
  121. #define N 256
  122. #include <stdio.h>
  123. int main(void)
  124. {
  125. int i, j, retry;
  126. uint32_t seeds[N];
  127. for (retry=0; retry<3; retry++){
  128. for (i=0; i<N; i++){
  129. seeds[i] = av_get_random_seed();
  130. for (j=0; j<i; j++)
  131. if (seeds[j] == seeds[i])
  132. goto retry;
  133. }
  134. printf("seeds OK\n");
  135. return 0;
  136. retry:;
  137. }
  138. printf("FAIL at %d with %X\n", j, seeds[j]);
  139. return 1;
  140. }
  141. #endif