app_rand.c revision 72613
159191Skris/* apps/app_rand.c */ 259191Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 359191Skris * All rights reserved. 459191Skris * 559191Skris * This package is an SSL implementation written 659191Skris * by Eric Young (eay@cryptsoft.com). 759191Skris * The implementation was written so as to conform with Netscapes SSL. 859191Skris * 959191Skris * This library is free for commercial and non-commercial use as long as 1059191Skris * the following conditions are aheared to. The following conditions 1159191Skris * apply to all code found in this distribution, be it the RC4, RSA, 1259191Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1359191Skris * included with this distribution is covered by the same copyright terms 1459191Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1559191Skris * 1659191Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1759191Skris * the code are not to be removed. 1859191Skris * If this package is used in a product, Eric Young should be given attribution 1959191Skris * as the author of the parts of the library used. 2059191Skris * This can be in the form of a textual message at program startup or 2159191Skris * in documentation (online or textual) provided with the package. 2259191Skris * 2359191Skris * Redistribution and use in source and binary forms, with or without 2459191Skris * modification, are permitted provided that the following conditions 2559191Skris * are met: 2659191Skris * 1. Redistributions of source code must retain the copyright 2759191Skris * notice, this list of conditions and the following disclaimer. 2859191Skris * 2. Redistributions in binary form must reproduce the above copyright 2959191Skris * notice, this list of conditions and the following disclaimer in the 3059191Skris * documentation and/or other materials provided with the distribution. 3159191Skris * 3. All advertising materials mentioning features or use of this software 3259191Skris * must display the following acknowledgement: 3359191Skris * "This product includes cryptographic software written by 3459191Skris * Eric Young (eay@cryptsoft.com)" 3559191Skris * The word 'cryptographic' can be left out if the rouines from the library 3659191Skris * being used are not cryptographic related :-). 3759191Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3859191Skris * the apps directory (application code) you must include an acknowledgement: 3959191Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4059191Skris * 4159191Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4259191Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4359191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4459191Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4559191Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4659191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4759191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4859191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4959191Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5059191Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5159191Skris * SUCH DAMAGE. 5259191Skris * 5359191Skris * The licence and distribution terms for any publically available version or 5459191Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5559191Skris * copied and put under another distribution licence 5659191Skris * [including the GNU Public Licence.] 5759191Skris */ 5859191Skris/* ==================================================================== 5959191Skris * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 6059191Skris * 6159191Skris * Redistribution and use in source and binary forms, with or without 6259191Skris * modification, are permitted provided that the following conditions 6359191Skris * are met: 6459191Skris * 6559191Skris * 1. Redistributions of source code must retain the above copyright 6659191Skris * notice, this list of conditions and the following disclaimer. 6759191Skris * 6859191Skris * 2. Redistributions in binary form must reproduce the above copyright 6959191Skris * notice, this list of conditions and the following disclaimer in 7059191Skris * the documentation and/or other materials provided with the 7159191Skris * distribution. 7259191Skris * 7359191Skris * 3. All advertising materials mentioning features or use of this 7459191Skris * software must display the following acknowledgment: 7559191Skris * "This product includes software developed by the OpenSSL Project 7659191Skris * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 7759191Skris * 7859191Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 7959191Skris * endorse or promote products derived from this software without 8059191Skris * prior written permission. For written permission, please contact 8159191Skris * openssl-core@openssl.org. 8259191Skris * 8359191Skris * 5. Products derived from this software may not be called "OpenSSL" 8459191Skris * nor may "OpenSSL" appear in their names without prior written 8559191Skris * permission of the OpenSSL Project. 8659191Skris * 8759191Skris * 6. Redistributions of any form whatsoever must retain the following 8859191Skris * acknowledgment: 8959191Skris * "This product includes software developed by the OpenSSL Project 9059191Skris * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 9159191Skris * 9259191Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 9359191Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9459191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 9559191Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 9659191Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 9759191Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 9859191Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 9959191Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 10059191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 10159191Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10259191Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 10359191Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 10459191Skris * ==================================================================== 10559191Skris * 10659191Skris * This product includes cryptographic software written by Eric Young 10759191Skris * (eay@cryptsoft.com). This product includes software written by Tim 10859191Skris * Hudson (tjh@cryptsoft.com). 10959191Skris * 11059191Skris */ 11159191Skris 11259191Skris#define NON_MAIN 11359191Skris#include "apps.h" 11459191Skris#undef NON_MAIN 11559191Skris#include <openssl/bio.h> 11659191Skris#include <openssl/rand.h> 11759191Skris 11859191Skris 11959191Skrisstatic int seeded = 0; 12059191Skrisstatic int egdsocket = 0; 12159191Skris 12259191Skrisint app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn) 12359191Skris { 12459191Skris int consider_randfile = (file == NULL); 12559191Skris char buffer[200]; 12659191Skris 12759191Skris#ifdef WINDOWS 12859191Skris BIO_printf(bio_e,"Loading 'screen' into random state -"); 12959191Skris BIO_flush(bio_e); 13059191Skris RAND_screen(); 13159191Skris BIO_printf(bio_e," done\n"); 13259191Skris#endif 13359191Skris 13459191Skris if (file == NULL) 13559191Skris file = RAND_file_name(buffer, sizeof buffer); 13659191Skris else if (RAND_egd(file) > 0) 13759191Skris { 13859191Skris /* we try if the given filename is an EGD socket. 13959191Skris if it is, we don't write anything back to the file. */ 14059191Skris egdsocket = 1; 14159191Skris return 1; 14259191Skris } 14359191Skris if (file == NULL || !RAND_load_file(file, -1)) 14459191Skris { 14559191Skris if (RAND_status() == 0 && !dont_warn) 14659191Skris { 14759191Skris BIO_printf(bio_e,"unable to load 'random state'\n"); 14859191Skris BIO_printf(bio_e,"This means that the random number generator has not been seeded\n"); 14959191Skris BIO_printf(bio_e,"with much random data.\n"); 15059191Skris if (consider_randfile) /* explanation does not apply when a file is explicitly named */ 15159191Skris { 15259191Skris BIO_printf(bio_e,"Consider setting the RANDFILE environment variable to point at a file that\n"); 15359191Skris BIO_printf(bio_e,"'random' data can be kept in (the file will be overwritten).\n"); 15459191Skris } 15559191Skris } 15659191Skris return 0; 15759191Skris } 15859191Skris seeded = 1; 15959191Skris return 1; 16059191Skris } 16159191Skris 16259191Skrislong app_RAND_load_files(char *name) 16359191Skris { 16459191Skris char *p,*n; 16559191Skris int last; 16659191Skris long tot=0; 16759191Skris int egd; 16859191Skris 16959191Skris for (;;) 17059191Skris { 17159191Skris last=0; 17259191Skris for (p=name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++); 17359191Skris if (*p == '\0') last=1; 17459191Skris *p='\0'; 17559191Skris n=name; 17659191Skris name=p+1; 17759191Skris if (*n == '\0') break; 17859191Skris 17959191Skris egd=RAND_egd(n); 18072613Skris if (egd > 0) 18172613Skris tot+=egd; 18272613Skris else 18372613Skris tot+=RAND_load_file(n,-1); 18459191Skris if (last) break; 18559191Skris } 18659191Skris if (tot > 512) 18759191Skris app_RAND_allow_write_file(); 18859191Skris return(tot); 18959191Skris } 19059191Skris 19159191Skrisint app_RAND_write_file(const char *file, BIO *bio_e) 19259191Skris { 19359191Skris char buffer[200]; 19459191Skris 19559191Skris if (egdsocket || !seeded) 19659191Skris /* If we did not manage to read the seed file, 19759191Skris * we should not write a low-entropy seed file back -- 19859191Skris * it would suppress a crucial warning the next time 19959191Skris * we want to use it. */ 20059191Skris return 0; 20159191Skris 20259191Skris if (file == NULL) 20359191Skris file = RAND_file_name(buffer, sizeof buffer); 20459191Skris if (file == NULL || !RAND_write_file(file)) 20559191Skris { 20659191Skris BIO_printf(bio_e,"unable to write 'random state'\n"); 20759191Skris return 0; 20859191Skris } 20959191Skris return 1; 21059191Skris } 21159191Skris 21259191Skrisvoid app_RAND_allow_write_file(void) 21359191Skris { 21459191Skris seeded = 1; 21559191Skris } 216