diff options
Diffstat (limited to 'src/lib/libcrypto/rand/md_rand.c')
-rw-r--r-- | src/lib/libcrypto/rand/md_rand.c | 102 |
1 files changed, 63 insertions, 39 deletions
diff --git a/src/lib/libcrypto/rand/md_rand.c b/src/lib/libcrypto/rand/md_rand.c index f44b36a8b9..6bd1960e1d 100644 --- a/src/lib/libcrypto/rand/md_rand.c +++ b/src/lib/libcrypto/rand/md_rand.c | |||
@@ -57,21 +57,25 @@ | |||
57 | */ | 57 | */ |
58 | 58 | ||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include "cryptlib.h" | ||
61 | #include <sys/types.h> | 60 | #include <sys/types.h> |
62 | #include <time.h> | 61 | #include <time.h> |
62 | #include <string.h> | ||
63 | |||
64 | #include "openssl/e_os.h" | ||
65 | |||
66 | #include <openssl/crypto.h> | ||
63 | 67 | ||
64 | #if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND) | 68 | #if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND) |
65 | #ifndef NO_MD5 | 69 | #if !defined(NO_SHA) && !defined(NO_SHA1) |
66 | #define USE_MD5_RAND | ||
67 | #elif !defined(NO_SHA1) | ||
68 | #define USE_SHA1_RAND | 70 | #define USE_SHA1_RAND |
69 | #elif !defined(NO_MDC2) | 71 | #elif !defined(NO_MD5) |
72 | #define USE_MD5_RAND | ||
73 | #elif !defined(NO_MDC2) && !defined(NO_DES) | ||
70 | #define USE_MDC2_RAND | 74 | #define USE_MDC2_RAND |
71 | #elif !defined(NO_MD2) | 75 | #elif !defined(NO_MD2) |
72 | #define USE_MD2_RAND | 76 | #define USE_MD2_RAND |
73 | #else | 77 | #else |
74 | We need a message digest of some type | 78 | #error No message digest algorithm available |
75 | #endif | 79 | #endif |
76 | #endif | 80 | #endif |
77 | 81 | ||
@@ -82,60 +86,78 @@ We need a message digest of some type | |||
82 | */ | 86 | */ |
83 | 87 | ||
84 | #if defined(USE_MD5_RAND) | 88 | #if defined(USE_MD5_RAND) |
85 | #include "md5.h" | 89 | #include <openssl/md5.h> |
86 | #define MD_DIGEST_LENGTH MD5_DIGEST_LENGTH | 90 | #define MD_DIGEST_LENGTH MD5_DIGEST_LENGTH |
87 | #define MD_CTX MD5_CTX | 91 | #define MD_CTX MD5_CTX |
88 | #define MD_Init(a) MD5_Init(a) | 92 | #define MD_Init(a) MD5_Init(a) |
89 | #define MD_Update(a,b,c) MD5_Update(a,b,c) | 93 | #define MD_Update(a,b,c) MD5_Update(a,b,c) |
90 | #define MD_Final(a,b) MD5_Final(a,b) | 94 | #define MD_Final(a,b) MD5_Final(a,b) |
95 | #define MD(a,b,c) MD5(a,b,c) | ||
91 | #elif defined(USE_SHA1_RAND) | 96 | #elif defined(USE_SHA1_RAND) |
92 | #include "sha.h" | 97 | #include <openssl/sha.h> |
93 | #define MD_DIGEST_LENGTH SHA_DIGEST_LENGTH | 98 | #define MD_DIGEST_LENGTH SHA_DIGEST_LENGTH |
94 | #define MD_CTX SHA_CTX | 99 | #define MD_CTX SHA_CTX |
95 | #define MD_Init(a) SHA1_Init(a) | 100 | #define MD_Init(a) SHA1_Init(a) |
96 | #define MD_Update(a,b,c) SHA1_Update(a,b,c) | 101 | #define MD_Update(a,b,c) SHA1_Update(a,b,c) |
97 | #define MD_Final(a,b) SHA1_Final(a,b) | 102 | #define MD_Final(a,b) SHA1_Final(a,b) |
103 | #define MD(a,b,c) SHA1(a,b,c) | ||
98 | #elif defined(USE_MDC2_RAND) | 104 | #elif defined(USE_MDC2_RAND) |
99 | #include "mdc2.h" | 105 | #include <openssl/mdc2.h> |
100 | #define MD_DIGEST_LENGTH MDC2_DIGEST_LENGTH | 106 | #define MD_DIGEST_LENGTH MDC2_DIGEST_LENGTH |
101 | #define MD_CTX MDC2_CTX | 107 | #define MD_CTX MDC2_CTX |
102 | #define MD_Init(a) MDC2_Init(a) | 108 | #define MD_Init(a) MDC2_Init(a) |
103 | #define MD_Update(a,b,c) MDC2_Update(a,b,c) | 109 | #define MD_Update(a,b,c) MDC2_Update(a,b,c) |
104 | #define MD_Final(a,b) MDC2_Final(a,b) | 110 | #define MD_Final(a,b) MDC2_Final(a,b) |
111 | #define MD(a,b,c) MDC2(a,b,c) | ||
105 | #elif defined(USE_MD2_RAND) | 112 | #elif defined(USE_MD2_RAND) |
106 | #include "md2.h" | 113 | #include <openssl/md2.h> |
107 | #define MD_DIGEST_LENGTH MD2_DIGEST_LENGTH | 114 | #define MD_DIGEST_LENGTH MD2_DIGEST_LENGTH |
108 | #define MD_CTX MD2_CTX | 115 | #define MD_CTX MD2_CTX |
109 | #define MD_Init(a) MD2_Init(a) | 116 | #define MD_Init(a) MD2_Init(a) |
110 | #define MD_Update(a,b,c) MD2_Update(a,b,c) | 117 | #define MD_Update(a,b,c) MD2_Update(a,b,c) |
111 | #define MD_Final(a,b) MD2_Final(a,b) | 118 | #define MD_Final(a,b) MD2_Final(a,b) |
119 | #define MD(a,b,c) MD2(a,b,c) | ||
112 | #endif | 120 | #endif |
113 | 121 | ||
114 | #include "rand.h" | 122 | #include <openssl/rand.h> |
115 | 123 | ||
116 | /*#define NORAND 1 */ | 124 | /* #define NORAND 1 */ |
117 | /*#define PREDICT 1 */ | 125 | /* #define PREDICT 1 */ |
118 | 126 | ||
119 | #define STATE_SIZE 1023 | 127 | #define STATE_SIZE 1023 |
120 | static int state_num=0,state_index=0; | 128 | static int state_num=0,state_index=0; |
121 | static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH]; | 129 | static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH]; |
122 | static unsigned char md[MD_DIGEST_LENGTH]; | 130 | static unsigned char md[MD_DIGEST_LENGTH]; |
123 | static int md_count=0; | 131 | static long md_count[2]={0,0}; |
132 | |||
133 | const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT; | ||
134 | |||
135 | static void ssleay_rand_cleanup(void); | ||
136 | static void ssleay_rand_seed(const void *buf, int num); | ||
137 | static void ssleay_rand_bytes(unsigned char *buf, int num); | ||
124 | 138 | ||
125 | char *RAND_version="RAND part of SSLeay 0.9.0b 29-Jun-1998"; | 139 | RAND_METHOD rand_ssleay_meth={ |
140 | ssleay_rand_seed, | ||
141 | ssleay_rand_bytes, | ||
142 | ssleay_rand_cleanup, | ||
143 | }; | ||
126 | 144 | ||
127 | void RAND_cleanup() | 145 | RAND_METHOD *RAND_SSLeay(void) |
146 | { | ||
147 | return(&rand_ssleay_meth); | ||
148 | } | ||
149 | |||
150 | static void ssleay_rand_cleanup(void) | ||
128 | { | 151 | { |
129 | memset(state,0,sizeof(state)); | 152 | memset(state,0,sizeof(state)); |
130 | state_num=0; | 153 | state_num=0; |
131 | state_index=0; | 154 | state_index=0; |
132 | memset(md,0,MD_DIGEST_LENGTH); | 155 | memset(md,0,MD_DIGEST_LENGTH); |
133 | md_count=0; | 156 | md_count[0]=0; |
157 | md_count[1]=0; | ||
134 | } | 158 | } |
135 | 159 | ||
136 | void RAND_seed(buf,num) | 160 | static void ssleay_rand_seed(const void *buf, int num) |
137 | unsigned char *buf; | ||
138 | int num; | ||
139 | { | 161 | { |
140 | int i,j,k,st_idx,st_num; | 162 | int i,j,k,st_idx,st_num; |
141 | MD_CTX m; | 163 | MD_CTX m; |
@@ -178,9 +200,11 @@ int num; | |||
178 | MD_Update(&m,&(state[st_idx]),j); | 200 | MD_Update(&m,&(state[st_idx]),j); |
179 | 201 | ||
180 | MD_Update(&m,buf,j); | 202 | MD_Update(&m,buf,j); |
203 | MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count)); | ||
181 | MD_Final(md,&m); | 204 | MD_Final(md,&m); |
205 | md_count[1]++; | ||
182 | 206 | ||
183 | buf+=j; | 207 | buf=(const char *)buf + j; |
184 | 208 | ||
185 | for (k=0; k<j; k++) | 209 | for (k=0; k<j; k++) |
186 | { | 210 | { |
@@ -195,9 +219,7 @@ int num; | |||
195 | memset((char *)&m,0,sizeof(m)); | 219 | memset((char *)&m,0,sizeof(m)); |
196 | } | 220 | } |
197 | 221 | ||
198 | void RAND_bytes(buf,num) | 222 | static void ssleay_rand_bytes(unsigned char *buf, int num) |
199 | unsigned char *buf; | ||
200 | int num; | ||
201 | { | 223 | { |
202 | int i,j,k,st_num,st_idx; | 224 | int i,j,k,st_num,st_idx; |
203 | MD_CTX m; | 225 | MD_CTX m; |
@@ -221,19 +243,18 @@ int num; | |||
221 | 243 | ||
222 | if (init) | 244 | if (init) |
223 | { | 245 | { |
224 | init=0; | ||
225 | CRYPTO_w_unlock(CRYPTO_LOCK_RAND); | 246 | CRYPTO_w_unlock(CRYPTO_LOCK_RAND); |
226 | /* put in some default random data, we need more than | 247 | /* put in some default random data, we need more than |
227 | * just this */ | 248 | * just this */ |
228 | RAND_seed((unsigned char *)&m,sizeof(m)); | 249 | RAND_seed(&m,sizeof(m)); |
229 | #ifndef MSDOS | 250 | #ifndef MSDOS |
230 | l=getpid(); | 251 | l=getpid(); |
231 | RAND_seed((unsigned char *)&l,sizeof(l)); | 252 | RAND_seed(&l,sizeof(l)); |
232 | l=getuid(); | 253 | l=getuid(); |
233 | RAND_seed((unsigned char *)&l,sizeof(l)); | 254 | RAND_seed(&l,sizeof(l)); |
234 | #endif | 255 | #endif |
235 | l=time(NULL); | 256 | l=time(NULL); |
236 | RAND_seed((unsigned char *)&l,sizeof(l)); | 257 | RAND_seed(&l,sizeof(l)); |
237 | 258 | ||
238 | /* #ifdef DEVRANDOM */ | 259 | /* #ifdef DEVRANDOM */ |
239 | /* | 260 | /* |
@@ -261,6 +282,7 @@ int num; | |||
261 | memset(md,0,MD_DIGEST_LENGTH); | 282 | memset(md,0,MD_DIGEST_LENGTH); |
262 | #endif | 283 | #endif |
263 | CRYPTO_w_lock(CRYPTO_LOCK_RAND); | 284 | CRYPTO_w_lock(CRYPTO_LOCK_RAND); |
285 | init=0; | ||
264 | } | 286 | } |
265 | 287 | ||
266 | st_idx=state_index; | 288 | st_idx=state_index; |
@@ -277,6 +299,7 @@ int num; | |||
277 | num-=j; | 299 | num-=j; |
278 | MD_Init(&m); | 300 | MD_Init(&m); |
279 | MD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2); | 301 | MD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2); |
302 | MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count)); | ||
280 | #ifndef PURIFY | 303 | #ifndef PURIFY |
281 | MD_Update(&m,buf,j); /* purify complains */ | 304 | MD_Update(&m,buf,j); /* purify complains */ |
282 | #endif | 305 | #endif |
@@ -300,7 +323,8 @@ int num; | |||
300 | } | 323 | } |
301 | 324 | ||
302 | MD_Init(&m); | 325 | MD_Init(&m); |
303 | MD_Update(&m,(unsigned char *)&md_count,sizeof(md_count)); md_count++; | 326 | MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count)); |
327 | md_count[0]++; | ||
304 | MD_Update(&m,md,MD_DIGEST_LENGTH); | 328 | MD_Update(&m,md,MD_DIGEST_LENGTH); |
305 | MD_Final(md,&m); | 329 | MD_Final(md,&m); |
306 | memset(&m,0,sizeof(m)); | 330 | memset(&m,0,sizeof(m)); |
@@ -308,7 +332,7 @@ int num; | |||
308 | 332 | ||
309 | #ifdef WINDOWS | 333 | #ifdef WINDOWS |
310 | #include <windows.h> | 334 | #include <windows.h> |
311 | #include <rand.h> | 335 | #include <openssl/rand.h> |
312 | 336 | ||
313 | /***************************************************************************** | 337 | /***************************************************************************** |
314 | * Initialisation function for the SSL random generator. Takes the contents | 338 | * Initialisation function for the SSL random generator. Takes the contents |
@@ -320,13 +344,13 @@ int num; | |||
320 | * <URL:http://www.microsoft.com/kb/developr/win_dk/q97193.htm>; | 344 | * <URL:http://www.microsoft.com/kb/developr/win_dk/q97193.htm>; |
321 | * the original copyright message is: | 345 | * the original copyright message is: |
322 | * | 346 | * |
323 | // (C) Copyright Microsoft Corp. 1993. All rights reserved. | 347 | * (C) Copyright Microsoft Corp. 1993. All rights reserved. |
324 | // | 348 | * |
325 | // You have a royalty-free right to use, modify, reproduce and | 349 | * You have a royalty-free right to use, modify, reproduce and |
326 | // distribute the Sample Files (and/or any modified version) in | 350 | * distribute the Sample Files (and/or any modified version) in |
327 | // any way you find useful, provided that you agree that | 351 | * any way you find useful, provided that you agree that |
328 | // Microsoft has no warranty obligations or liability for any | 352 | * Microsoft has no warranty obligations or liability for any |
329 | // Sample Application Files which are modified. | 353 | * Sample Application Files which are modified. |
330 | */ | 354 | */ |
331 | /* | 355 | /* |
332 | * I have modified the loading of bytes via RAND_seed() mechanism since | 356 | * I have modified the loading of bytes via RAND_seed() mechanism since |
@@ -385,7 +409,7 @@ void RAND_screen(void) | |||
385 | GetBitmapBits(hBitmap, size, bmbits); | 409 | GetBitmapBits(hBitmap, size, bmbits); |
386 | 410 | ||
387 | /* Get the MD5 of the bitmap */ | 411 | /* Get the MD5 of the bitmap */ |
388 | MD5(bmbits,size,md); | 412 | MD(bmbits,size,md); |
389 | 413 | ||
390 | /* Seed the random generator with the MD5 digest */ | 414 | /* Seed the random generator with the MD5 digest */ |
391 | RAND_seed(md, MD_DIGEST_LENGTH); | 415 | RAND_seed(md, MD_DIGEST_LENGTH); |