xref: /minix3/crypto/external/bsd/openssl/dist/crypto/rand/md_rand.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/rand/md_rand.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60ebfedea0SLionel Sambuc  *
61ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc  * are met:
64ebfedea0SLionel Sambuc  *
65ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc  *
68ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc  *    distribution.
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
75ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
82ebfedea0SLionel Sambuc  *
83ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc  *
87ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc  *    acknowledgment:
89ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc  *
92ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc  * ====================================================================
105ebfedea0SLionel Sambuc  *
106ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
108ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  */
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc #define OPENSSL_FIPSEVP
113ebfedea0SLionel Sambuc 
114ebfedea0SLionel Sambuc #ifdef MD_RAND_DEBUG
115ebfedea0SLionel Sambuc # ifndef NDEBUG
116ebfedea0SLionel Sambuc #  define NDEBUG
117ebfedea0SLionel Sambuc # endif
118ebfedea0SLionel Sambuc #endif
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc #include <assert.h>
121ebfedea0SLionel Sambuc #include <stdio.h>
122ebfedea0SLionel Sambuc #include <string.h>
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc #include "e_os.h"
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc #include <openssl/crypto.h>
127ebfedea0SLionel Sambuc #include <openssl/rand.h>
128ebfedea0SLionel Sambuc #include "rand_lcl.h"
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc #include <openssl/err.h>
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc #ifdef BN_DEBUG
133ebfedea0SLionel Sambuc # define PREDICT
134ebfedea0SLionel Sambuc #endif
135ebfedea0SLionel Sambuc 
136ebfedea0SLionel Sambuc /* #define PREDICT      1 */
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc #define STATE_SIZE      1023
139ebfedea0SLionel Sambuc static int state_num = 0, state_index = 0;
140ebfedea0SLionel Sambuc static unsigned char state[STATE_SIZE + MD_DIGEST_LENGTH];
141ebfedea0SLionel Sambuc static unsigned char md[MD_DIGEST_LENGTH];
142ebfedea0SLionel Sambuc static long md_count[2] = { 0, 0 };
143*0a6a1f1dSLionel Sambuc 
144ebfedea0SLionel Sambuc static double entropy = 0;
145*0a6a1f1dSLionel Sambuc static int initialized = 0;
146ebfedea0SLionel Sambuc 
147ebfedea0SLionel Sambuc static unsigned int crypto_lock_rand = 0; /* may be set only when a thread
148*0a6a1f1dSLionel Sambuc                                            * holds CRYPTO_LOCK_RAND (to
149*0a6a1f1dSLionel Sambuc                                            * prevent double locking) */
150ebfedea0SLionel Sambuc /* access to lockin_thread is synchronized by CRYPTO_LOCK_RAND2 */
151*0a6a1f1dSLionel Sambuc /* valid iff crypto_lock_rand is set */
152*0a6a1f1dSLionel Sambuc static CRYPTO_THREADID locking_threadid;
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc #ifdef PREDICT
155ebfedea0SLionel Sambuc int rand_predictable = 0;
156ebfedea0SLionel Sambuc #endif
157ebfedea0SLionel Sambuc 
158ebfedea0SLionel Sambuc const char RAND_version[] = "RAND" OPENSSL_VERSION_PTEXT;
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc static void ssleay_rand_cleanup(void);
161ebfedea0SLionel Sambuc static void ssleay_rand_seed(const void *buf, int num);
162ebfedea0SLionel Sambuc static void ssleay_rand_add(const void *buf, int num, double add_entropy);
163ebfedea0SLionel Sambuc static int ssleay_rand_nopseudo_bytes(unsigned char *buf, int num);
164ebfedea0SLionel Sambuc static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num);
165ebfedea0SLionel Sambuc static int ssleay_rand_status(void);
166ebfedea0SLionel Sambuc 
167ebfedea0SLionel Sambuc RAND_METHOD rand_ssleay_meth = {
168ebfedea0SLionel Sambuc     ssleay_rand_seed,
169ebfedea0SLionel Sambuc     ssleay_rand_nopseudo_bytes,
170ebfedea0SLionel Sambuc     ssleay_rand_cleanup,
171ebfedea0SLionel Sambuc     ssleay_rand_add,
172ebfedea0SLionel Sambuc     ssleay_rand_pseudo_bytes,
173ebfedea0SLionel Sambuc     ssleay_rand_status
174ebfedea0SLionel Sambuc };
175ebfedea0SLionel Sambuc 
RAND_SSLeay(void)176ebfedea0SLionel Sambuc RAND_METHOD *RAND_SSLeay(void)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc     return (&rand_ssleay_meth);
179ebfedea0SLionel Sambuc }
180ebfedea0SLionel Sambuc 
ssleay_rand_cleanup(void)181ebfedea0SLionel Sambuc static void ssleay_rand_cleanup(void)
182ebfedea0SLionel Sambuc {
183ebfedea0SLionel Sambuc     OPENSSL_cleanse(state, sizeof(state));
184ebfedea0SLionel Sambuc     state_num = 0;
185ebfedea0SLionel Sambuc     state_index = 0;
186ebfedea0SLionel Sambuc     OPENSSL_cleanse(md, MD_DIGEST_LENGTH);
187ebfedea0SLionel Sambuc     md_count[0] = 0;
188ebfedea0SLionel Sambuc     md_count[1] = 0;
189ebfedea0SLionel Sambuc     entropy = 0;
190*0a6a1f1dSLionel Sambuc     initialized = 0;
191ebfedea0SLionel Sambuc }
192ebfedea0SLionel Sambuc 
ssleay_rand_add(const void * buf,int num,double add)193ebfedea0SLionel Sambuc static void ssleay_rand_add(const void *buf, int num, double add)
194ebfedea0SLionel Sambuc {
195ebfedea0SLionel Sambuc     int i, j, k, st_idx;
196ebfedea0SLionel Sambuc     long md_c[2];
197ebfedea0SLionel Sambuc     unsigned char local_md[MD_DIGEST_LENGTH];
198ebfedea0SLionel Sambuc     EVP_MD_CTX m;
199ebfedea0SLionel Sambuc     int do_not_lock;
200ebfedea0SLionel Sambuc 
201*0a6a1f1dSLionel Sambuc     if (!num)
202*0a6a1f1dSLionel Sambuc         return;
203*0a6a1f1dSLionel Sambuc 
204ebfedea0SLionel Sambuc     /*
205ebfedea0SLionel Sambuc      * (Based on the rand(3) manpage)
206ebfedea0SLionel Sambuc      *
207ebfedea0SLionel Sambuc      * The input is chopped up into units of 20 bytes (or less for
208ebfedea0SLionel Sambuc      * the last block).  Each of these blocks is run through the hash
209ebfedea0SLionel Sambuc      * function as follows:  The data passed to the hash function
210ebfedea0SLionel Sambuc      * is the current 'md', the same number of bytes from the 'state'
211ebfedea0SLionel Sambuc      * (the location determined by in incremented looping index) as
212ebfedea0SLionel Sambuc      * the current 'block', the new key data 'block', and 'count'
213ebfedea0SLionel Sambuc      * (which is incremented after each use).
214ebfedea0SLionel Sambuc      * The result of this is kept in 'md' and also xored into the
215ebfedea0SLionel Sambuc      * 'state' at the same locations that were used as input into the
216ebfedea0SLionel Sambuc      * hash function.
217ebfedea0SLionel Sambuc      */
218ebfedea0SLionel Sambuc 
219ebfedea0SLionel Sambuc     /* check if we already have the lock */
220*0a6a1f1dSLionel Sambuc     if (crypto_lock_rand) {
221ebfedea0SLionel Sambuc         CRYPTO_THREADID cur;
222ebfedea0SLionel Sambuc         CRYPTO_THREADID_current(&cur);
223ebfedea0SLionel Sambuc         CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
224ebfedea0SLionel Sambuc         do_not_lock = !CRYPTO_THREADID_cmp(&locking_threadid, &cur);
225ebfedea0SLionel Sambuc         CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
226*0a6a1f1dSLionel Sambuc     } else
227ebfedea0SLionel Sambuc         do_not_lock = 0;
228ebfedea0SLionel Sambuc 
229*0a6a1f1dSLionel Sambuc     if (!do_not_lock)
230*0a6a1f1dSLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
231ebfedea0SLionel Sambuc     st_idx = state_index;
232ebfedea0SLionel Sambuc 
233*0a6a1f1dSLionel Sambuc     /*
234*0a6a1f1dSLionel Sambuc      * use our own copies of the counters so that even if a concurrent thread
235*0a6a1f1dSLionel Sambuc      * seeds with exactly the same data and uses the same subarray there's
236*0a6a1f1dSLionel Sambuc      * _some_ difference
237*0a6a1f1dSLionel Sambuc      */
238ebfedea0SLionel Sambuc     md_c[0] = md_count[0];
239ebfedea0SLionel Sambuc     md_c[1] = md_count[1];
240ebfedea0SLionel Sambuc 
241ebfedea0SLionel Sambuc     memcpy(local_md, md, sizeof md);
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc     /* state_index <= state_num <= STATE_SIZE */
244ebfedea0SLionel Sambuc     state_index += num;
245*0a6a1f1dSLionel Sambuc     if (state_index >= STATE_SIZE) {
246ebfedea0SLionel Sambuc         state_index %= STATE_SIZE;
247ebfedea0SLionel Sambuc         state_num = STATE_SIZE;
248*0a6a1f1dSLionel Sambuc     } else if (state_num < STATE_SIZE) {
249ebfedea0SLionel Sambuc         if (state_index > state_num)
250ebfedea0SLionel Sambuc             state_num = state_index;
251ebfedea0SLionel Sambuc     }
252ebfedea0SLionel Sambuc     /* state_index <= state_num <= STATE_SIZE */
253ebfedea0SLionel Sambuc 
254*0a6a1f1dSLionel Sambuc     /*
255*0a6a1f1dSLionel Sambuc      * state[st_idx], ..., state[(st_idx + num - 1) % STATE_SIZE] are what we
256*0a6a1f1dSLionel Sambuc      * will use now, but other threads may use them as well
257*0a6a1f1dSLionel Sambuc      */
258ebfedea0SLionel Sambuc 
259ebfedea0SLionel Sambuc     md_count[1] += (num / MD_DIGEST_LENGTH) + (num % MD_DIGEST_LENGTH > 0);
260ebfedea0SLionel Sambuc 
261*0a6a1f1dSLionel Sambuc     if (!do_not_lock)
262*0a6a1f1dSLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
263ebfedea0SLionel Sambuc 
264ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&m);
265*0a6a1f1dSLionel Sambuc     for (i = 0; i < num; i += MD_DIGEST_LENGTH) {
266ebfedea0SLionel Sambuc         j = (num - i);
267ebfedea0SLionel Sambuc         j = (j > MD_DIGEST_LENGTH) ? MD_DIGEST_LENGTH : j;
268ebfedea0SLionel Sambuc 
269ebfedea0SLionel Sambuc         MD_Init(&m);
270ebfedea0SLionel Sambuc         MD_Update(&m, local_md, MD_DIGEST_LENGTH);
271ebfedea0SLionel Sambuc         k = (st_idx + j) - STATE_SIZE;
272*0a6a1f1dSLionel Sambuc         if (k > 0) {
273ebfedea0SLionel Sambuc             MD_Update(&m, &(state[st_idx]), j - k);
274ebfedea0SLionel Sambuc             MD_Update(&m, &(state[0]), k);
275*0a6a1f1dSLionel Sambuc         } else
276ebfedea0SLionel Sambuc             MD_Update(&m, &(state[st_idx]), j);
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc         /* DO NOT REMOVE THE FOLLOWING CALL TO MD_Update()! */
279ebfedea0SLionel Sambuc         MD_Update(&m, buf, j);
280*0a6a1f1dSLionel Sambuc         /*
281*0a6a1f1dSLionel Sambuc          * We know that line may cause programs such as purify and valgrind
282*0a6a1f1dSLionel Sambuc          * to complain about use of uninitialized data.  The problem is not,
283*0a6a1f1dSLionel Sambuc          * it's with the caller.  Removing that line will make sure you get
284*0a6a1f1dSLionel Sambuc          * really bad randomness and thereby other problems such as very
285*0a6a1f1dSLionel Sambuc          * insecure keys.
286*0a6a1f1dSLionel Sambuc          */
287ebfedea0SLionel Sambuc 
288ebfedea0SLionel Sambuc         MD_Update(&m, (unsigned char *)&(md_c[0]), sizeof(md_c));
289ebfedea0SLionel Sambuc         MD_Final(&m, local_md);
290ebfedea0SLionel Sambuc         md_c[1]++;
291ebfedea0SLionel Sambuc 
292ebfedea0SLionel Sambuc         buf = (const char *)buf + j;
293ebfedea0SLionel Sambuc 
294*0a6a1f1dSLionel Sambuc         for (k = 0; k < j; k++) {
295*0a6a1f1dSLionel Sambuc             /*
296*0a6a1f1dSLionel Sambuc              * Parallel threads may interfere with this, but always each byte
297*0a6a1f1dSLionel Sambuc              * of the new state is the XOR of some previous value of its and
298*0a6a1f1dSLionel Sambuc              * local_md (itermediate values may be lost). Alway using locking
299*0a6a1f1dSLionel Sambuc              * could hurt performance more than necessary given that
300*0a6a1f1dSLionel Sambuc              * conflicts occur only when the total seeding is longer than the
301*0a6a1f1dSLionel Sambuc              * random state.
302*0a6a1f1dSLionel Sambuc              */
303ebfedea0SLionel Sambuc             state[st_idx++] ^= local_md[k];
304ebfedea0SLionel Sambuc             if (st_idx >= STATE_SIZE)
305ebfedea0SLionel Sambuc                 st_idx = 0;
306ebfedea0SLionel Sambuc         }
307ebfedea0SLionel Sambuc     }
308ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&m);
309ebfedea0SLionel Sambuc 
310*0a6a1f1dSLionel Sambuc     if (!do_not_lock)
311*0a6a1f1dSLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
312*0a6a1f1dSLionel Sambuc     /*
313*0a6a1f1dSLionel Sambuc      * Don't just copy back local_md into md -- this could mean that other
314*0a6a1f1dSLionel Sambuc      * thread's seeding remains without effect (except for the incremented
315*0a6a1f1dSLionel Sambuc      * counter).  By XORing it we keep at least as much entropy as fits into
316*0a6a1f1dSLionel Sambuc      * md.
317*0a6a1f1dSLionel Sambuc      */
318*0a6a1f1dSLionel Sambuc     for (k = 0; k < (int)sizeof(md); k++) {
319ebfedea0SLionel Sambuc         md[k] ^= local_md[k];
320ebfedea0SLionel Sambuc     }
321ebfedea0SLionel Sambuc     if (entropy < ENTROPY_NEEDED) /* stop counting when we have enough */
322ebfedea0SLionel Sambuc         entropy += add;
323*0a6a1f1dSLionel Sambuc     if (!do_not_lock)
324*0a6a1f1dSLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
325ebfedea0SLionel Sambuc 
326ebfedea0SLionel Sambuc #if !defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32)
327ebfedea0SLionel Sambuc     assert(md_c[1] == md_count[1]);
328ebfedea0SLionel Sambuc #endif
329ebfedea0SLionel Sambuc }
330ebfedea0SLionel Sambuc 
ssleay_rand_seed(const void * buf,int num)331ebfedea0SLionel Sambuc static void ssleay_rand_seed(const void *buf, int num)
332ebfedea0SLionel Sambuc {
333ebfedea0SLionel Sambuc     ssleay_rand_add(buf, num, (double)num);
334ebfedea0SLionel Sambuc }
335ebfedea0SLionel Sambuc 
ssleay_rand_bytes(unsigned char * buf,int num,int pseudo,int lock)336*0a6a1f1dSLionel Sambuc int ssleay_rand_bytes(unsigned char *buf, int num, int pseudo, int lock)
337ebfedea0SLionel Sambuc {
338ebfedea0SLionel Sambuc     static volatile int stirred_pool = 0;
339ebfedea0SLionel Sambuc     int i, j, k, st_num, st_idx;
340ebfedea0SLionel Sambuc     int num_ceil;
341ebfedea0SLionel Sambuc     int ok;
342ebfedea0SLionel Sambuc     long md_c[2];
343ebfedea0SLionel Sambuc     unsigned char local_md[MD_DIGEST_LENGTH];
344ebfedea0SLionel Sambuc     EVP_MD_CTX m;
345ebfedea0SLionel Sambuc #ifndef GETPID_IS_MEANINGLESS
346ebfedea0SLionel Sambuc     pid_t curr_pid = getpid();
347ebfedea0SLionel Sambuc #endif
348ebfedea0SLionel Sambuc     int do_stir_pool = 0;
349ebfedea0SLionel Sambuc 
350ebfedea0SLionel Sambuc #ifdef PREDICT
351*0a6a1f1dSLionel Sambuc     if (rand_predictable) {
352ebfedea0SLionel Sambuc         static unsigned char val = 0;
353ebfedea0SLionel Sambuc 
354ebfedea0SLionel Sambuc         for (i = 0; i < num; i++)
355ebfedea0SLionel Sambuc             buf[i] = val++;
356ebfedea0SLionel Sambuc         return (1);
357ebfedea0SLionel Sambuc     }
358ebfedea0SLionel Sambuc #endif
359ebfedea0SLionel Sambuc 
360ebfedea0SLionel Sambuc     if (num <= 0)
361ebfedea0SLionel Sambuc         return 1;
362ebfedea0SLionel Sambuc 
363ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&m);
364ebfedea0SLionel Sambuc     /* round upwards to multiple of MD_DIGEST_LENGTH/2 */
365*0a6a1f1dSLionel Sambuc     num_ceil =
366*0a6a1f1dSLionel Sambuc         (1 + (num - 1) / (MD_DIGEST_LENGTH / 2)) * (MD_DIGEST_LENGTH / 2);
367ebfedea0SLionel Sambuc 
368ebfedea0SLionel Sambuc     /*
369ebfedea0SLionel Sambuc      * (Based on the rand(3) manpage:)
370ebfedea0SLionel Sambuc      *
371ebfedea0SLionel Sambuc      * For each group of 10 bytes (or less), we do the following:
372ebfedea0SLionel Sambuc      *
373ebfedea0SLionel Sambuc      * Input into the hash function the local 'md' (which is initialized from
374ebfedea0SLionel Sambuc      * the global 'md' before any bytes are generated), the bytes that are to
375ebfedea0SLionel Sambuc      * be overwritten by the random bytes, and bytes from the 'state'
376ebfedea0SLionel Sambuc      * (incrementing looping index). From this digest output (which is kept
377ebfedea0SLionel Sambuc      * in 'md'), the top (up to) 10 bytes are returned to the caller and the
378ebfedea0SLionel Sambuc      * bottom 10 bytes are xored into the 'state'.
379ebfedea0SLionel Sambuc      *
380ebfedea0SLionel Sambuc      * Finally, after we have finished 'num' random bytes for the
381ebfedea0SLionel Sambuc      * caller, 'count' (which is incremented) and the local and global 'md'
382ebfedea0SLionel Sambuc      * are fed into the hash function and the results are kept in the
383ebfedea0SLionel Sambuc      * global 'md'.
384ebfedea0SLionel Sambuc      */
385*0a6a1f1dSLionel Sambuc     if (lock)
386ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
387ebfedea0SLionel Sambuc 
388ebfedea0SLionel Sambuc     /* prevent ssleay_rand_bytes() from trying to obtain the lock again */
389ebfedea0SLionel Sambuc     CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
390ebfedea0SLionel Sambuc     CRYPTO_THREADID_current(&locking_threadid);
391ebfedea0SLionel Sambuc     CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
392ebfedea0SLionel Sambuc     crypto_lock_rand = 1;
393ebfedea0SLionel Sambuc 
394*0a6a1f1dSLionel Sambuc     if (!initialized) {
395*0a6a1f1dSLionel Sambuc         RAND_poll();
396*0a6a1f1dSLionel Sambuc         initialized = 1;
397*0a6a1f1dSLionel Sambuc     }
398*0a6a1f1dSLionel Sambuc 
399ebfedea0SLionel Sambuc     if (!stirred_pool)
400ebfedea0SLionel Sambuc         do_stir_pool = 1;
401ebfedea0SLionel Sambuc 
402ebfedea0SLionel Sambuc     ok = (entropy >= ENTROPY_NEEDED);
403*0a6a1f1dSLionel Sambuc     if (!ok) {
404*0a6a1f1dSLionel Sambuc         /*
405*0a6a1f1dSLionel Sambuc          * If the PRNG state is not yet unpredictable, then seeing the PRNG
406*0a6a1f1dSLionel Sambuc          * output may help attackers to determine the new state; thus we have
407*0a6a1f1dSLionel Sambuc          * to decrease the entropy estimate. Once we've had enough initial
408*0a6a1f1dSLionel Sambuc          * seeding we don't bother to adjust the entropy count, though,
409*0a6a1f1dSLionel Sambuc          * because we're not ambitious to provide *information-theoretic*
410*0a6a1f1dSLionel Sambuc          * randomness. NOTE: This approach fails if the program forks before
411*0a6a1f1dSLionel Sambuc          * we have enough entropy. Entropy should be collected in a separate
412*0a6a1f1dSLionel Sambuc          * input pool and be transferred to the output pool only when the
413*0a6a1f1dSLionel Sambuc          * entropy limit has been reached.
414ebfedea0SLionel Sambuc          */
415ebfedea0SLionel Sambuc         entropy -= num;
416ebfedea0SLionel Sambuc         if (entropy < 0)
417ebfedea0SLionel Sambuc             entropy = 0;
418ebfedea0SLionel Sambuc     }
419ebfedea0SLionel Sambuc 
420*0a6a1f1dSLionel Sambuc     if (do_stir_pool) {
421*0a6a1f1dSLionel Sambuc         /*
422*0a6a1f1dSLionel Sambuc          * In the output function only half of 'md' remains secret, so we
423*0a6a1f1dSLionel Sambuc          * better make sure that the required entropy gets 'evenly
424*0a6a1f1dSLionel Sambuc          * distributed' through 'state', our randomness pool. The input
425*0a6a1f1dSLionel Sambuc          * function (ssleay_rand_add) chains all of 'md', which makes it more
426*0a6a1f1dSLionel Sambuc          * suitable for this purpose.
427ebfedea0SLionel Sambuc          */
428ebfedea0SLionel Sambuc 
429ebfedea0SLionel Sambuc         int n = STATE_SIZE;     /* so that the complete pool gets accessed */
430*0a6a1f1dSLionel Sambuc         while (n > 0) {
431ebfedea0SLionel Sambuc #if MD_DIGEST_LENGTH > 20
432ebfedea0SLionel Sambuc # error "Please adjust DUMMY_SEED."
433ebfedea0SLionel Sambuc #endif
434ebfedea0SLionel Sambuc #define DUMMY_SEED "...................." /* at least MD_DIGEST_LENGTH */
435*0a6a1f1dSLionel Sambuc             /*
436*0a6a1f1dSLionel Sambuc              * Note that the seed does not matter, it's just that
437*0a6a1f1dSLionel Sambuc              * ssleay_rand_add expects to have something to hash.
438*0a6a1f1dSLionel Sambuc              */
439ebfedea0SLionel Sambuc             ssleay_rand_add(DUMMY_SEED, MD_DIGEST_LENGTH, 0.0);
440ebfedea0SLionel Sambuc             n -= MD_DIGEST_LENGTH;
441ebfedea0SLionel Sambuc         }
442ebfedea0SLionel Sambuc         if (ok)
443ebfedea0SLionel Sambuc             stirred_pool = 1;
444ebfedea0SLionel Sambuc     }
445ebfedea0SLionel Sambuc 
446ebfedea0SLionel Sambuc     st_idx = state_index;
447ebfedea0SLionel Sambuc     st_num = state_num;
448ebfedea0SLionel Sambuc     md_c[0] = md_count[0];
449ebfedea0SLionel Sambuc     md_c[1] = md_count[1];
450ebfedea0SLionel Sambuc     memcpy(local_md, md, sizeof md);
451ebfedea0SLionel Sambuc 
452ebfedea0SLionel Sambuc     state_index += num_ceil;
453ebfedea0SLionel Sambuc     if (state_index > state_num)
454ebfedea0SLionel Sambuc         state_index %= state_num;
455ebfedea0SLionel Sambuc 
456*0a6a1f1dSLionel Sambuc     /*
457*0a6a1f1dSLionel Sambuc      * state[st_idx], ..., state[(st_idx + num_ceil - 1) % st_num] are now
458*0a6a1f1dSLionel Sambuc      * ours (but other threads may use them too)
459*0a6a1f1dSLionel Sambuc      */
460ebfedea0SLionel Sambuc 
461ebfedea0SLionel Sambuc     md_count[0] += 1;
462ebfedea0SLionel Sambuc 
463ebfedea0SLionel Sambuc     /* before unlocking, we must clear 'crypto_lock_rand' */
464ebfedea0SLionel Sambuc     crypto_lock_rand = 0;
465*0a6a1f1dSLionel Sambuc     if (lock)
466ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
467ebfedea0SLionel Sambuc 
468*0a6a1f1dSLionel Sambuc     while (num > 0) {
469ebfedea0SLionel Sambuc         /* num_ceil -= MD_DIGEST_LENGTH/2 */
470ebfedea0SLionel Sambuc         j = (num >= MD_DIGEST_LENGTH / 2) ? MD_DIGEST_LENGTH / 2 : num;
471ebfedea0SLionel Sambuc         num -= j;
472ebfedea0SLionel Sambuc         MD_Init(&m);
473ebfedea0SLionel Sambuc #ifndef GETPID_IS_MEANINGLESS
474*0a6a1f1dSLionel Sambuc         if (curr_pid) {         /* just in the first iteration to save time */
475ebfedea0SLionel Sambuc             MD_Update(&m, (unsigned char *)&curr_pid, sizeof curr_pid);
476ebfedea0SLionel Sambuc             curr_pid = 0;
477ebfedea0SLionel Sambuc         }
478ebfedea0SLionel Sambuc #endif
479ebfedea0SLionel Sambuc         MD_Update(&m, local_md, MD_DIGEST_LENGTH);
480ebfedea0SLionel Sambuc         MD_Update(&m, (unsigned char *)&(md_c[0]), sizeof(md_c));
481ebfedea0SLionel Sambuc 
482ebfedea0SLionel Sambuc #ifndef PURIFY                  /* purify complains */
483*0a6a1f1dSLionel Sambuc         /*
484*0a6a1f1dSLionel Sambuc          * The following line uses the supplied buffer as a small source of
485*0a6a1f1dSLionel Sambuc          * entropy: since this buffer is often uninitialised it may cause
486*0a6a1f1dSLionel Sambuc          * programs such as purify or valgrind to complain. So for those
487*0a6a1f1dSLionel Sambuc          * builds it is not used: the removal of such a small source of
488*0a6a1f1dSLionel Sambuc          * entropy has negligible impact on security.
489ebfedea0SLionel Sambuc          */
490ebfedea0SLionel Sambuc         MD_Update(&m, buf, j);
491ebfedea0SLionel Sambuc #endif
492ebfedea0SLionel Sambuc 
493ebfedea0SLionel Sambuc         k = (st_idx + MD_DIGEST_LENGTH / 2) - st_num;
494*0a6a1f1dSLionel Sambuc         if (k > 0) {
495ebfedea0SLionel Sambuc             MD_Update(&m, &(state[st_idx]), MD_DIGEST_LENGTH / 2 - k);
496ebfedea0SLionel Sambuc             MD_Update(&m, &(state[0]), k);
497*0a6a1f1dSLionel Sambuc         } else
498ebfedea0SLionel Sambuc             MD_Update(&m, &(state[st_idx]), MD_DIGEST_LENGTH / 2);
499ebfedea0SLionel Sambuc         MD_Final(&m, local_md);
500ebfedea0SLionel Sambuc 
501*0a6a1f1dSLionel Sambuc         for (i = 0; i < MD_DIGEST_LENGTH / 2; i++) {
502*0a6a1f1dSLionel Sambuc             /* may compete with other threads */
503*0a6a1f1dSLionel Sambuc             state[st_idx++] ^= local_md[i];
504ebfedea0SLionel Sambuc             if (st_idx >= st_num)
505ebfedea0SLionel Sambuc                 st_idx = 0;
506ebfedea0SLionel Sambuc             if (i < j)
507ebfedea0SLionel Sambuc                 *(buf++) = local_md[i + MD_DIGEST_LENGTH / 2];
508ebfedea0SLionel Sambuc         }
509ebfedea0SLionel Sambuc     }
510ebfedea0SLionel Sambuc 
511ebfedea0SLionel Sambuc     MD_Init(&m);
512ebfedea0SLionel Sambuc     MD_Update(&m, (unsigned char *)&(md_c[0]), sizeof(md_c));
513ebfedea0SLionel Sambuc     MD_Update(&m, local_md, MD_DIGEST_LENGTH);
514*0a6a1f1dSLionel Sambuc     if (lock)
515ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
516ebfedea0SLionel Sambuc     MD_Update(&m, md, MD_DIGEST_LENGTH);
517ebfedea0SLionel Sambuc     MD_Final(&m, md);
518*0a6a1f1dSLionel Sambuc     if (lock)
519ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
520ebfedea0SLionel Sambuc 
521ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&m);
522ebfedea0SLionel Sambuc     if (ok)
523ebfedea0SLionel Sambuc         return (1);
524ebfedea0SLionel Sambuc     else if (pseudo)
525ebfedea0SLionel Sambuc         return 0;
526*0a6a1f1dSLionel Sambuc     else {
527ebfedea0SLionel Sambuc         RANDerr(RAND_F_SSLEAY_RAND_BYTES, RAND_R_PRNG_NOT_SEEDED);
528ebfedea0SLionel Sambuc         ERR_add_error_data(1, "You need to read the OpenSSL FAQ, "
529ebfedea0SLionel Sambuc                            "http://www.openssl.org/support/faq.html");
530ebfedea0SLionel Sambuc         return (0);
531ebfedea0SLionel Sambuc     }
532ebfedea0SLionel Sambuc }
533ebfedea0SLionel Sambuc 
ssleay_rand_nopseudo_bytes(unsigned char * buf,int num)534ebfedea0SLionel Sambuc static int ssleay_rand_nopseudo_bytes(unsigned char *buf, int num)
535ebfedea0SLionel Sambuc {
536*0a6a1f1dSLionel Sambuc     return ssleay_rand_bytes(buf, num, 0, 1);
537ebfedea0SLionel Sambuc }
538ebfedea0SLionel Sambuc 
539*0a6a1f1dSLionel Sambuc /*
540*0a6a1f1dSLionel Sambuc  * pseudo-random bytes that are guaranteed to be unique but not unpredictable
541*0a6a1f1dSLionel Sambuc  */
ssleay_rand_pseudo_bytes(unsigned char * buf,int num)542ebfedea0SLionel Sambuc static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num)
543ebfedea0SLionel Sambuc {
544*0a6a1f1dSLionel Sambuc     return ssleay_rand_bytes(buf, num, 1, 1);
545ebfedea0SLionel Sambuc }
546ebfedea0SLionel Sambuc 
ssleay_rand_status(void)547ebfedea0SLionel Sambuc static int ssleay_rand_status(void)
548ebfedea0SLionel Sambuc {
549ebfedea0SLionel Sambuc     CRYPTO_THREADID cur;
550ebfedea0SLionel Sambuc     int ret;
551ebfedea0SLionel Sambuc     int do_not_lock;
552ebfedea0SLionel Sambuc 
553ebfedea0SLionel Sambuc     CRYPTO_THREADID_current(&cur);
554*0a6a1f1dSLionel Sambuc     /*
555*0a6a1f1dSLionel Sambuc      * check if we already have the lock (could happen if a RAND_poll()
556*0a6a1f1dSLionel Sambuc      * implementation calls RAND_status())
557*0a6a1f1dSLionel Sambuc      */
558*0a6a1f1dSLionel Sambuc     if (crypto_lock_rand) {
559ebfedea0SLionel Sambuc         CRYPTO_r_lock(CRYPTO_LOCK_RAND2);
560ebfedea0SLionel Sambuc         do_not_lock = !CRYPTO_THREADID_cmp(&locking_threadid, &cur);
561ebfedea0SLionel Sambuc         CRYPTO_r_unlock(CRYPTO_LOCK_RAND2);
562*0a6a1f1dSLionel Sambuc     } else
563ebfedea0SLionel Sambuc         do_not_lock = 0;
564ebfedea0SLionel Sambuc 
565*0a6a1f1dSLionel Sambuc     if (!do_not_lock) {
566ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
567ebfedea0SLionel Sambuc 
568*0a6a1f1dSLionel Sambuc         /*
569*0a6a1f1dSLionel Sambuc          * prevent ssleay_rand_bytes() from trying to obtain the lock again
570*0a6a1f1dSLionel Sambuc          */
571ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_RAND2);
572ebfedea0SLionel Sambuc         CRYPTO_THREADID_cpy(&locking_threadid, &cur);
573ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_RAND2);
574ebfedea0SLionel Sambuc         crypto_lock_rand = 1;
575ebfedea0SLionel Sambuc     }
576ebfedea0SLionel Sambuc 
577*0a6a1f1dSLionel Sambuc     if (!initialized) {
578ebfedea0SLionel Sambuc         RAND_poll();
579*0a6a1f1dSLionel Sambuc         initialized = 1;
580ebfedea0SLionel Sambuc     }
581ebfedea0SLionel Sambuc 
582ebfedea0SLionel Sambuc     ret = entropy >= ENTROPY_NEEDED;
583ebfedea0SLionel Sambuc 
584*0a6a1f1dSLionel Sambuc     if (!do_not_lock) {
585ebfedea0SLionel Sambuc         /* before unlocking, we must clear 'crypto_lock_rand' */
586ebfedea0SLionel Sambuc         crypto_lock_rand = 0;
587ebfedea0SLionel Sambuc 
588ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
589ebfedea0SLionel Sambuc     }
590ebfedea0SLionel Sambuc 
591ebfedea0SLionel Sambuc     return ret;
592ebfedea0SLionel Sambuc }
593