aa1082f2 |
1 | /* -*-c-*- |
2 | * |
76ba9c8f |
3 | * $Id: grand.h,v 1.4 2001/04/19 18:23:53 mdw Exp $ |
aa1082f2 |
4 | * |
5 | * Generic interface to random number generators |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: grand.h,v $ |
76ba9c8f |
33 | * Revision 1.4 2001/04/19 18:23:53 mdw |
34 | * Use unsigned integer rather than enum for flags. |
35 | * |
1709c9a1 |
36 | * Revision 1.3 2001/02/03 16:07:33 mdw |
37 | * Give generic random objects separate namespaces for their supported misc |
38 | * ops. |
39 | * |
159bb2d9 |
40 | * Revision 1.2 2000/06/17 11:23:11 mdw |
41 | * Typesetting fix. Add a flags word to the generic generator. |
42 | * |
aa1082f2 |
43 | * Revision 1.1 1999/12/10 23:16:01 mdw |
44 | * Generic interface. |
45 | * |
46 | */ |
47 | |
48 | #ifndef CATACOMB_GRAND_H |
49 | #define CATACOMB_GRAND_H |
50 | |
51 | #ifdef __cplusplus |
52 | extern "C" { |
53 | #endif |
54 | |
55 | /*----- Header files ------------------------------------------------------*/ |
56 | |
57 | #include <assert.h> |
58 | #include <stddef.h> |
59 | |
60 | #include <mLib/bits.h> |
61 | |
62 | /*----- Generic random number generator interface -------------------------*/ |
63 | |
64 | typedef struct grand { |
65 | const struct grand_ops *ops; |
66 | } grand; |
67 | |
68 | typedef struct grand_ops { |
69 | |
70 | /* --- Various important properties --- */ |
71 | |
72 | const char *name; /* Generator's name */ |
159bb2d9 |
73 | unsigned f; /* Various flags */ |
aa1082f2 |
74 | uint32 max; /* Maximum raw output */ |
75 | |
76 | /* --- Maintenance methods --- */ |
77 | |
78 | int (*misc)(grand */*r*/, unsigned /*op*/, ...); /* Miscellaneous ops */ |
79 | void (*destroy)(grand */*r*/); /* Destroy generator context */ |
80 | |
81 | /* --- Output methods --- * |
82 | * |
83 | * Only one of these operations need actually be implemented. All the |
84 | * other operations may be synthesized. Of course, performance is improved |
85 | * if more are provided. |
86 | */ |
87 | |
88 | uint32 (*raw)(grand */*r*/); /* Uniform over %$[0, max)$% */ |
159bb2d9 |
89 | octet (*byte)(grand */*r*/); /* Uniform over %$[0, 256)$% */ |
aa1082f2 |
90 | uint32 (*word)(grand */*r*/); /* Uniform over %$[0, 2^{32})$% */ |
91 | uint32 (*range)(grand */*r*/, uint32 /*l*/); /* Uniform over %$[0, l)$% */ |
92 | void (*fill)(grand */*r*/, void */*p*/, size_t /*sz*/); /* Fill buffer */ |
93 | } grand_ops; |
94 | |
159bb2d9 |
95 | /* --- Flag types --- */ |
96 | |
76ba9c8f |
97 | #define GRAND_CRYPTO 1u /* Cryptographically strong */ |
159bb2d9 |
98 | |
aa1082f2 |
99 | /* --- Operation types --- */ |
100 | |
101 | enum { |
102 | |
103 | /* --- Required operations --- */ |
104 | |
105 | GRAND_CHECK, /* @unsigned op2@ */ |
106 | |
107 | /* --- Standard seeding operations --- */ |
108 | |
109 | GRAND_SEEDINT, /* @int i@ */ |
110 | GRAND_SEEDUINT32, /* @uint32 i@ */ |
111 | GRAND_SEEDBLOCK, /* @const void *p, size_t sz@ */ |
112 | GRAND_SEEDMP, /* @mp *m@ */ |
1709c9a1 |
113 | GRAND_SEEDRAND /* @grand *g@ */ |
aa1082f2 |
114 | |
115 | /* --- Generator-specific operations --- */ |
116 | |
1709c9a1 |
117 | #define GRAND_SPECIFIC(ch) ((unsigned)(ch) << 8) |
aa1082f2 |
118 | }; |
119 | |
120 | #define GRAND_BADOP assert(((void)"bad grand_misc op", 0)) |
121 | |
122 | /*----- Functions provided ------------------------------------------------*/ |
123 | |
124 | /* --- @grand_byte@ --- * |
125 | * |
126 | * Arguments: @grand *r@ = pointet to generic generator |
127 | * |
128 | * Returns: A uniformly-distributed pseudorandom integer in the interval |
129 | * %$[0, 256)$%. |
130 | */ |
131 | |
132 | extern octet grand_byte(grand */*r*/); |
133 | |
134 | /* --- @grand_word@ --- * |
135 | * |
136 | * Arguments: @grand *r@ = pointet to generic generator |
137 | * |
138 | * Returns: A uniformly-distributed pseudorandom integer in the interval |
139 | * %$[0, 2^{32})$%. |
140 | */ |
141 | |
142 | extern uint32 grand_word(grand */*r*/); |
143 | |
144 | /* --- @grand_range@ --- * |
145 | * |
146 | * Arguments: @grand *r@ = pointet to generic generator |
147 | * @uint32 l@ = limit for acceptable results |
148 | * |
149 | * Returns: A uniformly-distributed pseudorandom integer in the interval |
150 | * %$[0, l)$%. |
151 | */ |
152 | |
153 | extern uint32 grand_range(grand */*r*/, uint32 /*l*/); |
154 | |
155 | /* --- @grand_fill@ --- * |
156 | * |
157 | * Arguments: @grand *r@ = pointet to generic generator |
158 | * @void *p@ = pointer to a buffer |
159 | * @size_t sz@ = size of the buffer |
160 | * |
161 | * Returns: --- |
162 | * |
163 | * Use: Fills a buffer with uniformly distributed pseudorandom bytes |
164 | * (see @grand_byte@). |
165 | */ |
166 | |
167 | extern void grand_fill(grand */*r*/, void */*p*/, size_t /*sz*/); |
168 | |
169 | /*----- That's all, folks -------------------------------------------------*/ |
170 | |
171 | #ifdef __cplusplus |
172 | } |
173 | #endif |
174 | |
175 | #endif |