SystemRandomSeed.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/seed/RandomSeed.h"
  16. #include "crypto/random/seed/SystemRandomSeed.h"
  17. #include "util/log/Log.h"
  18. Js({ this.RandomSeedProvider_providers = []; })
  19. #define RandomSeedProvider_register(name) Js({ this.RandomSeedProvider_providers.push(#name) })
  20. #define RandomSeedProvider_list() Js({ return this.RandomSeedProvider_providers.join(','); })
  21. #ifdef win32
  22. #include "crypto/random/seed/RtlGenRandomSeed.h"
  23. RandomSeedProvider_register(RtlGenRandomSeed_new)
  24. #else
  25. #include "crypto/random/seed/DevUrandomRandomSeed.h"
  26. RandomSeedProvider_register(DevUrandomRandomSeed_new)
  27. #ifdef linux
  28. #include "crypto/random/seed/ProcSysKernelRandomUuidRandomSeed.h"
  29. RandomSeedProvider_register(ProcSysKernelRandomUuidRandomSeed_new)
  30. #if !defined(__ILP32__) && !defined(__aarch64__) && defined(__GLIBC__)
  31. #include "crypto/random/seed/LinuxRandomUuidSysctlRandomSeed.h"
  32. RandomSeedProvider_register(LinuxRandomUuidSysctlRandomSeed_new)
  33. #endif
  34. #else
  35. #ifdef freebsd
  36. #include "crypto/random/seed/BsdKernArndSysctlRandomSeed.h"
  37. RandomSeedProvider_register(BsdKernArndSysctlRandomSeed_new)
  38. #endif
  39. #endif
  40. #include <sys/syscall.h>
  41. #if defined(__OPENBSD__) || (defined(SYS_getrandom) && \
  42. (SYS_getrandom != __NR_getrandom || defined(__NR_getrandom)))
  43. #include "crypto/random/seed/GetEntropyRandomSeed.h"
  44. RandomSeedProvider_register(GetEntropyRandomSeed_new)
  45. #endif
  46. #endif
  47. static RandomSeed_Provider PROVIDERS[] = { RandomSeedProvider_list() };
  48. #define PROVIDERS_COUNT ((int)(sizeof(PROVIDERS) / sizeof(RandomSeed_Provider)))
  49. RandomSeed_t* SystemRandomSeed_new(RandomSeed_Provider* additionalProviders,
  50. int additionalProviderCount,
  51. struct Log* logger,
  52. struct Allocator* alloc)
  53. {
  54. int providerCount = PROVIDERS_COUNT + additionalProviderCount;
  55. RandomSeed_Provider* allProviders =
  56. Allocator_calloc(alloc, sizeof(RandomSeed_Provider), providerCount+1);
  57. int i = 0;
  58. for (int j = 0; j < additionalProviderCount; j++) {
  59. allProviders[i++] = additionalProviders[j];
  60. }
  61. for (int j = 0; j < PROVIDERS_COUNT; j++) {
  62. allProviders[i++] = PROVIDERS[j];
  63. }
  64. return RandomSeed_new(allProviders, providerCount, logger, alloc);
  65. }