| 1 |
786 |
skrzyp |
/*****************************************************************************
|
| 2 |
|
|
* randm.c - Random number generator program file.
|
| 3 |
|
|
*
|
| 4 |
|
|
* Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
|
| 5 |
|
|
* Copyright (c) 1998 by Global Election Systems Inc.
|
| 6 |
|
|
*
|
| 7 |
|
|
* The authors hereby grant permission to use, copy, modify, distribute,
|
| 8 |
|
|
* and license this software and its documentation for any purpose, provided
|
| 9 |
|
|
* that existing copyright notices are retained in all copies and that this
|
| 10 |
|
|
* notice and the following disclaimer are included verbatim in any
|
| 11 |
|
|
* distributions. No written agreement, license, or royalty fee is required
|
| 12 |
|
|
* for any of the authorized uses.
|
| 13 |
|
|
*
|
| 14 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
|
| 15 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
| 16 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
| 17 |
|
|
* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
| 18 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
| 19 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 20 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 21 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 22 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
| 23 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 24 |
|
|
*
|
| 25 |
|
|
******************************************************************************
|
| 26 |
|
|
* REVISION HISTORY
|
| 27 |
|
|
*
|
| 28 |
|
|
* 03-01-01 Marc Boucher <marc@mbsi.ca>
|
| 29 |
|
|
* Ported to lwIP.
|
| 30 |
|
|
* 98-06-03 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
|
| 31 |
|
|
* Extracted from avos.
|
| 32 |
|
|
*****************************************************************************/
|
| 33 |
|
|
|
| 34 |
|
|
#include "lwip/opt.h"
|
| 35 |
|
|
|
| 36 |
|
|
#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
|
| 37 |
|
|
|
| 38 |
|
|
#include "md5.h"
|
| 39 |
|
|
#include "randm.h"
|
| 40 |
|
|
#include "timesys.h"
|
| 41 |
|
|
|
| 42 |
|
|
#include "netif/ppp/ppp.h"
|
| 43 |
|
|
#include "pppdebug.h"
|
| 44 |
|
|
|
| 45 |
|
|
#if MD5_SUPPORT /* this module depends on MD5 */
|
| 46 |
|
|
|
| 47 |
|
|
#define RAND_POOL_SIZE 16 /* Bytes stored in the pool of randomness. */
|
| 48 |
|
|
|
| 49 |
|
|
/*****************************/
|
| 50 |
|
|
/*** LOCAL DATA STRUCTURES ***/
|
| 51 |
|
|
/*****************************/
|
| 52 |
|
|
|
| 53 |
|
|
static char rand_pool[RAND_POOL_SIZE]; /* Pool of randomness. */
|
| 54 |
|
|
static long rand_count = 0; /* Pseudo-random incrementer */
|
| 55 |
|
|
|
| 56 |
|
|
/***********************************/
|
| 57 |
|
|
/*** PUBLIC FUNCTION DEFINITIONS ***/
|
| 58 |
|
|
/***********************************/
|
| 59 |
|
|
|
| 60 |
|
|
/*
|
| 61 |
|
|
* Initialize the random number generator.
|
| 62 |
|
|
*
|
| 63 |
|
|
* Since this is to be called on power up, we don't have much
|
| 64 |
|
|
* system randomess to work with. Here all we use is the
|
| 65 |
|
|
* real-time clock. We'll accumulate more randomness as soon
|
| 66 |
|
|
* as things start happening.
|
| 67 |
|
|
*/
|
| 68 |
|
|
void
|
| 69 |
|
|
randm_init()
|
| 70 |
|
|
{
|
| 71 |
|
|
randm_churn(NULL, 0);
|
| 72 |
|
|
}
|
| 73 |
|
|
|
| 74 |
|
|
/*
|
| 75 |
|
|
* Churn the randomness pool on a random event. Call this early and often
|
| 76 |
|
|
* on random and semi-random system events to build randomness in time for
|
| 77 |
|
|
* usage. For randomly timed events, pass a null pointer and a zero length
|
| 78 |
|
|
* and this will use the system timer and other sources to add randomness.
|
| 79 |
|
|
* If new random data is available, pass a pointer to that and it will be
|
| 80 |
|
|
* included.
|
| 81 |
|
|
*
|
| 82 |
|
|
* Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
|
| 83 |
|
|
*/
|
| 84 |
|
|
void
|
| 85 |
|
|
randm_churn(char *data, u32_t len)
|
| 86 |
|
|
{
|
| 87 |
|
|
md5_ctx md5;
|
| 88 |
|
|
|
| 89 |
|
|
/* ppp_trace(LOG_INFO, "churnRand: %u@%P\n", len, data); */
|
| 90 |
|
|
md5_init(&md5);
|
| 91 |
|
|
md5_update(&md5, (u8_t *) rand_pool, sizeof(rand_pool));
|
| 92 |
|
|
if (data) {
|
| 93 |
|
|
md5_update(&md5, (u8_t *) data, len);
|
| 94 |
|
|
} else {
|
| 95 |
|
|
struct {
|
| 96 |
|
|
/* INCLUDE fields for any system sources of randomness */
|
| 97 |
|
|
char foobar;
|
| 98 |
|
|
} sys_data;
|
| 99 |
|
|
/* Load sys_data fields here. */
|
| 100 |
|
|
md5_update(&md5, (u8_t *) &sys_data, sizeof(sys_data));
|
| 101 |
|
|
}
|
| 102 |
|
|
md5_final((u8_t *) rand_pool, &md5);
|
| 103 |
|
|
/* ppp_trace(LOG_INFO, "churnRand: -> 0\n"); */
|
| 104 |
|
|
}
|
| 105 |
|
|
|
| 106 |
|
|
/*
|
| 107 |
|
|
* Use the random pool to generate random data. This degrades to pseudo
|
| 108 |
|
|
* random when used faster than randomness is supplied using churnRand().
|
| 109 |
|
|
* Note: It's important that there be sufficient randomness in rand_pool
|
| 110 |
|
|
* before this is called for otherwise the range of the result may be
|
| 111 |
|
|
* narrow enough to make a search feasible.
|
| 112 |
|
|
*
|
| 113 |
|
|
* Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
|
| 114 |
|
|
*
|
| 115 |
|
|
* XXX Why does he not just call churnRand() for each block? Probably
|
| 116 |
|
|
* so that you don't ever publish the seed which could possibly help
|
| 117 |
|
|
* predict future values.
|
| 118 |
|
|
* XXX Why don't we preserve md5 between blocks and just update it with
|
| 119 |
|
|
* rand_count each time? Probably there is a weakness but I wish that
|
| 120 |
|
|
* it was documented.
|
| 121 |
|
|
*/
|
| 122 |
|
|
void
|
| 123 |
|
|
randm_gen_rand(char *buf, u32_t len)
|
| 124 |
|
|
{
|
| 125 |
|
|
md5_ctx md5;
|
| 126 |
|
|
u8_t tmp[16];
|
| 127 |
|
|
u32_t n;
|
| 128 |
|
|
|
| 129 |
|
|
while (len > 0) {
|
| 130 |
|
|
n = LWIP_MIN(len, RAND_POOL_SIZE);
|
| 131 |
|
|
md5_init(&md5);
|
| 132 |
|
|
md5_update(&md5, (u8_t *) rand_pool, sizeof(rand_pool));
|
| 133 |
|
|
md5_update(&md5, (u8_t *) &rand_count, sizeof(rand_count));
|
| 134 |
|
|
md5_final(tmp, &md5);
|
| 135 |
|
|
rand_count++;
|
| 136 |
|
|
MEMCPY(buf, tmp, n);
|
| 137 |
|
|
buf += n;
|
| 138 |
|
|
len -= n;
|
| 139 |
|
|
}
|
| 140 |
|
|
}
|
| 141 |
|
|
|
| 142 |
|
|
/*
|
| 143 |
|
|
* Return a new random number.
|
| 144 |
|
|
*/
|
| 145 |
|
|
u32_t
|
| 146 |
|
|
randm_rand()
|
| 147 |
|
|
{
|
| 148 |
|
|
u32_t rand;
|
| 149 |
|
|
|
| 150 |
|
|
randm_gen_rand((char *) &rand, sizeof(rand));
|
| 151 |
|
|
|
| 152 |
|
|
return rand;
|
| 153 |
|
|
}
|
| 154 |
|
|
|
| 155 |
|
|
#else /* MD5_SUPPORT */
|
| 156 |
|
|
|
| 157 |
|
|
/*****************************/
|
| 158 |
|
|
/*** LOCAL DATA STRUCTURES ***/
|
| 159 |
|
|
/*****************************/
|
| 160 |
|
|
|
| 161 |
|
|
static int randomized = 0; /* Set when truely randomized. */
|
| 162 |
|
|
static u32_t seed = 0; /* Seed used for random number generation. */
|
| 163 |
|
|
|
| 164 |
|
|
/***********************************/
|
| 165 |
|
|
/*** PUBLIC FUNCTION DEFINITIONS ***/
|
| 166 |
|
|
/***********************************/
|
| 167 |
|
|
|
| 168 |
|
|
/*
|
| 169 |
|
|
* Initialize the random number generator.
|
| 170 |
|
|
*
|
| 171 |
|
|
* Here we attempt to compute a random number seed but even if
|
| 172 |
|
|
* it isn't random, we'll randomize it later.
|
| 173 |
|
|
*
|
| 174 |
|
|
* The current method uses the fields from the real time clock,
|
| 175 |
|
|
* the idle process counter, the millisecond counter, and the
|
| 176 |
|
|
* hardware timer tick counter. When this is invoked
|
| 177 |
|
|
* in startup(), then the idle counter and timer values may
|
| 178 |
|
|
* repeat after each boot and the real time clock may not be
|
| 179 |
|
|
* operational. Thus we call it again on the first random
|
| 180 |
|
|
* event.
|
| 181 |
|
|
*/
|
| 182 |
|
|
void
|
| 183 |
|
|
randm_init()
|
| 184 |
|
|
{
|
| 185 |
|
|
/* XXX week but probably enough */
|
| 186 |
|
|
seed += ppp_jiffies();
|
| 187 |
|
|
|
| 188 |
|
|
/* Initialize the Borland random number generator. */
|
| 189 |
|
|
srand(seed);
|
| 190 |
|
|
}
|
| 191 |
|
|
|
| 192 |
|
|
/*
|
| 193 |
|
|
* Randomize our random seed value. Here we use the fact that
|
| 194 |
|
|
* this function is called at *truely random* times by the polling
|
| 195 |
|
|
* and network functions. Here we only get 16 bits of new random
|
| 196 |
|
|
* value but we use the previous value to randomize the other 16
|
| 197 |
|
|
* bits.
|
| 198 |
|
|
*/
|
| 199 |
|
|
void
|
| 200 |
|
|
randm_randomize(void)
|
| 201 |
|
|
{
|
| 202 |
|
|
static u32_t last_jiffies;
|
| 203 |
|
|
|
| 204 |
|
|
if (!randomized) {
|
| 205 |
|
|
randomized = 1;
|
| 206 |
|
|
randm_init();
|
| 207 |
|
|
/* The initialization function also updates the seed. */
|
| 208 |
|
|
} else {
|
| 209 |
|
|
/* XXX week but probably enough */
|
| 210 |
|
|
seed += (seed << 16) + (ppp_jiffies() - last_jiffies); /* XXX */
|
| 211 |
|
|
}
|
| 212 |
|
|
last_jiffies = ppp_jiffies();
|
| 213 |
|
|
}
|
| 214 |
|
|
|
| 215 |
|
|
/*
|
| 216 |
|
|
* Return a new random number.
|
| 217 |
|
|
* Here we use the Borland rand() function to supply a pseudo random
|
| 218 |
|
|
* number which we make truely random by combining it with our own
|
| 219 |
|
|
* seed which is randomized by truely random events.
|
| 220 |
|
|
* Thus the numbers will be truely random unless there have been no
|
| 221 |
|
|
* operator or network events in which case it will be pseudo random
|
| 222 |
|
|
* seeded by the real time clock.
|
| 223 |
|
|
*/
|
| 224 |
|
|
u32_t
|
| 225 |
|
|
randm_rand()
|
| 226 |
|
|
{
|
| 227 |
|
|
return ((((u32_t) rand() << 16) + rand()) + seed);
|
| 228 |
|
|
}
|
| 229 |
|
|
|
| 230 |
|
|
#endif /* MD5_SUPPORT */
|
| 231 |
|
|
|
| 232 |
|
|
#endif /* PPP_SUPPORT */
|