cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
random.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib Randomness Interface *
4 * Copyright Peter Gutmann 1995-2006 *
5 * *
6 ****************************************************************************/
7 
8 #ifndef _RANDOM_DEFINED
9 
10 #define _RANDOM_DEFINED
11 
12 /****************************************************************************
13 * *
14 * Randomness Polling Internal Functions *
15 * *
16 ****************************************************************************/
17 
18 /* Some systems systems require special-case initialisation to allow
19  background randomness gathering, where this doesn't apply the routines to
20  do this are nop'd out */
21 
22 #if defined( __WIN32__ ) || defined( __WINCE__ )
23  void initRandomPolling( void );
24  void endRandomPolling( void );
25  CHECK_RETVAL \
26  int waitforRandomCompletion( const BOOLEAN force );
27 #elif defined( __UNIX__ ) && \
28  !( defined( __MVS__ ) || defined( __TANDEM_NSK__ ) || \
29  defined( __TANDEM_OSS__ ) )
30  void initRandomPolling( void );
31  void endRandomPolling( void );
32  CHECK_RETVAL \
33  int waitforRandomCompletion( const BOOLEAN force );
34 #else
35  #define initRandomPolling()
36  #define endRandomPolling()
37  #define waitforRandomCompletion( dummy ) CRYPT_OK
38 #endif /* !( __WIN32__ || __UNIX__ ) */
39 
40 /* On Unix systems the randomness pool may be duplicated at any point if
41  the process forks (qualis pater, talis filius), so we need to perform a
42  complex check to make sure that we're running with a unique copy of the
43  pool contents rather than a clone of data held in another process. The
44  following function checks whether we've forked or not, which is used as a
45  signal to adjust the pool contents */
46 
47 #if defined( __UNIX__ ) && \
48  !( defined( __MVS__ ) || defined( __TANDEM_NSK__ ) || \
49  defined( __TANDEM_OSS__ ) )
50  CHECK_RETVAL_BOOL \
51  BOOLEAN checkForked( void );
52 #else
53  #define checkForked() FALSE
54 #endif /* __UNIX__ */
55 
56 /* Prototypes for functions in the OS-specific randomness polling routines */
57 
58 void slowPoll( void );
59 void fastPoll( void );
60 
61 /* In order to make it easier to add lots of arbitrary-sized random data
62  values, we make the following functions available to the polling code to
63  implement a clustered-write mechanism for small data quantities. These
64  add an integer, long, or (short) string value to a buffer and send it
65  through to the system device when the buffer is full. Using the
66  intermediate buffer ensures that we don't have to send a message to the
67  device for every bit of data added
68 
69  The caller declares a state variable of type RANDOM_STATE, calls
70  initRandomData() to initialise it, calls addRandomData() for each
71  consecutive piece of data to add to the buffer, and finally calls
72  endRandomData() to flush the data through to the system device. The
73  state pointer is declared as a void * because to the caller it's an
74  opaque memory block while to the randomData routines it's structured
75  storage */
76 
77 typedef BYTE RANDOM_STATE[ 128 ];
78 
79 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
80 int initRandomData( INOUT void *statePtr,
81  IN_BUFFER( maxSize ) void *buffer,
82  IN_LENGTH_SHORT_MIN( 16 ) const int maxSize );
83 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
84 int addRandomData( INOUT void *statePtr,
85  IN_BUFFER( valueLength ) const void *value,
87 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
88 int addRandomLong( INOUT void *statePtr, const long value );
89 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
90 int endRandomData( INOUT void *statePtr, \
91  IN_RANGE( 0, 100 ) const int quality );
92 
93 /* We also provide an addRandomValue() to make it easier to add function
94  return values for getXYZ()-style system calls that return system info as
95  their return value, for which we can't pass an address to addRandomData()
96  unless we copy it to a temporary var first */
97 
98 #define addRandomValue( statePtr, value ) \
99  addRandomLong( statePtr, ( long ) value )
100 
101 /****************************************************************************
102 * *
103 * Randomness External Interface Functions *
104 * *
105 ****************************************************************************/
106 
107 /* Prototypes for functions in random.c */
108 
110 int initRandomInfo( OUT_OPT_PTR void **randomInfoPtrPtr );
111 STDC_NONNULL_ARG( ( 1 ) ) \
112 void endRandomInfo( INOUT void **randomInfoPtrPtr );
113 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
114 int addEntropyData( INOUT void *randomInfoPtr,
115  IN_BUFFER( length ) const void *buffer,
116  IN_LENGTH const int length );
118 int addEntropyQuality( INOUT void *randomInfoPtr,
119  IN_RANGE( 1, 100 ) const int quality );
120 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
121 int getRandomData( INOUT void *randomInfoPtr,
122  OUT_BUFFER_FIXED( length ) void *buffer,
123  IN_RANGE( 1, MAX_RANDOM_BYTES ) const int length );
124 
125 #endif /* _RANDOM_DEFINED */