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.

126 lines
2.9KB

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