summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rand/md_rand.c
diff options
context:
space:
mode:
authorbeck <>2000-04-15 06:18:51 +0000
committerbeck <>2000-04-15 06:18:51 +0000
commitb608c7f2b175e121f2c22d53341a317153afdc8e (patch)
treee94b160b3fcd8180df79e4251d68d24d665f0195 /src/lib/libcrypto/rand/md_rand.c
parentc8d6701c396cebdcd0d45eac73b762e9498f6b01 (diff)
downloadopenbsd-b608c7f2b175e121f2c22d53341a317153afdc8e.tar.gz
openbsd-b608c7f2b175e121f2c22d53341a317153afdc8e.tar.bz2
openbsd-b608c7f2b175e121f2c22d53341a317153afdc8e.zip
OpenSSL 0.9.5a merge
Diffstat (limited to 'src/lib/libcrypto/rand/md_rand.c')
-rw-r--r--src/lib/libcrypto/rand/md_rand.c133
1 files changed, 125 insertions, 8 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.