Changeset 3977

Show
Ignore:
Timestamp:
12/27/11 19:48:29 (5 months ago)
Author:
tg
Message:

another long overdue update from MirBSD

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/freewrt_1_0/package/rdate/Makefile

    r3894 r3977  
    88 
    99PKG_NAME=               rdate 
    10 PKG_VERSION=            20090802 
     10PKG_VERSION=            20100805 
    1111PKG_RELEASE=            1 
    12 PKG_MD5SUM=             a8fa4550b5a77cff6db1ed0a9d8aa357 
     12PKG_MD5SUM=             69f5e1e0f08c300d8a211acc2b2cee5f 
    1313PKG_SOURCE_URL=         http://www.mirbsd.org/MirOS/dist/mir/rdate/ 
     14 
     15WRKSRC=                 ${WRKDIR}/rdate 
    1416 
    1517include $(TOPDIR)/mk/package.mk 
  • branches/freewrt_1_0/package/rdate/arc4random.c

    r3894 r3977  
    11static 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:56 tg Exp $" 
     2    "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.28 2010/09/12 12:24:27 tg Exp $" 
    33    ; 
    44 
     
    3333 
    3434/*- 
    35  * Copyright (c) 2008, 2009 
     35 * Copyright (c) 2008, 2009, 2010 
    3636 *      Thorsten Glaser <tg@mirbsd.org> 
    3737 * This is arc4random(3) made more portable, 
     
    117117 
    118118static uint8_t arc4_getbyte(void); 
    119 static void stir_finish(int); 
     119static void stir_finish(uint8_t); 
    120120static void arc4_atexit(void); 
    121121static char arc4_writeback(uint8_t *, size_t, char); 
     
    125125void arc4random_addrandom(u_char *, int); 
    126126void arc4random_stir(void); 
    127 #ifdef USE_MS_CRYPTOAPI 
     127#if defined(USE_MS_CRYPTOAPI) || defined(OPPORTUNISTIC_ROOT_PUSHB) 
    128128uint32_t arc4random_pushb(const void *, size_t); 
    129129#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 
     140u_int32_t arc4random_uniform(u_int32_t); 
     141void arc4random_buf(void *, size_t); 
    130142#endif 
    131143 
     
    142154 
    143155static void 
    144 arc4_addrandom(u_char *dat, int datlen) 
    145 { 
    146         int n
     156arc4_addrandom(const u_char *dat, size_t datlen) 
     157{ 
     158        size_t n = 0
    147159        uint8_t si; 
    148160 
    149161        arc4_ctx.i--; 
    150         for (n = 0; n < 256; n++) { 
     162        while (n < 256) { 
    151163                arc4_ctx.i++; 
    152164                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]); 
    154166                arc4_ctx.s[arc4_ctx.i] = arc4_ctx.s[arc4_ctx.j]; 
    155167                arc4_ctx.s[arc4_ctx.j] = si; 
    156168        } 
     169        arc4_ctx.i++; 
    157170        arc4_ctx.j = arc4_ctx.i; 
    158171} 
     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 
    159188 
    160189static void 
     
    165194                struct timeval tv; 
    166195                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)]; 
    168197        } rdat; 
    169198        size_t sz = 0; 
     
    233262 
    234263static void 
    235 stir_finish(int av) 
     264stir_finish(uint8_t av) 
    236265{ 
    237266        size_t n; 
     
    246275         * We also discard a randomly fuzzed amount. 
    247276         */ 
    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; 
    253279        while (n--) 
    254280                arc4_getbyte(); 
    255         while (n < sizeof(tb)) 
    256                 tb[n++] = arc4_getbyte(); 
     281        while (++n < sizeof(tb)) 
     282                tb[n] = arc4_getbyte(); 
    257283        if (arc4_writeback(tb, sizeof(tb), 0)) 
    258284                arc4_getbyte(); 
    259         arc4_count = 400000; 
     285        while (av--) 
     286                arc4_getbyte(); 
     287        arc4_count = 1600000; 
    260288} 
    261289 
     
    307335arc4random(void) 
    308336{ 
    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()) 
    310339                arc4random_stir(); 
    311340        return arc4_getword(); 
     
    454483} 
    455484 
    456 #if defined(USE_MS_CRYPTOAPI) || defined(arc4random_pushk) 
     485#if defined(USE_MS_CRYPTOAPI) || defined(arc4random_pushk) || \ 
     486    defined(OPPORTUNISTIC_ROOT_PUSHB) 
    457487uint32_t 
    458488arc4random_pushb(const void *src, size_t len) 
     
    461491        union { 
    462492                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; 
    464499                uint32_t xbuf; 
    465500        } idat; 
    466         const uint8_t *cbuf = (const uint8_t *)src; 
    467501        uint32_t res = 1; 
    468502 
     
    472506        } 
    473507 
    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)) 
    480521                res = 0; 
    481         arc4_addrandom(&idat.buf[0], rlen); 
     522        arc4_addrandom((void *)&idat, rlen); 
     523        rlen = arc4_getbyte() & 1; 
    482524        if (res) 
    483525                res = idat.xbuf; 
     
    485527                /* we got entropy from the kernel, so consider us stirred */ 
    486528                stir_finish(idat.buf[5]); 
     529        if (rlen) 
     530                (void)arc4_getbyte(); 
    487531        return (res ^ arc4_getword()); 
    488532} 
     
    506550        arc4_writeback((uint8_t *)&buf, sizeof(buf), 0); 
    507551} 
     552 
     553void 
     554arc4random_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 */ 
     586uint32_t 
     587arc4random_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  
    88 
    99PKG_NAME=               rdate 
    10 PKG_VERSION=            20091122 
     10PKG_VERSION=            20100805 
    1111PKG_RELEASE=            1 
    12 PKG_MD5SUM=             4914cdcf82cc6e8b30248fd6ea9e827
     12PKG_MD5SUM=             69f5e1e0f08c300d8a211acc2b2cee5
    1313MASTER_SITES=           ${MASTER_SITE_MIRBSD:distfiles/=dist/mir/rdate/} 
    1414WRKDIST=                ${WRKDIR}/${PKG_NAME} 
  • trunk/freewrt/toolchain/uClibc/files/arc4random.c

    r3909 r3977  
    11static const char __vcsid[] = "@(#) MirOS contributed arc4random.c (old)" 
    2     "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.24 2009/11/29 18:24:21 tg Exp $" 
     2    "\n @(#)rcsid_master: $MirOS: contrib/code/Snippets/arc4random.c,v 1.28 2010/09/12 12:24:27 tg Exp $" 
    33    ; 
    44 
     
    3333 
    3434/*- 
    35  * Copyright (c) 2008, 2009 
     35 * Copyright (c) 2008, 2009, 2010 
    3636 *      Thorsten Glaser <tg@mirbsd.org> 
    3737 * This is arc4random(3) made more portable, 
     
    167167                arc4_ctx.s[arc4_ctx.j] = si; 
    168168        } 
     169        arc4_ctx.i++; 
    169170        arc4_ctx.j = arc4_ctx.i; 
    170171} 
     
    278279        while (n--) 
    279280                arc4_getbyte(); 
    280         while (n < sizeof(tb)) 
    281                 tb[n++] = arc4_getbyte(); 
     281        while (++n < sizeof(tb)) 
     282                tb[n] = arc4_getbyte(); 
    282283        if (arc4_writeback(tb, sizeof(tb), 0)) 
    283284                arc4_getbyte(); 
     
    567568} 
    568569 
     570/*- 
     571 * Written by Damien Miller. 
     572 * With simplifications by Jinmei Tatuya. 
     573 */ 
     574 
    569575/* 
    570  * Calculate a uniformly distributed random number less than upper_bound 
    571  * avoiding "modulo bias". 
     576 * Calculate a uniformly distributed random number less than 
     577 * upper_bound avoiding "modulo bias". 
    572578 * 
    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. 
    578585 */ 
    579 u_int32_t 
    580 arc4random_uniform(u_int32_t upper_bound) 
    581 { 
    582         u_int32_t r, min; 
     586uint32_t 
     587arc4random_uniform(uint32_t upper_bound) 
     588{ 
     589        uint32_t r, min; 
    583590 
    584591        if (upper_bound < 2) 
    585592                return (0); 
    586593 
    587 #if defined(ULONG_MAX) && (ULONG_MAX > 0xffffffffUL) 
     594#if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL) 
    588595        min = 0x100000000UL % upper_bound; 
    589596#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; 
    597604#endif 
    598605 
     
    601608         * p > 0.5 (worst case, usually far better) of selecting a 
    602609         * number inside the range we need, so it should rarely need 
    603          * to re-roll
     610         * to re-roll (at all)
    604611         */ 
    605         if (!rs_initialized || arc4_stir_pid != getpid()) 
     612        arc4_count -= 4; 
     613        if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0) 
    606614                arc4random_stir(); 
    607615        if (arc4_getbyte() & 1) 
    608616                (void)arc4_getbyte(); 
    609         for (;;) { 
    610                 arc4_count -= 4; 
    611                 if (arc4_count <= 0) 
    612                         arc4random_stir(); 
     617        do { 
    613618                r = arc4_getword(); 
    614                 if (r >= min) 
    615                         break; 
    616         } 
     619        } while (r < min); 
    617620 
    618621        return (r % upper_bound);