3 * $Id: grand.h,v 1.2 2000/06/17 11:23:11 mdw Exp $
5 * Generic interface to random number generators
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/06/17 11:23:11 mdw
34 * Typesetting fix. Add a flags word to the generic generator.
36 * Revision 1.1 1999/12/10 23:16:01 mdw
41 #ifndef CATACOMB_GRAND_H
42 #define CATACOMB_GRAND_H
48 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/bits.h>
55 /*----- Generic random number generator interface -------------------------*/
57 typedef struct grand
{
58 const struct grand_ops
*ops
;
61 typedef struct grand_ops
{
63 /* --- Various important properties --- */
65 const char *name
; /* Generator's name */
66 unsigned f
; /* Various flags */
67 uint32 max
; /* Maximum raw output */
69 /* --- Maintenance methods --- */
71 int (*misc
)(grand */
*r*/
, unsigned /*op*/, ...); /* Miscellaneous ops */
72 void (*destroy
)(grand */
*r*/
); /* Destroy generator context */
74 /* --- Output methods --- *
76 * Only one of these operations need actually be implemented. All the
77 * other operations may be synthesized. Of course, performance is improved
78 * if more are provided.
81 uint32 (*raw
)(grand */
*r*/
); /* Uniform over %$[0, max)$% */
82 octet (*byte
)(grand */
*r*/
); /* Uniform over %$[0, 256)$% */
83 uint32 (*word
)(grand */
*r*/
); /* Uniform over %$[0, 2^{32})$% */
84 uint32 (*range
)(grand */
*r*/
, uint32
/*l*/); /* Uniform over %$[0, l)$% */
85 void (*fill
)(grand */
*r*/
, void */
*p*/
, size_t /*sz*/); /* Fill buffer */
88 /* --- Flag types --- */
91 GRAND_CRYPTO
= 1u /* Cryptographically strong */
94 /* --- Operation types --- */
98 /* --- Required operations --- */
100 GRAND_CHECK
, /* @unsigned op2@ */
102 /* --- Standard seeding operations --- */
104 GRAND_SEEDINT
, /* @int i@ */
105 GRAND_SEEDUINT32
, /* @uint32 i@ */
106 GRAND_SEEDBLOCK
, /* @const void *p, size_t sz@ */
107 GRAND_SEEDMP
, /* @mp *m@ */
108 GRAND_SEEDRAND
, /* @grand *g@ */
110 /* --- Generator-specific operations --- */
112 GRAND_SPECIFIC
= 256u
115 #define GRAND_BADOP assert(((void)"bad grand_misc op", 0))
117 /*----- Functions provided ------------------------------------------------*/
119 /* --- @grand_byte@ --- *
121 * Arguments: @grand *r@ = pointet to generic generator
123 * Returns: A uniformly-distributed pseudorandom integer in the interval
127 extern octet
grand_byte(grand */
*r*/
);
129 /* --- @grand_word@ --- *
131 * Arguments: @grand *r@ = pointet to generic generator
133 * Returns: A uniformly-distributed pseudorandom integer in the interval
137 extern uint32
grand_word(grand */
*r*/
);
139 /* --- @grand_range@ --- *
141 * Arguments: @grand *r@ = pointet to generic generator
142 * @uint32 l@ = limit for acceptable results
144 * Returns: A uniformly-distributed pseudorandom integer in the interval
148 extern uint32
grand_range(grand */
*r*/
, uint32
/*l*/);
150 /* --- @grand_fill@ --- *
152 * Arguments: @grand *r@ = pointet to generic generator
153 * @void *p@ = pointer to a buffer
154 * @size_t sz@ = size of the buffer
158 * Use: Fills a buffer with uniformly distributed pseudorandom bytes
159 * (see @grand_byte@).
162 extern void grand_fill(grand */
*r*/
, void */
*p*/
, size_t /*sz*/);
164 /*----- That's all, folks -------------------------------------------------*/