initialize pthread mutexes to avoid compiler common variable stupidity
[openafs.git] / src / des / new_rnd_key.c
1 /*
2  * Copyright 1988 by the Massachusetts Institute of Technology.
3  *
4  * For copying and distribution information, please see the file
5  * <mit-cpyright.h>.
6  *
7  * New pseudo-random key generator, using DES encryption to make the
8  * pseudo-random cycle as hard to break as DES.
9  *
10  * Written by Mark Lillibridge, MIT Project Athena
11  *
12  * Under U.S. law, this software may not be exported outside the US
13  * without license from the U.S. Commerce department.
14  */
15
16 #include "mit-cpyright.h"
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21
22 #ifndef KERNEL
23 #include <stdio.h>
24 #endif
25 #include "des.h"
26 #include "des_internal.h"
27 #include "des_prototypes.h"
28
29 #ifdef AFS_PTHREAD_ENV
30 #include <pthread.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #else
35 #ifdef HAVE_STRINGS_H
36 #include <strings.h>
37 #endif
38 #endif
39
40 static afs_int32 des_set_sequence_number(des_cblock new_sequence_number);
41 static afs_int32 des_generate_random_block(des_cblock block);
42
43 #define XPRT_NEW_RND_KEY
44
45 static int is_inited = 0;
46 #ifdef AFS_PTHREAD_ENV
47 /*
48  * This mutex protects the following global variables:
49  * is_inited
50  */
51
52 #include <assert.h>
53 pthread_mutex_t des_init_mutex = PTHREAD_MUTEX_INITIALIZER;
54 #define LOCK_INIT assert(pthread_mutex_lock(&des_init_mutex)==0)
55 #define UNLOCK_INIT assert(pthread_mutex_unlock(&des_init_mutex)==0)
56 #else
57 #define LOCK_INIT
58 #define UNLOCK_INIT
59 #endif
60 /*
61  * des_random_key: create a random des key
62  *
63  * You should call des_set_random_number_generater_seed at least
64  * once before this routine is called.  If you haven't, I'll try
65  * to add a little randomness to the start point anyway.  Yes,
66  * it recurses.  Deal with it.
67  *
68  * Notes: the returned key has correct parity and is guarenteed not
69  *        to be a weak des key.  Des_generate_random_block is used to
70  *        provide the random bits.
71  */
72 int
73 des_random_key(des_cblock key)
74 {
75     LOCK_INIT;
76     if (!is_inited) {
77         des_init_random_number_generator(key);
78     }
79     UNLOCK_INIT;
80     do {
81         des_generate_random_block(key);
82         des_fixup_key_parity(key);
83     } while (des_is_weak_key(key));
84
85     return (0);
86 }
87
88 /*
89  * des_init_random_number_generator:
90  *
91  *    This routine takes a secret key possibly shared by a number
92  * of servers and uses it to generate a random number stream that is
93  * not shared by any of the other servers.  It does this by using the current
94  * process id, host id, and the current time to the nearest second.  The
95  * resulting stream seed is not useful information for cracking the secret
96  * key.   Moreover, this routine keeps no copy of the secret key.
97  * This routine is used for example, by the kerberos server(s) with the
98  * key in question being the kerberos master key.
99  *
100  * Note: this routine calls des_set_random_generator_seed.
101  */
102 #if !defined(BSDUNIX) && !defined(AFS_SGI_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) 
103 you lose ... (aka, you get to implement an analog of this for your system ...)
104 #else
105
106 #ifdef AFS_NT40_ENV
107 #include <winsock2.h>
108 #include <process.h>
109 #include <afs/afsutil.h>
110 #else
111 #include <sys/time.h>
112 #include <unistd.h>
113 #endif
114
115 void
116 des_init_random_number_generator(des_cblock key)
117 {
118     struct {                    /* This must be 64 bits exactly */
119         afs_int32 process_id;
120         afs_int32 host_id;
121     } seed;
122     struct timeval time;        /* this must also be 64 bits exactly */
123     des_cblock new_key;
124
125     is_inited = 1;
126     /*
127      * use a host id and process id in generating the seed to ensure
128      * that different servers have different streams:
129      */
130 #if !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
131     seed.host_id = gethostid();
132 #endif
133     seed.process_id = getpid();
134
135     /*
136      * Generate a tempory value that depends on the key, host_id, and
137      * process_id such that it gives no useful information about the key:
138      */
139     des_set_random_generator_seed(key);
140     des_set_sequence_number((unsigned char *)&seed);
141     des_random_key(new_key);
142
143     /*
144      * use it to select a random stream:
145      */
146     des_set_random_generator_seed(new_key);
147
148     /*
149      * use a time stamp to ensure that a server started later does not reuse
150      * an old stream:
151      */
152     gettimeofday(&time, NULL);
153     des_set_sequence_number((unsigned char *)&time);
154
155     /*
156      * use the time stamp finally to select the final seed using the
157      * current random number stream:
158      */
159     des_random_key(new_key);
160     des_set_random_generator_seed(new_key);
161 }
162
163 #endif /* ifdef BSDUNIX */
164
165 /*
166  * This module implements a random number generator faculty such that the next
167  * number in any random number stream is very hard to predict without knowing
168  * the seed for that stream even given the preceeding random numbers.
169  */
170
171 /*
172  * The secret des key schedule for the current stream of random numbers:
173  */
174 static union {
175     afs_int32 align;
176     des_key_schedule d;
177 } random_sequence_key;
178
179 /*
180  * The sequence # in the current stream of random numbers:
181  */
182 static unsigned char sequence_number[8];
183
184 #ifdef AFS_PTHREAD_ENV
185 /*
186  * This mutex protects the following global variables:
187  * random_sequence_key
188  * sequence_number
189  */
190
191 #include <assert.h>
192 pthread_mutex_t des_random_mutex = PTHREAD_MUTEX_INITIALIZER;
193 #define LOCK_RANDOM assert(pthread_mutex_lock(&des_random_mutex)==0)
194 #define UNLOCK_RANDOM assert(pthread_mutex_unlock(&des_random_mutex)==0)
195 #else
196 #define LOCK_RANDOM
197 #define UNLOCK_RANDOM
198 #endif
199
200 /*
201  * des_set_random_generator_seed: this routine is used to select a random
202  *                                number stream.  The stream that results is
203  *                                totally determined by the passed in key.
204  *                                (I.e., calling this routine again with the
205  *                                same key allows repeating a sequence of
206  *                                random numbers)
207  *
208  * Requires: key is a valid des key.  I.e., has correct parity and is not a
209  *           weak des key.
210  */
211 void
212 des_set_random_generator_seed(des_cblock key)
213 {
214     register int i;
215
216     /* select the new stream: (note errors are not possible here...) */
217     LOCK_RANDOM;
218     des_key_sched(key, random_sequence_key.d);
219
220     /* "seek" to the start of the stream: */
221     for (i = 0; i < 8; i++)
222         sequence_number[i] = 0;
223     UNLOCK_RANDOM;
224 }
225
226 /*
227  * des_set_sequence_number: this routine is used to set the sequence number
228  *                          of the current random number stream.  This routine
229  *                          may be used to "seek" within the current random
230  *                          number stream.
231  *
232  * Note that des_set_random_generator_seed resets the sequence number to 0.
233  */
234 static afs_int32
235 des_set_sequence_number(des_cblock new_sequence_number)
236 {
237     LOCK_RANDOM;
238     memcpy((char *)sequence_number, (char *)new_sequence_number,
239            sizeof(sequence_number));
240     UNLOCK_RANDOM;
241     return 0;
242 }
243
244 /*
245  * des_generate_random_block: routine to return the next random number
246  *                            from the current random number stream.
247  *                            The returned number is 64 bits long.
248  *
249  * Requires: des_set_random_generator_seed must have been called at least once
250  *           before this routine is called.
251  */
252 static afs_int32
253 des_generate_random_block(des_cblock block)
254 {
255     int i;
256
257     /*
258      * Encrypt the sequence number to get the new random block:
259      */
260     LOCK_RANDOM;
261     des_ecb_encrypt(sequence_number, block, random_sequence_key.d, 1);
262
263     /*
264      * Increment the sequence number as an 8 byte unsigned number with wrap:
265      * (using LSB here)
266      */
267     for (i = 0; i < 8; i++) {
268         sequence_number[i] = (sequence_number[i] + 1) & 0xff;
269         if (sequence_number[i])
270             break;
271     }
272     UNLOCK_RANDOM;
273     return 0;
274 }