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