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