byte randPoolGetByte(void) { if (randPoolGetPos == sizeof(randPool)) randPoolStir(); return ((byte *)randPool)[randPoolGetPos++]; }
/* * Withdraw some bits from the pool. Regardless of the distribution of the * input bits, the bits returned are uniformly distributed, although they * cannot, of course, contain more Shannon entropy than the input bits. */ void randPoolGetBytes(byte *buf, unsigned len) { unsigned t; while (len > (t = sizeof(randPool) - randPoolGetPos)) { memcpy(buf, (byte *)randPool+randPoolGetPos, t); buf += t; len -= t; randPoolStir(); } if (len) { memcpy(buf, (byte *)randPool+randPoolGetPos, len); randPoolGetPos += len; } }
/* * Make a deposit of information (entropy) into the pool. The bits * deposited need not have any particular distribution; the stirring * operation transformes them to uniformly-distributed bits. */ void randPoolAddBytes(byte const *buf, unsigned len) { unsigned t; while (len > (t = sizeof(randPool) - randPoolAddPos)) { xorbytes((byte *)randPool+randPoolAddPos, buf, t); buf += t; len -= t; randPoolStir(); } if (len) { xorbytes((byte *)randPool+randPoolAddPos, buf, len); randPoolAddPos += len; randPoolGetPos = sizeof(randPool); /* Force stir on get */ } }
/* * Make a deposit of information (entropy) into the pool. This is done by * XORing them into the key which is used to encrypt the pool. Before any * bytes are retrieved from the pool, the altered key will be used to encrypt * the whole pool, causing all bits in the pool to depend on the new * information. * * The bits deposited need not have any particular distribution; the stirring * operation transforms them to uniformly-distributed bits. */ void randPoolAddBytes(byte const *buf, unsigned len) { byte *p = (byte *)randKey + randKeyAddPos; unsigned t = sizeof(randKey) - randKeyAddPos; while (len > t) { len -= t; while (t--) *p++ ^= *buf++; randPoolStir(); /* sets randKeyAddPos to 0 */ p = (byte *)randKey; t = sizeof(randKey); } if (len) { randKeyAddPos += len; do *p++ ^= *buf++; while (--len); randPoolGetPos = sizeof(randPool); /* Force stir on get */ } }