nsprpub/pr/src/md/unix/uxrng.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6
michael@0 7 #include "primpl.h"
michael@0 8
michael@0 9 #include <string.h>
michael@0 10 #include <unistd.h>
michael@0 11 #include <errno.h>
michael@0 12 #include <sys/time.h>
michael@0 13
michael@0 14
michael@0 15 #if defined(SOLARIS)
michael@0 16
michael@0 17 static size_t
michael@0 18 GetHighResClock(void *buf, size_t maxbytes)
michael@0 19 {
michael@0 20 hrtime_t t;
michael@0 21 t = gethrtime();
michael@0 22 if (t) {
michael@0 23 return _pr_CopyLowBits(buf, maxbytes, &t, sizeof(t));
michael@0 24 }
michael@0 25 return 0;
michael@0 26 }
michael@0 27
michael@0 28 #elif defined(HPUX)
michael@0 29
michael@0 30 #ifdef __ia64
michael@0 31 #include <ia64/sys/inline.h>
michael@0 32
michael@0 33 static size_t
michael@0 34 GetHighResClock(void *buf, size_t maxbytes)
michael@0 35 {
michael@0 36 PRUint64 t;
michael@0 37
michael@0 38 #ifdef __GNUC__
michael@0 39 __asm__ __volatile__("mov %0 = ar.itc" : "=r" (t));
michael@0 40 #else
michael@0 41 t = _Asm_mov_from_ar(_AREG44);
michael@0 42 #endif
michael@0 43 return _pr_CopyLowBits(buf, maxbytes, &t, sizeof(t));
michael@0 44 }
michael@0 45 #else
michael@0 46 static size_t
michael@0 47 GetHighResClock(void *buf, size_t maxbytes)
michael@0 48 {
michael@0 49 extern int ret_cr16();
michael@0 50 int cr16val;
michael@0 51
michael@0 52 cr16val = ret_cr16();
michael@0 53 return(_pr_CopyLowBits(buf, maxbytes, &cr16val, sizeof(cr16val)));
michael@0 54 }
michael@0 55 #endif
michael@0 56
michael@0 57 #elif defined(OSF1)
michael@0 58
michael@0 59 #include <c_asm.h>
michael@0 60
michael@0 61 /*
michael@0 62 * Use the "get the cycle counter" instruction on the alpha.
michael@0 63 * The low 32 bits completely turn over in less than a minute.
michael@0 64 * The high 32 bits are some non-counter gunk that changes sometimes.
michael@0 65 */
michael@0 66 static size_t
michael@0 67 GetHighResClock(void *buf, size_t maxbytes)
michael@0 68 {
michael@0 69 unsigned long t;
michael@0 70
michael@0 71 #ifdef __GNUC__
michael@0 72 __asm__("rpcc %0" : "=r" (t));
michael@0 73 #else
michael@0 74 t = asm("rpcc %v0");
michael@0 75 #endif
michael@0 76 return _pr_CopyLowBits(buf, maxbytes, &t, sizeof(t));
michael@0 77 }
michael@0 78
michael@0 79 #elif defined(AIX)
michael@0 80
michael@0 81 static size_t
michael@0 82 GetHighResClock(void *buf, size_t maxbytes)
michael@0 83 {
michael@0 84 return 0;
michael@0 85 }
michael@0 86
michael@0 87 #elif (defined(LINUX) || defined(FREEBSD) || defined(__FreeBSD_kernel__) \
michael@0 88 || defined(NETBSD) || defined(__NetBSD_kernel__) || defined(OPENBSD) \
michael@0 89 || defined(SYMBIAN) || defined(__GNU__))
michael@0 90 #include <sys/types.h>
michael@0 91 #include <sys/stat.h>
michael@0 92 #include <fcntl.h>
michael@0 93
michael@0 94 static int fdDevURandom;
michael@0 95 static PRCallOnceType coOpenDevURandom;
michael@0 96
michael@0 97 static PRStatus OpenDevURandom( void )
michael@0 98 {
michael@0 99 fdDevURandom = open( "/dev/urandom", O_RDONLY );
michael@0 100 return((-1 == fdDevURandom)? PR_FAILURE : PR_SUCCESS );
michael@0 101 } /* end OpenDevURandom() */
michael@0 102
michael@0 103 static size_t GetDevURandom( void *buf, size_t size )
michael@0 104 {
michael@0 105 int bytesIn;
michael@0 106 int rc;
michael@0 107
michael@0 108 rc = PR_CallOnce( &coOpenDevURandom, OpenDevURandom );
michael@0 109 if ( PR_FAILURE == rc ) {
michael@0 110 _PR_MD_MAP_OPEN_ERROR( errno );
michael@0 111 return(0);
michael@0 112 }
michael@0 113
michael@0 114 bytesIn = read( fdDevURandom, buf, size );
michael@0 115 if ( -1 == bytesIn ) {
michael@0 116 _PR_MD_MAP_READ_ERROR( errno );
michael@0 117 return(0);
michael@0 118 }
michael@0 119
michael@0 120 return( bytesIn );
michael@0 121 } /* end GetDevURandom() */
michael@0 122
michael@0 123 static size_t
michael@0 124 GetHighResClock(void *buf, size_t maxbytes)
michael@0 125 {
michael@0 126 return(GetDevURandom( buf, maxbytes ));
michael@0 127 }
michael@0 128
michael@0 129 #elif defined(IRIX)
michael@0 130 #include <fcntl.h>
michael@0 131 #undef PRIVATE
michael@0 132 #include <sys/mman.h>
michael@0 133 #include <sys/syssgi.h>
michael@0 134 #include <sys/immu.h>
michael@0 135 #include <sys/systeminfo.h>
michael@0 136 #include <sys/utsname.h>
michael@0 137
michael@0 138 static size_t GetHighResClock(void *buf, size_t maxbuf)
michael@0 139 {
michael@0 140 unsigned phys_addr, raddr, cycleval;
michael@0 141 static volatile unsigned *iotimer_addr = NULL;
michael@0 142 static int tries = 0;
michael@0 143 static int cntr_size;
michael@0 144 int mfd;
michael@0 145 unsigned s0[2];
michael@0 146
michael@0 147 #ifndef SGI_CYCLECNTR_SIZE
michael@0 148 #define SGI_CYCLECNTR_SIZE 165 /* Size user needs to use to read CC */
michael@0 149 #endif
michael@0 150
michael@0 151 if (iotimer_addr == NULL) {
michael@0 152 if (tries++ > 1) {
michael@0 153 /* Don't keep trying if it didn't work */
michael@0 154 return 0;
michael@0 155 }
michael@0 156
michael@0 157 /*
michael@0 158 ** For SGI machines we can use the cycle counter, if it has one,
michael@0 159 ** to generate some truly random numbers
michael@0 160 */
michael@0 161 phys_addr = syssgi(SGI_QUERY_CYCLECNTR, &cycleval);
michael@0 162 if (phys_addr) {
michael@0 163 int pgsz = getpagesize();
michael@0 164 int pgoffmask = pgsz - 1;
michael@0 165
michael@0 166 raddr = phys_addr & ~pgoffmask;
michael@0 167 mfd = open("/dev/mmem", O_RDONLY);
michael@0 168 if (mfd < 0) {
michael@0 169 return 0;
michael@0 170 }
michael@0 171 iotimer_addr = (unsigned *)
michael@0 172 mmap(0, pgoffmask, PROT_READ, MAP_PRIVATE, mfd, (int)raddr);
michael@0 173 if (iotimer_addr == (unsigned*)-1) {
michael@0 174 close(mfd);
michael@0 175 iotimer_addr = NULL;
michael@0 176 return 0;
michael@0 177 }
michael@0 178 iotimer_addr = (unsigned*)
michael@0 179 ((__psint_t)iotimer_addr | (phys_addr & pgoffmask));
michael@0 180 /*
michael@0 181 * The file 'mfd' is purposefully not closed.
michael@0 182 */
michael@0 183 cntr_size = syssgi(SGI_CYCLECNTR_SIZE);
michael@0 184 if (cntr_size < 0) {
michael@0 185 struct utsname utsinfo;
michael@0 186
michael@0 187 /*
michael@0 188 * We must be executing on a 6.0 or earlier system, since the
michael@0 189 * SGI_CYCLECNTR_SIZE call is not supported.
michael@0 190 *
michael@0 191 * The only pre-6.1 platforms with 64-bit counters are
michael@0 192 * IP19 and IP21 (Challenge, PowerChallenge, Onyx).
michael@0 193 */
michael@0 194 uname(&utsinfo);
michael@0 195 if (!strncmp(utsinfo.machine, "IP19", 4) ||
michael@0 196 !strncmp(utsinfo.machine, "IP21", 4))
michael@0 197 cntr_size = 64;
michael@0 198 else
michael@0 199 cntr_size = 32;
michael@0 200 }
michael@0 201 cntr_size /= 8; /* Convert from bits to bytes */
michael@0 202 }
michael@0 203 }
michael@0 204
michael@0 205 s0[0] = *iotimer_addr;
michael@0 206 if (cntr_size > 4)
michael@0 207 s0[1] = *(iotimer_addr + 1);
michael@0 208 memcpy(buf, (char *)&s0[0], cntr_size);
michael@0 209 return _pr_CopyLowBits(buf, maxbuf, &s0, cntr_size);
michael@0 210 }
michael@0 211
michael@0 212 #elif defined(SCO) || defined(UNIXWARE) || defined(BSDI) || defined(NTO) \
michael@0 213 || defined(QNX) || defined(DARWIN) || defined(RISCOS)
michael@0 214 #include <sys/times.h>
michael@0 215
michael@0 216 static size_t
michael@0 217 GetHighResClock(void *buf, size_t maxbytes)
michael@0 218 {
michael@0 219 int ticks;
michael@0 220 struct tms buffer;
michael@0 221
michael@0 222 ticks=times(&buffer);
michael@0 223 return _pr_CopyLowBits(buf, maxbytes, &ticks, sizeof(ticks));
michael@0 224 }
michael@0 225 #else
michael@0 226 #error! Platform undefined
michael@0 227 #endif /* defined(SOLARIS) */
michael@0 228
michael@0 229 extern PRSize _PR_MD_GetRandomNoise( void *buf, PRSize size )
michael@0 230 {
michael@0 231 struct timeval tv;
michael@0 232 int n = 0;
michael@0 233 int s;
michael@0 234
michael@0 235 n += GetHighResClock(buf, size);
michael@0 236 size -= n;
michael@0 237
michael@0 238 GETTIMEOFDAY(&tv);
michael@0 239
michael@0 240 if ( size > 0 ) {
michael@0 241 s = _pr_CopyLowBits((char*)buf+n, size, &tv.tv_usec, sizeof(tv.tv_usec));
michael@0 242 size -= s;
michael@0 243 n += s;
michael@0 244 }
michael@0 245 if ( size > 0 ) {
michael@0 246 s = _pr_CopyLowBits((char*)buf+n, size, &tv.tv_sec, sizeof(tv.tv_usec));
michael@0 247 size -= s;
michael@0 248 n += s;
michael@0 249 }
michael@0 250
michael@0 251 return n;
michael@0 252 } /* end _PR_MD_GetRandomNoise() */

mercurial