Adam Ierymenko 6 years ago
parent
commit
846c96e8d5
3 changed files with 21 additions and 3 deletions
  1. 1 1
      node/Packet.cpp
  2. 19 2
      node/Utils.cpp
  3. 1 0
      root/root.cpp

+ 1 - 1
node/Packet.cpp

@@ -938,7 +938,7 @@ uint64_t Packet::nextPacketId()
 	static uint64_t ctr = 0;
 	static Mutex lock;
 	lock.lock();
-	while (unlikely(ctr == 0))
+	while (ctr == 0)
 		Utils::getSecureRandom(&ctr,sizeof(ctr));
 	const uint64_t i = ctr++;
 	lock.unlock();

+ 19 - 2
node/Utils.cpp

@@ -143,7 +143,8 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes)
 {
 	static Mutex globalLock;
 	static bool initialized = false;
-	static uint8_t randomBuf[131072];
+	static uint64_t randomState[1024];
+	static uint8_t randomBuf[65536];
 	static unsigned long randomPtr = sizeof(randomBuf);
 #ifdef __WINDOWS__
 	static HCRYPTPROV cryptProvider = NULL;
@@ -164,6 +165,10 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes)
 			fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to obtain WinCrypt context!\r\n");
 			exit(1);
 		}
+		if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomState),(BYTE *)randomState)) {
+			fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
+			exit(1);
+		}
 		if (!CryptGenRandom(cryptProvider,(DWORD)sizeof(randomBuf),(BYTE *)randomBuf)) {
 			fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() CryptGenRandom failed!\r\n");
 			exit(1);
@@ -174,6 +179,11 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes)
 			fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to open /dev/urandom\n");
 			exit(1);
 		}
+		if ((int)::read(devURandomFd,randomState,sizeof(randomState)) != (int)sizeof(randomState)) {
+			::close(devURandomFd);
+			fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to read from /dev/urandom\n");
+			exit(1);
+		}
 		if ((int)::read(devURandomFd,randomBuf,sizeof(randomBuf)) != (int)sizeof(randomBuf)) {
 			::close(devURandomFd);
 			fprintf(stderr,"FATAL ERROR: Utils::getSecureRandom() unable to read from /dev/urandom\n");
@@ -186,8 +196,14 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes)
 
 	for(unsigned int i=0;i<bytes;++i) {
 		if (randomPtr >= sizeof(randomBuf)) {
+			for(unsigned int k=0;k<1024;++k) {
+				if (++randomState[k])
+					break;
+			}
+
 			uint8_t h[64];
-			SHA512(h,randomBuf,sizeof(randomBuf));
+			SHA512(h,randomState,sizeof(randomState));
+
 			if (AES::HW_ACCEL) {
 				AES c(h);
 				c.ctr(h + 32,randomBuf,sizeof(randomBuf),randomBuf);
@@ -195,6 +211,7 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes)
 				Salsa20 c(h,h + 32);
 				c.crypt12(randomBuf,randomBuf,sizeof(randomBuf));
 			}
+
 			randomPtr = 0;
 		}
 		((uint8_t *)buf)[i] = randomBuf[randomPtr++];

+ 1 - 0
root/root.cpp

@@ -213,6 +213,7 @@ static void handlePacket(const int sock,const InetAddress *const ip,Packet &pkt)
 				}	break;
 
 				case Packet::VERB_MULTICAST_LIKE: {
+					printf("LIKE\n");
 					Mutex::Lock l(peer->multicastGroups_l);
 					for(unsigned int ptr=ZT_PACKET_IDX_PAYLOAD;ptr<pkt.size();ptr+=18) {
 						const uint64_t nwid = pkt.template at<uint64_t>(ptr);