Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

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