5d4a6be9 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: dsarand.h,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
5d4a6be9 |
4 | * |
5 | * Random number generator for DSA |
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 | |
5d4a6be9 |
30 | #ifndef CATACOMB_DSARAND_H |
31 | #define CATACOMB_DSARAND_H |
32 | |
33 | #ifdef __cplusplus |
34 | extern "C" { |
35 | #endif |
36 | |
37 | /*----- Header files ------------------------------------------------------*/ |
38 | |
39 | #include <mLib/bits.h> |
40 | |
41 | #ifndef CATACOMB_GRAND_H |
42 | # include "grand.h" |
43 | #endif |
44 | |
45 | #ifndef CATACOMB_SHA_H |
46 | # include "sha.h" |
47 | #endif |
48 | |
49 | /*----- Data structures ---------------------------------------------------*/ |
50 | |
51 | typedef struct dsarand { |
52 | octet *p; /* Pointer to seed (modified) */ |
53 | size_t sz; /* Size of the seed buffer */ |
54 | unsigned passes; /* Number of passes to make */ |
55 | } dsarand; |
56 | |
57 | /*----- Functions provided ------------------------------------------------*/ |
58 | |
59 | /* --- @dsarand_init@ --- * |
60 | * |
61 | * Arguments: @dsarand *d@ = pointer to context |
62 | * @const void *p@ = pointer to seed buffer |
63 | * @size_t sz@ = size of the buffer |
64 | * |
65 | * Returns: --- |
66 | * |
67 | * Use: Initializes a DSA random number generator. |
68 | */ |
69 | |
70 | extern void dsarand_init(dsarand */*d*/, const void */*p*/, size_t /*sz*/); |
71 | |
72 | /* --- @dsarand_reseed@ --- * |
73 | * |
74 | * Arguments: @dsarand *d@ = pointer to context |
75 | * @const void *p@ = pointer to seed buffer |
76 | * @size_t sz@ = size of the buffer |
77 | * |
78 | * Returns: --- |
79 | * |
80 | * Use: Initializes a DSA random number generator. |
81 | */ |
82 | |
83 | extern void dsarand_reseed(dsarand */*d*/, const void */*p*/, size_t /*sz*/); |
84 | |
85 | /* --- @dsarand_destroy@ --- * |
86 | * |
87 | * Arguments: @dsarand *d@ = pointer to context |
88 | * |
89 | * Returns: --- |
90 | * |
91 | * Use: Disposes of a DSA random number generation context. |
92 | */ |
93 | |
94 | extern void dsarand_destroy(dsarand */*d*/); |
95 | |
96 | /* --- @dsarand_fill@ --- * |
97 | * |
98 | * Arguments: @dsarand *d@ = pointer to context |
99 | * @void *p@ = pointer to output buffer |
100 | * @size_t sz@ = size of output buffer |
101 | * |
102 | * Returns: --- |
103 | * |
104 | * Use: Fills an output buffer with pseudorandom data. |
105 | * |
106 | * Let %$p$% be the numerical value of the input buffer, and let |
107 | * %$b$% be the number of bytes required. Let |
e8c3c91f |
108 | * %$z = \lceil b / 20 \rceil$% be the number of SHA outputs |
5d4a6be9 |
109 | * required. Then the output of pass %$n$% is |
110 | * |
111 | * %$P_n = \sum_{0 \le i < z} 2^{160i} SHA(p + nz + i)$% |
112 | * %${} \bmod 2^{8b}$% |
113 | * |
114 | * and the actual result in the output buffer is the XOR of all |
115 | * of the output passes. |
116 | * |
117 | * The DSA procedure for choosing @q@ involves two passes with |
118 | * %$z = 1$%; the procedure for choosing @p@ involves one pass |
119 | * with larger %$z$%. This generalization of the DSA generation |
120 | * procedure is my own invention but it seems relatively sound. |
121 | */ |
122 | |
123 | extern void dsarand_fill(dsarand */*d*/, void */*p*/, size_t /*sz*/); |
124 | |
125 | /*----- Generic pseudorandom-number generator interface -------------------*/ |
126 | |
127 | /* --- Miscellaneous operations --- */ |
128 | |
129 | enum { |
518749ae |
130 | DSARAND_PASSES = GRAND_SPECIFIC('D'), /* @unsigned n@ */ |
131 | DSARAND_SEEDSZ, /* No args */ |
132 | DSARAND_GETSEED /* @void *buf@ */ |
5d4a6be9 |
133 | }; |
134 | |
135 | /* --- @dsarand_create@ --- * |
136 | * |
137 | * Arguments: @const void *p@ = pointer to seed buffer |
138 | * @size_t sz@ = size of seed buffer |
139 | * |
140 | * Returns: Pointer to a generic generator. |
141 | * |
518749ae |
142 | * Use: Constructs a generic generator interface to a DSA generator. |
5d4a6be9 |
143 | */ |
144 | |
145 | extern grand *dsarand_create(const void */*p*/, size_t /*sz*/); |
146 | |
147 | /*----- That's all, folks -------------------------------------------------*/ |
148 | |
149 | #ifdef __cplusplus |
150 | } |
151 | #endif |
152 | |
153 | #endif |