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