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