SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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