void CPFWindow::OnMouseMove(UINT nFlags, CPoint point) { //This uses the mouse movements for random data. The nFlags //as a general rule will equal 0, but will allow an extra little //bit of randomness (no pun intended) if the user is clicking or //shifting or what-have-you-ing. randPoolAddBytes((uchar *) &nFlags, sizeof(nFlags)); randPoolAddBytes((uchar *) &point, sizeof(point)); CView::OnMouseMove(nFlags, point); }
/* * Load the RNG state from the randseed.bin file on disk. * Returns 0 on success, <0 on error. * * If cfb is non-zero, prewashes the data by encrypting with it. */ int cryptRandOpen(struct IdeaCfbContext *cfb) { byte buf[256]; int len; FILE *f; if (randSeedOpen) return 0; /* Already open */ f = fopen(globalRandseedName, FOPRBIN); if (!f) return -1; /* First get the bare minimum 24 bytes we need for the IDEA RNG */ len = fread((char *)buf, 1, 24, f); if (cfb) ideaCfbEncrypt(cfb, buf, buf, 24); ideaRandInit(&randContext, buf, buf+16); randSeedOpen = TRUE; if (len != 24) { /* Error */ fclose(f); return -1; } /* Read any extra into the random pool */ for (;;) { len = fread((char *)buf, 1, sizeof(buf), f); if (len <= 0) break; if (cfb) ideaCfbEncrypt(cfb, buf, buf, len); randPoolAddBytes(buf, len); } fclose(f); burn(buf); #ifdef MACTC5 PGPSetFinfo(globalRandseedName,'RSed','MPGP'); #endif return 0; }
void CPFWindow::OnTimer(UINT nIDEvent) { switch(nIDEvent) { case RANDOM_DATA_TIMER: { ulong ticks; static BOOL performanceCounterAvailable; static BOOL checkedPerformanceCounterAvailability = FALSE; LARGE_INTEGER performanceCount; /*First, get clock tick info...*/ ticks = GetTickCount(); randPoolAddWord(ticks); /*The performance counter is available on some platforms. If it is *available, it can have a precision of about one microsecond. If it *is available, we use it as a random bit source. Since it seems *silly to check if the thing is available every time we get a timer *message, we only check our first run through. */ if(!checkedPerformanceCounterAvailability) { checkedPerformanceCounterAvailability = TRUE; performanceCounterAvailable = QueryPerformanceCounter(&performanceCount); } if(performanceCounterAvailable) { if(QueryPerformanceCounter(&performanceCount)) randPoolAddBytes((uchar *) &performanceCount, sizeof(performanceCount)); else { /*This is a very bad thing: we said it was available before, *but now MS says it's not! */ pgp_errstring( "performanceCounterAvailable contains inconsistant value!"); } } break; } #if 0 case DEBUG_TIMER: { ShowStatus(); break; } #endif case UPDATE_TIMER: { char s[128], t[128]; long samp, inx; if(mHPPOutQueue) { samp = mHPPOutQueue->GetSize(); if(samp > mMaxQueueSize) { mMaxQueueSize = samp; } if(mLastSampleTime + 1000 <= pgp_getticks()) { mOutQueueSizes[mEntryPos] = samp; mEntryPos++; mEntryPos %= OUTQUEUEENTRIES; mLastSampleTime = pgp_getticks(); for(inx=mAvgHPP=0;inx<OUTQUEUEENTRIES;inx++) { mAvgHPP += mOutQueueSizes[inx]; } mAvgHPP /= OUTQUEUEENTRIES; if(mAvgHPP >= SOUNDINPUTQUEUEAVGMAX) { // we have become very backed up mOverflows += samp; for(inx=mAvgHPP=0;inx<OUTQUEUEENTRIES;inx++) { mOutQueueSizes[inx]=0; } // so ditch outgoing mHPPOutQueue->FreeType(_mt_voicePacket); InterlockedIncrement(&mDirty); } } } if(!mDirty) { return; } InterlockedExchange(&mDirty, 0); _itoa(mRTTms, t, 10); sprintf(s, "%d\r%d\r%d\r%sms\r%d%%", mPacketsSent, mGoodPackets, mBadPackets, t, mGSMfull); mInfo1.SetWindowText(s); _itoa(mSoundOutput->GetNumPlaying(), t, 10); sprintf(s, "%s\r%d\r%d\r%dms\r%d", t, mOutputUnderflows, mOverflows, mJitter, mBandwidth); mInfo2.SetWindowText(s); mStatusPane.UpdateStatus(); break; } } CView::OnTimer(nIDEvent); }
/* * Add as much environmentally-derived random noise as possible * to the randPool. Typically, this involves reading the most * accurate system clocks available. * * Returns the number of ticks that have passed since the last call, * for entropy estimation purposes. */ word32 noise(void) { word32 delta; #if defined(MSDOS) static unsigned deltamask = 0; static unsigned prevt; unsigned t; time_t tnow; clock_t cnow; if (deltamask == 0) deltamask = has8254() ? 0xffff : 0x7fff; t = (deltamask & 0x8000) ? read8254() : read8253(); randPoolAddBytes((byte const *)&t, sizeof(t)); delta = deltamask & (t - prevt); prevt = t; /* Add more-significant time components. */ cnow = clock(); randPoolAddBytes((byte *)&cnow, sizeof(cnow)); tnow = time((time_t *)0); randPoolAddBytes((byte *)&tnow, sizeof(tnow)); /* END OF DOS */ #elif defined(VMS) word32 t[2]; /* little-endian 64-bit timer */ word32 d1; /* MSW of difference */ static word32 prevt[2]; SYS$GETTIM(t); /* VMS hardware clock increments by 100000 per tick */ randPoolAddBytes((byte const *)t, sizeof(t)); /* Get difference in d1 and delta, and old time in prevt */ d1 = t[1] - prevt[1] + (t[0] < prevt[0]); prevt[1] = t[1]; delta = t[0] - prevt[0]; prevt[0] = t[0]; /* Now, divide the 64-bit value by 100000 = 2^5 * 5^5 = 32 * 3125 */ /* Divide value, MSW in d1 and LSW in delta, by 32 */ delta >>= 5; delta |= d1 << (32-5); d1 >>= 5; /* * Divide by 3125. This fits into 16 bits, so the following * code is possible. 2^32 = 3125 * 1374389 + 1671. * * This code has confused people reading it, so here's a detailed * explanation. First, since we only want a 32-bit result, * reduce the input mod 3125 * 2^32 before starting. This * amounts to reducing the most significant word mod 3125 and * leaving the least-significant word alone. * * Then, using / for mathematical (real, not integer) division, we * want to compute floor(d1 * 2^32 + d0) / 3125), which I'll denote * using the old [ ] syntax for floor, so it's * [ (d1 * 2^32 + d0) / 3125 ] * = [ (d1 * (3125 * 1374389 + 1671) + d0) / 3125 ] * = [ d1 * 1374389 + (d1 * 1671 + d0) / 3125 ] * = d1 * 137438 + [ (d1 * 1671 + d0) / 3125 ] * = d1 * 137438 + [ d0 / 3125 ] + [ (d1 * 1671 + d0 % 3125) / 3125 ] * * The C / operator, applied to integers, performs [ a / b ], so * this can be implemented in C, and since d1 < 3125 (by the first * modulo operation), d1 * 1671 + d0 % 3125 < 3125 * 1672, which * is 5225000, less than 2^32, so it all fits into 32 bits. */ d1 %= 3125; /* Ignore overflow past 32 bits */ delta = delta/3125 + d1*1374389 + (delta%3125 + d1*1671) / 3125; /* END OF VMS */ #elif defined(UNIX) timetype t; static unsigned ticksize = 0; static timetype prevt; gettime(&t); #if CHOICE_GETITIMER /* If itimer isn't started, start it */ if (t.it_value.tv_sec == 0 && t.it_value.tv_usec == 0) { /* * start the timer - assume that PGP won't be running for * more than 11 days, 13 hours, 46 minutes and 40 seconds. */ t.it_value.tv_sec = 1000000; t.it_interval.tv_sec = 1000000; t.it_interval.tv_usec = 0; signal(SIGALRM, SIG_IGN); /* just in case.. */ setitimer(ITIMER_REAL, &t, NULL); t.it_value.tv_sec = 0; } randPoolAddBytes((byte const *)&t.it_value, sizeof(t.it_value)); #else randPoolAddBytes((byte const *)&t, sizeof(t)); #endif if (!ticksize) ticksize = noiseTickSize(); delta = (word32)(tickdiff(t, prevt) / ticksize); prevt = t; /* END OF UNIX */ #else #error Unknown OS - define UNIX or MSDOS or add code for high-resolution timers #endif return delta; }