Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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