From 846c96e8d5308a2c8e9f13ed00c876b14a1be4a0 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 28 Aug 2019 14:36:29 -0700 Subject: [PATCH] . --- node/Packet.cpp | 2 +- node/Utils.cpp | 21 +++++++++++++++++++-- root/root.cpp | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/node/Packet.cpp b/node/Packet.cpp index 142b78658..5e5357ce1 100644 --- a/node/Packet.cpp +++ b/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(); diff --git a/node/Utils.cpp b/node/Utils.cpp index eae37551e..719e4066a 100644 --- a/node/Utils.cpp +++ b/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= 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++]; diff --git a/root/root.cpp b/root/root.cpp index 7baa529f9..598cd85f1 100644 --- a/root/root.cpp +++ b/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(ptr);