Example self-announcing webserver which controls a servo through a smallHTML userinterface.

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:56:01 2010 +0000
Revision:
0:a259777c45a3

        

Who changed what in which revision?

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