Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

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