Donatien Garnier / LwIPNetworking

Dependencies:   DebugLib Socket lwip lwip-sys

Dependents:   EthernetInterface

Fork of NetworkingCoreLib by Donatien Garnier

Committer:
donatien
Date:
Thu May 24 14:49:40 2012 +0000
Revision:
0:013f5d54248c
Initial Commit

Who changed what in which revision?

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