Back to index

lightning-sunbird  0.9+nobinonly
win_rand.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "secrng.h"
00038 #include "secerr.h"
00039 #ifdef XP_WIN
00040 #include <windows.h>
00041 
00042 #if defined(_WIN32_WCE)
00043 #include <stdlib.h>  /* Win CE puts lots of stuff here. */
00044 #include "prprf.h"   /* for PR_snprintf */
00045 #else
00046 #include <time.h>
00047 #include <io.h>
00048 #include <sys/types.h>
00049 #include <sys/stat.h>
00050 #endif
00051 #include <stdio.h>
00052 
00053 #ifndef _WIN32
00054 #define VTD_Device_ID   5
00055 #define OP_OVERRIDE     _asm _emit 0x66
00056 #include <dos.h>
00057 #endif
00058 
00059 #include "prio.h"
00060 #include "prerror.h"
00061 
00062 static PRInt32  filesToRead;
00063 static DWORD    totalFileBytes;
00064 static DWORD    maxFileBytes       = 250000;     /* 250 thousand */
00065 static DWORD    dwNumFiles, dwReadEvery;
00066 
00067 static BOOL
00068 CurrentClockTickTime(LPDWORD lpdwHigh, LPDWORD lpdwLow)
00069 {
00070 #ifdef _WIN32
00071     LARGE_INTEGER   liCount;
00072 
00073     if (!QueryPerformanceCounter(&liCount))
00074         return FALSE;
00075 
00076     *lpdwHigh = liCount.u.HighPart;
00077     *lpdwLow = liCount.u.LowPart;
00078     return TRUE;
00079 
00080 #else   /* is WIN16 */
00081     BOOL    bRetVal;
00082     FARPROC lpAPI;
00083     WORD    w1, w2, w3, w4;
00084 
00085     // Get direct access to the VTD and query the current clock tick time
00086     _asm {
00087         xor   di, di
00088         mov   es, di
00089         mov   ax, 1684h
00090         mov   bx, VTD_Device_ID
00091         int   2fh
00092         mov   ax, es
00093         or    ax, di
00094         jz    EnumerateFailed
00095 
00096         ; VTD API is available. First store the API address (the address actually
00097         ; contains an instruction that causes a fault, the fault handler then
00098         ; makes the ring transition and calls the API in the VxD)
00099         mov   word ptr lpAPI, di
00100         mov   word ptr lpAPI+2, es
00101         mov   ax, 100h      ; API function to VTD_Get_Real_Time
00102 ;       call  dword ptr [lpAPI]
00103         call  [lpAPI]
00104 
00105         ; Result is in EDX:EAX which we will get 16-bits at a time
00106         mov   w2, dx
00107         OP_OVERRIDE
00108         shr   dx,10h        ; really "shr edx, 16"
00109         mov   w1, dx
00110 
00111         mov   w4, ax
00112         OP_OVERRIDE
00113         shr   ax,10h        ; really "shr eax, 16"
00114         mov   w3, ax
00115 
00116         mov   bRetVal, 1    ; return TRUE
00117         jmp   EnumerateExit
00118 
00119       EnumerateFailed:
00120         mov   bRetVal, 0    ; return FALSE
00121 
00122       EnumerateExit:
00123     }
00124 
00125     *lpdwHigh = MAKELONG(w2, w1);
00126     *lpdwLow = MAKELONG(w4, w3);
00127 
00128     return bRetVal;
00129 #endif  /* is WIN16 */
00130 }
00131 
00132 size_t RNG_GetNoise(void *buf, size_t maxbuf)
00133 {
00134     DWORD   dwHigh, dwLow, dwVal;
00135     int     n = 0;
00136     int     nBytes;
00137 
00138     if (maxbuf <= 0)
00139         return 0;
00140 
00141     CurrentClockTickTime(&dwHigh, &dwLow);
00142 
00143     // get the maximally changing bits first
00144     nBytes = sizeof(dwLow) > maxbuf ? maxbuf : sizeof(dwLow);
00145     memcpy((char *)buf, &dwLow, nBytes);
00146     n += nBytes;
00147     maxbuf -= nBytes;
00148 
00149     if (maxbuf <= 0)
00150         return n;
00151 
00152     nBytes = sizeof(dwHigh) > maxbuf ? maxbuf : sizeof(dwHigh);
00153     memcpy(((char *)buf) + n, &dwHigh, nBytes);
00154     n += nBytes;
00155     maxbuf -= nBytes;
00156 
00157     if (maxbuf <= 0)
00158         return n;
00159 
00160     // get the number of milliseconds that have elapsed since Windows started
00161     dwVal = GetTickCount();
00162 
00163     nBytes = sizeof(dwVal) > maxbuf ? maxbuf : sizeof(dwVal);
00164     memcpy(((char *)buf) + n, &dwVal, nBytes);
00165     n += nBytes;
00166     maxbuf -= nBytes;
00167 
00168     if (maxbuf <= 0)
00169         return n;
00170 
00171 #if defined(_WIN32_WCE)
00172     {
00173     // get the number of milliseconds elapsed since Windows CE was started. 
00174     DWORD  tickCount = GetTickCount();
00175     nBytes = (sizeof tickCount) > maxbuf ? maxbuf : (sizeof tickCount);
00176     memcpy(((char *)buf) + n, &tickCount, nBytes);
00177     n += nBytes;
00178     }
00179 #else
00180     {
00181     time_t  sTime;
00182     // get the time in seconds since midnight Jan 1, 1970
00183     time(&sTime);
00184     nBytes = sizeof(sTime) > maxbuf ? maxbuf : sizeof(sTime);
00185     memcpy(((char *)buf) + n, &sTime, nBytes);
00186     n += nBytes;
00187     }
00188 #endif
00189 
00190     return n;
00191 }
00192 
00193 #if defined(_WIN32_WCE)
00194 static BOOL
00195 EnumSystemFilesWithNSPR(const char * dirName, 
00196                      BOOL recursive,
00197                         PRInt32 (*func)(const char *))
00198 {
00199     PRDir *      pDir;
00200     PRDirEntry * pEntry;
00201     BOOL         rv         = FALSE;
00202 
00203     pDir = PR_OpenDir(dirName);
00204     if (!pDir)
00205        return rv;
00206     while ((pEntry = PR_ReadDir(pDir, PR_SKIP_BOTH|PR_SKIP_HIDDEN)) != NULL) {
00207        PRStatus    status;
00208        PRInt32     count;
00209        PRInt32     stop;
00210        PRFileInfo  fileInfo;
00211        char        szFileName[_MAX_PATH];
00212 
00213         count = (PRInt32)PR_snprintf(szFileName, sizeof szFileName, "%s\\%s", 
00214                                     dirName, PR_DirName(pEntry));
00215        if (count < 1)
00216            continue;
00217        status = PR_GetFileInfo(szFileName, &fileInfo);
00218        if (status != PR_SUCCESS)
00219            continue;
00220        if (fileInfo.type == PR_FILE_FILE) {
00221            stop = (*func)(szFileName);
00222            rv = TRUE;
00223            if (stop)
00224                break;
00225            continue;
00226        }
00227        if (recursive && fileInfo.type == PR_FILE_DIRECTORY) {
00228            rv |= EnumSystemFilesWithNSPR(szFileName, recursive, func);
00229        }
00230     }
00231     PR_CloseDir(pDir);
00232     return rv;
00233 }
00234 #endif
00235 
00236 static BOOL
00237 EnumSystemFiles(PRInt32 (*func)(const char *))
00238 {
00239 #if defined(_WIN32_WCE)
00240     BOOL rv = FALSE;
00241     rv |= EnumSystemFilesWithNSPR("\\Windows\\Temporary Internet Files", TRUE, func);
00242     rv |= EnumSystemFilesWithNSPR("\\Temp",    FALSE, func);
00243     rv |= EnumSystemFilesWithNSPR("\\Windows", FALSE, func);
00244     return rv;
00245 #else
00246     int                 iStatus;
00247     char                szSysDir[_MAX_PATH];
00248     char                szFileName[_MAX_PATH];
00249 #ifdef _WIN32
00250     struct _finddata_t  fdData;
00251     long                lFindHandle;
00252 #else
00253     struct _find_t  fdData;
00254 #endif
00255 
00256     if (!GetSystemDirectory(szSysDir, sizeof(szSysDir)))
00257         return FALSE;
00258 
00259     // tack *.* on the end so we actually look for files. this will
00260     // not overflow
00261     strcpy(szFileName, szSysDir);
00262     strcat(szFileName, "\\*.*");
00263 
00264 #ifdef _WIN32
00265     lFindHandle = _findfirst(szFileName, &fdData);
00266     if (lFindHandle == -1)
00267         return FALSE;
00268 #else
00269     if (_dos_findfirst(szFileName, _A_NORMAL | _A_RDONLY | _A_ARCH | _A_SUBDIR, &fdData) != 0)
00270         return FALSE;
00271 #endif
00272 
00273     do {
00274         // pass the full pathname to the callback
00275         sprintf(szFileName, "%s\\%s", szSysDir, fdData.name);
00276         (*func)(szFileName);
00277 
00278 #ifdef _WIN32
00279         iStatus = _findnext(lFindHandle, &fdData);
00280 #else
00281         iStatus = _dos_findnext(&fdData);
00282 #endif
00283     } while (iStatus == 0);
00284 
00285 #ifdef _WIN32
00286     _findclose(lFindHandle);
00287 #endif
00288 
00289     return TRUE;
00290 #endif
00291 }
00292 
00293 static PRInt32
00294 CountFiles(const char *file)
00295 {
00296     dwNumFiles++;
00297     return 0;
00298 }
00299 
00300 static PRInt32
00301 ReadFiles(const char *file)
00302 {
00303     if ((dwNumFiles % dwReadEvery) == 0) {
00304        ++filesToRead;
00305     }
00306     if (filesToRead) {
00307        DWORD    prevFileBytes = totalFileBytes;
00308         RNG_FileForRNG(file);
00309        if (prevFileBytes < totalFileBytes) {
00310            --filesToRead;
00311        }
00312     }
00313     dwNumFiles++;
00314     return (totalFileBytes >= maxFileBytes);
00315 }
00316 
00317 static void
00318 ReadSystemFiles()
00319 {
00320     // first count the number of files
00321     dwNumFiles = 0;
00322     if (!EnumSystemFiles(CountFiles))
00323         return;
00324 
00325     RNG_RandomUpdate(&dwNumFiles, sizeof(dwNumFiles));
00326 
00327     // now read 10 files
00328     filesToRead = 10;
00329     if (dwNumFiles == 0)
00330         return;
00331 
00332     dwReadEvery = dwNumFiles / 10;
00333     if (dwReadEvery == 0)
00334         dwReadEvery = 1;  // less than 10 files
00335 
00336     dwNumFiles = 0;
00337     EnumSystemFiles(ReadFiles);
00338 }
00339 
00340 void RNG_SystemInfoForRNG(void)
00341 {
00342     DWORD           dwVal;
00343     char            buffer[256];
00344     int             nBytes;
00345 #ifdef _WIN32
00346     MEMORYSTATUS    sMem;
00347     HANDLE          hVal;
00348 #if !defined(_WIN32_WCE)
00349     DWORD           dwSerialNum;
00350     DWORD           dwComponentLen;
00351     DWORD           dwSysFlags;
00352     char            volName[128];
00353     DWORD           dwSectors, dwBytes, dwFreeClusters, dwNumClusters;
00354 #endif
00355 #else
00356     int             iVal;
00357     HTASK           hTask;
00358     WORD            wDS, wCS;
00359     LPSTR           lpszEnv;
00360 #endif
00361 
00362     nBytes = RNG_GetNoise(buffer, 20);  // get up to 20 bytes
00363     RNG_RandomUpdate(buffer, nBytes);
00364 
00365 #ifdef _WIN32
00366     sMem.dwLength = sizeof(sMem);
00367     GlobalMemoryStatus(&sMem);                // assorted memory stats
00368     RNG_RandomUpdate(&sMem, sizeof(sMem));
00369 #if !defined(_WIN32_WCE)
00370     dwVal = GetLogicalDrives();
00371     RNG_RandomUpdate(&dwVal, sizeof(dwVal));  // bitfields in bits 0-25
00372 #endif
00373 #else
00374     dwVal = GetFreeSpace(0);
00375     RNG_RandomUpdate(&dwVal, sizeof(dwVal));
00376 
00377     _asm    mov wDS, ds;
00378     _asm    mov wCS, cs;
00379     RNG_RandomUpdate(&wDS, sizeof(wDS));
00380     RNG_RandomUpdate(&wCS, sizeof(wCS));
00381 #endif
00382 
00383 #ifdef _WIN32
00384 #if !defined(_WIN32_WCE)
00385     dwVal = sizeof(buffer);
00386     if (GetComputerName(buffer, &dwVal))
00387         RNG_RandomUpdate(buffer, dwVal);
00388 #endif
00389 /* XXX This is code that got yanked because of NSPR20.  We should put it
00390  * back someday.
00391  */
00392 #ifdef notdef
00393     {
00394     POINT ptVal;
00395     GetCursorPos(&ptVal);
00396     RNG_RandomUpdate(&ptVal, sizeof(ptVal));
00397     }
00398 
00399     dwVal = GetQueueStatus(QS_ALLINPUT);      // high and low significant
00400     RNG_RandomUpdate(&dwVal, sizeof(dwVal));
00401 
00402     {
00403     HWND hWnd;
00404     hWnd = GetClipboardOwner();               // 2 or 4 bytes
00405     RNG_RandomUpdate((void *)&hWnd, sizeof(hWnd));
00406     }
00407 
00408     {
00409     UUID sUuid;
00410     UuidCreate(&sUuid);                       // this will fail on machines with no ethernet
00411     RNG_RandomUpdate(&sUuid, sizeof(sUuid));  // boards. shove the bits in regardless
00412     }
00413 #endif
00414 
00415     hVal = GetCurrentProcess();               // 4 byte handle of current task
00416     RNG_RandomUpdate(&hVal, sizeof(hVal));
00417 
00418     dwVal = GetCurrentProcessId();            // process ID (4 bytes)
00419     RNG_RandomUpdate(&dwVal, sizeof(dwVal));
00420 
00421 #if !defined(_WIN32_WCE)
00422     volName[0] = '\0';
00423     buffer[0] = '\0';
00424     GetVolumeInformation(NULL,
00425                          volName,
00426                          sizeof(volName),
00427                          &dwSerialNum,
00428                          &dwComponentLen,
00429                          &dwSysFlags,
00430                          buffer,
00431                          sizeof(buffer));
00432 
00433     RNG_RandomUpdate(volName,         strlen(volName));
00434     RNG_RandomUpdate(&dwSerialNum,    sizeof(dwSerialNum));
00435     RNG_RandomUpdate(&dwComponentLen, sizeof(dwComponentLen));
00436     RNG_RandomUpdate(&dwSysFlags,     sizeof(dwSysFlags));
00437     RNG_RandomUpdate(buffer,          strlen(buffer));
00438 
00439     if (GetDiskFreeSpace(NULL, &dwSectors, &dwBytes, &dwFreeClusters, &dwNumClusters)) {
00440         RNG_RandomUpdate(&dwSectors,      sizeof(dwSectors));
00441         RNG_RandomUpdate(&dwBytes,        sizeof(dwBytes));
00442         RNG_RandomUpdate(&dwFreeClusters, sizeof(dwFreeClusters));
00443         RNG_RandomUpdate(&dwNumClusters,  sizeof(dwNumClusters));
00444     }
00445 #endif
00446 #else   /* is WIN16 */
00447     hTask = GetCurrentTask();
00448     RNG_RandomUpdate((void *)&hTask, sizeof(hTask));
00449 
00450     iVal = GetNumTasks();
00451     RNG_RandomUpdate(&iVal, sizeof(iVal));      // number of running tasks
00452 
00453     lpszEnv = GetDOSEnvironment();
00454     while (*lpszEnv != '\0') {
00455         RNG_RandomUpdate(lpszEnv, strlen(lpszEnv));
00456 
00457         lpszEnv += strlen(lpszEnv) + 1;
00458     }
00459 #endif  /* is WIN16 */
00460 
00461     // now let's do some files
00462     ReadSystemFiles();
00463 
00464     nBytes = RNG_GetNoise(buffer, 20);  // get up to 20 bytes
00465     RNG_RandomUpdate(buffer, nBytes);
00466 }
00467 
00468 #if defined(_WIN32_WCE)
00469 void RNG_FileForRNG(const char *filename)
00470 {
00471     PRFileDesc *    file;
00472     int             nBytes;
00473     PRFileInfo      infoBuf;
00474     unsigned char   buffer[1024];
00475 
00476     /* windows doesn't initialize all the bytes in the stat buf,
00477      * so initialize them all here to avoid UMRs.
00478      */
00479     memset(&infoBuf, 0, sizeof infoBuf);
00480 
00481     if (PR_GetFileInfo(filename, &infoBuf) < 0)
00482         return;
00483 
00484     RNG_RandomUpdate((unsigned char*)&infoBuf, sizeof(infoBuf));
00485 
00486     file = PR_Open(filename, PR_RDONLY, 0);
00487     if (file != NULL) {
00488         for (;;) {
00489             PRInt32 bytes = PR_Read(file, buffer, sizeof buffer);
00490 
00491             if (bytes <= 0)
00492                 break;
00493 
00494             RNG_RandomUpdate(buffer, bytes);
00495             totalFileBytes += bytes;
00496             if (totalFileBytes > maxFileBytes)
00497                 break;
00498         }
00499 
00500         PR_Close(file);
00501     }
00502 
00503     nBytes = RNG_GetNoise(buffer, 20);  // get up to 20 bytes
00504     RNG_RandomUpdate(buffer, nBytes);
00505 }
00506 
00507 #else /* not WinCE */
00508 
00509 void RNG_FileForRNG(const char *filename)
00510 {
00511     FILE*           file;
00512     int             nBytes;
00513     struct stat     stat_buf;
00514     unsigned char   buffer[1024];
00515 
00516    /* static DWORD    totalFileBytes = 0; */
00517 
00518     /* windows doesn't initialize all the bytes in the stat buf,
00519      * so initialize them all here to avoid UMRs.
00520      */
00521     memset(&stat_buf, 0, sizeof stat_buf);
00522 
00523     if (stat((char *)filename, &stat_buf) < 0)
00524         return;
00525 
00526     RNG_RandomUpdate((unsigned char*)&stat_buf, sizeof(stat_buf));
00527 
00528     file = fopen((char *)filename, "r");
00529     if (file != NULL) {
00530         for (;;) {
00531             size_t  bytes = fread(buffer, 1, sizeof(buffer), file);
00532 
00533             if (bytes == 0)
00534                 break;
00535 
00536             RNG_RandomUpdate(buffer, bytes);
00537             totalFileBytes += bytes;
00538             if (totalFileBytes > maxFileBytes)
00539                 break;
00540         }
00541 
00542         fclose(file);
00543     }
00544 
00545     nBytes = RNG_GetNoise(buffer, 20);  // get up to 20 bytes
00546     RNG_RandomUpdate(buffer, nBytes);
00547 }
00548 
00549 #endif  /* not WinCE */
00550 
00551 /*
00552  * CryptoAPI requires Windows NT 4.0 or Windows 95 OSR2 and later.
00553  * Until we drop support for Windows 95, we need to emulate some
00554  * definitions and declarations in <wincrypt.h> and look up the
00555  * functions in advapi32.dll at run time.
00556  */
00557 
00558 typedef unsigned long HCRYPTPROV;
00559 
00560 #define CRYPT_VERIFYCONTEXT 0xF0000000
00561 
00562 #define PROV_RSA_FULL 1
00563 
00564 typedef BOOL
00565 (WINAPI *CryptAcquireContextAFn)(
00566     HCRYPTPROV *phProv,
00567     LPCSTR pszContainer,
00568     LPCSTR pszProvider,
00569     DWORD dwProvType,
00570     DWORD dwFlags);
00571 
00572 typedef BOOL
00573 (WINAPI *CryptReleaseContextFn)(
00574     HCRYPTPROV hProv,
00575     DWORD dwFlags);
00576 
00577 typedef BOOL
00578 (WINAPI *CryptGenRandomFn)(
00579     HCRYPTPROV hProv,
00580     DWORD dwLen,
00581     BYTE *pbBuffer);
00582 
00583 /*
00584  * Windows XP and Windows Server 2003 and later have RtlGenRandom,
00585  * which must be looked up by the name SystemFunction036.
00586  */
00587 typedef BOOLEAN
00588 (APIENTRY *RtlGenRandomFn)(
00589     PVOID RandomBuffer,
00590     ULONG RandomBufferLength);
00591 
00592 size_t RNG_SystemRNG(void *dest, size_t maxLen)
00593 {
00594     HMODULE hModule;
00595     RtlGenRandomFn pRtlGenRandom;
00596     CryptAcquireContextAFn pCryptAcquireContextA;
00597     CryptReleaseContextFn pCryptReleaseContext;
00598     CryptGenRandomFn pCryptGenRandom;
00599     HCRYPTPROV hCryptProv;
00600     size_t bytes = 0;
00601 
00602     hModule = LoadLibrary("advapi32.dll");
00603     if (hModule == NULL) {
00604        PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
00605        return 0;
00606     }
00607     pRtlGenRandom = (RtlGenRandomFn)
00608        GetProcAddress(hModule, "SystemFunction036");
00609     if (pRtlGenRandom) {
00610        if (pRtlGenRandom(dest, maxLen)) {
00611            bytes = maxLen;
00612        } else {
00613            PORT_SetError(SEC_ERROR_NEED_RANDOM);  /* system RNG failed */
00614        }
00615        goto done;
00616     }
00617     pCryptAcquireContextA = (CryptAcquireContextAFn)
00618        GetProcAddress(hModule, "CryptAcquireContextA");
00619     pCryptReleaseContext = (CryptReleaseContextFn)
00620        GetProcAddress(hModule, "CryptReleaseContext");
00621     pCryptGenRandom = (CryptGenRandomFn)
00622        GetProcAddress(hModule, "CryptGenRandom");
00623     if (!pCryptAcquireContextA || !pCryptReleaseContext || !pCryptGenRandom) {
00624        PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
00625        goto done;
00626     }
00627     if (pCryptAcquireContextA(&hCryptProv, NULL, NULL,
00628        PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
00629        if (pCryptGenRandom(hCryptProv, maxLen, dest)) {
00630            bytes = maxLen;
00631        }
00632        pCryptReleaseContext(hCryptProv, 0);
00633     }
00634     if (bytes == 0) {
00635        PORT_SetError(SEC_ERROR_NEED_RANDOM);  /* system RNG failed */
00636     }
00637 done:
00638     FreeLibrary(hModule);
00639     return bytes;
00640 }
00641 
00642 #endif  /* is XP_WIN */