random_seed.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_CRYPTGENRANDOM
  25. #include <windows.h>
  26. #include <wincrypt.h>
  27. #endif
  28. #include <fcntl.h>
  29. #include <math.h>
  30. #include <time.h>
  31. #include <string.h>
  32. #include "avassert.h"
  33. #include "internal.h"
  34. #include "timer.h"
  35. #include "random_seed.h"
  36. #include "sha.h"
  37. #include "intreadwrite.h"
  38. #ifndef TEST
  39. #define TEST 0
  40. #endif
  41. static int read_random(uint32_t *dst, const char *file)
  42. {
  43. #if HAVE_UNISTD_H
  44. int fd = avpriv_open(file, O_RDONLY);
  45. int err = -1;
  46. if (fd == -1)
  47. return -1;
  48. err = read(fd, dst, sizeof(*dst));
  49. close(fd);
  50. return err;
  51. #else
  52. return -1;
  53. #endif
  54. }
  55. static uint32_t get_generic_seed(void)
  56. {
  57. uint8_t tmp[120];
  58. struct AVSHA *sha = (void*)tmp;
  59. clock_t last_t = 0;
  60. static uint64_t i = 0;
  61. static uint32_t buffer[512] = {0};
  62. unsigned char digest[20];
  63. uint64_t last_i = i;
  64. av_assert0(sizeof(tmp) >= av_sha_size);
  65. if(TEST){
  66. memset(buffer, 0, sizeof(buffer));
  67. last_i = i = 0;
  68. }else{
  69. #ifdef AV_READ_TIME
  70. buffer[13] ^= AV_READ_TIME();
  71. buffer[41] ^= AV_READ_TIME()>>32;
  72. #endif
  73. }
  74. for (;;) {
  75. clock_t t = clock();
  76. if(last_t == t){
  77. buffer[i&511]++;
  78. }else{
  79. buffer[++i&511]+= (t-last_t) % 3294638521U;
  80. if(last_i && i-last_i > 4 || i-last_i > 64 || TEST && i-last_i > 8)
  81. break;
  82. }
  83. last_t = t;
  84. }
  85. if(TEST)
  86. buffer[0] = buffer[1] = 0;
  87. av_sha_init(sha, 160);
  88. av_sha_update(sha, (uint8_t*)buffer, sizeof(buffer));
  89. av_sha_final(sha, digest);
  90. return AV_RB32(digest) + AV_RB32(digest+16);
  91. }
  92. uint32_t av_get_random_seed(void)
  93. {
  94. uint32_t seed;
  95. #if HAVE_CRYPTGENRANDOM
  96. HCRYPTPROV provider;
  97. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
  98. CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  99. BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
  100. CryptReleaseContext(provider, 0);
  101. if (ret)
  102. return seed;
  103. }
  104. #endif
  105. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  106. return seed;
  107. if (read_random(&seed, "/dev/random") == sizeof(seed))
  108. return seed;
  109. return get_generic_seed();
  110. }
  111. #if TEST
  112. #undef printf
  113. #define N 256
  114. #include <stdio.h>
  115. int main(void)
  116. {
  117. int i, j, retry;
  118. uint32_t seeds[N];
  119. for (retry=0; retry<3; retry++){
  120. for (i=0; i<N; i++){
  121. seeds[i] = av_get_random_seed();
  122. for (j=0; j<i; j++)
  123. if (seeds[j] == seeds[i])
  124. goto retry;
  125. }
  126. printf("seeds OK\n");
  127. return 0;
  128. retry:;
  129. }
  130. printf("FAIL at %d with %X\n", j, seeds[j]);
  131. return 1;
  132. }
  133. #endif