Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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