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