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