Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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