summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rand
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/rand')
-rw-r--r--src/lib/libcrypto/rand/md_rand.c133
-rw-r--r--src/lib/libcrypto/rand/rand.h9
-rw-r--r--src/lib/libcrypto/rand/rand_egd.c2
-rw-r--r--src/lib/libcrypto/rand/rand_err.c5
-rw-r--r--src/lib/libcrypto/rand/rand_lib.c7
-rw-r--r--src/lib/libcrypto/rand/randfile.c50
6 files changed, 158 insertions, 48 deletions
diff --git a/src/lib/libcrypto/rand/md_rand.c b/src/lib/libcrypto/rand/md_rand.c
index 6b158f0349..da4258c479 100644
--- a/src/lib/libcrypto/rand/md_rand.c
+++ b/src/lib/libcrypto/rand/md_rand.c
@@ -55,6 +55,59 @@
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58/* ====================================================================
59 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
58 111
59#define ENTROPY_NEEDED 16 /* require 128 bits = 16 bytes of randomness */ 112#define ENTROPY_NEEDED 16 /* require 128 bits = 16 bytes of randomness */
60 113
@@ -130,6 +183,10 @@
130 183
131#include <openssl/rand.h> 184#include <openssl/rand.h>
132 185
186#ifdef BN_DEBUG
187# define PREDICT
188#endif
189
133/* #define NORAND 1 */ 190/* #define NORAND 1 */
134/* #define PREDICT 1 */ 191/* #define PREDICT 1 */
135 192
@@ -141,6 +198,10 @@ static long md_count[2]={0,0};
141static double entropy=0; 198static double entropy=0;
142static int initialized=0; 199static int initialized=0;
143 200
201#ifdef PREDICT
202int rand_predictable=0;
203#endif
204
144const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT; 205const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT;
145 206
146static void ssleay_rand_cleanup(void); 207static void ssleay_rand_cleanup(void);
@@ -148,6 +209,7 @@ static void ssleay_rand_seed(const void *buf, int num);
148static void ssleay_rand_add(const void *buf, int num, double add_entropy); 209static void ssleay_rand_add(const void *buf, int num, double add_entropy);
149static int ssleay_rand_bytes(unsigned char *buf, int num); 210static int ssleay_rand_bytes(unsigned char *buf, int num);
150static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num); 211static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num);
212static int ssleay_rand_status(void);
151 213
152RAND_METHOD rand_ssleay_meth={ 214RAND_METHOD rand_ssleay_meth={
153 ssleay_rand_seed, 215 ssleay_rand_seed,
@@ -155,6 +217,7 @@ RAND_METHOD rand_ssleay_meth={
155 ssleay_rand_cleanup, 217 ssleay_rand_cleanup,
156 ssleay_rand_add, 218 ssleay_rand_add,
157 ssleay_rand_pseudo_bytes, 219 ssleay_rand_pseudo_bytes,
220 ssleay_rand_status
158 }; 221 };
159 222
160RAND_METHOD *RAND_SSLeay(void) 223RAND_METHOD *RAND_SSLeay(void)
@@ -306,6 +369,10 @@ static void ssleay_rand_initialize(void)
306 FILE *fh; 369 FILE *fh;
307#endif 370#endif
308 371
372#ifdef NORAND
373 return;
374#endif
375
309 CRYPTO_w_unlock(CRYPTO_LOCK_RAND); 376 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
310 /* put in some default random data, we need more than just this */ 377 /* put in some default random data, we need more than just this */
311#ifndef GETPID_IS_MEANINGLESS 378#ifndef GETPID_IS_MEANINGLESS
@@ -354,13 +421,14 @@ static int ssleay_rand_bytes(unsigned char *buf, int num)
354#endif 421#endif
355 422
356#ifdef PREDICT 423#ifdef PREDICT
357 { 424 if (rand_predictable)
358 static unsigned char val=0; 425 {
426 static unsigned char val=0;
359 427
360 for (i=0; i<num; i++) 428 for (i=0; i<num; i++)
361 buf[i]=val++; 429 buf[i]=val++;
362 return(1); 430 return(1);
363 } 431 }
364#endif 432#endif
365 433
366 /* 434 /*
@@ -489,17 +557,66 @@ static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num)
489 return (ret); 557 return (ret);
490 } 558 }
491 559
492int RAND_status(void) 560static int ssleay_rand_status(void)
493 { 561 {
562 int ret;
563
564 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
565
494 if (!initialized) 566 if (!initialized)
495 ssleay_rand_initialize(); 567 ssleay_rand_initialize();
496 return (entropy >= ENTROPY_NEEDED); 568 ret = entropy >= ENTROPY_NEEDED;
569
570 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
571
572 return ret;
497 } 573 }
498 574
499#ifdef WINDOWS 575#ifdef WINDOWS
500#include <windows.h> 576#include <windows.h>
501#include <openssl/rand.h> 577#include <openssl/rand.h>
502 578
579int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
580 {
581 double add_entropy=0;
582 SYSTEMTIME t;
583
584 switch (iMsg)
585 {
586 case WM_KEYDOWN:
587 {
588 static WPARAM key;
589 if (key != wParam)
590 add_entropy = 0.05;
591 key = wParam;
592 }
593 break;
594 case WM_MOUSEMOVE:
595 {
596 static int lastx,lasty,lastdx,lastdy;
597 int x,y,dx,dy;
598
599 x=LOWORD(lParam);
600 y=HIWORD(lParam);
601 dx=lastx-x;
602 dy=lasty-y;
603 if (dx != 0 && dy != 0 && dx-lastdx != 0 && dy-lastdy != 0)
604 add_entropy=.2;
605 lastx=x, lasty=y;
606 lastdx=dx, lastdy=dy;
607 }
608 break;
609 }
610
611 GetSystemTime(&t);
612 RAND_add(&iMsg, sizeof(iMsg), add_entropy);
613 RAND_add(&wParam, sizeof(wParam), 0);
614 RAND_add(&lParam, sizeof(lParam), 0);
615 RAND_add(&t, sizeof(t), 0);
616
617 return (RAND_status());
618 }
619
503/***************************************************************************** 620/*****************************************************************************
504 * Initialisation function for the SSL random generator. Takes the contents 621 * Initialisation function for the SSL random generator. Takes the contents
505 * of the screen as random seed. 622 * of the screen as random seed.
diff --git a/src/lib/libcrypto/rand/rand.h b/src/lib/libcrypto/rand/rand.h
index 28f45ec052..2973ee90e4 100644
--- a/src/lib/libcrypto/rand/rand.h
+++ b/src/lib/libcrypto/rand/rand.h
@@ -70,8 +70,13 @@ typedef struct rand_meth_st
70 void (*cleanup)(void); 70 void (*cleanup)(void);
71 void (*add)(const void *buf, int num, double entropy); 71 void (*add)(const void *buf, int num, double entropy);
72 int (*pseudorand)(unsigned char *buf, int num); 72 int (*pseudorand)(unsigned char *buf, int num);
73 int (*status)(void);
73 } RAND_METHOD; 74 } RAND_METHOD;
74 75
76#ifdef BN_DEBUG
77extern int rand_predictable;
78#endif
79
75void RAND_set_rand_method(RAND_METHOD *meth); 80void RAND_set_rand_method(RAND_METHOD *meth);
76RAND_METHOD *RAND_get_rand_method(void ); 81RAND_METHOD *RAND_get_rand_method(void );
77RAND_METHOD *RAND_SSLeay(void); 82RAND_METHOD *RAND_SSLeay(void);
@@ -85,8 +90,10 @@ int RAND_write_file(const char *file);
85const char *RAND_file_name(char *file,int num); 90const char *RAND_file_name(char *file,int num);
86int RAND_status(void); 91int RAND_status(void);
87int RAND_egd(const char *path); 92int RAND_egd(const char *path);
88#ifdef WINDOWS 93#if defined(WINDOWS) || defined(WIN32)
94#include <windows.h>
89void RAND_screen(void); 95void RAND_screen(void);
96int RAND_event(UINT, WPARAM, LPARAM);
90#endif 97#endif
91void ERR_load_RAND_strings(void); 98void ERR_load_RAND_strings(void);
92 99
diff --git a/src/lib/libcrypto/rand/rand_egd.c b/src/lib/libcrypto/rand/rand_egd.c
index d834408bd4..380c7828c3 100644
--- a/src/lib/libcrypto/rand/rand_egd.c
+++ b/src/lib/libcrypto/rand/rand_egd.c
@@ -97,7 +97,7 @@ int RAND_egd(const char *path)
97 buf[1] = 255; 97 buf[1] = 255;
98 write(fd, buf, 2); 98 write(fd, buf, 2);
99 if (read(fd, buf, 1) != 1) goto err; 99 if (read(fd, buf, 1) != 1) goto err;
100 if (buf[0] == 0) goto err; 100 if (buf[0] == 0) goto err;
101 num = read(fd, buf, 255); 101 num = read(fd, buf, 255);
102 if (num < 1) goto err; 102 if (num < 1) goto err;
103 RAND_seed(buf, num); 103 RAND_seed(buf, num);
diff --git a/src/lib/libcrypto/rand/rand_err.c b/src/lib/libcrypto/rand/rand_err.c
index d1263edf80..1af0aa0b8a 100644
--- a/src/lib/libcrypto/rand/rand_err.c
+++ b/src/lib/libcrypto/rand/rand_err.c
@@ -54,7 +54,8 @@
54 */ 54 */
55 55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes 56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file. 57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
58 */ 59 */
59 60
60#include <stdio.h> 61#include <stdio.h>
@@ -71,7 +72,7 @@ static ERR_STRING_DATA RAND_str_functs[]=
71 72
72static ERR_STRING_DATA RAND_str_reasons[]= 73static ERR_STRING_DATA RAND_str_reasons[]=
73 { 74 {
74{RAND_R_PRNG_NOT_SEEDED ,"prng not seeded"}, 75{RAND_R_PRNG_NOT_SEEDED ,"PRNG not seeded"},
75{0,NULL} 76{0,NULL}
76 }; 77 };
77 78
diff --git a/src/lib/libcrypto/rand/rand_lib.c b/src/lib/libcrypto/rand/rand_lib.c
index b09a300c46..7da74aab0e 100644
--- a/src/lib/libcrypto/rand/rand_lib.c
+++ b/src/lib/libcrypto/rand/rand_lib.c
@@ -108,3 +108,10 @@ int RAND_pseudo_bytes(unsigned char *buf, int num)
108 return rand_meth->pseudorand(buf,num); 108 return rand_meth->pseudorand(buf,num);
109 return(-1); 109 return(-1);
110 } 110 }
111
112int RAND_status(void)
113 {
114 if (rand_meth != NULL)
115 return rand_meth->status();
116 return 0;
117 }
diff --git a/src/lib/libcrypto/rand/randfile.c b/src/lib/libcrypto/rand/randfile.c
index 658a8d6b65..9ff3974cc7 100644
--- a/src/lib/libcrypto/rand/randfile.c
+++ b/src/lib/libcrypto/rand/randfile.c
@@ -133,35 +133,20 @@ int RAND_write_file(const char *file)
133 int i,ret=0,err=0; 133 int i,ret=0,err=0;
134 FILE *out = NULL; 134 FILE *out = NULL;
135 int n; 135 int n;
136 136
137#ifdef VMS 137#if defined(O_CREAT) && defined(O_EXCL) && !defined(WIN32)
138 /* Under VMS, fopen(file, "wb") will create a new version of the 138 /* For some reason Win32 can't write to files created this way */
139 same file. This is not good, so let's try updating an existing 139
140 one, and create file only if it doesn't already exist. */ 140 /* chmod(..., 0600) is too late to protect the file,
141 /* At the same time, if we just update a file, we also need to 141 * permissions should be restrictive from the start */
142 truncate it, and unfortunately, ftruncate() and truncate() do 142 int fd = open(file, O_CREAT | O_EXCL, 0600);
143 not exist everywhere. All that remains is to delete old versions 143 if (fd != -1)
144 of the random data file (done at the end). */ 144 out = fdopen(fd, "wb");
145#if 0
146 out=fopen(file,"rb+");
147 if (out == NULL && errno != ENOENT)
148 goto err;
149#endif
150#endif 145#endif
146 if (out == NULL)
147 out = fopen(file,"wb");
148 if (out == NULL) goto err;
151 149
152 if (out == NULL)
153 {
154#if defined O_CREAT && defined O_EXCL
155 /* chmod(..., 0600) is too late to protect the file,
156 * permissions should be restrictive from the start */
157 int fd = open(file, O_CREAT | O_EXCL, 0600);
158 if (fd != -1)
159 out = fdopen(fd, "wb");
160#else
161 out=fopen(file,"wb");
162#endif
163 }
164 if (out == NULL) goto err;
165#ifndef NO_CHMOD 150#ifndef NO_CHMOD
166 chmod(file,0600); 151 chmod(file,0600);
167#endif 152#endif
@@ -180,14 +165,8 @@ int RAND_write_file(const char *file)
180 } 165 }
181 ret+=i; 166 ret+=i;
182 if (n <= 0) break; 167 if (n <= 0) break;
183 } 168 }
184#ifdef VMS 169#ifdef VMS
185 /* We may have updated an existing file using mode "rb+",
186 * now remove any old extra bytes */
187#if 0
188 if (ret > 0)
189 ftruncate(fileno(out), ret);
190#else
191 /* Try to delete older versions of the file, until there aren't 170 /* Try to delete older versions of the file, until there aren't
192 any */ 171 any */
193 { 172 {
@@ -205,8 +184,7 @@ int RAND_write_file(const char *file)
205 some point... */ 184 some point... */
206 } 185 }
207 } 186 }
208#endif 187#endif /* VMS */
209#endif
210 188
211 fclose(out); 189 fclose(out);
212 memset(buf,0,BUFSIZE); 190 memset(buf,0,BUFSIZE);