md_rand.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /* crypto/rand/md_rand.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  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. * 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. */
  111. #define ENTROPY_NEEDED 16 /* require 128 bits = 16 bytes of randomness */
  112. #ifndef MD_RAND_DEBUG
  113. # ifndef NDEBUG
  114. # define NDEBUG
  115. # endif
  116. #endif
  117. #include <assert.h>
  118. #include <stdio.h>
  119. #include <time.h>
  120. #include <string.h>
  121. #include "openssl/e_os.h"
  122. #include <openssl/crypto.h>
  123. #include <openssl/err.h>
  124. #if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND)
  125. #if !defined(NO_SHA) && !defined(NO_SHA1)
  126. #define USE_SHA1_RAND
  127. #elif !defined(NO_MD5)
  128. #define USE_MD5_RAND
  129. #elif !defined(NO_MDC2) && !defined(NO_DES)
  130. #define USE_MDC2_RAND
  131. #elif !defined(NO_MD2)
  132. #define USE_MD2_RAND
  133. #else
  134. #error No message digest algorithm available
  135. #endif
  136. #endif
  137. /* Changed how the state buffer used. I now attempt to 'wrap' such
  138. * that I don't run over the same locations the next time go through
  139. * the 1023 bytes - many thanks to
  140. * Robert J. LeBlanc <rjl@renaissoft.com> for his comments
  141. */
  142. #if defined(USE_MD5_RAND)
  143. #include <openssl/md5.h>
  144. #define MD_DIGEST_LENGTH MD5_DIGEST_LENGTH
  145. #define MD_CTX MD5_CTX
  146. #define MD_Init(a) MD5_Init(a)
  147. #define MD_Update(a,b,c) MD5_Update(a,b,c)
  148. #define MD_Final(a,b) MD5_Final(a,b)
  149. #define MD(a,b,c) MD5(a,b,c)
  150. #elif defined(USE_SHA1_RAND)
  151. #include <openssl/sha.h>
  152. #define MD_DIGEST_LENGTH SHA_DIGEST_LENGTH
  153. #define MD_CTX SHA_CTX
  154. #define MD_Init(a) SHA1_Init(a)
  155. #define MD_Update(a,b,c) SHA1_Update(a,b,c)
  156. #define MD_Final(a,b) SHA1_Final(a,b)
  157. #define MD(a,b,c) SHA1(a,b,c)
  158. #elif defined(USE_MDC2_RAND)
  159. #include <openssl/mdc2.h>
  160. #define MD_DIGEST_LENGTH MDC2_DIGEST_LENGTH
  161. #define MD_CTX MDC2_CTX
  162. #define MD_Init(a) MDC2_Init(a)
  163. #define MD_Update(a,b,c) MDC2_Update(a,b,c)
  164. #define MD_Final(a,b) MDC2_Final(a,b)
  165. #define MD(a,b,c) MDC2(a,b,c)
  166. #elif defined(USE_MD2_RAND)
  167. #include <openssl/md2.h>
  168. #define MD_DIGEST_LENGTH MD2_DIGEST_LENGTH
  169. #define MD_CTX MD2_CTX
  170. #define MD_Init(a) MD2_Init(a)
  171. #define MD_Update(a,b,c) MD2_Update(a,b,c)
  172. #define MD_Final(a,b) MD2_Final(a,b)
  173. #define MD(a,b,c) MD2(a,b,c)
  174. #endif
  175. #include <openssl/rand.h>
  176. #ifdef BN_DEBUG
  177. # define PREDICT
  178. #endif
  179. /* #define NORAND 1 */
  180. /* #define PREDICT 1 */
  181. #define STATE_SIZE 1023
  182. static int state_num=0,state_index=0;
  183. static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH];
  184. static unsigned char md[MD_DIGEST_LENGTH];
  185. static long md_count[2]={0,0};
  186. static double entropy=0;
  187. static int initialized=0;
  188. #ifdef PREDICT
  189. int rand_predictable=0;
  190. #endif
  191. const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT;
  192. static void ssleay_rand_cleanup(void);
  193. static void ssleay_rand_seed(const void *buf, int num);
  194. static void ssleay_rand_add(const void *buf, int num, double add_entropy);
  195. static int ssleay_rand_bytes(unsigned char *buf, int num);
  196. static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num);
  197. static int ssleay_rand_status(void);
  198. RAND_METHOD rand_ssleay_meth={
  199. ssleay_rand_seed,
  200. ssleay_rand_bytes,
  201. ssleay_rand_cleanup,
  202. ssleay_rand_add,
  203. ssleay_rand_pseudo_bytes,
  204. ssleay_rand_status
  205. };
  206. RAND_METHOD *RAND_SSLeay(void)
  207. {
  208. return(&rand_ssleay_meth);
  209. }
  210. static void ssleay_rand_cleanup(void)
  211. {
  212. memset(state,0,sizeof(state));
  213. state_num=0;
  214. state_index=0;
  215. memset(md,0,MD_DIGEST_LENGTH);
  216. md_count[0]=0;
  217. md_count[1]=0;
  218. entropy=0;
  219. }
  220. static void ssleay_rand_add(const void *buf, int num, double add)
  221. {
  222. int i,j,k,st_idx;
  223. long md_c[2];
  224. unsigned char local_md[MD_DIGEST_LENGTH];
  225. MD_CTX m;
  226. #ifdef NORAND
  227. return;
  228. #endif
  229. /*
  230. * (Based on the rand(3) manpage)
  231. *
  232. * The input is chopped up into units of 20 bytes (or less for
  233. * the last block). Each of these blocks is run through the hash
  234. * function as follows: The data passed to the hash function
  235. * is the current 'md', the same number of bytes from the 'state'
  236. * (the location determined by in incremented looping index) as
  237. * the current 'block', the new key data 'block', and 'count'
  238. * (which is incremented after each use).
  239. * The result of this is kept in 'md' and also xored into the
  240. * 'state' at the same locations that were used as input into the
  241. * hash function.
  242. */
  243. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  244. st_idx=state_index;
  245. /* use our own copies of the counters so that even
  246. * if a concurrent thread seeds with exactly the
  247. * same data and uses the same subarray there's _some_
  248. * difference */
  249. md_c[0] = md_count[0];
  250. md_c[1] = md_count[1];
  251. memcpy(local_md, md, sizeof md);
  252. /* state_index <= state_num <= STATE_SIZE */
  253. state_index += num;
  254. if (state_index >= STATE_SIZE)
  255. {
  256. state_index%=STATE_SIZE;
  257. state_num=STATE_SIZE;
  258. }
  259. else if (state_num < STATE_SIZE)
  260. {
  261. if (state_index > state_num)
  262. state_num=state_index;
  263. }
  264. /* state_index <= state_num <= STATE_SIZE */
  265. /* state[st_idx], ..., state[(st_idx + num - 1) % STATE_SIZE]
  266. * are what we will use now, but other threads may use them
  267. * as well */
  268. md_count[1] += (num / MD_DIGEST_LENGTH) + (num % MD_DIGEST_LENGTH > 0);
  269. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  270. for (i=0; i<num; i+=MD_DIGEST_LENGTH)
  271. {
  272. j=(num-i);
  273. j=(j > MD_DIGEST_LENGTH)?MD_DIGEST_LENGTH:j;
  274. MD_Init(&m);
  275. MD_Update(&m,local_md,MD_DIGEST_LENGTH);
  276. k=(st_idx+j)-STATE_SIZE;
  277. if (k > 0)
  278. {
  279. MD_Update(&m,&(state[st_idx]),j-k);
  280. MD_Update(&m,&(state[0]),k);
  281. }
  282. else
  283. MD_Update(&m,&(state[st_idx]),j);
  284. MD_Update(&m,buf,j);
  285. MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
  286. MD_Final(local_md,&m);
  287. md_c[1]++;
  288. buf=(const char *)buf + j;
  289. for (k=0; k<j; k++)
  290. {
  291. /* Parallel threads may interfere with this,
  292. * but always each byte of the new state is
  293. * the XOR of some previous value of its
  294. * and local_md (itermediate values may be lost).
  295. * Alway using locking could hurt performance more
  296. * than necessary given that conflicts occur only
  297. * when the total seeding is longer than the random
  298. * state. */
  299. state[st_idx++]^=local_md[k];
  300. if (st_idx >= STATE_SIZE)
  301. st_idx=0;
  302. }
  303. }
  304. memset((char *)&m,0,sizeof(m));
  305. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  306. /* Don't just copy back local_md into md -- this could mean that
  307. * other thread's seeding remains without effect (except for
  308. * the incremented counter). By XORing it we keep at least as
  309. * much entropy as fits into md. */
  310. for (k = 0; k < sizeof md; k++)
  311. {
  312. md[k] ^= local_md[k];
  313. }
  314. if (entropy < ENTROPY_NEEDED) /* stop counting when we have enough */
  315. entropy += add;
  316. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  317. #ifndef THREADS
  318. assert(md_c[1] == md_count[1]);
  319. #endif
  320. }
  321. static void ssleay_rand_seed(const void *buf, int num)
  322. {
  323. ssleay_rand_add(buf, num, num);
  324. }
  325. static void ssleay_rand_initialize(void)
  326. {
  327. unsigned long l;
  328. #ifndef GETPID_IS_MEANINGLESS
  329. pid_t curr_pid = getpid();
  330. #endif
  331. #ifdef DEVRANDOM
  332. FILE *fh;
  333. #endif
  334. #ifdef NORAND
  335. return;
  336. #endif
  337. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  338. /* put in some default random data, we need more than just this */
  339. #ifndef GETPID_IS_MEANINGLESS
  340. l=curr_pid;
  341. RAND_add(&l,sizeof(l),0);
  342. l=getuid();
  343. RAND_add(&l,sizeof(l),0);
  344. #endif
  345. l=time(NULL);
  346. RAND_add(&l,sizeof(l),0);
  347. #ifdef DEVRANDOM
  348. /* Use a random entropy pool device. Linux, FreeBSD and OpenBSD
  349. * have this. Use /dev/urandom if you can as /dev/random may block
  350. * if it runs out of random entries. */
  351. if ((fh = fopen(DEVRANDOM, "r")) != NULL)
  352. {
  353. unsigned char tmpbuf[ENTROPY_NEEDED];
  354. int n;
  355. setvbuf(fh, NULL, _IONBF, 0);
  356. n=fread((unsigned char *)tmpbuf,1,ENTROPY_NEEDED,fh);
  357. fclose(fh);
  358. RAND_add(tmpbuf,sizeof tmpbuf,n);
  359. memset(tmpbuf,0,n);
  360. }
  361. #endif
  362. #ifdef PURIFY
  363. memset(state,0,STATE_SIZE);
  364. memset(md,0,MD_DIGEST_LENGTH);
  365. #endif
  366. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  367. initialized=1;
  368. }
  369. static int ssleay_rand_bytes(unsigned char *buf, int num)
  370. {
  371. int i,j,k,st_num,st_idx;
  372. int ok;
  373. long md_c[2];
  374. unsigned char local_md[MD_DIGEST_LENGTH];
  375. MD_CTX m;
  376. #ifndef GETPID_IS_MEANINGLESS
  377. pid_t curr_pid = getpid();
  378. #endif
  379. #ifdef PREDICT
  380. if (rand_predictable)
  381. {
  382. static unsigned char val=0;
  383. for (i=0; i<num; i++)
  384. buf[i]=val++;
  385. return(1);
  386. }
  387. #endif
  388. /*
  389. * (Based on the rand(3) manpage:)
  390. *
  391. * For each group of 10 bytes (or less), we do the following:
  392. *
  393. * Input into the hash function the top 10 bytes from the
  394. * local 'md' (which is initialized from the global 'md'
  395. * before any bytes are generated), the bytes that are
  396. * to be overwritten by the random bytes, and bytes from the
  397. * 'state' (incrementing looping index). From this digest output
  398. * (which is kept in 'md'), the top (up to) 10 bytes are
  399. * returned to the caller and the bottom (up to) 10 bytes are xored
  400. * into the 'state'.
  401. * Finally, after we have finished 'num' random bytes for the
  402. * caller, 'count' (which is incremented) and the local and global 'md'
  403. * are fed into the hash function and the results are kept in the
  404. * global 'md'.
  405. */
  406. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  407. if (!initialized)
  408. ssleay_rand_initialize();
  409. ok = (entropy >= ENTROPY_NEEDED);
  410. if (!ok)
  411. {
  412. /* If the PRNG state is not yet unpredictable, then seeing
  413. * the PRNG output may help attackers to determine the new
  414. * state; thus we have to decrease the entropy estimate.
  415. * Once we've had enough initial seeding we don't bother to
  416. * adjust the entropy count, though, because we're not ambitious
  417. * to provide *information-theoretic* randomness.
  418. */
  419. entropy -= num;
  420. if (entropy < 0)
  421. entropy = 0;
  422. }
  423. st_idx=state_index;
  424. st_num=state_num;
  425. md_c[0] = md_count[0];
  426. md_c[1] = md_count[1];
  427. memcpy(local_md, md, sizeof md);
  428. state_index+=num;
  429. if (state_index > state_num)
  430. state_index %= state_num;
  431. /* state[st_idx], ..., state[(st_idx + num - 1) % st_num]
  432. * are now ours (but other threads may use them too) */
  433. md_count[0] += 1;
  434. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  435. while (num > 0)
  436. {
  437. j=(num >= MD_DIGEST_LENGTH/2)?MD_DIGEST_LENGTH/2:num;
  438. num-=j;
  439. MD_Init(&m);
  440. #ifndef GETPID_IS_MEANINGLESS
  441. if (curr_pid) /* just in the first iteration to save time */
  442. {
  443. MD_Update(&m,(unsigned char*)&curr_pid,sizeof curr_pid);
  444. curr_pid = 0;
  445. }
  446. #endif
  447. MD_Update(&m,&(local_md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2);
  448. MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
  449. #ifndef PURIFY
  450. MD_Update(&m,buf,j); /* purify complains */
  451. #endif
  452. k=(st_idx+j)-st_num;
  453. if (k > 0)
  454. {
  455. MD_Update(&m,&(state[st_idx]),j-k);
  456. MD_Update(&m,&(state[0]),k);
  457. }
  458. else
  459. MD_Update(&m,&(state[st_idx]),j);
  460. MD_Final(local_md,&m);
  461. for (i=0; i<j; i++)
  462. {
  463. state[st_idx++]^=local_md[i]; /* may compete with other threads */
  464. *(buf++)=local_md[i+MD_DIGEST_LENGTH/2];
  465. if (st_idx >= st_num)
  466. st_idx=0;
  467. }
  468. }
  469. MD_Init(&m);
  470. MD_Update(&m,(unsigned char *)&(md_c[0]),sizeof(md_c));
  471. MD_Update(&m,local_md,MD_DIGEST_LENGTH);
  472. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  473. MD_Update(&m,md,MD_DIGEST_LENGTH);
  474. MD_Final(md,&m);
  475. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  476. memset(&m,0,sizeof(m));
  477. if (ok)
  478. return(1);
  479. else
  480. {
  481. RANDerr(RAND_F_SSLEAY_RAND_BYTES,RAND_R_PRNG_NOT_SEEDED);
  482. return(0);
  483. }
  484. }
  485. /* pseudo-random bytes that are guaranteed to be unique but not
  486. unpredictable */
  487. static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num)
  488. {
  489. int ret, err;
  490. ret = RAND_bytes(buf, num);
  491. if (ret == 0)
  492. {
  493. err = ERR_peek_error();
  494. if (ERR_GET_LIB(err) == ERR_LIB_RAND &&
  495. ERR_GET_REASON(err) == RAND_R_PRNG_NOT_SEEDED)
  496. (void)ERR_get_error();
  497. }
  498. return (ret);
  499. }
  500. static int ssleay_rand_status(void)
  501. {
  502. if (!initialized)
  503. ssleay_rand_initialize();
  504. return (entropy >= ENTROPY_NEEDED);
  505. }
  506. #ifdef WINDOWS
  507. #include <windows.h>
  508. #include <openssl/rand.h>
  509. /*****************************************************************************
  510. * Initialisation function for the SSL random generator. Takes the contents
  511. * of the screen as random seed.
  512. *
  513. * Created 960901 by Gertjan van Oosten, gertjan@West.NL, West Consulting B.V.
  514. *
  515. * Code adapted from
  516. * <URL:http://www.microsoft.com/kb/developr/win_dk/q97193.htm>;
  517. * the original copyright message is:
  518. *
  519. * (C) Copyright Microsoft Corp. 1993. All rights reserved.
  520. *
  521. * You have a royalty-free right to use, modify, reproduce and
  522. * distribute the Sample Files (and/or any modified version) in
  523. * any way you find useful, provided that you agree that
  524. * Microsoft has no warranty obligations or liability for any
  525. * Sample Application Files which are modified.
  526. */
  527. /*
  528. * I have modified the loading of bytes via RAND_seed() mechanism since
  529. * the original would have been very very CPU intensive since RAND_seed()
  530. * does an MD5 per 16 bytes of input. The cost to digest 16 bytes is the same
  531. * as that to digest 56 bytes. So under the old system, a screen of
  532. * 1024*768*256 would have been CPU cost of approximately 49,000 56 byte MD5
  533. * digests or digesting 2.7 mbytes. What I have put in place would
  534. * be 48 16k MD5 digests, or effectively 48*16+48 MD5 bytes or 816 kbytes
  535. * or about 3.5 times as much.
  536. * - eric
  537. */
  538. void RAND_screen(void)
  539. {
  540. HDC hScrDC; /* screen DC */
  541. HDC hMemDC; /* memory DC */
  542. HBITMAP hBitmap; /* handle for our bitmap */
  543. HBITMAP hOldBitmap; /* handle for previous bitmap */
  544. BITMAP bm; /* bitmap properties */
  545. unsigned int size; /* size of bitmap */
  546. char *bmbits; /* contents of bitmap */
  547. int w; /* screen width */
  548. int h; /* screen height */
  549. int y; /* y-coordinate of screen lines to grab */
  550. int n = 16; /* number of screen lines to grab at a time */
  551. /* Create a screen DC and a memory DC compatible to screen DC */
  552. hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
  553. hMemDC = CreateCompatibleDC(hScrDC);
  554. /* Get screen resolution */
  555. w = GetDeviceCaps(hScrDC, HORZRES);
  556. h = GetDeviceCaps(hScrDC, VERTRES);
  557. /* Create a bitmap compatible with the screen DC */
  558. hBitmap = CreateCompatibleBitmap(hScrDC, w, n);
  559. /* Select new bitmap into memory DC */
  560. hOldBitmap = SelectObject(hMemDC, hBitmap);
  561. /* Get bitmap properties */
  562. GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
  563. size = (unsigned int)bm.bmWidthBytes * bm.bmHeight * bm.bmPlanes;
  564. bmbits = Malloc(size);
  565. if (bmbits) {
  566. /* Now go through the whole screen, repeatedly grabbing n lines */
  567. for (y = 0; y < h-n; y += n)
  568. {
  569. unsigned char md[MD_DIGEST_LENGTH];
  570. /* Bitblt screen DC to memory DC */
  571. BitBlt(hMemDC, 0, 0, w, n, hScrDC, 0, y, SRCCOPY);
  572. /* Copy bitmap bits from memory DC to bmbits */
  573. GetBitmapBits(hBitmap, size, bmbits);
  574. /* Get the MD5 of the bitmap */
  575. MD(bmbits,size,md);
  576. /* Seed the random generator with the MD5 digest */
  577. RAND_seed(md, MD_DIGEST_LENGTH);
  578. }
  579. Free(bmbits);
  580. }
  581. /* Select old bitmap back into memory DC */
  582. hBitmap = SelectObject(hMemDC, hOldBitmap);
  583. /* Clean up */
  584. DeleteObject(hBitmap);
  585. DeleteDC(hMemDC);
  586. DeleteDC(hScrDC);
  587. }
  588. #endif