]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/random/YarrowCoreLib/src/prngpriv.h
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / dev / random / YarrowCoreLib / src / prngpriv.h
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 /*
32 File: prngpriv.h
33
34 Contains: Private typedefs and #defines for Counterpane Yarrow PRNG.
35
36 Written by: Counterpane, Inc.
37
38 Copyright: (c) 2000 by Apple Computer, Inc., all rights reserved.
39
40 Change History (most recent first):
41
42 02/10/99 dpm Created, based on Counterpane source.
43
44 */
45 /*
46 prngpriv.h
47
48 Completely private header for the Counterpane PRNG. Should only be included by prng.c
49 */
50
51 #ifndef __YARROW_PRNG_PRIV_H__
52 #define __YARROW_PRNG_PRIV_H__
53
54 #include "userdefines.h"
55 #include "dev/random/YarrowCoreLib/include/yarrow.h"
56 #include "entropysources.h"
57 #include "comp.h"
58 #include "sha1mod.h"
59 #include "smf.h"
60
61 #define TOTAL_SOURCES ENTROPY_SOURCES+USER_SOURCES
62
63 #ifdef COMPRESSION_ON
64 #define COMP_SOURCES TOTAL_SOURCES
65 #else
66 #define COMP_SOURCES ENTROPY_SOURCES
67 #endif
68
69 /* Error numbers */
70 typedef enum prng_ready_status {
71 PRNG_READY = 33, /* Compiler will initialize to either 0 or random if allowed to */
72 PRNG_NOT_READY = 0
73 } prng_ready_status;
74
75 /* Top level output state */
76 typedef struct{
77 BYTE IV[20];
78 BYTE out[20];
79 UINT index; /* current byte to output */
80 UINT numout; /* bytes since last prng_make_new_state */
81 } GEN_CTX;
82
83 /* PRNG state structure */
84 struct PRNG {
85 /* Output State */
86 GEN_CTX outstate;
87
88 /* Entropy Pools (somewhat unlike a gene pool) */
89 SHA1_CTX pool;
90 UINT poolSize[TOTAL_SOURCES]; /* Note that size is in bytes and est in bits */
91 UINT poolEstBits[TOTAL_SOURCES];
92 COMP_CTX comp_state[COMP_SOURCES];
93
94 /* Status Flags */
95 prng_ready_status ready;
96 };
97
98 /*
99 * Clients see an opaque PrngRef; internal code uses the
100 * following typedef.
101 */
102 typedef struct PRNG PRNG;
103
104
105 /* Test Macros */
106 #define CHECKSTATE(p) \
107 if(p==NULL) {return PRNG_ERR_NOT_READY;} /* Does the state exist? */ \
108 if(p->ready != PRNG_READY) {return PRNG_ERR_NOT_READY;} /* Set error state and return */
109 /* To make sure that a pointer isn't NULL */
110 #define PCHECK(ptr) if(ptr==NULL) {return PRNG_ERR_NULL_POINTER;}
111 /* To make sure that malloc returned a valid value */
112 #define MCHECK(ptr) if(ptr==NULL) {return PRNG_ERR_LOW_MEMORY;}
113 /* To make sure that a given value is non-negative */
114 #if defined(macintosh) || defined(__APPLE__)
115 /* original looks like a bogon */
116 #define ZCHECK(val) if(val<0) {return PRNG_ERR_OUT_OF_BOUNDS;}
117 #else
118 #define ZCHECK(val) if(p<0) {return PRNG_ERR_OUT_OF_BOUNDS;}
119 #endif /* macintosh */
120 /* To make sure that the generator state is valid */
121 #define GENCHECK(p) if(p->outstate.index>20) {return PRNG_ERR_OUT_OF_BOUNDS;} /* index is unsigned */
122 /* To make sure that the entropy pool is valid */
123 #define POOLCHECK(p) /* */
124
125
126 #endif