HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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