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