Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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