app_rand.c revision 59191
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. 8280304Sjkim * 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). 15280304Sjkim * 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. 22280304Sjkim * 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 :-). 37280304Sjkim * 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)" 40280304Sjkim * 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. 52280304Sjkim * 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 66280304Sjkim * 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 122280304Sjkimint app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn) 123280304Sjkim { 124280304Sjkim int consider_randfile = (file == NULL); 125280304Sjkim char buffer[200]; 126109998Smarkm 127280304Sjkim#ifdef WINDOWS 128280304Sjkim BIO_printf(bio_e,"Loading 'screen' into random state -"); 129280304Sjkim BIO_flush(bio_e); 130280304Sjkim RAND_screen(); 13159191Skris BIO_printf(bio_e," done\n"); 13259191Skris#endif 133280304Sjkim 134280304Sjkim if (file == NULL) 135280304Sjkim file = RAND_file_name(buffer, sizeof buffer); 136280304Sjkim else if (RAND_egd(file) > 0) 137280304Sjkim { 138280304Sjkim /* we try if the given filename is an EGD socket. 139280304Sjkim if it is, we don't write anything back to the file. */ 140280304Sjkim egdsocket = 1; 141280304Sjkim return 1; 142280304Sjkim } 143280304Sjkim if (file == NULL || !RAND_load_file(file, -1)) 144280304Sjkim { 145280304Sjkim if (RAND_status() == 0 && !dont_warn) 146280304Sjkim { 147280304Sjkim BIO_printf(bio_e,"unable to load 'random state'\n"); 148280304Sjkim BIO_printf(bio_e,"This means that the random number generator has not been seeded\n"); 149280304Sjkim BIO_printf(bio_e,"with much random data.\n"); 150280304Sjkim if (consider_randfile) /* explanation does not apply when a file is explicitly named */ 151280304Sjkim { 152280304Sjkim BIO_printf(bio_e,"Consider setting the RANDFILE environment variable to point at a file that\n"); 153280304Sjkim BIO_printf(bio_e,"'random' data can be kept in (the file will be overwritten).\n"); 154280304Sjkim } 155280304Sjkim } 156280304Sjkim return 0; 157280304Sjkim } 158280304Sjkim seeded = 1; 159280304Sjkim return 1; 160280304Sjkim } 161280304Sjkim 162280304Sjkimlong app_RAND_load_files(char *name) 163280304Sjkim { 16459191Skris char *p,*n; 16559191Skris int last; 166280304Sjkim long tot=0; 167280304Sjkim int egd; 168280304Sjkim 169280304Sjkim for (;;) 170280304Sjkim { 17159191Skris last=0; 172280304Sjkim for (p=name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++); 173280304Sjkim if (*p == '\0') last=1; 174280304Sjkim *p='\0'; 175280304Sjkim n=name; 176280304Sjkim name=p+1; 177280304Sjkim if (*n == '\0') break; 178280304Sjkim 179280304Sjkim egd=RAND_egd(n); 180280304Sjkim if (egd > 0) tot+=egd; 181280304Sjkim tot+=RAND_load_file(n,-1); 18259191Skris if (last) break; 183280304Sjkim } 184280304Sjkim if (tot > 512) 185280304Sjkim app_RAND_allow_write_file(); 186280304Sjkim return(tot); 187280304Sjkim } 188280304Sjkim 189280304Sjkimint app_RAND_write_file(const char *file, BIO *bio_e) 190280304Sjkim { 191280304Sjkim char buffer[200]; 192280304Sjkim 193280304Sjkim if (egdsocket || !seeded) 194280304Sjkim /* If we did not manage to read the seed file, 195280304Sjkim * we should not write a low-entropy seed file back -- 19659191Skris * it would suppress a crucial warning the next time 197280304Sjkim * we want to use it. */ 198280304Sjkim return 0; 19959191Skris 200280304Sjkim if (file == NULL) 201280304Sjkim file = RAND_file_name(buffer, sizeof buffer); 202280304Sjkim if (file == NULL || !RAND_write_file(file)) 203280304Sjkim { 204280304Sjkim BIO_printf(bio_e,"unable to write 'random state'\n"); 205280304Sjkim return 0; 206280304Sjkim } 20759191Skris return 1; 208280304Sjkim } 209280304Sjkim 210280304Sjkimvoid app_RAND_allow_write_file(void) 211280304Sjkim { 212280304Sjkim seeded = 1; 213280304Sjkim } 214280304Sjkim