You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.6KB

  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. av_sha_init(sha, 160);
  91. av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer));
  92. av_sha_final(sha, digest);
  93. return AV_RB32(digest) + AV_RB32(digest + 16);
  94. }
  95. uint32_t av_get_random_seed(void)
  96. {
  97. uint32_t seed;
  98. #if HAVE_CRYPTGENRANDOM
  99. HCRYPTPROV provider;
  100. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
  101. CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  102. BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
  103. CryptReleaseContext(provider, 0);
  104. if (ret)
  105. return seed;
  106. }
  107. #endif
  108. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  109. return seed;
  110. if (read_random(&seed, "/dev/random") == sizeof(seed))
  111. return seed;
  112. return get_generic_seed();
  113. }
  114. #if TEST
  115. #undef printf
  116. #define N 256
  117. #include <stdio.h>
  118. int main(void)
  119. {
  120. int i, j, retry;
  121. uint32_t seeds[N];
  122. for (retry=0; retry<3; retry++){
  123. for (i=0; i<N; i++){
  124. seeds[i] = av_get_random_seed();
  125. for (j=0; j<i; j++)
  126. if (seeds[j] == seeds[i])
  127. goto retry;
  128. }
  129. printf("seeds OK\n");
  130. return 0;
  131. retry:;
  132. }
  133. printf("FAIL at %d with %X\n", j, seeds[j]);
  134. return 1;
  135. }
  136. #endif