Utils.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <time.h>
  18. #include <sys/stat.h>
  19. #include "Constants.hpp"
  20. #ifdef __UNIX_LIKE__
  21. #include <unistd.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/uio.h>
  27. #include <dirent.h>
  28. #endif
  29. #ifdef __WINDOWS__
  30. #include <wincrypt.h>
  31. #endif
  32. #include "Utils.hpp"
  33. #include "Mutex.hpp"
  34. #include "Salsa20.hpp"
  35. namespace ZeroTier {
  36. const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
  37. // Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
  38. static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
  39. {
  40. volatile uint8_t *const end = ptr + len;
  41. while (ptr != end) *(ptr++) = (uint8_t)0;
  42. }
  43. static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
  44. void Utils::burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
  45. static unsigned long _Utils_itoa(unsigned long n,char *s)
  46. {
  47. if (n == 0)
  48. return 0;
  49. unsigned long pos = _Utils_itoa(n / 10,s);
  50. if (pos >= 22) // sanity check, should be impossible
  51. pos = 22;
  52. s[pos] = '0' + (char)(n % 10);
  53. return pos + 1;
  54. }
  55. char *Utils::decimal(unsigned long n,char s[24])
  56. {
  57. if (n == 0) {
  58. s[0] = '0';
  59. s[1] = (char)0;
  60. return s;
  61. }
  62. s[_Utils_itoa(n,s)] = (char)0;
  63. return s;
  64. }
  65. void Utils::getSecureRandom(void *buf,unsigned int bytes)
  66. {
  67. static Mutex globalLock;
  68. static Salsa20 s20;
  69. static bool s20Initialized = false;
  70. static uint8_t randomBuf[65536];
  71. static unsigned int randomPtr = sizeof(randomBuf);
  72. Mutex::Lock _l(globalLock);
  73. /* Just for posterity we Salsa20 encrypt the result of whatever system
  74. * CSPRNG we use. There have been several bugs at the OS or OS distribution
  75. * level in the past that resulted in systematically weak or predictable
  76. * keys due to random seeding problems. This mitigates that by grabbing
  77. * a bit of extra entropy and further randomizing the result, and comes
  78. * at almost no cost and with no real downside if the random source is
  79. * good. */
  80. if (!s20Initialized) {
  81. s20Initialized = true;
  82. uint64_t s20Key[4];
  83. s20Key[0] = (uint64_t)time(0); // system clock
  84. s20Key[1] = (uint64_t)buf; // address of buf
  85. s20Key[2] = (uint64_t)s20Key; // address of s20Key[]
  86. s20Key[3] = (uint64_t)&s20; // address of s20
  87. s20.init(s20Key,s20Key);
  88. }
  89. #ifdef __WINDOWS__
  90. static HCRYPTPROV cryptProvider = NULL;
  91. for(unsigned int i=0;i<bytes;++i) {
  92. if (randomPtr >= sizeof(randomBuf)) {
  93. if (cryptProvider == NULL) {
  94. if (!CryptAcquireContextA(&cryptProvider,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
  95. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
  96. exit(1);
  97. }
  98. }
  99. if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomBuf),(BYTE *)randomBuf)) {
  100. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
  101. exit(1);
  102. }
  103. randomPtr = 0;
  104. s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
  105. s20.init(randomBuf,randomBuf);
  106. }
  107. ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
  108. }
  109. #else // not __WINDOWS__
  110. static int devURandomFd = -1;
  111. if (devURandomFd < 0) {
  112. devURandomFd = ::open("/dev/urandom",O_RDONLY);
  113. if (devURandomFd < 0) {
  114. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  115. exit(1);
  116. return;
  117. }
  118. }
  119. for(unsigned int i=0;i<bytes;++i) {
  120. if (randomPtr >= sizeof(randomBuf)) {
  121. for(;;) {
  122. if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) {
  123. ::close(devURandomFd);
  124. devURandomFd = ::open("/dev/urandom",O_RDONLY);
  125. if (devURandomFd < 0) {
  126. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  127. exit(1);
  128. return;
  129. }
  130. } else break;
  131. }
  132. randomPtr = 0;
  133. s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
  134. s20.init(randomBuf,randomBuf);
  135. }
  136. ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
  137. }
  138. #endif // __WINDOWS__ or not
  139. }
  140. } // namespace ZeroTier