ProjetoBB

Dependencies:   F7_Ethernet WebSocketClient mbed mcp3008

Fork of Nucleo_F746ZG_Ethernet by Dieter Graef

Committer:
DieterGraef
Date:
Sat Jun 18 10:49:12 2016 +0000
Revision:
0:f9b6112278fe
Ethernet for the NUCLEO STM32F746 Board Testprogram uses DHCP and NTP to set the clock

Who changed what in which revision?

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