@@ -1474,33 +1474,48 @@ random_s_rand(int argc, VALUE *argv, VALUE obj)
1474
1474
#include "siphash.c"
1475
1475
1476
1476
typedef uint8_t sipseed_keys_t [16 ];
1477
- static union {
1477
+ static union sipseed {
1478
1478
sipseed_keys_t key ;
1479
1479
uint32_t u32 [type_roomof (sipseed_keys_t , uint32_t )];
1480
1480
} sipseed ;
1481
1481
1482
1482
static void
1483
- init_hashseed (struct MT * mt )
1483
+ init_hashseed (sipseed_keys_t key , uint32_t * cnt )
1484
1484
{
1485
1485
st_index_t hashseed [2 ];
1486
1486
int i ,b ;
1487
1487
for (i = 0 ; i < 2 ; i ++ ) {
1488
- hashseed [i ] = genrand_int32 (mt );
1489
- for (b = ST_INDEX_BITS - 32 ; b > 0 ; b -= 32 ) {
1488
+ hashseed [i ] = 0 ;
1489
+ for (b = ST_INDEX_BITS ; b > 0 ; b -= 32 ) {
1490
+ sip_uint64_t h = sip_hash24 (key , (void * )cnt , sizeof (uint32_t ));
1491
+ (* cnt )++ ;
1492
+ #if ST_INDEX_BITS > 32
1490
1493
hashseed [i ] <<= 32 ;
1491
- hashseed [i ] |= genrand_int32 (mt );
1494
+ #endif
1495
+ #ifdef HAVE_UINT64_T
1496
+ hashseed [i ] ^= (st_index_t )h ;
1497
+ #else
1498
+ hashseed [i ] ^= h .u32 [0 ] ^ h .u32 [1 ];
1499
+ #endif
1492
1500
}
1493
1501
}
1494
1502
st_hash_seed (hashseed );
1495
1503
}
1496
1504
1497
1505
static void
1498
- init_siphash (struct MT * mt )
1506
+ init_siphash (sipseed_keys_t key , uint32_t * cnt )
1499
1507
{
1500
1508
int i ;
1501
1509
1502
- for (i = 0 ; i < numberof (sipseed .u32 ); ++ i )
1503
- 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
+ }
1504
1519
}
1505
1520
1506
1521
st_index_t
@@ -1519,20 +1534,15 @@ rb_memhash(const void *ptr, long len)
1519
1534
void
1520
1535
Init_RandomSeedCore (void )
1521
1536
{
1522
- /*
1523
- Don't reuse this MT for Random::DEFAULT. Random::DEFAULT::seed shouldn't
1524
- provide a hint that an attacker guess siphash's seed.
1525
- */
1526
- struct MT mt ;
1527
- uint32_t initial_seed [DEFAULT_SEED_CNT ];
1537
+ union sipseed seed = { {0 } };
1538
+ uint32_t cnt = 1 ;
1528
1539
1529
- fill_random_seed (initial_seed , DEFAULT_SEED_CNT );
1530
- init_by_array (& mt , initial_seed , DEFAULT_SEED_CNT );
1540
+ fill_random_seed (seed .u32 , numberof (seed .u32 ));
1531
1541
1532
- init_hashseed (& mt );
1533
- init_siphash (& mt );
1542
+ init_hashseed (seed . key , & cnt );
1543
+ init_siphash (seed . key , & cnt );
1534
1544
1535
- explicit_bzero (initial_seed , DEFAULT_SEED_LEN );
1545
+ explicit_bzero (seed . key , sizeof ( seed . key ) );
1536
1546
}
1537
1547
1538
1548
static VALUE
0 commit comments