NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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