Yasushi TAUCHI / Mbed 2 deprecated giken9_HTMLServer_Sample

Dependencies:   EthernetInterface HttpServer TextLCD mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
yueee_yt
Date:
Wed Mar 12 04:39:15 2014 +0000
Revision:
2:14b689a85306
Parent:
0:7766f6712673
bug fix

Who changed what in which revision?

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