5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Created: Sun Mar 9 00:09:18 1997
23 * This RNG is based on Secure Shell's random number generator.
25 /* XXX: Some operations block resulting slow initialization.
26 * XXX: I have some pending changes to make this better. */
30 * Revision 1.2 2000/07/05 06:08:43 priikone
31 * Global cosmetic change.
33 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
34 * Imported from internal CVS/Added Log headers.
39 #include "silcincludes.h"
42 /* #define SILC_RNG_DEBUG */
45 SILC SilcRng State context.
47 This object is used by the random number generator to provide
48 variable points where the actual random number is fetched from
49 the random pool. This provides that the data is not fetched always
50 from the same point of the pool. Short description of the fields
56 The index for the random pool buffer. Lowest and current
59 SilcRngStateContext *next
61 Pointer to the next state. If this is the last state this
62 will point to the first state thus providing circular list.
65 typedef struct SilcRngStateContext {
68 struct SilcRngStateContext *next;
72 SILC Random Number Generator object.
74 This object holds random pool which is used to generate the random
75 numbers used by various routines needing cryptographically strong
76 random numbers. Following short descriptions of the fields.
80 The random pool. This buffer holds the random data. This is
81 frequently stirred thus providing ever changing randomnes.
85 Key used in stirring the random pool. The pool is encrypted
86 with SHA1 hash function in CFB (Cipher Feedback) mode.
88 SilcSilcRngState state
90 State object that is used to get the next position for the
91 random pool. This position is used to fetch data from pool
92 or to save the data to the pool. The state changes everytime
97 Hash object (SHA1) used to make the CFB encryption to the
98 random pool. This is allocated when RNG object is allocated and
99 free'd when RNG object is free'd.
102 typedef struct SilcRngObjectStruct {
103 unsigned char pool[SILC_RNG_POOLSIZE];
104 unsigned char key[64];
109 /* Allocates new RNG object. */
111 SilcRng silc_rng_alloc()
115 SILC_LOG_DEBUG(("Allocating new RNG object"));
117 new = silc_calloc(1, sizeof(*new));
119 memset(new->pool, 0, sizeof(new->pool));
120 memset(new->key, 0, sizeof(new->key));
122 silc_hash_alloc("sha1", &new->sha1);
127 /* Free's RNG object. */
129 void silc_rng_free(SilcRng rng)
132 memset(rng->pool, 0, sizeof(rng->pool));
133 memset(rng->key, 0, sizeof(rng->key));
134 silc_free(rng->sha1);
139 /* Initializes random number generator by getting noise from environment.
140 The environmental noise is our so called seed. One should not call
141 this function more than once. */
143 void silc_rng_init(SilcRng rng)
146 SilcRngState first, next;
150 SILC_LOG_DEBUG(("Initializing RNG object"));
152 /* Initialize the states for the RNG. */
153 rng->state = silc_calloc(1, sizeof(*rng->state));
156 rng->state->next = NULL;
158 for (i = SILC_RNG_STATE_NUM - 1; i >= 1; i--) {
159 next = silc_calloc(1, sizeof(*rng->state));
161 (i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM));
163 (i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM)) + 8;
165 next->pos = sizeof(rng->pool) -
166 ((i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM))) + 8;
168 next->next = rng->state;
174 memset(rng->pool, 0, sizeof(rng->pool));
176 /* Get noise from various environmental sources */
177 silc_rng_get_soft_noise(rng);
178 silc_rng_get_medium_noise(rng);
179 silc_rng_get_hard_noise(rng);
182 /* This function gets 'soft' noise from environment. */
184 void silc_rng_get_soft_noise(SilcRng rng)
188 silc_rng_xor(rng, clock(), 0);
189 silc_rng_xor(rng, getpid(), 1);
190 silc_rng_xor(rng, getpgid(getpid() << 8), 2);
191 silc_rng_xor(rng, getpgid(getpid() << 8), 3);
192 silc_rng_xor(rng, getgid(), 4);
193 silc_rng_xor(rng, getpgrp(), 5);
194 silc_rng_xor(rng, getsid(getpid() << 16), 6);
195 silc_rng_xor(rng, times(&ptime), 7);
196 silc_rng_xor(rng, ptime.tms_utime, 8);
197 silc_rng_xor(rng, (ptime.tms_utime + ptime.tms_stime), 9);
198 silc_rng_xor(rng, (ptime.tms_stime + ptime.tms_cutime), 10);
199 silc_rng_xor(rng, (ptime.tms_utime + ptime.tms_stime), 11);
200 silc_rng_xor(rng, (ptime.tms_cutime ^ ptime.tms_stime), 12);
201 silc_rng_xor(rng, (ptime.tms_cutime ^ ptime.tms_cstime), 13);
202 silc_rng_xor(rng, (ptime.tms_utime ^ ptime.tms_stime), 14);
203 silc_rng_xor(rng, (ptime.tms_stime ^ ptime.tms_cutime), 15);
204 silc_rng_xor(rng, (ptime.tms_cutime + ptime.tms_stime), 16);
205 silc_rng_xor(rng, (ptime.tms_stime << 8), 17);
206 silc_rng_xor(rng, clock() << 4, 18);
207 silc_rng_xor(rng, getpgid(getpid() << 8), 19);
208 silc_rng_xor(rng, getpgrp(), 20);
209 silc_rng_xor(rng, getsid(getpid() << 16), 21);
210 silc_rng_xor(rng, times(&ptime), 22);
211 silc_rng_xor(rng, ptime.tms_utime, 23);
212 silc_rng_xor(rng, getpgrp(), 24);
214 /* Stir random pool */
215 silc_rng_stir_pool(rng);
218 /* This function gets noise from different commands */
220 void silc_rng_get_medium_noise(SilcRng rng)
222 silc_rng_exec_command(rng, "ps -lefaww 2> /dev/null");
223 silc_rng_exec_command(rng, "ls -afiln 2> /dev/null");
224 silc_rng_exec_command(rng, "ps -asww 2> /dev/null");
225 silc_rng_exec_command(rng, "ls -afiln /proc 2> /dev/null");
227 silc_rng_exec_command(rng, "ps -ef 2> /dev/null");
228 silc_rng_exec_command(rng, "ls -alin /dev 2> /dev/null");
232 /* This function gets 'hard' noise from environment. This tries to
233 get the noise from /dev/random if available. */
235 void silc_rng_get_hard_noise(SilcRng rng)
240 /* Get noise from /dev/random if available */
241 fd = open("/dev/random", O_RDONLY);
245 fcntl(fd, F_SETFL, O_NONBLOCK);
247 for (i = 0; i < 8; i++) {
248 len = read(fd, buf, sizeof(buf));
251 silc_rng_add_noise(rng, buf, len);
256 memset(buf, 0, sizeof(buf));
259 /* Execs command and gets noise from its output */
261 void silc_rng_exec_command(SilcRng rng, char *command)
269 fd = popen(command, "r");
273 /* Get data as much as we can get into the buffer */
274 for (i = 0; i < sizeof(buf); i++) {
286 /* Add the buffer into random pool */
287 silc_rng_add_noise(rng, buf, strlen(buf));
288 memset(buf, 0, sizeof(buf));
291 /* This function adds the contents of the buffer as noise into random
292 pool. After adding the noise the pool is stirred. */
294 void silc_rng_add_noise(SilcRng rng, unsigned char *buffer,
299 pos = silc_rng_get_position(rng);
301 /* Add the buffer one by one into the pool */
302 for(i = 0; i < len; i++, buffer++) {
303 if(pos >= SILC_RNG_POOLSIZE)
305 rng->pool[pos++] ^= *buffer;
308 /* Stir random pool */
309 silc_rng_stir_pool(rng);
312 /* XOR's data into the pool */
314 void silc_rng_xor(SilcRng rng, unsigned int val, unsigned int pos)
317 rng->pool[pos] ^= val + val;
320 /* This function stirs the random pool by encrypting buffer in CFB
321 (cipher feedback) mode with SHA1 algorithm. */
323 void silc_rng_stir_pool(SilcRng rng)
329 memcpy(iv, &rng->pool[SILC_RNG_POOLSIZE - 256], sizeof(iv));
332 for (i = 0; i < SILC_RNG_POOLSIZE; i += 5) {
333 rng->sha1->hash->transform(iv, rng->key);
334 iv[0] = rng->pool[i] ^= iv[0];
335 iv[1] = rng->pool[i + 1] ^= iv[1];
336 iv[2] = rng->pool[i + 2] ^= iv[2];
337 iv[3] = rng->pool[i + 3] ^= iv[3];
338 iv[4] = rng->pool[i + 4] ^= iv[4];
342 memcpy(rng->key, &rng->pool[silc_rng_get_position(rng)], sizeof(rng->key));
344 /* Second CFB pass */
345 for (i = 0; i < SILC_RNG_POOLSIZE; i += 5) {
346 rng->sha1->hash->transform(iv, rng->key);
347 iv[0] = rng->pool[i] ^= iv[0];
348 iv[1] = rng->pool[i + 1] ^= iv[1];
349 iv[2] = rng->pool[i + 2] ^= iv[2];
350 iv[3] = rng->pool[i + 3] ^= iv[3];
351 iv[4] = rng->pool[i + 4] ^= iv[4];
354 memset(iv, 0, sizeof(iv));
357 /* Returns next position where data is fetched from the pool or
360 unsigned int silc_rng_get_position(SilcRng rng)
365 next = rng->state->next;
367 pos = rng->state->pos++;
368 if ((next->low != 0 && pos >= next->low) || (pos >= SILC_RNG_POOLSIZE))
369 rng->state->pos = rng->state->low;
371 #ifdef SILC_RNG_DEBUG
372 fprintf(stderr, "state: %p: low: %d, pos: %d\n",
373 rng->state, rng->state->low, rng->state->pos);
381 /* returns random byte. Every two byte is from pools low or high state. */
383 unsigned char silc_rng_get_byte(SilcRng rng)
385 return rng->pool[silc_rng_get_position(rng)];
388 /* Returns 16 bit random number */
390 unsigned short silc_rng_get_rn16(SilcRng rng)
395 rn[0] = silc_rng_get_byte(rng);
396 rn[1] = silc_rng_get_byte(rng);
397 SILC_GET16_MSB(num, rn);
402 /* Returns 32 bit random number */
404 unsigned int silc_rng_get_rn32(SilcRng rng)
409 rn[0] = silc_rng_get_byte(rng);
410 rn[1] = silc_rng_get_byte(rng);
411 rn[2] = silc_rng_get_byte(rng);
412 rn[3] = silc_rng_get_byte(rng);
413 SILC_GET32_MSB(num, rn);
418 /* Returns random number string. Returned string is in HEX format. */
420 unsigned char *silc_rng_get_rn_string(SilcRng rng, unsigned int len)
423 unsigned char *string;
425 string = silc_calloc((len * 2 + 1), sizeof(unsigned char));
427 for (i = 0; i < len; i++)
428 sprintf(string + 2 * i, "%02x", silc_rng_get_byte(rng));