]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rand/md_rand.c
Change functions to ANSI C.
[thirdparty/openssl.git] / crypto / rand / md_rand.c
CommitLineData
d02b48c6 1/* crypto/rand/md_rand.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
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.
8 *
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).
15 *
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.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
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)"
40 *
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
51 * SUCH DAMAGE.
52 *
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.]
57 */
58
59#include <stdio.h>
d02b48c6
RE
60#include <sys/types.h>
61#include <time.h>
a224de3f
BL
62#include <string.h>
63#include "e_os.h"
64#include "crypto.h"
d02b48c6 65
d02b48c6
RE
66#if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND)
67#ifndef NO_MD5
68#define USE_MD5_RAND
69#elif !defined(NO_SHA1)
70#define USE_SHA1_RAND
71#elif !defined(NO_MDC2)
72#define USE_MDC2_RAND
73#elif !defined(NO_MD2)
74#define USE_MD2_RAND
75#else
76We need a message digest of some type
77#endif
78#endif
79
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
84 */
85
86#if defined(USE_MD5_RAND)
87#include "md5.h"
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)
dfeab068 93#define MD(a,b,c) MD5(a,b,c)
d02b48c6
RE
94#elif defined(USE_SHA1_RAND)
95#include "sha.h"
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)
dfeab068 101#define MD(a,b,c) SHA1(a,b,c)
d02b48c6
RE
102#elif defined(USE_MDC2_RAND)
103#include "mdc2.h"
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)
dfeab068 109#define MD(a,b,c) MDC2(a,b,c)
d02b48c6
RE
110#elif defined(USE_MD2_RAND)
111#include "md2.h"
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)
dfeab068 117#define MD(a,b,c) MD2(a,b,c)
d02b48c6
RE
118#endif
119
120#include "rand.h"
121
dfeab068
RE
122/* #define NORAND 1 */
123/* #define PREDICT 1 */
d02b48c6
RE
124
125#define STATE_SIZE 1023
126static int state_num=0,state_index=0;
58964a49 127static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH];
d02b48c6 128static unsigned char md[MD_DIGEST_LENGTH];
dfeab068 129static long md_count[2]={0,0};
d02b48c6 130
e778802f 131const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT;
d02b48c6 132
dfeab068 133static void ssleay_rand_cleanup(void);
bf5dcd13 134static void ssleay_rand_seed(const void *buf, int num);
dfeab068
RE
135static void ssleay_rand_bytes(unsigned char *buf, int num);
136
651d0aff 137RAND_METHOD rand_ssleay_meth={
dfeab068
RE
138 ssleay_rand_seed,
139 ssleay_rand_bytes,
140 ssleay_rand_cleanup,
141 };
142
6b691a5c 143RAND_METHOD *RAND_SSLeay(void)
dfeab068 144 {
651d0aff 145 return(&rand_ssleay_meth);
dfeab068
RE
146 }
147
6b691a5c 148static void ssleay_rand_cleanup(void)
d02b48c6 149 {
58964a49 150 memset(state,0,sizeof(state));
d02b48c6
RE
151 state_num=0;
152 state_index=0;
153 memset(md,0,MD_DIGEST_LENGTH);
dfeab068
RE
154 md_count[0]=0;
155 md_count[1]=0;
d02b48c6
RE
156 }
157
6b691a5c 158static void ssleay_rand_seed(const void *buf, int num)
d02b48c6
RE
159 {
160 int i,j,k,st_idx,st_num;
161 MD_CTX m;
162
163#ifdef NORAND
164 return;
165#endif
166
167 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
168 st_idx=state_index;
169 st_num=state_num;
170
171 state_index=(state_index+num);
58964a49 172 if (state_index >= STATE_SIZE)
d02b48c6
RE
173 {
174 state_index%=STATE_SIZE;
175 state_num=STATE_SIZE;
176 }
177 else if (state_num < STATE_SIZE)
178 {
179 if (state_index > state_num)
180 state_num=state_index;
181 }
182 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
183
184 for (i=0; i<num; i+=MD_DIGEST_LENGTH)
185 {
186 j=(num-i);
187 j=(j > MD_DIGEST_LENGTH)?MD_DIGEST_LENGTH:j;
188
189 MD_Init(&m);
190 MD_Update(&m,md,MD_DIGEST_LENGTH);
191 k=(st_idx+j)-STATE_SIZE;
192 if (k > 0)
193 {
194 MD_Update(&m,&(state[st_idx]),j-k);
195 MD_Update(&m,&(state[0]),k);
196 }
197 else
198 MD_Update(&m,&(state[st_idx]),j);
199
200 MD_Update(&m,buf,j);
dfeab068 201 MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
d02b48c6 202 MD_Final(md,&m);
dfeab068 203 md_count[1]++;
d02b48c6 204
e778802f 205 buf=(const char *)buf + j;
d02b48c6
RE
206
207 for (k=0; k<j; k++)
208 {
209 state[st_idx++]^=md[k];
210 if (st_idx >= STATE_SIZE)
211 {
212 st_idx=0;
213 st_num=STATE_SIZE;
214 }
215 }
216 }
217 memset((char *)&m,0,sizeof(m));
218 }
219
6b691a5c 220static void ssleay_rand_bytes(unsigned char *buf, int num)
d02b48c6
RE
221 {
222 int i,j,k,st_num,st_idx;
223 MD_CTX m;
224 static int init=1;
225 unsigned long l;
226#ifdef DEVRANDOM
227 FILE *fh;
228#endif
229
230#ifdef PREDICT
231 {
232 static unsigned char val=0;
233
234 for (i=0; i<num; i++)
235 buf[i]=val++;
236 return;
237 }
238#endif
239
240 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
241
242 if (init)
243 {
244 init=0;
245 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
246 /* put in some default random data, we need more than
247 * just this */
bf5dcd13 248 RAND_seed(&m,sizeof(m));
d02b48c6
RE
249#ifndef MSDOS
250 l=getpid();
bf5dcd13 251 RAND_seed(&l,sizeof(l));
d02b48c6 252 l=getuid();
bf5dcd13 253 RAND_seed(&l,sizeof(l));
d02b48c6
RE
254#endif
255 l=time(NULL);
bf5dcd13 256 RAND_seed(&l,sizeof(l));
d02b48c6 257
58964a49 258/* #ifdef DEVRANDOM */
d02b48c6
RE
259 /*
260 * Use a random entropy pool device.
261 * Linux 1.3.x and FreeBSD-Current has
262 * this. Use /dev/urandom if you can
263 * as /dev/random will block if it runs out
264 * of random entries.
265 */
266 if ((fh = fopen(DEVRANDOM, "r")) != NULL)
267 {
58964a49 268 unsigned char tmpbuf[32];
d02b48c6 269
58964a49 270 fread((unsigned char *)tmpbuf,1,32,fh);
d02b48c6
RE
271 /* we don't care how many bytes we read,
272 * we will just copy the 'stack' if there is
273 * nothing else :-) */
274 fclose(fh);
58964a49
RE
275 RAND_seed(tmpbuf,32);
276 memset(tmpbuf,0,32);
d02b48c6 277 }
58964a49 278/* #endif */
d02b48c6
RE
279#ifdef PURIFY
280 memset(state,0,STATE_SIZE);
281 memset(md,0,MD_DIGEST_LENGTH);
282#endif
283 CRYPTO_w_lock(CRYPTO_LOCK_RAND);
284 }
285
286 st_idx=state_index;
287 st_num=state_num;
288 state_index+=num;
289 if (state_index > state_num)
290 state_index=(state_index%state_num);
291
292 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
293
294 while (num > 0)
295 {
296 j=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num;
297 num-=j;
298 MD_Init(&m);
299 MD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2);
dfeab068 300 MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
d02b48c6
RE
301#ifndef PURIFY
302 MD_Update(&m,buf,j); /* purify complains */
303#endif
304 k=(st_idx+j)-st_num;
305 if (k > 0)
306 {
307 MD_Update(&m,&(state[st_idx]),j-k);
308 MD_Update(&m,&(state[0]),k);
309 }
310 else
311 MD_Update(&m,&(state[st_idx]),j);
312 MD_Final(md,&m);
313
314 for (i=0; i<j; i++)
315 {
316 if (st_idx >= st_num)
317 st_idx=0;
318 state[st_idx++]^=md[i];
319 *(buf++)=md[i+MD_DIGEST_LENGTH/2];
320 }
321 }
322
323 MD_Init(&m);
dfeab068
RE
324 MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
325 md_count[0]++;
d02b48c6
RE
326 MD_Update(&m,md,MD_DIGEST_LENGTH);
327 MD_Final(md,&m);
328 memset(&m,0,sizeof(m));
329 }
330
331#ifdef WINDOWS
332#include <windows.h>
333#include <rand.h>
334
335/*****************************************************************************
336 * Initialisation function for the SSL random generator. Takes the contents
337 * of the screen as random seed.
338 *
339 * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
340 *
341 * Code adapted from
342 * <URL:http://www.microsoft.com/kb/developr/win_dk/q97193.htm>;
343 * the original copyright message is:
344 *
74d7abc2
RE
345 * (C) Copyright Microsoft Corp. 1993. All rights reserved.
346 *
347 * You have a royalty-free right to use, modify, reproduce and
348 * distribute the Sample Files (and/or any modified version) in
349 * any way you find useful, provided that you agree that
350 * Microsoft has no warranty obligations or liability for any
351 * Sample Application Files which are modified.
d02b48c6
RE
352 */
353/*
354 * I have modified the loading of bytes via RAND_seed() mechanism since
355 * the origional would have been very very CPU intensive since RAND_seed()
356 * does an MD5 per 16 bytes of input. The cost to digest 16 bytes is the same
357 * as that to digest 56 bytes. So under the old system, a screen of
358 * 1024*768*256 would have been CPU cost of approximatly 49,000 56 byte MD5
359 * digests or digesting 2.7 mbytes. What I have put in place would
360 * be 48 16k MD5 digests, or efectivly 48*16+48 MD5 bytes or 816 kbytes
361 * or about 3.5 times as much.
362 * - eric
363 */
364void RAND_screen(void)
365{
366 HDC hScrDC; /* screen DC */
367 HDC hMemDC; /* memory DC */
368 HBITMAP hBitmap; /* handle for our bitmap */
369 HBITMAP hOldBitmap; /* handle for previous bitmap */
370 BITMAP bm; /* bitmap properties */
371 unsigned int size; /* size of bitmap */
372 char *bmbits; /* contents of bitmap */
373 int w; /* screen width */
374 int h; /* screen height */
375 int y; /* y-coordinate of screen lines to grab */
376 int n = 16; /* number of screen lines to grab at a time */
377
378 /* Create a screen DC and a memory DC compatible to screen DC */
379 hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
380 hMemDC = CreateCompatibleDC(hScrDC);
381
382 /* Get screen resolution */
383 w = GetDeviceCaps(hScrDC, HORZRES);
384 h = GetDeviceCaps(hScrDC, VERTRES);
385
386 /* Create a bitmap compatible with the screen DC */
387 hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
388
389 /* Select new bitmap into memory DC */
390 hOldBitmap = SelectObject(hMemDC, hBitmap);
391
392 /* Get bitmap properties */
393 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
394 size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
395
396 bmbits = Malloc(size);
397 if (bmbits) {
398 /* Now go through the whole screen, repeatedly grabbing n lines */
399 for (y = 0; y < h-n; y += n)
400 {
401 unsigned char md[MD_DIGEST_LENGTH];
402
403 /* Bitblt screen DC to memory DC */
404 BitBlt(hMemDC, 0, 0, w, n, hScrDC, 0, y, SRCCOPY);
405
406 /* Copy bitmap bits from memory DC to bmbits */
407 GetBitmapBits(hBitmap, size, bmbits);
408
409 /* Get the MD5 of the bitmap */
dfeab068 410 MD(bmbits,size,md);
d02b48c6
RE
411
412 /* Seed the random generator with the MD5 digest */
413 RAND_seed(md, MD_DIGEST_LENGTH);
414 }
415
416 Free(bmbits);
417 }
418
419 /* Select old bitmap back into memory DC */
420 hBitmap = SelectObject(hMemDC, hOldBitmap);
421
422 /* Clean up */
423 DeleteObject(hBitmap);
424 DeleteDC(hMemDC);
425 DeleteDC(hScrDC);
426}
427#endif