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