random_seed.c 3.5 KB

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