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