diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-01-04 14:24:24 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-01-04 14:24:24 +0000 |
commit | dc1565216a5d20ae0d75872151523252309a1292 (patch) | |
tree | d57454ba9a40386552179eddf60d28bd1e8f3d54 /nss/lib/freebl/os2_rand.c | |
parent | 26c046fbc57d53136b4fb3b5e0d18298318125d4 (diff) | |
download | nss-dc1565216a5d20ae0d75872151523252309a1292.tar.gz |
nss-3.28.1nss-3.28.1
Diffstat (limited to 'nss/lib/freebl/os2_rand.c')
-rw-r--r-- | nss/lib/freebl/os2_rand.c | 349 |
1 files changed, 171 insertions, 178 deletions
diff --git a/nss/lib/freebl/os2_rand.c b/nss/lib/freebl/os2_rand.c index bfe28cf..407b080 100644 --- a/nss/lib/freebl/os2_rand.c +++ b/nss/lib/freebl/os2_rand.c @@ -12,32 +12,34 @@ #include <stdio.h> #include <sys/stat.h> -static BOOL clockTickTime(unsigned long *phigh, unsigned long *plow) +static BOOL +clockTickTime(unsigned long *phigh, unsigned long *plow) { APIRET rc = NO_ERROR; - QWORD qword = {0,0}; + QWORD qword = { 0, 0 }; rc = DosTmrQueryTime(&qword); if (rc != NO_ERROR) - return FALSE; + return FALSE; *phigh = qword.ulHi; - *plow = qword.ulLo; + *plow = qword.ulLo; return TRUE; } -size_t RNG_GetNoise(void *buf, size_t maxbuf) +size_t +RNG_GetNoise(void *buf, size_t maxbuf) { unsigned long high = 0; - unsigned long low = 0; + unsigned long low = 0; clock_t val = 0; int n = 0; int nBytes = 0; time_t sTime; if (maxbuf <= 0) - return 0; + return 0; clockTickTime(&high, &low); @@ -48,7 +50,7 @@ size_t RNG_GetNoise(void *buf, size_t maxbuf) maxbuf -= nBytes; if (maxbuf <= 0) - return n; + return n; nBytes = sizeof(high) > maxbuf ? maxbuf : sizeof(high); memcpy(((char *)buf) + n, &high, nBytes); @@ -56,7 +58,7 @@ size_t RNG_GetNoise(void *buf, size_t maxbuf) maxbuf -= nBytes; if (maxbuf <= 0) - return n; + return n; /* get the number of milliseconds that have elapsed since application started */ val = clock(); @@ -67,7 +69,7 @@ size_t RNG_GetNoise(void *buf, size_t maxbuf) maxbuf -= nBytes; if (maxbuf <= 0) - return n; + return n; /* get the time in seconds since midnight Jan 1, 1970 */ time(&sTime); @@ -81,54 +83,51 @@ size_t RNG_GetNoise(void *buf, size_t maxbuf) static BOOL EnumSystemFiles(void (*func)(const char *)) { - APIRET rc; - ULONG sysInfo = 0; - char bootLetter[2]; - char sysDir[_MAX_PATH] = ""; - char filename[_MAX_PATH]; - HDIR hdir = HDIR_CREATE; - ULONG numFiles = 1; - FILEFINDBUF3 fileBuf = {0}; - ULONG buflen = sizeof(FILEFINDBUF3); + APIRET rc; + ULONG sysInfo = 0; + char bootLetter[2]; + char sysDir[_MAX_PATH] = ""; + char filename[_MAX_PATH]; + HDIR hdir = HDIR_CREATE; + ULONG numFiles = 1; + FILEFINDBUF3 fileBuf = { 0 }; + ULONG buflen = sizeof(FILEFINDBUF3); if (DosQuerySysInfo(QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, (PVOID)&sysInfo, - sizeof(ULONG)) == NO_ERROR) - { - bootLetter[0] = sysInfo + 'A' -1; - bootLetter[1] = '\0'; - strcpy(sysDir, bootLetter); - strcpy(sysDir+1, ":\\OS2\\"); - - strcpy( filename, sysDir ); - strcat( filename, "*.*" ); + sizeof(ULONG)) == NO_ERROR) { + bootLetter[0] = sysInfo + 'A' - 1; + bootLetter[1] = '\0'; + strcpy(sysDir, bootLetter); + strcpy(sysDir + 1, ":\\OS2\\"); + + strcpy(filename, sysDir); + strcat(filename, "*.*"); } - rc =DosFindFirst( filename, &hdir, FILE_NORMAL, &fileBuf, buflen, - &numFiles, FIL_STANDARD ); - if( rc == NO_ERROR ) - { - do { - // pass the full pathname to the callback - sprintf( filename, "%s%s", sysDir, fileBuf.achName ); - (*func)(filename); - - numFiles = 1; - rc = DosFindNext( hdir, &fileBuf, buflen, &numFiles ); - if( rc != NO_ERROR && rc != ERROR_NO_MORE_FILES ) - printf( "DosFindNext errod code = %d\n", rc ); - } while ( rc == NO_ERROR ); - - rc = DosFindClose(hdir); - if( rc != NO_ERROR ) - printf( "DosFindClose error code = %d", rc ); - } - else - printf( "DosFindFirst error code = %d", rc ); + rc = DosFindFirst(filename, &hdir, FILE_NORMAL, &fileBuf, buflen, + &numFiles, FIL_STANDARD); + if (rc == NO_ERROR) { + do { + // pass the full pathname to the callback + sprintf(filename, "%s%s", sysDir, fileBuf.achName); + (*func)(filename); + + numFiles = 1; + rc = DosFindNext(hdir, &fileBuf, buflen, &numFiles); + if (rc != NO_ERROR && rc != ERROR_NO_MORE_FILES) + printf("DosFindNext errod code = %d\n", rc); + } while (rc == NO_ERROR); + + rc = DosFindClose(hdir); + if (rc != NO_ERROR) + printf("DosFindClose error code = %d", rc); + } else + printf("DosFindFirst error code = %d", rc); return TRUE; } -static int dwNumFiles, dwReadEvery, dwFileToRead=0; +static int dwNumFiles, dwReadEvery, dwFileToRead = 0; static void CountFiles(const char *file) @@ -145,17 +144,17 @@ ReadFiles(const char *file) dwNumFiles++; } -static void +static void ReadSingleFile(const char *filename) { unsigned char buffer[1024]; - FILE *file; - + FILE *file; + file = fopen((char *)filename, "rb"); if (file != NULL) { - while (fread(buffer, 1, sizeof(buffer), file) > 0) - ; - fclose(file); + while (fread(buffer, 1, sizeof(buffer), file) > 0) + ; + fclose(file); } } @@ -185,157 +184,151 @@ ReadSystemFiles(void) dwReadEvery = dwNumFiles / 10; if (dwReadEvery == 0) - dwReadEvery = 1; // less than 10 files + dwReadEvery = 1; // less than 10 files dwNumFiles = 0; EnumSystemFiles(ReadFiles); } -void RNG_SystemInfoForRNG(void) +void +RNG_SystemInfoForRNG(void) { - unsigned long *plong = 0; - PTIB ptib; - PPIB ppib; - APIRET rc = NO_ERROR; - DATETIME dt; - COUNTRYCODE cc = {0}; - COUNTRYINFO ci = {0}; - unsigned long actual = 0; - char path[_MAX_PATH]=""; - char fullpath[_MAX_PATH]=""; - unsigned long pathlength = sizeof(path); - FSALLOCATE fsallocate; - FILESTATUS3 fstatus; - unsigned long defaultdrive = 0; - unsigned long logicaldrives = 0; - unsigned long sysInfo[QSV_MAX] = {0}; - char buffer[20]; - int nBytes = 0; - - nBytes = RNG_GetNoise(buffer, sizeof(buffer)); - RNG_RandomUpdate(buffer, nBytes); - - /* allocate memory and use address and memory */ - plong = (unsigned long *)malloc(sizeof(*plong)); - RNG_RandomUpdate(&plong, sizeof(plong)); - RNG_RandomUpdate(plong, sizeof(*plong)); - free(plong); - - /* process info */ - rc = DosGetInfoBlocks(&ptib, &ppib); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(ptib, sizeof(*ptib)); - RNG_RandomUpdate(ppib, sizeof(*ppib)); - } - - /* time */ - rc = DosGetDateTime(&dt); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(&dt, sizeof(dt)); - } - - /* country */ - rc = DosQueryCtryInfo(sizeof(ci), &cc, &ci, &actual); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(&cc, sizeof(cc)); - RNG_RandomUpdate(&ci, sizeof(ci)); - RNG_RandomUpdate(&actual, sizeof(actual)); - } - - /* current directory */ - rc = DosQueryCurrentDir(0, path, &pathlength); - strcat(fullpath, "\\"); - strcat(fullpath, path); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(fullpath, strlen(fullpath)); - // path info - rc = DosQueryPathInfo(fullpath, FIL_STANDARD, &fstatus, sizeof(fstatus)); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(&fstatus, sizeof(fstatus)); - } - } - - /* file system info */ - rc = DosQueryFSInfo(0, FSIL_ALLOC, &fsallocate, sizeof(fsallocate)); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(&fsallocate, sizeof(fsallocate)); - } - - /* drive info */ - rc = DosQueryCurrentDisk(&defaultdrive, &logicaldrives); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(&defaultdrive, sizeof(defaultdrive)); - RNG_RandomUpdate(&logicaldrives, sizeof(logicaldrives)); - } - - /* system info */ - rc = DosQuerySysInfo(1L, QSV_MAX, (PVOID)&sysInfo, sizeof(ULONG)*QSV_MAX); - if (rc == NO_ERROR) - { - RNG_RandomUpdate(&sysInfo, sizeof(sysInfo)); - } - - // now let's do some files - ReadSystemFiles(); - - /* more noise */ - nBytes = RNG_GetNoise(buffer, sizeof(buffer)); - RNG_RandomUpdate(buffer, nBytes); + unsigned long *plong = 0; + PTIB ptib; + PPIB ppib; + APIRET rc = NO_ERROR; + DATETIME dt; + COUNTRYCODE cc = { 0 }; + COUNTRYINFO ci = { 0 }; + unsigned long actual = 0; + char path[_MAX_PATH] = ""; + char fullpath[_MAX_PATH] = ""; + unsigned long pathlength = sizeof(path); + FSALLOCATE fsallocate; + FILESTATUS3 fstatus; + unsigned long defaultdrive = 0; + unsigned long logicaldrives = 0; + unsigned long sysInfo[QSV_MAX] = { 0 }; + char buffer[20]; + int nBytes = 0; + + nBytes = RNG_GetNoise(buffer, sizeof(buffer)); + RNG_RandomUpdate(buffer, nBytes); + + /* allocate memory and use address and memory */ + plong = (unsigned long *)malloc(sizeof(*plong)); + RNG_RandomUpdate(&plong, sizeof(plong)); + RNG_RandomUpdate(plong, sizeof(*plong)); + free(plong); + + /* process info */ + rc = DosGetInfoBlocks(&ptib, &ppib); + if (rc == NO_ERROR) { + RNG_RandomUpdate(ptib, sizeof(*ptib)); + RNG_RandomUpdate(ppib, sizeof(*ppib)); + } + + /* time */ + rc = DosGetDateTime(&dt); + if (rc == NO_ERROR) { + RNG_RandomUpdate(&dt, sizeof(dt)); + } + + /* country */ + rc = DosQueryCtryInfo(sizeof(ci), &cc, &ci, &actual); + if (rc == NO_ERROR) { + RNG_RandomUpdate(&cc, sizeof(cc)); + RNG_RandomUpdate(&ci, sizeof(ci)); + RNG_RandomUpdate(&actual, sizeof(actual)); + } + + /* current directory */ + rc = DosQueryCurrentDir(0, path, &pathlength); + strcat(fullpath, "\\"); + strcat(fullpath, path); + if (rc == NO_ERROR) { + RNG_RandomUpdate(fullpath, strlen(fullpath)); + // path info + rc = DosQueryPathInfo(fullpath, FIL_STANDARD, &fstatus, sizeof(fstatus)); + if (rc == NO_ERROR) { + RNG_RandomUpdate(&fstatus, sizeof(fstatus)); + } + } + + /* file system info */ + rc = DosQueryFSInfo(0, FSIL_ALLOC, &fsallocate, sizeof(fsallocate)); + if (rc == NO_ERROR) { + RNG_RandomUpdate(&fsallocate, sizeof(fsallocate)); + } + + /* drive info */ + rc = DosQueryCurrentDisk(&defaultdrive, &logicaldrives); + if (rc == NO_ERROR) { + RNG_RandomUpdate(&defaultdrive, sizeof(defaultdrive)); + RNG_RandomUpdate(&logicaldrives, sizeof(logicaldrives)); + } + + /* system info */ + rc = DosQuerySysInfo(1L, QSV_MAX, (PVOID)&sysInfo, sizeof(ULONG) * QSV_MAX); + if (rc == NO_ERROR) { + RNG_RandomUpdate(&sysInfo, sizeof(sysInfo)); + } + + // now let's do some files + ReadSystemFiles(); + + /* more noise */ + nBytes = RNG_GetNoise(buffer, sizeof(buffer)); + RNG_RandomUpdate(buffer, nBytes); } -void RNG_FileForRNG(const char *filename) +void +RNG_FileForRNG(const char *filename) { struct stat stat_buf; unsigned char buffer[1024]; FILE *file = 0; int nBytes = 0; static int totalFileBytes = 0; - + if (stat((char *)filename, &stat_buf) < 0) - return; + return; + + RNG_RandomUpdate((unsigned char *)&stat_buf, sizeof(stat_buf)); - RNG_RandomUpdate((unsigned char*)&stat_buf, sizeof(stat_buf)); - file = fopen((char *)filename, "r"); - if (file != NULL) - { - for (;;) - { - size_t bytes = fread(buffer, 1, sizeof(buffer), file); - - if (bytes == 0) - break; - - RNG_RandomUpdate(buffer, bytes); - totalFileBytes += bytes; - if (totalFileBytes > 250000) - break; - } - fclose(file); + if (file != NULL) { + for (;;) { + size_t bytes = fread(buffer, 1, sizeof(buffer), file); + + if (bytes == 0) + break; + + RNG_RandomUpdate(buffer, bytes); + totalFileBytes += bytes; + if (totalFileBytes > 250000) + break; + } + fclose(file); } - nBytes = RNG_GetNoise(buffer, 20); + nBytes = RNG_GetNoise(buffer, 20); RNG_RandomUpdate(buffer, nBytes); } -static void rng_systemJitter(void) +static void +rng_systemJitter(void) { dwNumFiles = 0; EnumSystemFiles(ReadOneFile); dwFileToRead++; if (dwFileToRead >= dwNumFiles) { - dwFileToRead = 0; + dwFileToRead = 0; } } -size_t RNG_SystemRNG(void *dest, size_t maxLen) +size_t +RNG_SystemRNG(void *dest, size_t maxLen) { - return rng_systemFromNoise(dest,maxLen); + return rng_systemFromNoise(dest, maxLen); } |