Utils.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stdarg.h>
  30. #include <time.h>
  31. #include <sys/stat.h>
  32. #include "Constants.hpp"
  33. #ifdef __UNIX_LIKE__
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <fcntl.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <sys/uio.h>
  40. #include <dirent.h>
  41. #endif
  42. #ifdef __WINDOWS__
  43. #include <wincrypt.h>
  44. #endif
  45. #include "Utils.hpp"
  46. #include "Mutex.hpp"
  47. #include "Salsa20.hpp"
  48. namespace ZeroTier {
  49. const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
  50. const char Utils::BASE32CHARS[32] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','2','3','4','5','6','7' };
  51. const uint8_t Utils::BASE32BITS[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  52. // Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
  53. static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
  54. {
  55. volatile uint8_t *const end = ptr + len;
  56. while (ptr != end) *(ptr++) = (uint8_t)0;
  57. }
  58. static void (*volatile _Utils_doBurn_ptr)(volatile uint8_t *,unsigned int) = _Utils_doBurn;
  59. void Utils::burn(void *ptr,unsigned int len) { (_Utils_doBurn_ptr)((volatile uint8_t *)ptr,len); }
  60. static unsigned long _Utils_itoa(unsigned long n,char *s)
  61. {
  62. if (n == 0)
  63. return 0;
  64. unsigned long pos = _Utils_itoa(n / 10,s);
  65. if (pos >= 22) // sanity check, should be impossible
  66. pos = 22;
  67. s[pos] = '0' + (char)(n % 10);
  68. return pos + 1;
  69. }
  70. char *Utils::decimal(unsigned long n,char s[24])
  71. {
  72. if (n == 0) {
  73. s[0] = '0';
  74. s[1] = (char)0;
  75. return s;
  76. }
  77. s[_Utils_itoa(n,s)] = (char)0;
  78. return s;
  79. }
  80. void Utils::getSecureRandom(void *buf,unsigned int bytes)
  81. {
  82. static Mutex globalLock;
  83. static Salsa20 s20;
  84. static bool s20Initialized = false;
  85. static uint8_t randomBuf[65536];
  86. static unsigned int randomPtr = sizeof(randomBuf);
  87. Mutex::Lock _l(globalLock);
  88. /* Just for posterity we Salsa20 encrypt the result of whatever system
  89. * CSPRNG we use. There have been several bugs at the OS or OS distribution
  90. * level in the past that resulted in systematically weak or predictable
  91. * keys due to random seeding problems. This mitigates that by grabbing
  92. * a bit of extra entropy and further randomizing the result, and comes
  93. * at almost no cost and with no real downside if the random source is
  94. * good. */
  95. if (!s20Initialized) {
  96. s20Initialized = true;
  97. uint64_t s20Key[4];
  98. s20Key[0] = (uint64_t)time(0); // system clock
  99. s20Key[1] = (uint64_t)buf; // address of buf
  100. s20Key[2] = (uint64_t)s20Key; // address of s20Key[]
  101. s20Key[3] = (uint64_t)&s20; // address of s20
  102. s20.init(s20Key,s20Key);
  103. }
  104. #ifdef __WINDOWS__
  105. static HCRYPTPROV cryptProvider = NULL;
  106. for(unsigned int i=0;i<bytes;++i) {
  107. if (randomPtr >= sizeof(randomBuf)) {
  108. if (cryptProvider == NULL) {
  109. if (!CryptAcquireContextA(&cryptProvider,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) {
  110. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
  111. exit(1);
  112. }
  113. }
  114. if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomBuf),(BYTE *)randomBuf)) {
  115. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
  116. exit(1);
  117. }
  118. randomPtr = 0;
  119. s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
  120. s20.init(randomBuf,randomBuf);
  121. }
  122. ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
  123. }
  124. #else // not __WINDOWS__
  125. static int devURandomFd = -1;
  126. if (devURandomFd < 0) {
  127. devURandomFd = ::open("/dev/urandom",O_RDONLY);
  128. if (devURandomFd < 0) {
  129. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  130. exit(1);
  131. return;
  132. }
  133. }
  134. for(unsigned int i=0;i<bytes;++i) {
  135. if (randomPtr >= sizeof(randomBuf)) {
  136. for(;;) {
  137. if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) {
  138. ::close(devURandomFd);
  139. devURandomFd = ::open("/dev/urandom",O_RDONLY);
  140. if (devURandomFd < 0) {
  141. fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
  142. exit(1);
  143. return;
  144. }
  145. } else break;
  146. }
  147. randomPtr = 0;
  148. s20.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
  149. s20.init(randomBuf,randomBuf);
  150. }
  151. ((uint8_t *)buf)[i] = randomBuf[randomPtr++];
  152. }
  153. #endif // __WINDOWS__ or not
  154. }
  155. } // namespace ZeroTier