Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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