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.

107 lines
2.6KB

  1. /*
  2. * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "internal.h"
  32. #include "timer.h"
  33. #include "random_seed.h"
  34. static int read_random(uint32_t *dst, const char *file)
  35. {
  36. #if HAVE_UNISTD_H
  37. int fd = avpriv_open(file, O_RDONLY);
  38. int err = -1;
  39. if (fd == -1)
  40. return -1;
  41. err = read(fd, dst, sizeof(*dst));
  42. close(fd);
  43. return err;
  44. #else
  45. return -1;
  46. #endif
  47. }
  48. static uint32_t get_generic_seed(void)
  49. {
  50. clock_t last_t = 0;
  51. int bits = 0;
  52. uint64_t random = 0;
  53. unsigned i;
  54. float s = 0.000000000001;
  55. for (i = 0; bits < 64; i++) {
  56. clock_t t = clock();
  57. if (last_t && fabs(t - last_t) > s || t == (clock_t) -1) {
  58. if (i < 10000 && s < (1 << 24)) {
  59. s += s;
  60. i = t = 0;
  61. } else {
  62. random = 2 * random + (i & 1);
  63. bits++;
  64. }
  65. }
  66. last_t = t;
  67. }
  68. #ifdef AV_READ_TIME
  69. random ^= AV_READ_TIME();
  70. #else
  71. random ^= clock();
  72. #endif
  73. random += random >> 32;
  74. return random;
  75. }
  76. uint32_t av_get_random_seed(void)
  77. {
  78. uint32_t seed;
  79. #if HAVE_CRYPTGENRANDOM
  80. HCRYPTPROV provider;
  81. if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
  82. CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  83. BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
  84. CryptReleaseContext(provider, 0);
  85. if (ret)
  86. return seed;
  87. }
  88. #endif
  89. if (read_random(&seed, "/dev/urandom") == sizeof(seed))
  90. return seed;
  91. if (read_random(&seed, "/dev/random") == sizeof(seed))
  92. return seed;
  93. return get_generic_seed();
  94. }