aa1082f2 |
1 | /* -*-c-*- |
2 | * |
1709c9a1 |
3 | * $Id: grand.h,v 1.3 2001/02/03 16:07:33 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 $ |
1709c9a1 |
33 | * Revision 1.3 2001/02/03 16:07:33 mdw |
34 | * Give generic random objects separate namespaces for their supported misc |
35 | * ops. |
36 | * |
159bb2d9 |
37 | * Revision 1.2 2000/06/17 11:23:11 mdw |
38 | * Typesetting fix. Add a flags word to the generic generator. |
39 | * |
aa1082f2 |
40 | * Revision 1.1 1999/12/10 23:16:01 mdw |
41 | * Generic interface. |
42 | * |
43 | */ |
44 | |
45 | #ifndef CATACOMB_GRAND_H |
46 | #define CATACOMB_GRAND_H |
47 | |
48 | #ifdef __cplusplus |
49 | extern "C" { |
50 | #endif |
51 | |
52 | /*----- Header files ------------------------------------------------------*/ |
53 | |
54 | #include <assert.h> |
55 | #include <stddef.h> |
56 | |
57 | #include <mLib/bits.h> |
58 | |
59 | /*----- Generic random number generator interface -------------------------*/ |
60 | |
61 | typedef struct grand { |
62 | const struct grand_ops *ops; |
63 | } grand; |
64 | |
65 | typedef struct grand_ops { |
66 | |
67 | /* --- Various important properties --- */ |
68 | |
69 | const char *name; /* Generator's name */ |
159bb2d9 |
70 | unsigned f; /* Various flags */ |
aa1082f2 |
71 | uint32 max; /* Maximum raw output */ |
72 | |
73 | /* --- Maintenance methods --- */ |
74 | |
75 | int (*misc)(grand */*r*/, unsigned /*op*/, ...); /* Miscellaneous ops */ |
76 | void (*destroy)(grand */*r*/); /* Destroy generator context */ |
77 | |
78 | /* --- Output methods --- * |
79 | * |
80 | * Only one of these operations need actually be implemented. All the |
81 | * other operations may be synthesized. Of course, performance is improved |
82 | * if more are provided. |
83 | */ |
84 | |
85 | uint32 (*raw)(grand */*r*/); /* Uniform over %$[0, max)$% */ |
159bb2d9 |
86 | octet (*byte)(grand */*r*/); /* Uniform over %$[0, 256)$% */ |
aa1082f2 |
87 | uint32 (*word)(grand */*r*/); /* Uniform over %$[0, 2^{32})$% */ |
88 | uint32 (*range)(grand */*r*/, uint32 /*l*/); /* Uniform over %$[0, l)$% */ |
89 | void (*fill)(grand */*r*/, void */*p*/, size_t /*sz*/); /* Fill buffer */ |
90 | } grand_ops; |
91 | |
159bb2d9 |
92 | /* --- Flag types --- */ |
93 | |
94 | enum { |
95 | GRAND_CRYPTO = 1u /* Cryptographically strong */ |
96 | }; |
97 | |
aa1082f2 |
98 | /* --- Operation types --- */ |
99 | |
100 | enum { |
101 | |
102 | /* --- Required operations --- */ |
103 | |
104 | GRAND_CHECK, /* @unsigned op2@ */ |
105 | |
106 | /* --- Standard seeding operations --- */ |
107 | |
108 | GRAND_SEEDINT, /* @int i@ */ |
109 | GRAND_SEEDUINT32, /* @uint32 i@ */ |
110 | GRAND_SEEDBLOCK, /* @const void *p, size_t sz@ */ |
111 | GRAND_SEEDMP, /* @mp *m@ */ |
1709c9a1 |
112 | GRAND_SEEDRAND /* @grand *g@ */ |
aa1082f2 |
113 | |
114 | /* --- Generator-specific operations --- */ |
115 | |
1709c9a1 |
116 | #define GRAND_SPECIFIC(ch) ((unsigned)(ch) << 8) |
aa1082f2 |
117 | }; |
118 | |
119 | #define GRAND_BADOP assert(((void)"bad grand_misc op", 0)) |
120 | |
121 | /*----- Functions provided ------------------------------------------------*/ |
122 | |
123 | /* --- @grand_byte@ --- * |
124 | * |
125 | * Arguments: @grand *r@ = pointet to generic generator |
126 | * |
127 | * Returns: A uniformly-distributed pseudorandom integer in the interval |
128 | * %$[0, 256)$%. |
129 | */ |
130 | |
131 | extern octet grand_byte(grand */*r*/); |
132 | |
133 | /* --- @grand_word@ --- * |
134 | * |
135 | * Arguments: @grand *r@ = pointet to generic generator |
136 | * |
137 | * Returns: A uniformly-distributed pseudorandom integer in the interval |
138 | * %$[0, 2^{32})$%. |
139 | */ |
140 | |
141 | extern uint32 grand_word(grand */*r*/); |
142 | |
143 | /* --- @grand_range@ --- * |
144 | * |
145 | * Arguments: @grand *r@ = pointet to generic generator |
146 | * @uint32 l@ = limit for acceptable results |
147 | * |
148 | * Returns: A uniformly-distributed pseudorandom integer in the interval |
149 | * %$[0, l)$%. |
150 | */ |
151 | |
152 | extern uint32 grand_range(grand */*r*/, uint32 /*l*/); |
153 | |
154 | /* --- @grand_fill@ --- * |
155 | * |
156 | * Arguments: @grand *r@ = pointet to generic generator |
157 | * @void *p@ = pointer to a buffer |
158 | * @size_t sz@ = size of the buffer |
159 | * |
160 | * Returns: --- |
161 | * |
162 | * Use: Fills a buffer with uniformly distributed pseudorandom bytes |
163 | * (see @grand_byte@). |
164 | */ |
165 | |
166 | extern void grand_fill(grand */*r*/, void */*p*/, size_t /*sz*/); |
167 | |
168 | /*----- That's all, folks -------------------------------------------------*/ |
169 | |
170 | #ifdef __cplusplus |
171 | } |
172 | #endif |
173 | |
174 | #endif |