Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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