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.

134 lines
3.0KB

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