@@ -1475,39 +1475,47 @@ random_s_rand(int argc, VALUE *argv, VALUE obj)
1475
1475
1476
1476
static st_index_t hashseed [3 ];
1477
1477
typedef uint8_t sipseed_keys_t [16 ];
1478
- static union {
1478
+ static union sipseed {
1479
1479
sipseed_keys_t key ;
1480
1480
uint32_t u32 [type_roomof (sipseed_keys_t , uint32_t )];
1481
1481
} sipseed ;
1482
1482
1483
1483
static void
1484
- init_hashseed (struct MT * mt )
1484
+ init_hashseed (sipseed_keys_t key , uint32_t * cnt )
1485
1485
{
1486
1486
int i ;
1487
1487
for (i = 0 ; i < numberof (hashseed ); i ++ ) {
1488
- hashseed [i ] = genrand_int32 (mt );
1489
- #if SIZEOF_ST_INDEX_T * CHAR_BIT > 4 * 8
1490
- hashseed [i ] <<= 32 ;
1491
- hashseed [i ] |= genrand_int32 (mt );
1488
+ int bits = SIZEOF_ST_INDEX_T * CHAR_BIT ;
1489
+ hashseed [i ] = 0 ;
1490
+ for (; bits > 0 ; bits -= 32 ) {
1491
+ sip_uint64_t h = sip_hash24 (key , (void * )cnt , sizeof (uint32_t ));
1492
+ (* cnt )++ ;
1493
+ #if SIZEOF_ST_INDEX_T > 32
1494
+ hashseed [i ] <<= 32 ;
1492
1495
#endif
1493
- #if SIZEOF_ST_INDEX_T * CHAR_BIT > 8 * 8
1494
- hashseed [i ] <<= 32 ;
1495
- hashseed [i ] |= genrand_int32 (mt );
1496
- #endif
1497
- #if SIZEOF_ST_INDEX_T * CHAR_BIT > 12 * 8
1498
- hashseed [i ] <<= 32 ;
1499
- hashseed [i ] |= genrand_int32 (mt );
1496
+ #ifdef HAVE_UINT64_T
1497
+ hashseed [i ] ^= (st_index_t )h ;
1498
+ #else
1499
+ hashseed [i ] ^= h .u32 [0 ] ^ h .u32 [1 ];
1500
1500
#endif
1501
+ }
1501
1502
}
1502
1503
}
1503
1504
1504
1505
static void
1505
- init_siphash (struct MT * mt )
1506
+ init_siphash (sipseed_keys_t key , uint32_t * cnt )
1506
1507
{
1507
1508
int i ;
1508
1509
1509
- for (i = 0 ; i < numberof (sipseed .u32 ); ++ i )
1510
- sipseed .u32 [i ] = genrand_int32 (mt );
1510
+ for (i = 0 ; i < numberof (sipseed .u32 ); ++ i ) {
1511
+ sip_uint64_t h = sip_hash24 (key , (void * )cnt , sizeof (uint32_t ));
1512
+ (* cnt )++ ;
1513
+ #ifdef HAVE_UINT64_T
1514
+ sipseed .u32 [i ] = (uint32_t )h ^ (uint32_t )(h >> 32 );
1515
+ #else
1516
+ sipseed .u32 [i ] = h .u32 [0 ] ^ h .u32 [1 ];
1517
+ #endif
1518
+ }
1511
1519
}
1512
1520
1513
1521
#define MurmurMagic_1 (st_index_t)0xc6a4a793
@@ -1580,20 +1588,15 @@ rb_memhash(const void *ptr, long len)
1580
1588
void
1581
1589
Init_RandomSeedCore (void )
1582
1590
{
1583
- /*
1584
- Don't reuse this MT for Random::DEFAULT. Random::DEFAULT::seed shouldn't
1585
- provide a hint that an attacker guess siphash's seed.
1586
- */
1587
- struct MT mt ;
1588
- uint32_t initial_seed [DEFAULT_SEED_CNT ];
1591
+ union sipseed seed = { {0 } };
1592
+ uint32_t cnt = 1 ;
1589
1593
1590
- fill_random_seed (initial_seed , DEFAULT_SEED_CNT );
1591
- init_by_array (& mt , initial_seed , DEFAULT_SEED_CNT );
1594
+ fill_random_seed (seed .u32 , numberof (seed .u32 ));
1592
1595
1593
- init_hashseed (& mt );
1594
- init_siphash (& mt );
1596
+ init_hashseed (seed . key , & cnt );
1597
+ init_siphash (seed . key , & cnt );
1595
1598
1596
- explicit_bzero (initial_seed , DEFAULT_SEED_LEN );
1599
+ explicit_bzero (seed . key , sizeof ( seed . key ) );
1597
1600
}
1598
1601
1599
1602
static VALUE
0 commit comments