First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 15:54:21 2010 +0000
Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc

        

Who changed what in which revision?

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