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