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