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