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