3 * Generic interface to random number generators
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_GRAND_H
29 #define CATACOMB_GRAND_H
35 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
42 /*----- Generic random number generator interface -------------------------*/
44 typedef struct grand {
45 const struct grand_ops *ops;
48 typedef struct grand_ops {
50 /* --- Various important properties --- */
52 const char *name; /* Generator's name */
53 unsigned f; /* Various flags */
54 uint32 max; /* Maximum raw output */
56 /* --- Maintenance methods --- */
58 int (*misc)(grand */*r*/, unsigned /*op*/, ...); /* Miscellaneous ops */
59 void (*destroy)(grand */*r*/); /* Destroy generator context */
61 /* --- Output methods --- *
63 * Only one of these operations need actually be implemented. All the
64 * other operations may be synthesized. Of course, performance is improved
65 * if more are provided.
68 uint32 (*raw)(grand */*r*/); /* Uniform over %$[0, max)$% */
69 octet (*byte)(grand */*r*/); /* Uniform over %$[0, 256)$% */
70 uint32 (*word)(grand */*r*/); /* Uniform over %$[0, 2^{32})$% */
71 uint32 (*range)(grand */*r*/, uint32 /*l*/); /* Uniform over %$[0, l)$% */
72 void (*fill)(grand */*r*/, void */*p*/, size_t /*sz*/); /* Fill buffer */
75 #define GR_DESTROY(r) (r)->ops->destroy((r))
76 #define GR_RAW(r) (r)->ops->raw((r))
77 #define GR_WORD(r) (r)->ops->word((r))
78 #define GR_RANGE(r, l) (r)->ops->range((r), (l))
79 #define GR_FILL(r, p, sz) (r)->ops->fill((r), (p), (sz))
81 /* --- Flag types --- */
83 #define GRAND_CRYPTO 1u /* Cryptographically strong */
85 /* --- Operation types --- */
89 /* --- Required operations --- */
91 GRAND_CHECK, /* @unsigned op2@ */
93 /* --- Standard seeding operations --- */
95 GRAND_SEEDINT, /* @int i@ */
96 GRAND_SEEDUINT32, /* @uint32 i@ */
97 GRAND_SEEDBLOCK, /* @const void *p, size_t sz@ */
98 GRAND_SEEDMP, /* @mp *m@ */
99 GRAND_SEEDRAND /* @grand *g@ */
101 /* --- Generator-specific operations --- */
103 #define GRAND_SPECIFIC(ch) ((unsigned)(ch) << 8)
106 #define GRAND_BADOP assert(((void)"bad grand_misc op", 0))
108 /*----- Functions provided ------------------------------------------------*/
110 /* --- @grand_byte@ --- *
112 * Arguments: @grand *r@ = pointet to generic generator
114 * Returns: A uniformly-distributed pseudorandom integer in the interval
118 extern octet grand_byte(grand */*r*/);
120 /* --- @grand_word@ --- *
122 * Arguments: @grand *r@ = pointet to generic generator
124 * Returns: A uniformly-distributed pseudorandom integer in the interval
128 extern uint32 grand_word(grand */*r*/);
130 /* --- @grand_range@ --- *
132 * Arguments: @grand *r@ = pointet to generic generator
133 * @uint32 l@ = limit for acceptable results
135 * Returns: A uniformly-distributed pseudorandom integer in the interval
139 extern uint32 grand_range(grand */*r*/, uint32 /*l*/);
141 /* --- @grand_fill@ --- *
143 * Arguments: @grand *r@ = pointet to generic generator
144 * @void *p@ = pointer to a buffer
145 * @size_t sz@ = size of the buffer
149 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
150 * (see @grand_byte@).
153 extern void grand_fill(grand */*r*/, void */*p*/, size_t /*sz*/);
155 /*----- That's all, folks -------------------------------------------------*/