1 /* crypto/rand/md_rand.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
60 #include <sys/types.h>
66 #if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND)
69 #elif !defined(NO_SHA1)
71 #elif !defined(NO_MDC2)
73 #elif !defined(NO_MD2)
76 We need a message digest of some type
80 /* Changed how the state buffer used. I now attempt to 'wrap' such
81 * that I don't run over the same locations the next time go through
82 * the 1023 bytes - many thanks to
83 * Robert J. LeBlanc <rjl@renaissoft.com> for his comments
86 #if defined(USE_MD5_RAND)
88 #define MD_DIGEST_LENGTH MD5_DIGEST_LENGTH
89 #define MD_CTX MD5_CTX
90 #define MD_Init(a) MD5_Init(a)
91 #define MD_Update(a,b,c) MD5_Update(a,b,c)
92 #define MD_Final(a,b) MD5_Final(a,b)
93 #define MD(a,b,c) MD5(a,b,c)
94 #elif defined(USE_SHA1_RAND)
96 #define MD_DIGEST_LENGTH SHA_DIGEST_LENGTH
97 #define MD_CTX SHA_CTX
98 #define MD_Init(a) SHA1_Init(a)
99 #define MD_Update(a,b,c) SHA1_Update(a,b,c)
100 #define MD_Final(a,b) SHA1_Final(a,b)
101 #define MD(a,b,c) SHA1(a,b,c)
102 #elif defined(USE_MDC2_RAND)
104 #define MD_DIGEST_LENGTH MDC2_DIGEST_LENGTH
105 #define MD_CTX MDC2_CTX
106 #define MD_Init(a) MDC2_Init(a)
107 #define MD_Update(a,b,c) MDC2_Update(a,b,c)
108 #define MD_Final(a,b) MDC2_Final(a,b)
109 #define MD(a,b,c) MDC2(a,b,c)
110 #elif defined(USE_MD2_RAND)
112 #define MD_DIGEST_LENGTH MD2_DIGEST_LENGTH
113 #define MD_CTX MD2_CTX
114 #define MD_Init(a) MD2_Init(a)
115 #define MD_Update(a,b,c) MD2_Update(a,b,c)
116 #define MD_Final(a,b) MD2_Final(a,b)
117 #define MD(a,b,c) MD2(a,b,c)
122 /* #define NORAND 1 */
123 /* #define PREDICT 1 */
125 #define STATE_SIZE 1023
126 static int state_num=0,state_index=0;
127 static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH];
128 static unsigned char md[MD_DIGEST_LENGTH];
129 static long md_count[2]={0,0};
131 char *RAND_version="RAND part of OpenSSL 0.9.2 31-Dec-1998";
133 static void ssleay_rand_cleanup(void);
134 static void ssleay_rand_seed(const void *buf, int num);
135 static void ssleay_rand_bytes(unsigned char *buf, int num);
137 RAND_METHOD rand_ssleay_meth={
143 RAND_METHOD *RAND_SSLeay()
145 return(&rand_ssleay_meth);
148 static void ssleay_rand_cleanup()
150 memset(state,0,sizeof(state));
153 memset(md,0,MD_DIGEST_LENGTH);
158 static void ssleay_rand_seed(buf,num)
162 int i,j,k,st_idx,st_num;
169 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
173 state_index=(state_index+num);
174 if (state_index >= STATE_SIZE)
176 state_index%=STATE_SIZE;
177 state_num=STATE_SIZE;
179 else if (state_num < STATE_SIZE)
181 if (state_index > state_num)
182 state_num=state_index;
184 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
186 for (i=0; i<num; i+=MD_DIGEST_LENGTH)
189 j=(j > MD_DIGEST_LENGTH)?MD_DIGEST_LENGTH:j;
192 MD_Update(&m,md,MD_DIGEST_LENGTH);
193 k=(st_idx+j)-STATE_SIZE;
196 MD_Update(&m,&(state[st_idx]),j-k);
197 MD_Update(&m,&(state[0]),k);
200 MD_Update(&m,&(state[st_idx]),j);
203 MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
211 state[st_idx++]^=md[k];
212 if (st_idx >= STATE_SIZE)
219 memset((char *)&m,0,sizeof(m));
222 static void ssleay_rand_bytes(buf,num)
226 int i,j,k,st_num,st_idx;
236 static unsigned char val=0;
238 for (i=0; i<num; i++)
244 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
249 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
250 /* put in some default random data, we need more than
252 RAND_seed(&m,sizeof(m));
255 RAND_seed(&l,sizeof(l));
257 RAND_seed(&l,sizeof(l));
260 RAND_seed(&l,sizeof(l));
262 /* #ifdef DEVRANDOM */
264 * Use a random entropy pool device.
265 * Linux 1.3.x and FreeBSD-Current has
266 * this. Use /dev/urandom if you can
267 * as /dev/random will block if it runs out
270 if ((fh = fopen(DEVRANDOM, "r")) != NULL)
272 unsigned char tmpbuf[32];
274 fread((unsigned char *)tmpbuf,1,32,fh);
275 /* we don't care how many bytes we read,
276 * we will just copy the 'stack' if there is
277 * nothing else :-) */
279 RAND_seed(tmpbuf,32);
284 memset(state,0,STATE_SIZE);
285 memset(md,0,MD_DIGEST_LENGTH);
287 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
293 if (state_index > state_num)
294 state_index=(state_index%state_num);
296 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
300 j=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num;
303 MD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2);
304 MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
306 MD_Update(&m,buf,j); /* purify complains */
311 MD_Update(&m,&(state[st_idx]),j-k);
312 MD_Update(&m,&(state[0]),k);
315 MD_Update(&m,&(state[st_idx]),j);
320 if (st_idx >= st_num)
322 state[st_idx++]^=md[i];
323 *(buf++)=md[i+MD_DIGEST_LENGTH/2];
328 MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
330 MD_Update(&m,md,MD_DIGEST_LENGTH);
332 memset(&m,0,sizeof(m));
339 /*****************************************************************************
340 * Initialisation function for the SSL random generator. Takes the contents
341 * of the screen as random seed.
343 * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
346 * <URL:http://www.microsoft.com/kb/developr/win_dk/q97193.htm>;
347 * the original copyright message is:
349 * (C) Copyright Microsoft Corp. 1993. All rights reserved.
351 * You have a royalty-free right to use, modify, reproduce and
352 * distribute the Sample Files (and/or any modified version) in
353 * any way you find useful, provided that you agree that
354 * Microsoft has no warranty obligations or liability for any
355 * Sample Application Files which are modified.
358 * I have modified the loading of bytes via RAND_seed() mechanism since
359 * the origional would have been very very CPU intensive since RAND_seed()
360 * does an MD5 per 16 bytes of input. The cost to digest 16 bytes is the same
361 * as that to digest 56 bytes. So under the old system, a screen of
362 * 1024*768*256 would have been CPU cost of approximatly 49,000 56 byte MD5
363 * digests or digesting 2.7 mbytes. What I have put in place would
364 * be 48 16k MD5 digests, or efectivly 48*16+48 MD5 bytes or 816 kbytes
365 * or about 3.5 times as much.
368 void RAND_screen(void)
370 HDC hScrDC; /* screen DC */
371 HDC hMemDC; /* memory DC */
372 HBITMAP hBitmap; /* handle for our bitmap */
373 HBITMAP hOldBitmap; /* handle for previous bitmap */
374 BITMAP bm; /* bitmap properties */
375 unsigned int size; /* size of bitmap */
376 char *bmbits; /* contents of bitmap */
377 int w; /* screen width */
378 int h; /* screen height */
379 int y; /* y-coordinate of screen lines to grab */
380 int n = 16; /* number of screen lines to grab at a time */
382 /* Create a screen DC and a memory DC compatible to screen DC */
383 hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
384 hMemDC = CreateCompatibleDC(hScrDC);
386 /* Get screen resolution */
387 w = GetDeviceCaps(hScrDC, HORZRES);
388 h = GetDeviceCaps(hScrDC, VERTRES);
390 /* Create a bitmap compatible with the screen DC */
391 hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
393 /* Select new bitmap into memory DC */
394 hOldBitmap = SelectObject(hMemDC, hBitmap);
396 /* Get bitmap properties */
397 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
398 size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
400 bmbits = Malloc(size);
402 /* Now go through the whole screen, repeatedly grabbing n lines */
403 for (y = 0; y < h-n; y += n)
405 unsigned char md[MD_DIGEST_LENGTH];
407 /* Bitblt screen DC to memory DC */
408 BitBlt(hMemDC, 0, 0, w, n, hScrDC, 0, y, SRCCOPY);
410 /* Copy bitmap bits from memory DC to bmbits */
411 GetBitmapBits(hBitmap, size, bmbits);
413 /* Get the MD5 of the bitmap */
416 /* Seed the random generator with the MD5 digest */
417 RAND_seed(md, MD_DIGEST_LENGTH);
423 /* Select old bitmap back into memory DC */
424 hBitmap = SelectObject(hMemDC, hOldBitmap);
427 DeleteObject(hBitmap);