This commit is contained in:
Adam Ierymenko 2019-08-28 14:36:29 -07:00
parent 63775723c1
commit 846c96e8d5
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
3 changed files with 21 additions and 3 deletions

View file

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

View file

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

View file

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