Import OpenSSL-0.9.8i.
[dragonfly.git] / crypto / openssl-0.9.7e / fips / rand / fips_rand.c
blobb78042b1dd1a0beae63396ce13cc7b9870a33590
1 /* ====================================================================
2 * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * This is a FIPS approved PRNG, ANSI X9.31 A.2.4.
54 #include "e_os.h"
56 /* If we don't define _XOPEN_SOURCE_EXTENDED, struct timeval won't
57 be defined and gettimeofday() won't be declared with strict compilers
58 like DEC C in ANSI C mode. */
59 #define _XOPEN_SOURCE_EXTENDED
61 #include <openssl/des.h>
62 #include <openssl/rand.h>
63 #include <openssl/err.h>
64 #include <openssl/fips_rand.h>
65 #ifndef OPENSSL_SYS_WIN32
66 #include <sys/time.h>
67 #endif
68 #include <assert.h>
69 #ifndef OPENSSL_SYS_WIN32
70 # ifdef OPENSSL_UNISTD
71 # include OPENSSL_UNISTD
72 # else
73 # include <unistd.h>
74 # endif
75 #endif
76 #include <string.h>
78 #ifdef OPENSSL_FIPS
80 #define SEED_SIZE 8
82 static unsigned char seed[SEED_SIZE];
83 static FIPS_RAND_SIZE_T n_seed;
84 static FIPS_RAND_SIZE_T o_seed;
85 static DES_cblock key1;
86 static DES_cblock key2;
87 static DES_key_schedule ks1,ks2;
88 static int key_set;
89 static int test_mode;
90 static unsigned char test_faketime[8];
92 #ifndef GETPID_IS_MEANINGLESS
93 static int seed_pid;
94 static int key_pid;
95 #endif
97 static void fips_rand_cleanup(void);
98 static void fips_rand_add(const void *buf, FIPS_RAND_SIZE_T num, double add_entropy);
99 static int fips_rand_bytes(unsigned char *buf, FIPS_RAND_SIZE_T num);
100 static int fips_rand_status(void);
102 static RAND_METHOD rand_fips_meth=
104 FIPS_rand_seed,
105 fips_rand_bytes,
106 fips_rand_cleanup,
107 fips_rand_add,
108 fips_rand_bytes,
109 fips_rand_status
112 static int second;
114 RAND_METHOD *FIPS_rand_method(void)
116 return &rand_fips_meth;
119 void FIPS_set_prng_key(const unsigned char k1[8],const unsigned char k2[8])
121 memcpy(&key1,k1,sizeof key1);
122 memcpy(&key2,k2,sizeof key2);
123 key_set=1;
124 #ifndef GETPID_IS_MEANINGLESS
125 key_pid=getpid();
126 #endif
127 second=0;
130 void FIPS_test_mode(int test,const unsigned char faketime[8])
132 test_mode=test;
133 if(!test_mode)
134 return;
135 memcpy(test_faketime,faketime,sizeof test_faketime);
138 /* NB: this returns true if _partially_ seeded */
139 int FIPS_rand_seeded()
140 { return key_set || n_seed; }
142 static void fips_gettime(unsigned char buf[8])
144 #ifdef OPENSSL_SYS_WIN32
145 FILETIME ft;
146 #else
147 struct timeval tv;
148 #endif
150 if(test_mode)
152 fprintf(stderr,"WARNING!!! PRNG IN TEST MODE!!!\n");
153 memcpy(buf,test_faketime,sizeof test_faketime);
154 return;
156 #ifdef OPENSSL_SYS_WIN32
157 GetSystemTimeAsFileTime(&ft);
158 buf[0] = (unsigned char) (ft.dwHighDateTime & 0xff);
159 buf[1] = (unsigned char) ((ft.dwHighDateTime >> 8) & 0xff);
160 buf[2] = (unsigned char) ((ft.dwHighDateTime >> 16) & 0xff);
161 buf[3] = (unsigned char) ((ft.dwHighDateTime >> 24) & 0xff);
162 buf[4] = (unsigned char) (ft.dwLowDateTime & 0xff);
163 buf[5] = (unsigned char) ((ft.dwLowDateTime >> 8) & 0xff);
164 buf[6] = (unsigned char) ((ft.dwLowDateTime >> 16) & 0xff);
165 buf[7] = (unsigned char) ((ft.dwLowDateTime >> 24) & 0xff);
166 #else
167 gettimeofday(&tv,NULL);
168 buf[0] = (unsigned char) (tv.tv_sec & 0xff);
169 buf[1] = (unsigned char) ((tv.tv_sec >> 8) & 0xff);
170 buf[2] = (unsigned char) ((tv.tv_sec >> 16) & 0xff);
171 buf[3] = (unsigned char) ((tv.tv_sec >> 24) & 0xff);
172 buf[4] = (unsigned char) (tv.tv_usec & 0xff);
173 buf[5] = (unsigned char) ((tv.tv_usec >> 8) & 0xff);
174 buf[6] = (unsigned char) ((tv.tv_usec >> 16) & 0xff);
175 buf[7] = (unsigned char) ((tv.tv_usec >> 24) & 0xff);
176 #endif
178 #if 0 /* This eminently sensible strategy is not acceptable to NIST. Sigh. */
179 #ifndef GETPID_IS_MEANINGLESS
180 /* we mix in the PID to ensure that after a fork the children don't give
181 * the same results as each other
183 pid=getpid();
184 /* make sure we shift the pid to the MSB */
185 if((pid&0xffff0000) == 0)
186 pid<<=16;
187 *(long *)&buf[0]^=pid;
188 #endif
189 #endif
192 static void fips_rand_encrypt(unsigned char *out,const unsigned char *in)
194 DES_ecb2_encrypt(in,out,&ks1,&ks2,1);
197 static void fips_rand_cleanup(void)
199 OPENSSL_cleanse(seed,sizeof seed);
200 n_seed=0;
203 void FIPS_rand_seed(const void *buf_, FIPS_RAND_SIZE_T num)
205 const char *buf=buf_;
206 FIPS_RAND_SIZE_T n;
207 static int init;
209 /* If the key hasn't been set, we can't seed! */
210 if(!key_set)
211 return;
213 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
214 if(!init)
216 init=1;
217 DES_set_key(&key1,&ks1);
218 DES_set_key(&key2,&ks2);
222 * This algorithm only uses 64 bits of seed, so ensure that we use
223 * the most recent 64 bits.
225 for(n=0 ; n < num ; )
227 FIPS_RAND_SIZE_T t=num-n;
229 if(o_seed+t > sizeof seed)
230 t=sizeof seed-o_seed;
231 memcpy(seed+o_seed,buf+n,t);
232 n+=t;
233 o_seed+=t;
234 if(o_seed == sizeof seed)
235 o_seed=0;
236 if(n_seed < sizeof seed)
237 n_seed+=t;
240 #ifndef GETPID_IS_MEANINGLESS
241 seed_pid=getpid();
242 #endif
244 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
247 static void fips_rand_add(const void *buf, FIPS_RAND_SIZE_T num, double add_entropy)
249 FIPS_rand_seed(buf,num);
252 static int fips_rand_bytes(unsigned char *buf,FIPS_RAND_SIZE_T num)
254 FIPS_RAND_SIZE_T n;
255 unsigned char timeseed[8];
256 unsigned char intermediate[SEED_SIZE];
257 unsigned char output[SEED_SIZE];
258 static unsigned char previous[SEED_SIZE];
259 #ifndef GETPID_IS_MEANINGLESS
260 int pid;
261 #endif
263 if(n_seed < sizeof seed)
265 RANDerr(RAND_F_FIPS_RAND_BYTES,RAND_R_PRNG_NOT_SEEDED);
266 return 0;
269 #ifdef FIPS_RAND_MAX_SIZE_T
270 if (num > FIPS_RAND_MAX_SIZE_T)
272 #ifdef RAND_R_PRNG_ASKING_FOR_TOO_MUCH
273 RANDerr(RAND_F_FIPS_RAND_BYTES,RAND_R_PRNG_ASKING_FOR_TOO_MUCH);
274 return 0;
275 #else
276 return -1; /* signal "not supported" condition */
277 #endif
279 #endif
281 #ifndef GETPID_IS_MEANINGLESS
282 pid=getpid();
283 if(pid != seed_pid)
285 RANDerr(RAND_F_FIPS_RAND_BYTES,RAND_R_PRNG_NOT_RESEEDED);
286 return 0;
288 if(pid != key_pid)
290 RANDerr(RAND_F_FIPS_RAND_BYTES,RAND_R_PRNG_NOT_REKEYED);
291 return 0;
293 #endif
295 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
297 for(n=0 ; n < num ; )
299 unsigned char t[SEED_SIZE];
300 FIPS_RAND_SIZE_T l;
302 /* ANS X9.31 A.2.4: I = ede*K(DT)
303 timeseed == DT
304 intermediate == I
306 fips_gettime(timeseed);
307 fips_rand_encrypt(intermediate,timeseed);
309 /* ANS X9.31 A.2.4: R = ede*K(I^V)
310 intermediate == I
311 seed == V
312 output == R
314 for(l=0 ; l < sizeof t ; ++l)
315 t[l]=intermediate[l]^seed[l];
316 fips_rand_encrypt(output,t);
318 /* ANS X9.31 A.2.4: V = ede*K(R^I)
319 output == R
320 intermediate == I
321 seed == V
323 for(l=0 ; l < sizeof t ; ++l)
324 t[l]=output[l]^intermediate[l];
325 fips_rand_encrypt(seed,t);
327 if(second && !memcmp(output,previous,sizeof previous))
329 RANDerr(RAND_F_FIPS_RAND_BYTES,RAND_R_PRNG_STUCK);
330 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
331 return 0;
333 memcpy(previous,output,sizeof previous);
334 second=1;
336 /* Successive values of R may be concatenated to produce a
337 pseudo random number of the desired length */
338 l=SEED_SIZE < num-n ? SEED_SIZE : num-n;
339 memcpy(buf+n,output,l);
340 n+=l;
343 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
345 return 1;
348 static int fips_rand_status(void)
350 return n_seed == sizeof seed;
353 #endif /* OPENSSL_FIPS */