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