2 ---------------------------------------------------------------------------
3 Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
8 The free distribution and use of this software in both source and binary
9 form is allowed (with or without changes) provided that:
11 1. distributions of this source code include the above copyright
12 notice, this list of conditions and the following disclaimer;
14 2. distributions in binary form include the above copyright
15 notice, this list of conditions and the following disclaimer
16 in the documentation and/or other associated materials;
18 3. the copyright holder's name is not used to endorse products
19 built using this software without specific written permission.
21 ALTERNATIVELY, provided that this notice is retained in full, this product
22 may be distributed under the terms of the GNU General Public License (GPL),
23 in which case the provisions of the GPL apply INSTEAD OF those given above.
27 This software is provided 'as is' with no explicit or implied warranties
28 in respect of its properties, including, but not limited to, correctness
29 and/or fitness for purpose.
30 ---------------------------------------------------------------------------
31 Issue Date: 24/01/2003
33 This file implements a random data pool based on the use of an external
34 entropy function. It is based on the ideas advocated by Peter Gutmann in
35 his work on pseudo random sequence generators. It is not a 'paranoid'
36 random sequence generator and no attempt is made to protect the pool
37 from prying eyes either by memory locking or by techniques to obscure
38 its location in memory.
44 #if defined(__cplusplus)
49 /* mix a random data pool using the SHA1 compression function (as */
50 /* suggested by Peter Gutmann in his paper on random pools) */
52 static void prng_mix(unsigned char buf[])
53 { unsigned int i, len;
56 /*lint -e{663} unusual array to pointer conversion */
57 for(i = 0; i < PRNG_POOL_SIZE; i += SHA1_DIGEST_SIZE)
59 /* copy digest size pool block into SHA1 hash block */
60 memcpy(ctx->hash, buf + (i ? i : PRNG_POOL_SIZE)
61 - SHA1_DIGEST_SIZE, SHA1_DIGEST_SIZE);
63 /* copy data from pool into the SHA1 data buffer */
64 len = PRNG_POOL_SIZE - i;
65 memcpy(ctx->wbuf, buf + i, (len > SHA1_BLOCK_SIZE ? SHA1_BLOCK_SIZE : len));
67 if(len < SHA1_BLOCK_SIZE)
68 memcpy(((char*)ctx->wbuf) + len, buf, SHA1_BLOCK_SIZE - len);
70 /* compress using the SHA1 compression function */
73 /* put digest size block back into the random pool */
74 memcpy(buf + i, ctx->hash, SHA1_DIGEST_SIZE);
78 /* refresh the output buffer and update the random pool by adding */
79 /* entropy and remixing */
81 static void update_pool(prng_ctx ctx[1])
84 /* transfer random pool data to the output buffer */
85 memcpy(ctx->obuf, ctx->rbuf, PRNG_POOL_SIZE);
87 /* enter entropy data into the pool */
88 while(i < PRNG_POOL_SIZE)
89 i += ctx->entropy(ctx->rbuf + i, PRNG_POOL_SIZE - i);
91 /* invert and xor the original pool data into the pool */
92 for(i = 0; i < PRNG_POOL_SIZE; ++i)
93 ctx->rbuf[i] ^= ~ctx->obuf[i];
95 /* mix the pool and the output buffer */
100 void prng_init(prng_entropy_fn fun, prng_ctx ctx[1])
103 /* clear the buffers and the counter in the context */
104 memset(ctx, 0, sizeof(prng_ctx));
106 /* set the pointer to the entropy collection function */
109 /* initialise the random data pool */
112 /* mix the pool a minimum number of times */
113 for(i = 0; i < PRNG_MIN_MIX; ++i)
116 /* update the pool to prime the pool output buffer */
120 /* provide random bytes from the random data pool */
122 void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1])
123 { unsigned char *rp = data;
124 unsigned int len, pos = ctx->pos;
128 /* transfer 'data_len' bytes (or the number of bytes remaining */
129 /* the pool output buffer if less) into the output */
130 len = (data_len < PRNG_POOL_SIZE - pos ? data_len : PRNG_POOL_SIZE - pos);
131 memcpy(rp, ctx->obuf + pos, len);
132 rp += len; /* update ouput buffer position pointer */
133 pos += len; /* update pool output buffer pointer */
134 data_len -= len; /* update the remaining data count */
136 /* refresh the random pool if necessary */
137 if(pos == PRNG_POOL_SIZE)
139 update_pool(ctx); pos = 0;
146 void prng_end(prng_ctx ctx[1])
148 /* ensure the data in the context is destroyed */
149 memset(ctx, 0, sizeof(prng_ctx));
152 #if defined(__cplusplus)