5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2003 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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
21 * Created: Sun Mar 9 00:09:18 1997
23 * The original RNG was based on Secure Shell's random number generator
24 * by Tatu Ylönen and was used as reference when programming this RNG.
25 * This RNG has been rewritten twice since the creation.
28 #include "silcincludes.h"
32 extern pid_t getsid (pid_t __pid);
36 extern pid_t getpgid (pid_t __pid);
41 /*#define SILC_RNG_DEBUG*/
43 /* Number of states to fetch data from pool. */
44 #define SILC_RNG_STATE_NUM 4
46 /* Byte size of the random data pool. */
47 #define SILC_RNG_POOLSIZE (20 * 48)
49 static SilcUInt32 silc_rng_get_position(SilcRng rng);
50 static void silc_rng_stir_pool(SilcRng rng);
51 static void silc_rng_xor(SilcRng rng, SilcUInt32 val, unsigned int pos);
52 static void silc_rng_exec_command(SilcRng rng, char *command);
53 static void silc_rng_get_hard_noise(SilcRng rng);
54 static void silc_rng_get_medium_noise(SilcRng rng);
55 static void silc_rng_get_soft_noise(SilcRng rng);
58 SILC SilcRng State context.
60 This object is used by the random number generator to provide
61 variable points where the actual random number is fetched from
62 the random pool. This provides that the data is not fetched always
63 from the same point of the pool. Short description of the fields
69 The index for the random pool buffer. Lowest and current
72 SilcRngStateContext *next
74 Pointer to the next state. If this is the last state this
75 will point to the first state thus providing circular list.
78 typedef struct SilcRngStateContext {
81 struct SilcRngStateContext *next;
85 SILC Random Number Generator object.
87 This object holds random pool which is used to generate the random
88 numbers used by various routines needing cryptographically strong
89 random numbers. Following short descriptions of the fields.
93 The random pool. This buffer holds the random data. This is
94 frequently stirred thus providing ever changing randomnes.
98 Key used in stirring the random pool. The pool is encrypted
99 with SHA1 hash function in CFB (Cipher Feedback) mode.
101 SilcSilcRngState state
103 State object that is used to get the next position for the
104 random pool. This position is used to fetch data from pool
105 or to save the data to the pool. The state changes everytime
110 Hash object (SHA1) used to make the CFB encryption to the
111 random pool. This is allocated when RNG object is allocated and
112 free'd when RNG object is free'd.
116 Threshold to indicate when it is required to acquire more
117 noise from the environment. More soft noise is acquired after
118 64 bits of output and hard noise every 160 bits of output.
121 struct SilcRngStruct {
122 unsigned char pool[SILC_RNG_POOLSIZE];
123 unsigned char key[64];
131 /* Allocates new RNG object. */
133 SilcRng silc_rng_alloc(void)
137 SILC_LOG_DEBUG(("Allocating new RNG object"));
139 new = silc_calloc(1, sizeof(*new));
140 new->fd_devurandom = -1;
142 memset(new->pool, 0, sizeof(new->pool));
143 memset(new->key, 0, sizeof(new->key));
145 if (!silc_hash_alloc("sha1", &new->sha1)) {
147 SILC_LOG_ERROR(("Could not allocate sha1 hash, probably not registered"));
151 new->devrandom = strdup("/dev/random");
156 /* Free's RNG object. */
158 void silc_rng_free(SilcRng rng)
163 memset(rng->pool, 0, sizeof(rng->pool));
164 memset(rng->key, 0, sizeof(rng->key));
165 silc_hash_free(rng->sha1);
166 silc_free(rng->devrandom);
168 if (rng->fd_devurandom != -1)
169 close(rng->fd_devurandom);
171 for (t = rng->state->next; t != rng->state; ) {
176 silc_free(rng->state);
182 /* Initializes random number generator by getting noise from environment.
183 The environmental noise is our so called seed. One should not call
184 this function more than once. */
186 void silc_rng_init(SilcRng rng)
189 SilcRngState first, next;
193 SILC_LOG_DEBUG(("Initializing RNG object"));
195 /* Initialize the states for the RNG. */
196 rng->state = silc_calloc(1, sizeof(*rng->state));
199 rng->state->next = NULL;
201 for (i = SILC_RNG_STATE_NUM - 1; i >= 1; i--) {
202 next = silc_calloc(1, sizeof(*rng->state));
204 (i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM));
206 (i * (sizeof(rng->pool) / SILC_RNG_STATE_NUM)) + 8;
207 next->next = rng->state;
213 memset(rng->pool, 0, sizeof(rng->pool));
215 /* Get noise from various environmental sources */
216 silc_rng_get_soft_noise(rng);
217 silc_rng_get_medium_noise(rng);
218 silc_rng_get_hard_noise(rng);
219 silc_rng_get_soft_noise(rng);
220 silc_free(rng->devrandom);
221 rng->devrandom = strdup("/dev/urandom");
224 /* This function gets 'soft' noise from environment. */
226 static void silc_rng_get_soft_noise(SilcRng rng)
232 #ifdef HAVE_GETRUSAGE
236 pos = silc_rng_get_position(rng);
238 silc_rng_xor(rng, clock(), 0);
241 silc_rng_xor(rng, getpid(), 1);
243 silc_rng_xor(rng, getpgid(getpid()) << 8, 2);
244 silc_rng_xor(rng, getpgid(getpid()) << 8, 3);
246 silc_rng_xor(rng, getgid(), 4);
249 silc_rng_xor(rng, getpgrp(), 5);
252 silc_rng_xor(rng, getsid(getpid()) << 16, 6);
254 silc_rng_xor(rng, times(&ptime), 7);
255 silc_rng_xor(rng, ptime.tms_utime, 8);
256 silc_rng_xor(rng, (ptime.tms_utime + ptime.tms_stime), pos++);
257 silc_rng_xor(rng, (ptime.tms_stime + ptime.tms_cutime), pos++);
258 silc_rng_xor(rng, (ptime.tms_utime + ptime.tms_stime), pos++);
259 silc_rng_xor(rng, (ptime.tms_cutime ^ ptime.tms_stime), pos++);
260 silc_rng_xor(rng, (ptime.tms_cutime ^ ptime.tms_cstime), pos++);
261 silc_rng_xor(rng, (ptime.tms_utime ^ ptime.tms_stime), pos++);
262 silc_rng_xor(rng, (ptime.tms_stime ^ ptime.tms_cutime), pos++);
263 silc_rng_xor(rng, (ptime.tms_cutime + ptime.tms_stime), pos++);
264 silc_rng_xor(rng, (ptime.tms_stime << 8), pos++);
266 silc_rng_xor(rng, clock() << 4, pos++);
269 silc_rng_xor(rng, getpgid(getpid()) << 8, pos++);
272 silc_rng_xor(rng, getpgrp(), pos++);
275 silc_rng_xor(rng, getsid(getpid()) << 16, pos++);
277 silc_rng_xor(rng, times(&ptime), pos++);
278 silc_rng_xor(rng, ptime.tms_utime, pos++);
280 silc_rng_xor(rng, getpgrp(), pos++);
283 #ifdef HAVE_GETRUSAGE
284 getrusage(RUSAGE_SELF, &r);
285 silc_rng_xor(rng, (r.ru_utime.tv_sec + r.ru_utime.tv_usec), pos++);
286 silc_rng_xor(rng, (r.ru_utime.tv_sec ^ r.ru_utime.tv_usec), pos++);
287 silc_rng_xor(rng, (r.ru_stime.tv_sec + r.ru_stime.tv_usec), pos++);
288 silc_rng_xor(rng, (r.ru_stime.tv_sec ^ r.ru_stime.tv_usec), pos++);
289 silc_rng_xor(rng, (r.ru_maxrss + r.ru_ixrss), pos++);
290 silc_rng_xor(rng, (r.ru_maxrss ^ r.ru_ixrss), pos++);
291 silc_rng_xor(rng, (r.ru_idrss + r.ru_idrss), pos++);
292 silc_rng_xor(rng, (r.ru_idrss ^ r.ru_idrss), pos++);
293 silc_rng_xor(rng, (r.ru_idrss << 16), pos++);
294 silc_rng_xor(rng, (r.ru_minflt + r.ru_majflt), pos++);
295 silc_rng_xor(rng, (r.ru_minflt ^ r.ru_majflt), pos++);
296 silc_rng_xor(rng, (r.ru_nswap + r.ru_oublock + r.ru_inblock), pos++);
297 silc_rng_xor(rng, (r.ru_nswap << 8), pos++);
298 silc_rng_xor(rng, (r.ru_inblock + r.ru_oublock), pos++);
299 silc_rng_xor(rng, (r.ru_inblock ^ r.ru_oublock), pos++);
300 silc_rng_xor(rng, (r.ru_msgsnd ^ r.ru_msgrcv), pos++);
301 silc_rng_xor(rng, (r.ru_nsignals + r.ru_msgsnd + r.ru_msgrcv), pos++);
302 silc_rng_xor(rng, (r.ru_nsignals << 16), pos++);
303 silc_rng_xor(rng, (r.ru_nvcsw + r.ru_nivcsw), pos++);
304 silc_rng_xor(rng, (r.ru_nvcsw ^ r.ru_nivcsw), pos++);
307 #ifdef SILC_RNG_DEBUG
308 SILC_LOG_HEXDUMP(("pool"), rng->pool, sizeof(rng->pool));
311 /* Stir random pool */
312 silc_rng_stir_pool(rng);
315 /* This function gets noise from different commands */
317 static void silc_rng_get_medium_noise(SilcRng rng)
319 /* If getrusage is available, there is no need for shell commands */
320 #ifdef HAVE_GETRUSAGE
323 silc_rng_exec_command(rng, "ps -leaww 2> /dev/null");
324 silc_rng_exec_command(rng, "ls -afiln ~ 2> /dev/null");
325 silc_rng_exec_command(rng, "ls -afiln /proc 2> /dev/null");
326 silc_rng_exec_command(rng, "ps -axww 2> /dev/null");
328 #ifdef SILC_RNG_DEBUG
329 SILC_LOG_HEXDUMP(("pool"), rng->pool, sizeof(rng->pool));
333 /* This function gets 'hard' noise from environment. This tries to
334 get the noise from /dev/random if available. */
336 static void silc_rng_get_hard_noise(SilcRng rng)
339 unsigned char buf[32];
342 /* Get noise from /dev/[u]random if available */
343 fd = open(rng->devrandom, O_RDONLY);
347 fcntl(fd, F_SETFL, O_NONBLOCK);
349 for (i = 0; i < 2; i++) {
350 len = read(fd, buf, sizeof(buf));
353 silc_rng_add_noise(rng, buf, len);
356 #ifdef SILC_RNG_DEBUG
357 SILC_LOG_HEXDUMP(("pool"), rng->pool, sizeof(rng->pool));
362 memset(buf, 0, sizeof(buf));
366 /* Execs command and gets noise from its output */
368 static void silc_rng_exec_command(SilcRng rng, char *command)
371 unsigned char buf[1024];
377 fd = popen(command, "r");
381 /* Get data as much as we can get into the buffer */
382 for (i = 0; i < sizeof(buf); i++) {
392 /* Add the buffer into random pool */
393 silc_rng_add_noise(rng, buf, i);
394 memset(buf, 0, sizeof(buf));
399 /* This function adds the contents of the buffer as noise into random
400 pool. After adding the noise the pool is stirred. */
402 void silc_rng_add_noise(SilcRng rng, unsigned char *buffer, SilcUInt32 len)
406 pos = silc_rng_get_position(rng);
408 /* Add the buffer one by one into the pool */
409 for(i = 0; i < len; i++, buffer++) {
410 if(pos >= SILC_RNG_POOLSIZE)
412 rng->pool[pos++] ^= *buffer;
415 /* Stir random pool */
416 silc_rng_stir_pool(rng);
419 /* XOR's data into the pool */
421 static void silc_rng_xor(SilcRng rng, SilcUInt32 val, unsigned int pos)
425 SILC_GET32_MSB(tmp, &rng->pool[pos]);
427 SILC_PUT32_MSB(val, &rng->pool[pos]);
430 /* This function stirs the random pool by encrypting buffer in CFB
431 (cipher feedback) mode with SHA1 algorithm. */
433 static void silc_rng_stir_pool(SilcRng rng)
436 SilcUInt32 iv[5], tmp;
439 SILC_GET32_MSB(iv[0], &rng->pool[16 ]);
440 SILC_GET32_MSB(iv[1], &rng->pool[16 + 4]);
441 SILC_GET32_MSB(iv[2], &rng->pool[16 + 8]);
442 SILC_GET32_MSB(iv[3], &rng->pool[16 + 12]);
443 SILC_GET32_MSB(iv[4], &rng->pool[16 + 16]);
446 for (i = 0; i < SILC_RNG_POOLSIZE; i += 20) {
447 silc_hash_transform(rng->sha1, iv, rng->key);
449 SILC_GET32_MSB(tmp, &rng->pool[i]);
451 SILC_PUT32_MSB(iv[0], &rng->pool[i]);
453 SILC_GET32_MSB(tmp, &rng->pool[i + 4]);
455 SILC_PUT32_MSB(iv[1], &rng->pool[i + 4]);
457 SILC_GET32_MSB(tmp, &rng->pool[i + 8]);
459 SILC_PUT32_MSB(iv[2], &rng->pool[i + 8]);
461 SILC_GET32_MSB(tmp, &rng->pool[i + 12]);
463 SILC_PUT32_MSB(iv[3], &rng->pool[i + 12]);
465 SILC_GET32_MSB(tmp, &rng->pool[i + 16]);
467 SILC_PUT32_MSB(iv[4], &rng->pool[i + 16]);
471 memcpy(rng->key, &rng->pool[silc_rng_get_position(rng)], sizeof(rng->key));
473 /* Second CFB pass */
474 for (i = 0; i < SILC_RNG_POOLSIZE; i += 20) {
475 silc_hash_transform(rng->sha1, iv, rng->key);
477 SILC_GET32_MSB(tmp, &rng->pool[i]);
479 SILC_PUT32_MSB(iv[0], &rng->pool[i]);
481 SILC_GET32_MSB(tmp, &rng->pool[i + 4]);
483 SILC_PUT32_MSB(iv[1], &rng->pool[i + 4]);
485 SILC_GET32_MSB(tmp, &rng->pool[i + 8]);
487 SILC_PUT32_MSB(iv[2], &rng->pool[i + 8]);
489 SILC_GET32_MSB(tmp, &rng->pool[i + 12]);
491 SILC_PUT32_MSB(iv[3], &rng->pool[i + 12]);
493 SILC_GET32_MSB(tmp, &rng->pool[i + 16]);
495 SILC_PUT32_MSB(iv[4], &rng->pool[i + 16]);
498 memset(iv, 0, sizeof(iv));
501 /* Returns next position where data is fetched from the pool or
504 static SilcUInt32 silc_rng_get_position(SilcRng rng)
509 next = rng->state->next;
511 pos = rng->state->pos++;
512 if ((next->low != 0 && pos >= next->low) || (pos >= SILC_RNG_POOLSIZE))
513 rng->state->pos = rng->state->low;
515 #ifdef SILC_RNG_DEBUG
516 fprintf(stderr, "state: %p: low: %lu, pos: %lu\n",
517 rng->state, rng->state->low, rng->state->pos);
525 /* Returns random byte. */
527 SilcUInt8 silc_rng_get_byte(SilcRng rng)
533 /* Get more soft noise after 64 bits threshold */
534 if (rng->threshold >= 8)
535 silc_rng_get_soft_noise(rng);
537 /* Get hard noise after 160 bits threshold, zero the threshold. */
538 if (rng->threshold >= 20) {
540 silc_rng_get_hard_noise(rng);
543 do byte = rng->pool[silc_rng_get_position(rng)]; while (byte == 0x00);
547 /* Return random byte as fast as possible. Reads from /dev/urandom if
548 available. If not then return from normal RNG (not so fast). */
550 SilcUInt8 silc_rng_get_byte_fast(SilcRng rng)
553 unsigned char buf[1];
555 if (rng->fd_devurandom == -1) {
556 rng->fd_devurandom = open("/dev/urandom", O_RDONLY);
557 if (rng->fd_devurandom < 0)
558 return silc_rng_get_byte(rng);
559 fcntl(rng->fd_devurandom, F_SETFL, O_NONBLOCK);
562 if (read(rng->fd_devurandom, buf, sizeof(buf)) < 0)
563 return silc_rng_get_byte(rng);
565 return buf[0] != 0x00 ? buf[0] : silc_rng_get_byte(rng);
567 return silc_rng_get_byte(rng);
571 /* Returns 16 bit random number */
573 SilcUInt16 silc_rng_get_rn16(SilcRng rng)
578 rn[0] = silc_rng_get_byte(rng);
579 rn[1] = silc_rng_get_byte(rng);
580 SILC_GET16_MSB(num, rn);
585 /* Returns 32 bit random number */
587 SilcUInt32 silc_rng_get_rn32(SilcRng rng)
592 rn[0] = silc_rng_get_byte(rng);
593 rn[1] = silc_rng_get_byte(rng);
594 rn[2] = silc_rng_get_byte(rng);
595 rn[3] = silc_rng_get_byte(rng);
596 SILC_GET32_MSB(num, rn);
601 /* Returns non-zero random number string. Returned string is in HEX format. */
603 unsigned char *silc_rng_get_rn_string(SilcRng rng, SilcUInt32 len)
606 unsigned char *string;
608 string = silc_calloc((len * 2 + 1), sizeof(unsigned char));
610 for (i = 0; i < len; i++)
611 sprintf(string + 2 * i, "%02x", silc_rng_get_byte(rng));
616 /* Returns non-zero random number binary data. */
618 unsigned char *silc_rng_get_rn_data(SilcRng rng, SilcUInt32 len)
623 data = silc_calloc(len + 1, sizeof(*data));
625 for (i = 0; i < len; i++)
626 data[i] = silc_rng_get_byte(rng);
631 /* Global RNG. This is global RNG that application can initialize so
632 that any part of code anywhere can use RNG without having to allocate
633 new RNG object everytime. If this is not initialized then these routines
634 will fail. Note: currently in SILC applications always initialize this. */
636 SilcRng global_rng = NULL;
638 /* Initialize global RNG. If `rng' is provided it is set as the global
639 RNG object (it can be allocated by the application for example). */
641 bool silc_rng_global_init(SilcRng rng)
648 global_rng = silc_rng_alloc();
649 silc_rng_init(global_rng);
654 /* Uninitialize global RNG */
656 bool silc_rng_global_uninit(void)
659 silc_rng_free(global_rng);
666 /* These are analogous to the functions above. */
668 SilcUInt8 silc_rng_global_get_byte(void)
670 return global_rng ? silc_rng_get_byte(global_rng) : 0;
673 /* Return random byte as fast as possible. Reads from /dev/urandom if
674 available. If not then return from normal RNG (not so fast). */
676 SilcUInt8 silc_rng_global_get_byte_fast(void)
678 return global_rng ? silc_rng_get_byte_fast(global_rng) : 0;
681 SilcUInt16 silc_rng_global_get_rn16(void)
683 return global_rng ? silc_rng_get_rn16(global_rng) : 0;
686 SilcUInt32 silc_rng_global_get_rn32(void)
688 return global_rng ? silc_rng_get_rn32(global_rng) : 0;
691 unsigned char *silc_rng_global_get_rn_string(SilcUInt32 len)
693 return global_rng ? silc_rng_get_rn_string(global_rng, len) : NULL;
696 unsigned char *silc_rng_global_get_rn_data(SilcUInt32 len)
698 return global_rng ? silc_rng_get_rn_data(global_rng, len) : NULL;
701 void silc_rng_global_add_noise(unsigned char *buffer, SilcUInt32 len)
704 silc_rng_add_noise(global_rng, buffer, len);