fix alignment problem in util/random.c for sparc64/freebsd.

git-svn-id: file:///svn/unbound/trunk@2666 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2012-05-09 09:13:57 +00:00
parent 6819016a83
commit 3930e23b8c
2 changed files with 15 additions and 7 deletions

View File

@ -1,3 +1,6 @@
9 May 2012: Wouter
- Fix alignment problem in util/random on sparc64/freebsd.
8 May 2012: Wouter
- Fix for accept spinning reported by OpenBSD.
- iana portlist updated.

View File

@ -75,8 +75,8 @@ struct ub_randstate {
int rc4_ready;
};
/** Size of key to use */
#define SEED_SIZE 20
/** Size of key to use (must be multiple of 8) */
#define SEED_SIZE 24
/**
* Max random value. Similar to RAND_MAX, but more portable
@ -116,18 +116,22 @@ ub_systemseed(unsigned int seed)
static void
ub_arc4random_stir(struct ub_randstate* s, struct ub_randstate* from)
{
unsigned char rand_buf[SEED_SIZE];
/* not as unsigned char, but longerint so that it is
aligned properly on alignment sensitive platforms */
uint64_t rand_buf[SEED_SIZE/sizeof(uint64_t)];
int i;
memset(&s->rc4, 0, sizeof(s->rc4));
memset(rand_buf, 0xc, sizeof(rand_buf));
if (from) {
uint8_t* rbuf = (uint8_t*)rand_buf;
for(i=0; i<SEED_SIZE; i++)
rand_buf[i] = (unsigned char)ub_random(from);
rbuf[i] = (uint8_t)ub_random(from);
} else {
if(!RAND_status())
ub_systemseed((unsigned)getpid()^(unsigned)time(NULL));
if (RAND_bytes(rand_buf, (int)sizeof(rand_buf)) <= 0) {
if (RAND_bytes((unsigned char*)rand_buf,
(int)sizeof(rand_buf)) <= 0) {
/* very unlikely that this happens, since we seeded
* above, if it does; complain and keep going */
log_err("Couldn't obtain random bytes (error %ld)",
@ -136,14 +140,15 @@ ub_arc4random_stir(struct ub_randstate* s, struct ub_randstate* from)
return;
}
}
RC4_set_key(&s->rc4, SEED_SIZE, rand_buf);
RC4_set_key(&s->rc4, SEED_SIZE, (unsigned char*)rand_buf);
/*
* Discard early keystream, as per recommendations in:
* http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
*/
for(i = 0; i <= 256; i += sizeof(rand_buf))
RC4(&s->rc4, sizeof(rand_buf), rand_buf, rand_buf);
RC4(&s->rc4, sizeof(rand_buf), (unsigned char*)rand_buf,
(unsigned char*)rand_buf);
memset(rand_buf, 0, sizeof(rand_buf));