Changeset 3977
- Timestamp:
- 12/27/11 19:48:29 (5 months ago)
- Files:
-
- branches/freewrt_1_0/package/rdate/Makefile (modified) (1 diff)
- branches/freewrt_1_0/package/rdate/arc4random.c (modified) (14 diffs)
- trunk/freewrt/package/rdate/Makefile (modified) (1 diff)
- trunk/freewrt/toolchain/uClibc/files/arc4random.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/freewrt_1_0/package/rdate/Makefile
r3894 r3977 8 8 9 9 PKG_NAME= rdate 10 PKG_VERSION= 20 09080210 PKG_VERSION= 20100805 11 11 PKG_RELEASE= 1 12 PKG_MD5SUM= a8fa4550b5a77cff6db1ed0a9d8aa35712 PKG_MD5SUM= 69f5e1e0f08c300d8a211acc2b2cee5f 13 13 PKG_SOURCE_URL= http://www.mirbsd.org/MirOS/dist/mir/rdate/ 14 15 WRKSRC= ${WRKDIR}/rdate 14 16 15 17 include $(TOPDIR)/mk/package.mk branches/freewrt_1_0/package/rdate/arc4random.c
r3894 r3977 1 1 static const char __vcsid[] = "@(#) MirOS contributed arc4random.c (old)" 2 "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1. 19 2009/09/27 10:45:56tg Exp $"2 "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.28 2010/09/12 12:24:27 tg Exp $" 3 3 ; 4 4 … … 33 33 34 34 /*- 35 * Copyright (c) 2008, 2009 35 * Copyright (c) 2008, 2009, 2010 36 36 * Thorsten Glaser <tg@mirbsd.org> 37 37 * This is arc4random(3) made more portable, … … 117 117 118 118 static uint8_t arc4_getbyte(void); 119 static void stir_finish( int);119 static void stir_finish(uint8_t); 120 120 static void arc4_atexit(void); 121 121 static char arc4_writeback(uint8_t *, size_t, char); … … 125 125 void arc4random_addrandom(u_char *, int); 126 126 void arc4random_stir(void); 127 #if def USE_MS_CRYPTOAPI127 #if defined(USE_MS_CRYPTOAPI) || defined(OPPORTUNISTIC_ROOT_PUSHB) 128 128 uint32_t arc4random_pushb(const void *, size_t); 129 129 #endif 130 #endif 131 132 #define NEED_UNIFORM_BUF_PROTO 133 #if defined(__OpenBSD__) && defined(OpenBSD) && (OpenBSD > 200805) 134 #undef NEED_UNIFORM_BUF_PROTO 135 #elif defined(__MirBSD__) && defined(MirBSD) && (MirBSD > 0x0AA4) 136 #undef NEED_UNIFORM_BUF_PROTO 137 #endif 138 139 #ifdef NEED_UNIFORM_BUF_PROTO 140 u_int32_t arc4random_uniform(u_int32_t); 141 void arc4random_buf(void *, size_t); 130 142 #endif 131 143 … … 142 154 143 155 static void 144 arc4_addrandom( u_char *dat, int datlen)145 { 146 int n;156 arc4_addrandom(const u_char *dat, size_t datlen) 157 { 158 size_t n = 0; 147 159 uint8_t si; 148 160 149 161 arc4_ctx.i--; 150 for (n = 0; n < 256; n++) {162 while (n < 256) { 151 163 arc4_ctx.i++; 152 164 si = arc4_ctx.s[arc4_ctx.i]; 153 arc4_ctx.j = (uint8_t)(arc4_ctx.j + si + dat[n % datlen]);165 arc4_ctx.j = (uint8_t)(arc4_ctx.j + si + dat[n++ % datlen]); 154 166 arc4_ctx.s[arc4_ctx.i] = arc4_ctx.s[arc4_ctx.j]; 155 167 arc4_ctx.s[arc4_ctx.j] = si; 156 168 } 169 arc4_ctx.i++; 157 170 arc4_ctx.j = arc4_ctx.i; 158 171 } 172 173 #if defined(USE_MS_CRYPTOAPI) 174 #define RNDEV_BYTES 128 175 #elif defined(__INTERIX) 176 #define RNDEV_BYTES 4 /* slow /dev/urandom */ 177 #elif defined(__OpenBSD__) 178 #define RNDEV_BYTES (256 - (sizeof(struct timeval) + sizeof(pid_t))) 179 #elif defined(__CYGWIN__) 180 #define RNDEV_BYTES 64 /* /dev/urandom probably CryptoAPI */ 181 #elif defined(__FreeBSD__) 182 #define RNDEV_BYTES 16 /* Yarrow has few state */ 183 #elif defined(__GLIBC__) 184 #define RNDEV_BYTES 16 /* requested by maintainers */ 185 #else 186 #define RNDEV_BYTES 8 /* unknown OS? */ 187 #endif 159 188 160 189 static void … … 165 194 struct timeval tv; 166 195 pid_t pid; 167 u_int rnd[( 128 - (sizeof(struct timeval) + sizeof(pid_t))) / sizeof(u_int)];196 u_int rnd[(RNDEV_BYTES + sizeof(u_int) - 1) / sizeof(u_int)]; 168 197 } rdat; 169 198 size_t sz = 0; … … 233 262 234 263 static void 235 stir_finish( int av)264 stir_finish(uint8_t av) 236 265 { 237 266 size_t n; … … 246 275 * We also discard a randomly fuzzed amount. 247 276 */ 248 n = 256 * 4 + (arc4_getbyte() & 0x0FU); 249 while (av) { 250 n += (av & 0x0F); 251 av >>= 4; 252 } 277 n = 256 * 4 + (arc4_getbyte() & 0x0FU) + (av & 0xF0U); 278 av &= 0x0FU; 253 279 while (n--) 254 280 arc4_getbyte(); 255 while ( n < sizeof(tb))256 tb[n ++] = arc4_getbyte();281 while (++n < sizeof(tb)) 282 tb[n] = arc4_getbyte(); 257 283 if (arc4_writeback(tb, sizeof(tb), 0)) 258 284 arc4_getbyte(); 259 arc4_count = 400000; 285 while (av--) 286 arc4_getbyte(); 287 arc4_count = 1600000; 260 288 } 261 289 … … 307 335 arc4random(void) 308 336 { 309 if (--arc4_count == 0 || !rs_initialized || arc4_stir_pid != getpid()) 337 arc4_count -= 4; 338 if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid()) 310 339 arc4random_stir(); 311 340 return arc4_getword(); … … 454 483 } 455 484 456 #if defined(USE_MS_CRYPTOAPI) || defined(arc4random_pushk) 485 #if defined(USE_MS_CRYPTOAPI) || defined(arc4random_pushk) || \ 486 defined(OPPORTUNISTIC_ROOT_PUSHB) 457 487 uint32_t 458 488 arc4random_pushb(const void *src, size_t len) … … 461 491 union { 462 492 uint8_t buf[256]; 463 struct timeval tv; 493 struct { 494 struct timeval tv; 495 const void *sp, *dp; 496 size_t sz; 497 uint32_t vu; 498 } s; 464 499 uint32_t xbuf; 465 500 } idat; 466 const uint8_t *cbuf = (const uint8_t *)src;467 501 uint32_t res = 1; 468 502 … … 472 506 } 473 507 474 gettimeofday(&idat.tv, NULL); 475 for (rlen = 0; rlen < len; ++rlen) 476 idat.buf[rlen % sizeof(idat)] ^= cbuf[rlen]; 477 rlen = MIN(sizeof(idat), MAX(sizeof(struct timeval), len)); 478 479 if (arc4_writeback(&idat.buf[0], rlen, 1)) 508 idat.s.sp = &idat; 509 idat.s.dp = src; 510 idat.s.sz = len; 511 idat.s.vu = arc4_getword(); 512 gettimeofday(&idat.s.tv, NULL); 513 514 rlen = MAX(sizeof(idat.s), len); 515 while (rlen--) 516 idat.buf[rlen % sizeof(idat.buf)] ^= 517 ((const uint8_t *)src)[rlen % len]; 518 rlen = MIN(sizeof(idat), MAX(sizeof(idat.s), len)); 519 520 if (arc4_writeback((void *)&idat, rlen, 1)) 480 521 res = 0; 481 arc4_addrandom(&idat.buf[0], rlen); 522 arc4_addrandom((void *)&idat, rlen); 523 rlen = arc4_getbyte() & 1; 482 524 if (res) 483 525 res = idat.xbuf; … … 485 527 /* we got entropy from the kernel, so consider us stirred */ 486 528 stir_finish(idat.buf[5]); 529 if (rlen) 530 (void)arc4_getbyte(); 487 531 return (res ^ arc4_getword()); 488 532 } … … 506 550 arc4_writeback((uint8_t *)&buf, sizeof(buf), 0); 507 551 } 552 553 void 554 arc4random_buf(void *_buf, size_t n) 555 { 556 uint8_t *buf = (uint8_t *)_buf; 557 558 if (!rs_initialized || arc4_stir_pid != getpid()) 559 arc4random_stir(); 560 buf[0] = arc4_getbyte() % 3; 561 while (buf[0]--) 562 (void)arc4_getbyte(); 563 while (n--) { 564 if (--arc4_count <= 0) 565 arc4_stir(); 566 buf[n] = arc4_getbyte(); 567 } 568 } 569 570 /*- 571 * Written by Damien Miller. 572 * With simplifications by Jinmei Tatuya. 573 */ 574 575 /* 576 * Calculate a uniformly distributed random number less than 577 * upper_bound avoiding "modulo bias". 578 * 579 * Uniformity is achieved by generating new random numbers 580 * until the one returned is outside the range 581 * [0, 2^32 % upper_bound[. This guarantees the selected 582 * random number will be inside the range 583 * [2^32 % upper_bound, 2^32[ which maps back to 584 * [0, upper_bound[ after reduction modulo upper_bound. 585 */ 586 uint32_t 587 arc4random_uniform(uint32_t upper_bound) 588 { 589 uint32_t r, min; 590 591 if (upper_bound < 2) 592 return (0); 593 594 #if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL) 595 min = 0x100000000UL % upper_bound; 596 #else 597 /* calculate (2^32 % upper_bound) avoiding 64-bit math */ 598 if (upper_bound > 0x80000000U) 599 /* 2^32 - upper_bound (only one "value area") */ 600 min = 1 + ~upper_bound; 601 else 602 /* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */ 603 min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound; 604 #endif 605 606 /* 607 * This could theoretically loop forever but each retry has 608 * p > 0.5 (worst case, usually far better) of selecting a 609 * number inside the range we need, so it should rarely need 610 * to re-roll (at all). 611 */ 612 arc4_count -= 4; 613 if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0) 614 arc4random_stir(); 615 if (arc4_getbyte() & 1) 616 (void)arc4_getbyte(); 617 do { 618 r = arc4_getword(); 619 } while (r < min); 620 621 return (r % upper_bound); 622 } trunk/freewrt/package/rdate/Makefile
r3905 r3977 8 8 9 9 PKG_NAME= rdate 10 PKG_VERSION= 20 09112210 PKG_VERSION= 20100805 11 11 PKG_RELEASE= 1 12 PKG_MD5SUM= 4914cdcf82cc6e8b30248fd6ea9e827f12 PKG_MD5SUM= 69f5e1e0f08c300d8a211acc2b2cee5f 13 13 MASTER_SITES= ${MASTER_SITE_MIRBSD:distfiles/=dist/mir/rdate/} 14 14 WRKDIST= ${WRKDIR}/${PKG_NAME} trunk/freewrt/toolchain/uClibc/files/arc4random.c
r3909 r3977 1 1 static const char __vcsid[] = "@(#) MirOS contributed arc4random.c (old)" 2 "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.2 4 2009/11/29 18:24:21tg Exp $"2 "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.28 2010/09/12 12:24:27 tg Exp $" 3 3 ; 4 4 … … 33 33 34 34 /*- 35 * Copyright (c) 2008, 2009 35 * Copyright (c) 2008, 2009, 2010 36 36 * Thorsten Glaser <tg@mirbsd.org> 37 37 * This is arc4random(3) made more portable, … … 167 167 arc4_ctx.s[arc4_ctx.j] = si; 168 168 } 169 arc4_ctx.i++; 169 170 arc4_ctx.j = arc4_ctx.i; 170 171 } … … 278 279 while (n--) 279 280 arc4_getbyte(); 280 while ( n < sizeof(tb))281 tb[n ++] = arc4_getbyte();281 while (++n < sizeof(tb)) 282 tb[n] = arc4_getbyte(); 282 283 if (arc4_writeback(tb, sizeof(tb), 0)) 283 284 arc4_getbyte(); … … 567 568 } 568 569 570 /*- 571 * Written by Damien Miller. 572 * With simplifications by Jinmei Tatuya. 573 */ 574 569 575 /* 570 * Calculate a uniformly distributed random number less than upper_bound571 * avoiding "modulo bias".576 * Calculate a uniformly distributed random number less than 577 * upper_bound avoiding "modulo bias". 572 578 * 573 * Uniformity is achieved by generating new random numbers until the one 574 * returned is outside the range [0, 2**32 % upper_bound). This 575 * guarantees the selected random number will be inside 576 * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound) 577 * after reduction modulo upper_bound. 579 * Uniformity is achieved by generating new random numbers 580 * until the one returned is outside the range 581 * [0, 2^32 % upper_bound[. This guarantees the selected 582 * random number will be inside the range 583 * [2^32 % upper_bound, 2^32[ which maps back to 584 * [0, upper_bound[ after reduction modulo upper_bound. 578 585 */ 579 u _int32_t580 arc4random_uniform(u _int32_t upper_bound)581 { 582 u _int32_t r, min;586 uint32_t 587 arc4random_uniform(uint32_t upper_bound) 588 { 589 uint32_t r, min; 583 590 584 591 if (upper_bound < 2) 585 592 return (0); 586 593 587 #if defined(ULONG_MAX) && (ULONG_MAX > 0x ffffffffUL)594 #if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL) 588 595 min = 0x100000000UL % upper_bound; 589 596 #else 590 /* Calculate (2**32 % upper_bound) avoiding 64-bit math */591 if (upper_bound > 0x80000000 )592 min = 1 + ~upper_bound; /* 2**32 - upper_bound*/593 else {594 /* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */595 min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;596 }597 /* calculate (2^32 % upper_bound) avoiding 64-bit math */ 598 if (upper_bound > 0x80000000U) 599 /* 2^32 - upper_bound (only one "value area") */ 600 min = 1 + ~upper_bound; 601 else 602 /* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */ 603 min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound; 597 604 #endif 598 605 … … 601 608 * p > 0.5 (worst case, usually far better) of selecting a 602 609 * number inside the range we need, so it should rarely need 603 * to re-roll .610 * to re-roll (at all). 604 611 */ 605 if (!rs_initialized || arc4_stir_pid != getpid()) 612 arc4_count -= 4; 613 if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0) 606 614 arc4random_stir(); 607 615 if (arc4_getbyte() & 1) 608 616 (void)arc4_getbyte(); 609 for (;;) { 610 arc4_count -= 4; 611 if (arc4_count <= 0) 612 arc4random_stir(); 617 do { 613 618 r = arc4_getword(); 614 if (r >= min) 615 break; 616 } 619 } while (r < min); 617 620 618 621 return (r % upper_bound);


