random_seed.c 3.1 KB

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