Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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