diff -r 5b3ca7b7c9dd content/html/content/src/nsFormSubmission.cpp --- a/content/html/content/src/nsFormSubmission.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/content/html/content/src/nsFormSubmission.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -71,6 +71,7 @@ #include "nsIStringBundle.h" #include "nsCExternalHandlerService.h" #include "nsIFileStreams.h" +#include "prrng.h" static void SendJSWarning(nsIDocument* aDocument, @@ -411,9 +412,9 @@ do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1"); mBoundary.AssignLiteral("---------------------------"); - mBoundary.AppendInt(rand()); - mBoundary.AppendInt(rand()); - mBoundary.AppendInt(rand()); + mBoundary.AppendInt(PR_GetRandom32()); + mBoundary.AppendInt(PR_GetRandom32()); + mBoundary.AppendInt(PR_GetRandom32()); } nsFSMultipartFormData::~nsFSMultipartFormData() diff -r 5b3ca7b7c9dd js/src/jscntxt.h --- a/js/src/jscntxt.h Wed Apr 07 18:25:26 2010 +0300 +++ b/js/src/jscntxt.h Wed Apr 07 23:11:46 2010 +0300 @@ -1380,7 +1380,9 @@ uintN resolveFlags; /* Random number generator state, used by jsmath.cpp. */ - int64 rngSeed; + uint32 salsa20_out[16]; + uint32 salsa20_in[16]; + uint32 salsa20_nrints; #ifdef JS_TRACER /* diff -r 5b3ca7b7c9dd js/src/jsmath.cpp --- a/js/src/jsmath.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/js/src/jsmath.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -41,6 +41,11 @@ * JS math package. */ #include +#include +#include +#include +#include +#include #include "jstypes.h" #include "jsstdint.h" #include "jslong.h" @@ -431,43 +436,104 @@ static const jsdouble RNG_DSCALE = jsdouble(1LL << 53); /* - * Math.random() support, lifted from java.util.Random.java. + * Math.random() support */ -static inline void -random_setSeed(JSContext *cx, int64 seed) +#define ROTATE(v,c) (((v) << (c)) | ((v) >> (32 - (c)))) +#define XOR(v,w) ((v) ^ (w)) +#define PLUS(v,w) (((v) + (w))) +#define PLUSONE(v) (PLUS((v),1)) + +#define QUARTERROUND(a,b,c,d) \ + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \ + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \ + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \ + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7); + +static void salsa20(void *__out, void *__in) { - cx->rngSeed = (seed ^ RNG_MULTIPLIER) & RNG_MASK; + int i; + uint32_t x[16]; + uint32_t *input = (uint32_t*)__in; + uint32_t *output32 = (uint32_t*)__out; + + for (i = 0;i < 16;++i) x[i] = input[i]; + for (i = 8;i > 0;i -= 2) { + QUARTERROUND( 0, 4, 8,12) + QUARTERROUND( 1, 5, 9,13) + QUARTERROUND( 2, 6,10,14) + QUARTERROUND( 3, 7,11,15) + QUARTERROUND( 0, 5,10,15) + QUARTERROUND( 1, 6,11,12) + QUARTERROUND( 2, 7, 8,13) + QUARTERROUND( 3, 4, 9,14) + } + for (i = 0;i < 16;++i) x[i] = PLUS(x[i],input[i]); + for (i = 0;i < 16;++i) output32[i] = x[i]; +} + +static void salsa_random_init(JSContext *cx) +{ + int fd; + + cx->salsa20_nrints = 0; + fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK | O_CLOEXEC); + if (fd != -1) { + read(fd, cx->salsa20_in, sizeof(cx->salsa20_in)); + close(fd); + } +} + +static uint32 get_random32(JSContext *cx) +{ + uint32 *salsa20_in = cx->salsa20_in; + uint32 ret; + + if (cx->salsa20_nrints == 0) { + if (!++salsa20_in[0]) if (!++salsa20_in[1]) if (!++salsa20_in[2]) if (!++salsa20_in[3]) + if (!++salsa20_in[4]) if (!++salsa20_in[5]) if (!++salsa20_in[6]) if (!++salsa20_in[7]) + if (!++salsa20_in[8]) if (!++salsa20_in[9]) if (!++salsa20_in[10]) if (!++salsa20_in[11]) + if (!++salsa20_in[12]) if (!++salsa20_in[13]) if (!++salsa20_in[14]) ++salsa20_in[15]; + salsa20(cx->salsa20_out, salsa20_in); + cx->salsa20_nrints = 16; + } + + ret = cx->salsa20_out[--(cx->salsa20_nrints)]; + if (cx->salsa20_nrints > 16) { + fprintf(stderr, "cx=%p salsa20_nrints=%u\n", (void*)cx, cx->salsa20_nrints); + cx->salsa20_nrints = 8; + ret = cx->salsa20_out[--(cx->salsa20_nrints)]; + } + return ret; +} + +static inline uint64 get_random64(JSContext *cx) +{ + uint64 ret64; + + ret64 = get_random32(cx); + ret64 <<= 32; + ret64 += get_random32(cx); + return ret64; +} + +static inline double get_randomdouble(JSContext *cx) +{ + uint64 ret64; + + ret64 = get_random64(cx); + return ret64 / (double)(0xFFFFFFFFFFFFFFFFULL); } void js_InitRandom(JSContext *cx) { - /* - * Set the seed from current time. Since we have a RNG per context and we often bring - * up several contexts at the same time, we xor in some additional values, namely - * the context and its successor. We don't just use the context because it might be - * possible to reverse engineer the context pointer if one guesses the time right. - */ - random_setSeed(cx, - (PRMJ_Now() / 1000) ^ - int64(cx) ^ - int64(cx->link.next)); -} - -static inline uint64 -random_next(JSContext *cx, int bits) -{ - uint64 nextseed = cx->rngSeed * RNG_MULTIPLIER; - nextseed += RNG_ADDEND; - nextseed &= RNG_MASK; - cx->rngSeed = nextseed; - return nextseed >> (48 - bits); + salsa_random_init(cx); } static inline jsdouble random_nextDouble(JSContext *cx) { - return jsdouble((random_next(cx, 26) << 27) + random_next(cx, 27)) / RNG_DSCALE; + return get_randomdouble(cx); } static JSBool diff -r 5b3ca7b7c9dd memory/jemalloc/jemalloc.c --- a/memory/jemalloc/jemalloc.c Wed Apr 07 18:25:26 2010 +0300 +++ b/memory/jemalloc/jemalloc.c Wed Apr 07 23:11:46 2010 +0300 @@ -1288,12 +1288,18 @@ WRT_PRINT(p4); #else #if defined(MOZ_MEMORY) && !defined(MOZ_MEMORY_WINDOWS) -#define _write write -#endif - _write(STDERR_FILENO, p1, (unsigned int) strlen(p1)); - _write(STDERR_FILENO, p2, (unsigned int) strlen(p2)); - _write(STDERR_FILENO, p3, (unsigned int) strlen(p3)); - _write(STDERR_FILENO, p4, (unsigned int) strlen(p4)); +#endif + struct iovec iov[4]; + + iov[0].iov_base = (void*)p1; + iov[0].iov_len = strlen(p1); + iov[1].iov_base = (void*)p2; + iov[1].iov_len = strlen(p2); + iov[2].iov_base = (void*)p3; + iov[2].iov_len = strlen(p3); + iov[3].iov_base = (void*)p4; + iov[3].iov_len = strlen(p4); + writev(STDERR_FILENO, iov, 4); #endif } diff -r 5b3ca7b7c9dd netwerk/base/src/nsDownloader.cpp --- a/netwerk/base/src/nsDownloader.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/netwerk/base/src/nsDownloader.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -41,29 +41,28 @@ #include "nsDirectoryServiceUtils.h" #include "nsDirectoryServiceDefs.h" #include "nsNetUtil.h" +#include "prrng.h" // XXX this code is ripped from profile/src/nsProfile.cpp and is further // duplicated in uriloader/exthandler. this should probably be moved // into xpcom or some other shared library. #include -#define TABLE_SIZE 36 -static const char table[] = - { 'a','b','c','d','e','f','g','h','i','j', - 'k','l','m','n','o','p','q','r','s','t', - 'u','v','w','x','y','z','0','1','2','3', - '4','5','6','7','8','9' }; + +static const unsigned char table[] = { + 'a','b','c','d','e','f','g','h','i','j', + 'k','l','m','n','o','p','q','r','s','t', + 'u','v','w','x','y','z','0','1','2','3', + '4','5','6','7','8','9' +}; + static void -MakeRandomString(char *buf, PRInt32 bufLen) +MakeRandomString(unsigned char *buf, PRInt32 bufLen) { - // turn PR_Now() into milliseconds since epoch - // and salt rand with that. - double fpTime; - LL_L2D(fpTime, PR_Now()); - srand((uint)(fpTime * 1e-6 + 0.5)); // use 1e-6, granularity of PR_Now() on the mac is seconds - PRInt32 i; - for (i=0;iAppendNative(nsDependentCString(buf, 12)); if (NS_FAILED(rv)) return rv; diff -r 5b3ca7b7c9dd netwerk/protocol/http/src/nsHttpDigestAuth.cpp --- a/netwerk/protocol/http/src/nsHttpDigestAuth.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/netwerk/protocol/http/src/nsHttpDigestAuth.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -56,6 +56,7 @@ #include "prprf.h" #include "prmem.h" #include "nsCRT.h" +#include "prrng.h" //----------------------------------------------------------------------------- // nsHttpDigestAuth @@ -322,7 +323,7 @@ nsCAutoString cnonce; static const char hexChar[] = "0123456789abcdef"; for (int i=0; i<16; ++i) { - cnonce.Append(hexChar[(int)(15.0 * rand()/(RAND_MAX + 1.0))]); + cnonce.Append(hexChar[PR_GetRandom32() % sizeof(hexChar)]); } LOG((" cnonce=%s\n", cnonce.get())); diff -r 5b3ca7b7c9dd nsprpub/pr/include/private/primpl.h --- a/nsprpub/pr/include/private/primpl.h Wed Apr 07 18:25:26 2010 +0300 +++ b/nsprpub/pr/include/private/primpl.h Wed Apr 07 23:11:46 2010 +0300 @@ -2116,6 +2116,8 @@ extern PRSize _PR_MD_GetRandomNoise( void *buf, PRSize size ); #define _PR_MD_GET_RANDOM_NOISE(buf,size) _PR_MD_GetRandomNoise((buf),(size)) extern PRSize _pr_CopyLowBits( void *dest, PRSize dstlen, void *src, PRSize srclen ); +extern PRUint32 PR_GetRandom32(void); +extern PRUint64 PR_GetRandom64(void); /* end PR_GetRandomNoise() related */ diff -r 5b3ca7b7c9dd nsprpub/pr/include/prrng.h --- a/nsprpub/pr/include/prrng.h Wed Apr 07 18:25:26 2010 +0300 +++ b/nsprpub/pr/include/prrng.h Wed Apr 07 23:11:46 2010 +0300 @@ -101,6 +101,14 @@ PRSize size ); +NSPR_API(PRUint32) PR_GetRandom32( + void +); + +NSPR_API(PRUint64) PR_GetRandom64( + void +); + PR_END_EXTERN_C #endif /* prrng_h___ */ diff -r 5b3ca7b7c9dd nsprpub/pr/src/md/unix/uxrng.c --- a/nsprpub/pr/src/md/unix/uxrng.c Wed Apr 07 18:25:26 2010 +0300 +++ b/nsprpub/pr/src/md/unix/uxrng.c Wed Apr 07 23:11:46 2010 +0300 @@ -42,7 +42,7 @@ #include #include #include - +#include #if defined(SOLARIS) @@ -298,27 +298,105 @@ #error! Platform undefined #endif /* defined(SOLARIS) */ + +#include +#include +#include +#include +#include + +#define ROTATE(v,c) (((v) << (c)) | ((v) >> (32 - (c)))) +#define XOR(v,w) ((v) ^ (w)) +#define PLUS(v,w) (((v) + (w))) +#define PLUSONE(v) (PLUS((v),1)) + +#define QUARTERROUND(a,b,c,d) \ + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \ + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \ + x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \ + x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7); + +static void salsa20(void * __out, void *__in) +{ + int i; + PRUint32 x[16]; + PRUint32 *in = __in; + PRUint32 *out = __out; + + for (i = 0;i < 16;++i) x[i] = in[i]; + for (i = 8;i > 0;i -= 2) { + QUARTERROUND( 0, 4, 8,12) + QUARTERROUND( 1, 5, 9,13) + QUARTERROUND( 2, 6,10,14) + QUARTERROUND( 3, 7,11,15) + QUARTERROUND( 0, 5,10,15) + QUARTERROUND( 1, 6,11,12) + QUARTERROUND( 2, 7, 8,13) + QUARTERROUND( 3, 4, 9,14) + } + for (i = 0;i < 16;++i) x[i] = PLUS(x[i],in[i]); + for (i = 0;i < 16;++i) out[i] = x[i]; +} + +static PRUint32 get_random32(void) +{ + static __thread PRUint32 salsa20_out[16]; + static __thread PRUint32 salsa20_in[16]; + PRUint32 ret; + static __thread unsigned int nrints; + static __thread int rndinit; + + if (!rndinit) { + int fd; + + rndinit = 1; + + fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK | O_CLOEXEC); + if (fd != -1) { + read(fd, salsa20_in, sizeof(salsa20_in)); + close(fd); + } + } + + if (nrints == 0) { + if (!++salsa20_in[0]) if (!++salsa20_in[1]) if (!++salsa20_in[2]) if (!++salsa20_in[3]) + if (!++salsa20_in[4]) if (!++salsa20_in[5]) if (!++salsa20_in[6]) if (!++salsa20_in[7]) + if (!++salsa20_in[8]) if (!++salsa20_in[9]) if (!++salsa20_in[10]) if (!++salsa20_in[11]) + if (!++salsa20_in[12]) if (!++salsa20_in[13]) if (!++salsa20_in[14]) ++salsa20_in[15]; + salsa20(salsa20_out, salsa20_in); + nrints = 16; + } + ret = salsa20_out[--nrints]; + return ret; +} + +#define PRMAX(a,b) ({typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; }) +#define PRMIN(a,b) ({typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; }) + extern PRSize _PR_MD_GetRandomNoise( void *buf, PRSize size ) { - struct timeval tv; - int n = 0; - int s; + PRUint8 *ptrend = buf + size; + PRUint8 *ptrstart = buf; + PRUint32 rnd32; + PRSize tocopy; - n += GetHighResClock(buf, size); - size -= n; - - GETTIMEOFDAY(&tv); - - if ( size > 0 ) { - s = _pr_CopyLowBits((char*)buf+n, size, &tv.tv_usec, sizeof(tv.tv_usec)); - size -= s; - n += s; - } - if ( size > 0 ) { - s = _pr_CopyLowBits((char*)buf+n, size, &tv.tv_sec, sizeof(tv.tv_usec)); - size -= s; - n += s; + while (ptrstart < ptrend) { + rnd32 = get_random32(); + tocopy = PRMIN((ptrend - ptrstart), sizeof(rnd32)); + memcpy(ptrstart, &rnd32, tocopy); /* To prevent unaligned access exceptions */ + ptrstart += tocopy; } - return n; + return size; } /* end _PR_MD_GetRandomNoise() */ + +extern PRUint32 PR_GetRandom32(void) +{ + return get_random32(); +} + +extern PRUint64 PR_GetRandom64(void) +{ + return (PRUint64)get_random32() << 32 | get_random32(); +} + diff -r 5b3ca7b7c9dd other-licenses/ply/ply/yacc.py --- a/other-licenses/ply/ply/yacc.py Wed Apr 07 18:25:26 2010 +0300 +++ b/other-licenses/ply/ply/yacc.py Wed Apr 07 23:11:46 2010 +0300 @@ -2702,7 +2702,7 @@ # If running in optimized mode. We're going to read tables instead - if (optimize and lr_read_tables(tabmodule,1)): + if (not optimize and lr_read_tables(tabmodule,1)): # Read parse table del Productions[:] for p in _lr_productions: diff -r 5b3ca7b7c9dd security/nss/cmd/crmf-cgi/crmfcgi.c --- a/security/nss/cmd/crmf-cgi/crmfcgi.c Wed Apr 07 18:25:26 2010 +0300 +++ b/security/nss/cmd/crmf-cgi/crmfcgi.c Wed Apr 07 23:11:46 2010 +0300 @@ -45,6 +45,7 @@ #include "base64.h" #include "secasn1.h" #include "cryptohi.h" +#include "prrng.h" #include #include #include @@ -400,7 +401,7 @@ after = PR_ImplodeTime(&printableTime); validity = CERT_CreateValidity(now, after); newCert = *issuedCert = - CERT_CreateCertificate(rand(), &(issuerCert->subject), validity, + CERT_CreateCertificate(PR_GetRandom32(), &(issuerCert->subject), validity, oldCertReq); if (newCert == NULL) { rv = ERROR_CREATING_NEW_CERTIFICATE; @@ -685,7 +686,7 @@ switch (privKeyChoice) { case crmfSubsequentMessage: challs = &challs[*numChall]; - challs->random = rand(); + challs->random = PR_GetRandom32(); challs->pubKey = CERT_ExtractPublicKey(newCert); if (challs->pubKey == NULL) { rv = ERROR_RETRIEVING_PUB_KEY_FOR_CHALL; diff -r 5b3ca7b7c9dd security/nss/lib/freebl/sysrand.c --- a/security/nss/lib/freebl/sysrand.c Wed Apr 07 18:25:26 2010 +0300 +++ b/security/nss/lib/freebl/sysrand.c Wed Apr 07 23:11:46 2010 +0300 @@ -68,10 +68,6 @@ dest += nbytes; maxLen -= nbytes; - - /* some hw op to try to introduce more entropy into the next - * RNG_GetNoise call */ - rng_systemJitter(); } return retBytes; } diff -r 5b3ca7b7c9dd security/nss/lib/freebl/unix_rand.c --- a/security/nss/lib/freebl/unix_rand.c Wed Apr 07 18:25:26 2010 +0300 +++ b/security/nss/lib/freebl/unix_rand.c Wed Apr 07 23:11:46 2010 +0300 @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -48,7 +47,6 @@ #include "secerr.h" #include "prerror.h" #include "prthread.h" -#include "prprf.h" size_t RNG_FileUpdate(const char *fileName, size_t limit); @@ -284,7 +282,7 @@ } #endif #endif /* Sun */ - + #if defined(__hpux) #include @@ -325,7 +323,7 @@ RNG_RandomUpdate(&si, sizeof(si)); } #endif /* HPUX */ - + #if defined(OSF1) #include #include @@ -368,7 +366,7 @@ } #endif /* Alpha */ - + #if defined(_IBMR2) static size_t GetHighResClock(void *buf, size_t maxbytes) @@ -382,7 +380,7 @@ /* XXX haven't found any yet! */ } #endif /* IBM R2 */ - + #if defined(LINUX) #include @@ -437,6 +435,7 @@ #endif /* NCR */ + #if defined(sgi) #include #undef PRIVATE @@ -556,7 +555,7 @@ return CopyLowBits(buf, maxbuf, &s0, cntr_size); } #endif - + #if defined(sony) #include @@ -694,7 +693,7 @@ } } #endif /* nec_ews */ - + size_t RNG_GetNoise(void *buf, size_t maxbytes) { struct timeval tv; @@ -921,13 +920,7 @@ /* If the user points us to a random file, pass it through the rng */ randfile = getenv("NSRANDFILE"); if ( ( randfile != NULL ) && ( randfile[0] != '\0') ) { - char *randCountString = getenv("NSRANDCOUNT"); - int randCount = randCountString ? atoi(randCountString) : 0; - if (randCount != 0) { - RNG_FileUpdate(randfile, randCount); - } else { - RNG_FileForRNG(randfile); - } + RNG_FileForRNG(randfile); } /* pass other files through */ @@ -1033,148 +1026,22 @@ RNG_FileUpdate(fileName, TOTAL_FILE_LIMIT); } -void ReadSingleFile(const char *fileName) -{ - FILE * file; - unsigned char buffer[BUFSIZ]; - - file = fopen((char *)fileName, "rb"); - if (file != NULL) { - while (fread(buffer, 1, sizeof(buffer), file) > 0) - ; - fclose(file); - } -} - -#define _POSIX_PTHREAD_SEMANTICS -#include - -PRBool -ReadFileOK(char *dir, char *file) -{ - struct stat stat_buf; - char filename[PATH_MAX]; - int count = snprintf(filename, sizeof filename, "%s/%s",dir, file); - - if (count <= 0) { - return PR_FALSE; /* name too long, can't read it anyway */ - } - - if (stat(filename, &stat_buf) < 0) - return PR_FALSE; /* can't stat, probably can't read it then as well */ - return S_ISREG(stat_buf.st_mode) ? PR_TRUE : PR_FALSE; -} - -/* - * read one file out of either /etc or the user's home directory. - * fileToRead tells which file to read. - * - * return 1 if it's time to reset the fileToRead (no more files to read). - */ -int ReadOneFile(int fileToRead) -{ - char *dir = "/etc"; - DIR *fd = opendir(dir); - int resetCount = 0; -#ifdef SOLARIS - /* grumble, Solaris does not define struct dirent to be the full length */ - typedef union { - unsigned char space[sizeof(struct dirent) + MAXNAMELEN]; - struct dirent dir; - } dirent_hack; - dirent_hack entry, firstEntry; - -#define entry_dir entry.dir -#else - struct dirent entry, firstEntry; -#define entry_dir entry -#endif - - int i, error = -1; - - if (fd == NULL) { - dir = getenv("HOME"); - if (dir) { - fd = opendir(dir); - } - } - if (fd == NULL) { - return 1; - } - - for (i=0; i <= fileToRead; i++) { - struct dirent *result = NULL; - do { - error = readdir_r(fd, &entry_dir, &result); - } while (error == 0 && result != NULL && - !ReadFileOK(dir,&result->d_name[0])); - if (error != 0 || result == NULL) { - resetCount = 1; /* read to the end, start again at the beginning */ - if (i != 0) { - /* ran out of entries in the directory, use the first one */ - entry = firstEntry; - error = 0; - break; - } - /* if i== 0, there were no readable entries in the directory */ - break; - } - if (i==0) { - /* save the first entry in case we run out of entries */ - firstEntry = entry; - } - } - - if (error == 0) { - char filename[PATH_MAX]; - int count = snprintf(filename, sizeof filename, - "%s/%s",dir, &entry_dir.d_name[0]); - if (count >= 1) { - ReadSingleFile(filename); - } - } - - closedir(fd); - return resetCount; -} - -/* - * do something to try to introduce more noise into the 'GetNoise' call - */ -static void rng_systemJitter(void) -{ - static int fileToRead = 1; - - if (ReadOneFile(fileToRead)) { - fileToRead = 1; - } else { - fileToRead++; - } -} - size_t RNG_SystemRNG(void *dest, size_t maxLen) { FILE *file; size_t bytes; - size_t fileBytes = 0; - unsigned char *buffer = dest; file = fopen("/dev/urandom", "r"); if (file == NULL) { - return rng_systemFromNoise(dest, maxLen); + PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); + return 0; } - while (maxLen > fileBytes) { - bytes = maxLen - fileBytes; - bytes = fread(buffer, 1, bytes, file); - if (bytes == 0) - break; - fileBytes += bytes; - buffer += bytes; + setbuf(file, NULL); + bytes = fread(dest, 1, maxLen, file); + fclose(file); + if (bytes != maxLen) { + PORT_SetError(SEC_ERROR_NEED_RANDOM); /* system RNG failed */ + return 0; } - fclose(file); - if (fileBytes != maxLen) { - PORT_SetError(SEC_ERROR_NEED_RANDOM); /* system RNG failed */ - fileBytes = 0; - } - return fileBytes; + return bytes; } diff -r 5b3ca7b7c9dd security/nss/lib/pk11wrap/pk11merge.c --- a/security/nss/lib/pk11wrap/pk11merge.c Wed Apr 07 18:25:26 2010 +0300 +++ b/security/nss/lib/pk11wrap/pk11merge.c Wed Apr 07 23:11:46 2010 +0300 @@ -488,7 +488,7 @@ /* set up the input test */ input.data = (unsigned char *)testString; input.len = PK11_GetBlockSize(cryptoMechType, NULL); - if (input.len < 0) { + if ((int)input.len < 0) { rv = SECFailure; goto done; } diff -r 5b3ca7b7c9dd security/nss/lib/ssl/derive.c --- a/security/nss/lib/ssl/derive.c Wed Apr 07 18:25:26 2010 +0300 +++ b/security/nss/lib/ssl/derive.c Wed Apr 07 23:11:46 2010 +0300 @@ -526,7 +526,7 @@ PK11SymKey * ms = NULL; SECItem params = {siBuffer, NULL, 0}; CK_SSL3_MASTER_KEY_DERIVE_PARAMS master_params; - unsigned char rand[SSL3_RANDOM_LENGTH]; + unsigned char pkrand[SSL3_RANDOM_LENGTH]; CK_VERSION pms_version; CK_MECHANISM_TYPE master_derive; CK_MECHANISM_TYPE key_derive; @@ -535,7 +535,7 @@ if (pms == NULL) return(SECFailure); - PORT_Memset(rand, 0, SSL3_RANDOM_LENGTH); + PORT_Memset(pkrand, 0, SSL3_RANDOM_LENGTH); if (isTLS) { if(isDH) master_derive = CKM_TLS_MASTER_KEY_DERIVE_DH; @@ -550,9 +550,9 @@ } master_params.pVersion = &pms_version; - master_params.RandomInfo.pClientRandom = rand; + master_params.RandomInfo.pClientRandom = pkrand; master_params.RandomInfo.ulClientRandomLen = SSL3_RANDOM_LENGTH; - master_params.RandomInfo.pServerRandom = rand; + master_params.RandomInfo.pServerRandom = pkrand; master_params.RandomInfo.ulServerRandomLen = SSL3_RANDOM_LENGTH; params.data = (unsigned char *) &master_params; diff -r 5b3ca7b7c9dd toolkit/crashreporter/client/crashreporter.cpp --- a/toolkit/crashreporter/client/crashreporter.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/toolkit/crashreporter/client/crashreporter.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -382,7 +382,6 @@ bool ShouldEnableSending() { - srand(time(0)); return ((rand() % 100) < MOZ_CRASHREPORTER_ENABLE_PERCENT); } diff -r 5b3ca7b7c9dd toolkit/crashreporter/google-breakpad/src/common/linux/file_id.cc --- a/toolkit/crashreporter/google-breakpad/src/common/linux/file_id.cc Wed Apr 07 18:25:26 2010 +0300 +++ b/toolkit/crashreporter/google-breakpad/src/common/linux/file_id.cc Wed Apr 07 23:11:46 2010 +0300 @@ -42,6 +42,8 @@ #include #include #include +#include +#include #include #include diff -r 5b3ca7b7c9dd toolkit/crashreporter/google-breakpad/src/common/linux/guid_creator.cc --- a/toolkit/crashreporter/google-breakpad/src/common/linux/guid_creator.cc Wed Apr 07 18:25:26 2010 +0300 +++ b/toolkit/crashreporter/google-breakpad/src/common/linux/guid_creator.cc Wed Apr 07 23:11:46 2010 +0300 @@ -34,6 +34,7 @@ #include #include "common/linux/guid_creator.h" +#include "prrng.h" // // GUIDGenerator @@ -46,7 +47,6 @@ class GUIDGenerator { public: GUIDGenerator() { - srandom(time(NULL)); } static u_int32_t BytesToUInt32(const u_int8_t bytes[]) { @@ -64,11 +64,11 @@ } bool CreateGUID(GUID *guid) const { - guid->data1 = random(); - guid->data2 = (u_int16_t)(random()); - guid->data3 = (u_int16_t)(random()); - UInt32ToBytes(&guid->data4[0], random()); - UInt32ToBytes(&guid->data4[4], random()); + guid->data1 = PR_GetRandom32(); + guid->data2 = PR_GetRandom32(); + guid->data3 = PR_GetRandom32(); + UInt32ToBytes(&guid->data4[0], PR_GetRandom32()); + UInt32ToBytes(&guid->data4[4], PR_GetRandom32()); return true; } }; diff -r 5b3ca7b7c9dd toolkit/crashreporter/google-breakpad/src/common/windows/http_upload.cc --- a/toolkit/crashreporter/google-breakpad/src/common/windows/http_upload.cc Wed Apr 07 18:25:26 2010 +0300 +++ b/toolkit/crashreporter/google-breakpad/src/common/windows/http_upload.cc Wed Apr 07 23:11:46 2010 +0300 @@ -247,8 +247,8 @@ static const int kBoundaryLength = 27 + 16 + 1; // Generate some random numbers to fill out the boundary - int r0 = rand(); - int r1 = rand(); + int r0 = PR_GetRandom32(); + int r1 = PR_GetRandom32(); wchar_t temp[kBoundaryLength]; swprintf(temp, kBoundaryLength, L"%s%08X%08X", kBoundaryPrefix, r0, r1); diff -r 5b3ca7b7c9dd toolkit/profile/src/nsToolkitProfileService.cpp --- a/toolkit/profile/src/nsToolkitProfileService.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/toolkit/profile/src/nsToolkitProfileService.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -70,7 +70,7 @@ #include "nsString.h" #include "nsReadableUtils.h" #include "nsNativeCharsetUtils.h" - +#include "prrng.h" class nsToolkitProfile : public nsIToolkitProfile { @@ -613,19 +613,13 @@ static void SaltProfileName(nsACString& aName) { - double fpTime; - LL_L2D(fpTime, PR_Now()); - - // use 1e-6, granularity of PR_Now() on the mac is seconds - srand((uint)(fpTime * 1e-6 + 0.5)); - char salt[9]; int i; for (i = 0; i < 8; ++i) - salt[i] = kTable[rand() % NS_ARRAY_LENGTH(kTable)]; + salt[i] = kTable[PR_GetRandom32() % NS_ARRAY_LENGTH(kTable)]; - salt[8] = '.'; + salt[i] = '.'; aName.Insert(salt, 0, 9); } diff -r 5b3ca7b7c9dd tools/reorder/garope.cpp --- a/tools/reorder/garope.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/tools/reorder/garope.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -164,11 +164,7 @@ static long long llrand() { - long long result; - result = (long long) rand(); - result *= (long long) (unsigned int) (RAND_MAX + 1); - result += (long long) rand(); - return result; + return PR_GetRandom64(); } //---------------------------------------------------------------------- @@ -369,7 +365,7 @@ vector_t::iterator sym = m_ordering.begin(); vector_t::iterator end = m_ordering.end(); for (; sym != end; ++sym) { - int i = rand() % m_ordering.size(); + int i = PR_GetRandom32() % m_ordering.size(); const Elf32_Sym *temp = *sym; *sym = m_ordering[i]; m_ordering[i] = temp; @@ -388,7 +384,7 @@ vector_t::iterator end = m_ordering.end(); for (; sym != end; ++sym, ++parent_sym) { - if (rand() % 2) { + if (PR_GetRandom32() % 2) { *sym = *parent_sym; used[*parent_sym] = 1; } @@ -411,8 +407,8 @@ symbol_order::mutate() { int i, j; - i = rand() % m_ordering.size(); - j = rand() % m_ordering.size(); + i = PR_GetRandom32() % m_ordering.size(); + j = PR_GetRandom32() % m_ordering.size(); const Elf32_Sym *temp = m_ordering[i]; m_ordering[i] = m_ordering[j]; @@ -741,7 +737,7 @@ // Mutate, possibly. if (opt_mutate) { - if (rand() % opt_mutate == 0) + if (PR_GetRandom32() % opt_mutate == 0) kid->mutate(); } } diff -r 5b3ca7b7c9dd xpcom/base/nsUUIDGenerator.cpp --- a/xpcom/base/nsUUIDGenerator.cpp Wed Apr 07 18:25:26 2010 +0300 +++ b/xpcom/base/nsUUIDGenerator.cpp Wed Apr 07 23:11:46 2010 +0300 @@ -75,41 +75,6 @@ // We're a service, so we're guaranteed that Init() is not going // to be reentered while we're inside Init(). - -#if !defined(XP_WIN) && !defined(XP_MACOSX) - /* initialize random number generator using NSPR random noise */ - unsigned int seed; - - PRSize bytes = 0; - while (bytes < sizeof(seed)) { - PRSize nbytes = PR_GetRandomNoise(((unsigned char *)&seed)+bytes, - sizeof(seed)-bytes); - if (nbytes == 0) { - return NS_ERROR_FAILURE; - } - bytes += nbytes; - } - - /* Initialize a new RNG state, and immediately switch - * back to the previous one -- we want to use mState - * only for our own calls to random(). - */ - mSavedState = initstate(seed, mState, sizeof(mState)); - setstate(mSavedState); - - mRBytes = 4; -#ifdef RAND_MAX - if ((unsigned long) RAND_MAX < (unsigned long)0xffffffff) - mRBytes = 3; - if ((unsigned long) RAND_MAX < (unsigned long)0x00ffffff) - mRBytes = 2; - if ((unsigned long) RAND_MAX < (unsigned long)0x0000ffff) - mRBytes = 1; - if ((unsigned long) RAND_MAX < (unsigned long)0x000000ff) - return NS_ERROR_FAILURE; -#endif - -#endif /* non XP_WIN and non XP_MACOSX */ return NS_OK; } @@ -166,30 +131,8 @@ memcpy(id, &bytes, sizeof(nsID)); CFRelease(uuid); -#else /* not windows or OS X; generate randomness using random(). */ - /* XXX we should be saving the return of setstate here and switching - * back to it; instead, we use the value returned when we called - * initstate, since older glibc's have broken setstate() return values - */ - setstate(mState); - - PRSize bytesLeft = sizeof(nsID); - while (bytesLeft > 0) { - long rval = random(); - - PRUint8 *src = (PRUint8*)&rval; - // We want to grab the mRBytes least significant bytes of rval, since - // mRBytes less than sizeof(rval) means the high bytes are 0. -#ifdef IS_BIG_ENDIAN - src += sizeof(rval) - mRBytes; -#endif - PRUint8 *dst = ((PRUint8*) id) + (sizeof(nsID) - bytesLeft); - PRSize toWrite = (bytesLeft < mRBytes ? bytesLeft : mRBytes); - for (PRSize i = 0; i < toWrite; i++) - dst[i] = src[i]; - - bytesLeft -= toWrite; - } +#else + PR_GetRandomNoise(id, sizeof(*id)); /* Put in the version */ id->m2 &= 0x0fff; @@ -198,9 +141,6 @@ /* Put in the variant */ id->m3[0] &= 0x3f; id->m3[0] |= 0x80; - - /* Restore the previous RNG state */ - setstate(mSavedState); #endif return NS_OK;