]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/YarrowCoreLib/src/prng.c
e91b22150645c605e071e17aa5a998fbd0894058
[apple/xnu.git] / bsd / dev / random / YarrowCoreLib / src / prng.c
1 /*
2 * Copyright (c) 1999, 2000-2001 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 File: prng.c
28
29 Contains: Core routines for the Counterpane Yarrow PRNG.
30
31 Written by: Counterpane, Inc.
32
33 Copyright: (c) 2000 by Apple Computer, Inc., all rights reserved.
34
35 Change History (most recent first):
36
37 02/10/99 dpm Created, based on Counterpane source.
38
39 */
40 /*
41 prng.c
42
43 Core routines for the Counterpane PRNG
44 */
45 #include "userdefines.h"
46 #include "assertverify.h"
47 #include "dev/random/YarrowCoreLib/include/yarrowUtils.h"
48
49 #if defined(macintosh) || defined(__APPLE__)
50 /* FIXME - this file needs to be in a platform-independent place */
51
52 #include "macOnly.h"
53 #endif /* macintosh */
54 #include "smf.h"
55 #include "sha1mod.h"
56 #include "entropysources.h"
57 #include "comp.h"
58 #include "dev/random/YarrowCoreLib/include/yarrow.h"
59 #include "prng.h"
60 #include "prngpriv.h"
61
62
63 #define _MAX(a,b) (((a)>(b))?(a):(b))
64 #define _MIN(a,b) (((a)<(b))?(a):(b))
65
66 #if defined(macintosh) || defined(__APPLE__)
67 /*
68 * No mutexes in this module for Macintosh/OSX. We handle the
69 * required locking elsewhere.
70 */
71 #define MUTEX_ENABLE 0
72
73 #include <string.h> /* memcpy, etc. */
74 #if TARGET_API_MAC_OSX
75 #include <sys/time.h> /* for timespec */
76 #elif TARGET_API_MAC_CARBON
77 #include <Timer.h> /* Microseconds */
78 #include <Math64.h>
79 #elif KERNEL_BUILD
80 #include <sys/time.h>
81 #else
82 #error Unknown TARGET_API
83 #endif /* TARGET_API */
84 #else
85 #define MUTEX_ENABLE 1
86 #endif /* macintosh */
87
88 #if MUTEX_ENABLE
89 static HANDLE Statmutex = NULL;
90 static DWORD mutexCreatorId = 0;
91 #endif
92
93 #pragma mark -
94 #pragma mark * * * Static Utility functions * * *
95
96 /* All error checking should be done in the function that calls these */
97
98 /*
99 * out := SHA1(IV | out)
100 */
101 static void
102 prng_do_SHA1(GEN_CTX *ctx)
103 {
104 SHA1_CTX sha;
105
106 SHA1Init(&sha);
107 SHA1Update(&sha,ctx->IV,20);
108 SHA1Update(&sha,ctx->out,20);
109 SHA1Final(ctx->out,&sha);
110 ctx->index = 0;
111 }
112
113 /*
114 * IV := newState
115 * out := SHA1(IV)
116 *
117 * Called from init, prngForceReseed(), and prngOutput()
118 * as anti-backtracking mechanism.
119 */
120 static void
121 prng_make_new_state(GEN_CTX *ctx,BYTE *newState)
122 {
123 SHA1_CTX sha;
124
125 memcpy(ctx->IV,newState,20);
126 SHA1Init(&sha);
127 SHA1Update(&sha,ctx->IV,20);
128 SHA1Final(ctx->out,&sha);
129 ctx->numout = 0;
130 ctx->index = 0;
131 }
132
133 #if SLOW_POLL_ENABLE
134
135
136 /* Initialize the secret state with a slow poll */
137 /* Currently only called from prngInitialize */
138
139 #define SPLEN 65536 /* 64K */
140
141 static void
142 prng_slow_init(PRNG *p)
143 /* This fails silently and must be fixed. */
144 {
145 SHA1_CTX* ctx = NULL;
146 MMPTR mmctx = MM_NULL;
147 BYTE* bigbuf = NULL;
148 MMPTR mmbigbuf = MM_NULL;
149 BYTE* buf = NULL;
150 MMPTR mmbuf = MM_NULL;
151 DWORD polllength;
152
153 mmbigbuf = mmMalloc(SPLEN);
154 if(mmbigbuf == MM_NULL) {goto cleanup_slow_init;}
155 bigbuf = (BYTE*)mmGetPtr(mmbigbuf);
156
157 mmbuf = mmMalloc(20);
158 if(mmbuf == MM_NULL) {goto cleanup_slow_init;}
159 buf = (BYTE*)mmGetPtr(mmbuf);
160
161 mmctx = mmMalloc(sizeof(SHA1_CTX));
162 if(mmctx == MM_NULL) {goto cleanup_slow_init;}
163 ctx = (SHA1_CTX*)mmGetPtr(mmctx);
164
165
166 /* Initialize the secret state. */
167 /* Init entropy pool */
168 SHA1Init(&p->pool);
169 /* Init output generator */
170 polllength = prng_slow_poll(bigbuf,SPLEN);
171 SHA1Init(ctx);
172 SHA1Update(ctx,bigbuf,polllength);
173 SHA1Final(buf,ctx);
174 prng_make_new_state(&p->outstate, buf);
175
176 cleanup_slow_init:
177 mmFree(mmctx);
178 mmFree(mmbigbuf);
179 mmFree(mmbuf);
180
181 return;
182 }
183
184 #endif /* SLOW_POLL_ENABLE */
185
186 /* In-place modifed bubble sort */
187 static void
188 bubbleSort(UINT *data,UINT len)
189 {
190 UINT i,last,newlast,temp;
191
192 last = len-1;
193 while(last!=-1)
194 {
195 newlast = -1;
196 for(i=0;i<last;i++)
197 {
198 if(data[i+1] > data[i])
199 {
200 newlast = i;
201 temp = data[i];
202 data[i] = data[i+1];
203 data[i+1] = temp;
204 }
205 }
206 last = newlast;
207 }
208 }
209
210 #pragma mark -
211 #pragma mark * * * Public functions * * *
212
213 /* Set up the PRNG */
214 prng_error_status
215 prngInitialize(PrngRef *prng)
216 {
217 UINT i;
218 comp_error_status resp;
219 prng_error_status retval = PRNG_ERR_LOW_MEMORY;
220 MMPTR mmp;
221 PRNG *p;
222
223 mmInit();
224
225 #if MUTEX_ENABLE
226 /* Create the mutex */
227 /* NOTE: on return the mutex should bve held, since our caller (prngInitialize)
228 * will release it.
229 */
230 if(mutexCreatorId!=0) {return PRNG_ERR_REINIT;}
231 Statmutex = CreateMutex(NULL,TRUE,NULL);
232 if(Statmutex == NULL) {mutexCreatorId = 0; return PRNG_ERR_MUTEX;}
233 DuplicateHandle(GetCurrentProcess(),Statmutex,GetCurrentProcess(),&mutex,SYNCHRONIZE,FALSE,0);
234 mutexCreatorId = GetCurrentProcessId();
235 #endif /* MUTEX_ENABLE */
236
237 /* Assign memory */
238 mmp = mmMalloc(sizeof(PRNG));
239 if(mmp==MM_NULL)
240 {
241 goto cleanup_init;
242 }
243 else
244 {
245 p = (PRNG*)mmGetPtr(mmp);
246 memset(p, 0, sizeof(PRNG));
247 }
248
249 /* Initialize Variables */
250 for(i=0;i<TOTAL_SOURCES;i++)
251 {
252 p->poolSize[i] = 0;
253 p->poolEstBits[i] = 0;
254 }
255
256 #ifdef WIN_NT
257 /* Setup security on the registry so that remote users cannot predict the slow pool */
258 prng_set_NT_security();
259 #endif
260
261 /* Initialize the secret state. */
262 /* FIXME - might want to make this an option here and have the caller
263 * do it after we return....? */
264 SHA1Init(&p->pool);
265 #if SLOW_POLL_ENABLE
266 prng_slow_init(p); /* Does a slow poll and then calls prng_make_state(...) */
267 #else
268 /* NULL init */
269 prng_do_SHA1(&p->outstate);
270 prng_make_new_state(&p->outstate, p->outstate.out);
271 #endif /* SLOW_POLL_ENABLE */
272
273 /* Initialize compression routines */
274 for(i=0;i<COMP_SOURCES;i++)
275 {
276 resp = comp_init((p->comp_state)+i);
277 if(resp!=COMP_SUCCESS) {retval = PRNG_ERR_COMPRESSION; goto cleanup_init;}
278 }
279
280 p->ready = PRNG_READY;
281 *prng = (PrngRef)p;
282
283 return PRNG_SUCCESS;
284
285 cleanup_init:
286 /* Program failed on one of the mmmallocs */
287 mmFree(mmp);
288 mmp = MM_NULL;
289
290 #if MUTEX_ENABLE
291 CloseHandle(Statmutex);
292 Statmutex = NULL;
293 mutexCreatorId = 0;
294 #endif
295
296 return retval; /* default PRNG_ERR_LOW_MEMORY */
297 }
298
299 /* Provide output */
300 prng_error_status
301 prngOutput(PRNG *p, BYTE *outbuf,UINT outbuflen)
302 {
303 UINT i;
304 GEN_CTX *ctx = &p->outstate;
305
306 CHECKSTATE(p);
307 GENCHECK(p);
308 PCHECK(outbuf);
309 chASSERT(BACKTRACKLIMIT > 0);
310
311 for(i=0;i<outbuflen;i++,ctx->index++,ctx->numout++)
312 {
313 /* Check backtracklimit */
314 if(ctx->numout > BACKTRACKLIMIT)
315 {
316 prng_do_SHA1(ctx);
317 prng_make_new_state(ctx, ctx->out);
318 }
319 /* Check position in IV */
320 if(ctx->index>=20)
321 {
322 prng_do_SHA1(ctx);
323 }
324 /* Output data */
325 outbuf[i] = (ctx->out)[ctx->index];
326 }
327
328 return PRNG_SUCCESS;
329 }
330
331
332 /* Cause the PRNG to reseed now regardless of entropy pool */
333 /* Should this be public? */
334 prng_error_status
335 prngForceReseed(PRNG *p, LONGLONG ticks)
336 {
337 int i;
338 #ifdef WIN_NT
339 FILETIME a,b,c,usertime;
340 #endif
341 BYTE buf[64];
342 BYTE dig[20];
343 #if defined(macintosh) || defined(__APPLE__)
344 #if (defined(TARGET_API_MAC_OSX) || defined(KERNEL_BUILD))
345 struct timeval tv;
346 int64_t endTime, curTime;
347 #else /* TARGET_API_MAC_CARBON */
348 UnsignedWide uwide; /* struct needed for Microseconds() */
349 LONGLONG start;
350 LONGLONG now;
351 #endif
352 #endif
353
354 CHECKSTATE(p);
355 POOLCHECK(p);
356 ZCHECK(ticks);
357
358 /* Set up start and end times */
359 #if defined(macintosh) || defined(__APPLE__)
360 #if (defined(TARGET_API_MAC_OSX) || defined(KERNEL_BUILD))
361 /* note we can't loop for more than a million microseconds */
362 #ifdef KERNEL_BUILD
363 microuptime (&tv);
364 #else
365 gettimeofday(&tv, NULL);
366 #endif
367 endTime = (int64_t)tv.tv_sec*1000000LL + (int64_t)tv.tv_usec + ticks;
368 #else /* TARGET_API_MAC_OSX */
369 Microseconds(&uwide);
370 start = UnsignedWideToUInt64(uwide);
371 #endif /* TARGET_API_xxx */
372 #endif /* macintosh */
373 do
374 {
375 /* Do a couple of iterations between time checks */
376 prngOutput(p, buf,64);
377 SHA1Update(&p->pool,buf,64);
378 prngOutput(p, buf,64);
379 SHA1Update(&p->pool,buf,64);
380 prngOutput(p, buf,64);
381 SHA1Update(&p->pool,buf,64);
382 prngOutput(p, buf,64);
383 SHA1Update(&p->pool,buf,64);
384 prngOutput(p, buf,64);
385 SHA1Update(&p->pool,buf,64);
386
387 #if defined(macintosh) || defined(__APPLE__)
388 #if defined(TARGET_API_MAC_OSX) || defined(KERNEL_BUILD)
389 #ifdef TARGET_API_MAC_OSX
390 gettimeofday(&tv, NULL);
391 #else
392 microuptime (&tv);
393 curTime = (int64_t)tv.tv_sec*1000000LL + (int64_t)tv.tv_usec;
394 #endif
395 } while(curTime < endTime);
396 #else
397 Microseconds(&uwide);
398 now = UnsignedWideToUInt64(uwide);
399 } while ( (now-start) < ticks) ;
400 #endif
401 #else
402 } while ( (now-start) < ticks) ;
403 #endif
404 SHA1Final(dig,&p->pool);
405 SHA1Update(&p->pool,dig,20);
406 SHA1Final(dig,&p->pool);
407
408 /* Reset secret state */
409 SHA1Init(&p->pool);
410 prng_make_new_state(&p->outstate,dig);
411
412 /* Clear counter variables */
413 for(i=0;i<TOTAL_SOURCES;i++)
414 {
415 p->poolSize[i] = 0;
416 p->poolEstBits[i] = 0;
417 }
418
419 /* Cleanup memory */
420 trashMemory(dig,20*sizeof(char));
421 trashMemory(buf,64*sizeof(char));
422
423 return PRNG_SUCCESS;
424 }
425
426
427 /* Input a state into the PRNG */
428 prng_error_status
429 prngProcessSeedBuffer(PRNG *p, BYTE *buf,LONGLONG ticks)
430 {
431 CHECKSTATE(p);
432 GENCHECK(p);
433 PCHECK(buf);
434
435 /* Put the data into the entropy, add some data from the unknown state, reseed */
436 SHA1Update(&p->pool,buf,20); /* Put it into the entropy pool */
437 prng_do_SHA1(&p->outstate); /* Output 20 more bytes and */
438 SHA1Update(&p->pool,p->outstate.out,20);/* add it to the pool as well. */
439 prngForceReseed(p, ticks); /* Do a reseed */
440 return prngOutput(p, buf,20); /* Return the first 20 bytes of output in buf */
441 }
442
443
444 /* Take some "random" data and make more "random-looking" data from it */
445 /* note: this routine has no context, no mutex wrapper */
446 prng_error_status
447 prngStretch(BYTE *inbuf,UINT inbuflen,BYTE *outbuf,UINT outbuflen) {
448 long int left,prev;
449 SHA1_CTX ctx;
450 BYTE dig[20];
451
452 PCHECK(inbuf);
453 PCHECK(outbuf);
454
455 if(inbuflen >= outbuflen)
456 {
457 memcpy(outbuf,inbuf,outbuflen);
458 return PRNG_SUCCESS;
459 }
460 else /* Extend using SHA1 hash of inbuf */
461 {
462 SHA1Init(&ctx);
463 SHA1Update(&ctx,inbuf,inbuflen);
464 SHA1Final(dig,&ctx);
465 for(prev=0,left=outbuflen;left>0;prev+=20,left-=20)
466 {
467 SHA1Update(&ctx,dig,20);
468 SHA1Final(dig,&ctx);
469 memcpy(outbuf+prev,dig,(left>20)?20:left);
470 }
471 trashMemory(dig,20*sizeof(BYTE));
472
473 return PRNG_SUCCESS;
474 }
475
476 return PRNG_ERR_PROGRAM_FLOW;
477 }
478
479
480 /* Add entropy to the PRNG from a source */
481 prng_error_status
482 prngInput(PRNG *p, BYTE *inbuf,UINT inbuflen,UINT poolnum,UINT estbits)
483 {
484 #ifndef YARROW_KERNEL
485 comp_error_status resp;
486 #endif
487
488 CHECKSTATE(p);
489 POOLCHECK(p);
490 PCHECK(inbuf);
491 if(poolnum >= TOTAL_SOURCES) {return PRNG_ERR_OUT_OF_BOUNDS;}
492
493 /* Add to entropy pool */
494 SHA1Update(&p->pool,inbuf,inbuflen);
495
496 #ifndef YARROW_KERNEL
497 /* skip this step for the kernel */
498
499 /* Update pool size, pool user estimate and pool compression context */
500 p->poolSize[poolnum] += inbuflen;
501 p->poolEstBits[poolnum] += estbits;
502 if(poolnum<COMP_SOURCES)
503 {
504 resp = comp_add_data((p->comp_state)+poolnum,inbuf,inbuflen);
505 if(resp!=COMP_SUCCESS) {return PRNG_ERR_COMPRESSION;}
506 }
507 #endif /* YARROW_KERNEL */
508
509 return PRNG_SUCCESS;
510 }
511
512
513
514 /* If we have enough entropy, allow a reseed of the system */
515 prng_error_status
516 prngAllowReseed(PRNG *p, LONGLONG ticks)
517 {
518 UINT temp[TOTAL_SOURCES];
519 UINT i,sum;
520 #ifndef KERNEL_BUILD
521 float ratio;
522 #endif
523
524 comp_error_status resp;
525
526
527 CHECKSTATE(p);
528
529 for(i=0;i<ENTROPY_SOURCES;i++)
530 {
531 /* Make sure that compression-based entropy estimates are current */
532 #ifndef KERNEL_BUILD // floating point in a kernel is BAD!
533 resp = comp_get_ratio((p->comp_state)+i,&ratio);
534 if(resp!=COMP_SUCCESS) {return PRNG_ERR_COMPRESSION;}
535 /* Use 4 instead of 8 to half compression estimate */
536 temp[i] = (int)(ratio*p->poolSize[i]*4);
537 #else
538 temp[i] = p->poolSize[i] * 4;
539 #endif
540
541 }
542 /* Use minumum of user and compression estimate for compressed sources */
543 for(i=ENTROPY_SOURCES;i<COMP_SOURCES;i++)
544 {
545 #ifndef KERNEL_BUILD
546 /* Make sure that compression-based entropy estimates are current */
547 resp = comp_get_ratio((p->comp_state)+i,&ratio);
548 if(resp!=COMP_SUCCESS) {return PRNG_ERR_COMPRESSION;}
549 /* Use 4 instead of 8 to half compression estimate */
550 temp[i] = _MIN((int)(ratio*p->poolSize[i]*4),(int)p->poolEstBits[i]);
551 #else
552 temp[i] = _MIN (p->poolSize[i] * 4, p->poolEstBits[i]);
553 #endif
554
555 }
556 /* Use user estimate for remaining sources */
557 for(i=COMP_SOURCES;i<TOTAL_SOURCES;i++) {temp[i] = p->poolEstBits[i];}
558
559 if(K > 0) {
560 /* pointless if we're not ignoring any sources */
561 bubbleSort(temp,TOTAL_SOURCES);
562 }
563 for(i=K,sum=0;i<TOTAL_SOURCES;sum+=temp[i++]); /* Stupid C trick */
564 if(sum>THRESHOLD)
565 return prngForceReseed(p, ticks);
566 else
567 return PRNG_ERR_NOT_ENOUGH_ENTROPY;
568
569 return PRNG_ERR_PROGRAM_FLOW;
570 }
571
572 #if SLOW_POLL_ENABLE
573 /* Call a slow poll and insert the data into the entropy pool */
574 static prng_error_status
575 prngSlowPoll(PRNG *p, UINT pollsize)
576 {
577 BYTE *buf;
578 DWORD len;
579 prng_error_status retval;
580
581 CHECKSTATE(p);
582
583 buf = (BYTE*)malloc(pollsize);
584 if(buf==NULL) {return PRNG_ERR_LOW_MEMORY;}
585 len = prng_slow_poll(buf,pollsize); /* OS specific call */
586 retval = prngInput(p, buf,len,SLOWPOLLSOURCE, len * 8);
587 trashMemory(buf,pollsize);
588 free(buf);
589
590 return retval;
591 }
592 #endif /* SLOW_POLL_ENABLE */
593
594
595 /* Delete the PRNG */
596 prng_error_status
597 prngDestroy(PRNG *p)
598 {
599 UINT i;
600
601 #if MUTEX_ENABLE
602 if(GetCurrentProcessId()!=mutexCreatorId) {return PRNG_ERR_WRONG_CALLER;}
603 #endif
604 if(p==NULL) {return PRNG_SUCCESS;} /* Well, there is nothing to destroy... */
605
606 p->ready = PRNG_NOT_READY;
607
608 for(i=0;i<COMP_SOURCES;i++)
609 {
610 comp_end((p->comp_state)+i);
611 }
612
613 #if MUTEX_ENABLE
614 CloseHandle(Statmutex);
615 Statmutex = NULL;
616 mutexCreatorId = 0;
617 #endif
618
619 return PRNG_SUCCESS;
620 }
621
622