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