1 | /* |
---|
2 | |
---|
3 | random.h |
---|
4 | |
---|
5 | Author: Tatu Ylonen <ylo@cs.hut.fi> |
---|
6 | |
---|
7 | Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
---|
8 | All rights reserved |
---|
9 | |
---|
10 | Created: Sat Mar 4 14:49:05 1995 ylo |
---|
11 | |
---|
12 | Cryptographically strong random number generator. |
---|
13 | |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | * $Id: randoms.h,v 1.1.1.2 1999-03-08 17:43:35 danw Exp $ |
---|
18 | * $Log: not supported by cvs2svn $ |
---|
19 | * Revision 1.1.1.1 1996/02/18 21:38:10 ylo |
---|
20 | * Imported ssh-1.2.13. |
---|
21 | * |
---|
22 | * Revision 1.3 1995/09/13 12:00:02 ylo |
---|
23 | * Changes to make this work on Cray. |
---|
24 | * |
---|
25 | * Revision 1.2 1995/07/13 01:29:28 ylo |
---|
26 | * Removed "Last modified" header. |
---|
27 | * Added cvs log. |
---|
28 | * |
---|
29 | * $Endlog$ |
---|
30 | */ |
---|
31 | |
---|
32 | #ifndef RANDOM_H |
---|
33 | #define RANDOM_H |
---|
34 | |
---|
35 | #include "md5.h" |
---|
36 | |
---|
37 | #define RANDOM_STATE_BITS 8192 |
---|
38 | #define RANDOM_STATE_BYTES (RANDOM_STATE_BITS / 8) |
---|
39 | |
---|
40 | /* Structure for the random state. */ |
---|
41 | typedef struct |
---|
42 | { |
---|
43 | unsigned char state[RANDOM_STATE_BYTES];/* Pool of random data. */ |
---|
44 | unsigned char stir_key[64]; /* Extra data for next stirring. */ |
---|
45 | unsigned int next_available_byte; /* Index of next available byte. */ |
---|
46 | unsigned int add_position; /* Index to add noise. */ |
---|
47 | time_t last_dev_random_usage; /* Time of last /dev/random usage. */ |
---|
48 | } RandomState; |
---|
49 | |
---|
50 | /* Initializes the random number generator, loads any random information |
---|
51 | from the given file, and acquires as much environmental noise as it |
---|
52 | can to initialize the random number generator. More noise can be |
---|
53 | acquired later by calling random_add_noise + random_stir, or by |
---|
54 | calling random_get_environmental_noise again later when the environmental |
---|
55 | situation has changed. All I/O will be done with the given uid. */ |
---|
56 | void random_initialize(RandomState *state, uid_t uid, const char *filename); |
---|
57 | |
---|
58 | /* Acquires as much environmental noise as it can. This is probably quite |
---|
59 | sufficient on a unix machine, but might be grossly inadequate on a |
---|
60 | single-user PC or a Macintosh. This call random_stir automatically. |
---|
61 | This call may take many seconds to complete on a busy system. |
---|
62 | This will perform any commands with the given uid using userfile. */ |
---|
63 | void random_acquire_environmental_noise(RandomState *state, uid_t uid); |
---|
64 | |
---|
65 | /* Acquires easily available noise from the environment. */ |
---|
66 | void random_acquire_light_environmental_noise(RandomState *state); |
---|
67 | |
---|
68 | /* Executes the given command, and processes its output as noise. |
---|
69 | random_stir should be called after this. The command will be called |
---|
70 | with the given uid via userfile. */ |
---|
71 | void random_get_noise_from_command(RandomState *state, uid_t uid, |
---|
72 | const char *cmd); |
---|
73 | |
---|
74 | /* Adds the contents of the buffer as noise. random_stir should be called |
---|
75 | after this. */ |
---|
76 | void random_add_noise(RandomState *state, const void *buf, unsigned int bytes); |
---|
77 | |
---|
78 | /* Stirs the random pool to consume any newly acquired noise or to get more |
---|
79 | random numbers. This should be called after adding noise to properly |
---|
80 | mix the noise into the random pool. */ |
---|
81 | void random_stir(RandomState *state); |
---|
82 | |
---|
83 | /* Returns a random byte. Stirs the random pool if necessary. Acquires |
---|
84 | new environmental noise approximately every five minutes. */ |
---|
85 | unsigned int random_get_byte(RandomState *state); |
---|
86 | |
---|
87 | /* Saves some random bits in the file so that it can be used as a source |
---|
88 | of randomness for later runs. I/O will be done with the given uid using |
---|
89 | userfile. */ |
---|
90 | void random_save(RandomState *state, uid_t uid, const char *filename); |
---|
91 | |
---|
92 | /* Zeroes and frees any data structures associated with the random number |
---|
93 | generator. */ |
---|
94 | void random_clear(RandomState *state); |
---|
95 | |
---|
96 | #endif /* RANDOM_H */ |
---|