1*d6eebaa4SHervé Poussineau /*
2*d6eebaa4SHervé Poussineau  * magic.c - PPP Magic Number routines.
3*d6eebaa4SHervé Poussineau  *
4*d6eebaa4SHervé Poussineau  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
5*d6eebaa4SHervé Poussineau  *
6*d6eebaa4SHervé Poussineau  * Redistribution and use in source and binary forms, with or without
7*d6eebaa4SHervé Poussineau  * modification, are permitted provided that the following conditions
8*d6eebaa4SHervé Poussineau  * are met:
9*d6eebaa4SHervé Poussineau  *
10*d6eebaa4SHervé Poussineau  * 1. Redistributions of source code must retain the above copyright
11*d6eebaa4SHervé Poussineau  *    notice, this list of conditions and the following disclaimer.
12*d6eebaa4SHervé Poussineau  *
13*d6eebaa4SHervé Poussineau  * 2. Redistributions in binary form must reproduce the above copyright
14*d6eebaa4SHervé Poussineau  *    notice, this list of conditions and the following disclaimer in
15*d6eebaa4SHervé Poussineau  *    the documentation and/or other materials provided with the
16*d6eebaa4SHervé Poussineau  *    distribution.
17*d6eebaa4SHervé Poussineau  *
18*d6eebaa4SHervé Poussineau  * 3. The name "Carnegie Mellon University" must not be used to
19*d6eebaa4SHervé Poussineau  *    endorse or promote products derived from this software without
20*d6eebaa4SHervé Poussineau  *    prior written permission. For permission or any legal
21*d6eebaa4SHervé Poussineau  *    details, please contact
22*d6eebaa4SHervé Poussineau  *      Office of Technology Transfer
23*d6eebaa4SHervé Poussineau  *      Carnegie Mellon University
24*d6eebaa4SHervé Poussineau  *      5000 Forbes Avenue
25*d6eebaa4SHervé Poussineau  *      Pittsburgh, PA  15213-3890
26*d6eebaa4SHervé Poussineau  *      (412) 268-4387, fax: (412) 268-7395
27*d6eebaa4SHervé Poussineau  *      tech-transfer@andrew.cmu.edu
28*d6eebaa4SHervé Poussineau  *
29*d6eebaa4SHervé Poussineau  * 4. Redistributions of any form whatsoever must retain the following
30*d6eebaa4SHervé Poussineau  *    acknowledgment:
31*d6eebaa4SHervé Poussineau  *    "This product includes software developed by Computing Services
32*d6eebaa4SHervé Poussineau  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33*d6eebaa4SHervé Poussineau  *
34*d6eebaa4SHervé Poussineau  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35*d6eebaa4SHervé Poussineau  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36*d6eebaa4SHervé Poussineau  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37*d6eebaa4SHervé Poussineau  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38*d6eebaa4SHervé Poussineau  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39*d6eebaa4SHervé Poussineau  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40*d6eebaa4SHervé Poussineau  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41*d6eebaa4SHervé Poussineau  */
421734f297SVictor Perevertkin /*****************************************************************************
43*d6eebaa4SHervé Poussineau * randm.c - Random number generator program file.
441734f297SVictor Perevertkin *
451734f297SVictor Perevertkin * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
46*d6eebaa4SHervé Poussineau * Copyright (c) 1998 by Global Election Systems Inc.
471734f297SVictor Perevertkin *
481734f297SVictor Perevertkin * The authors hereby grant permission to use, copy, modify, distribute,
491734f297SVictor Perevertkin * and license this software and its documentation for any purpose, provided
501734f297SVictor Perevertkin * that existing copyright notices are retained in all copies and that this
511734f297SVictor Perevertkin * notice and the following disclaimer are included verbatim in any
521734f297SVictor Perevertkin * distributions. No written agreement, license, or royalty fee is required
531734f297SVictor Perevertkin * for any of the authorized uses.
541734f297SVictor Perevertkin *
551734f297SVictor Perevertkin * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
561734f297SVictor Perevertkin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
571734f297SVictor Perevertkin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
581734f297SVictor Perevertkin * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
591734f297SVictor Perevertkin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
601734f297SVictor Perevertkin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
611734f297SVictor Perevertkin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
621734f297SVictor Perevertkin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
631734f297SVictor Perevertkin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
641734f297SVictor Perevertkin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
651734f297SVictor Perevertkin *
661734f297SVictor Perevertkin ******************************************************************************
671734f297SVictor Perevertkin * REVISION HISTORY
681734f297SVictor Perevertkin *
691734f297SVictor Perevertkin * 03-01-01 Marc Boucher <marc@mbsi.ca>
701734f297SVictor Perevertkin *   Ported to lwIP.
71*d6eebaa4SHervé Poussineau * 98-06-03 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
72*d6eebaa4SHervé Poussineau *   Extracted from avos.
731734f297SVictor Perevertkin *****************************************************************************/
741734f297SVictor Perevertkin 
75*d6eebaa4SHervé Poussineau #include "netif/ppp/ppp_opts.h"
76*d6eebaa4SHervé Poussineau #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
771734f297SVictor Perevertkin 
78*d6eebaa4SHervé Poussineau #include "netif/ppp/ppp_impl.h"
79*d6eebaa4SHervé Poussineau #include "netif/ppp/magic.h"
801734f297SVictor Perevertkin 
81*d6eebaa4SHervé Poussineau #if PPP_MD5_RANDM /* Using MD5 for better randomness if enabled */
821734f297SVictor Perevertkin 
83*d6eebaa4SHervé Poussineau #include "netif/ppp/pppcrypt.h"
84*d6eebaa4SHervé Poussineau 
85*d6eebaa4SHervé Poussineau #define MD5_HASH_SIZE 16
86*d6eebaa4SHervé Poussineau static char magic_randpool[MD5_HASH_SIZE];   /* Pool of randomness. */
87*d6eebaa4SHervé Poussineau static long magic_randcount;      /* Pseudo-random incrementer */
88*d6eebaa4SHervé Poussineau static u32_t magic_randomseed;    /* Seed used for random number generation. */
891734f297SVictor Perevertkin 
901734f297SVictor Perevertkin /*
91*d6eebaa4SHervé Poussineau  * Churn the randomness pool on a random event.  Call this early and often
92*d6eebaa4SHervé Poussineau  *  on random and semi-random system events to build randomness in time for
93*d6eebaa4SHervé Poussineau  *  usage.  For randomly timed events, pass a null pointer and a zero length
94*d6eebaa4SHervé Poussineau  *  and this will use the system timer and other sources to add randomness.
95*d6eebaa4SHervé Poussineau  *  If new random data is available, pass a pointer to that and it will be
96*d6eebaa4SHervé Poussineau  *  included.
971734f297SVictor Perevertkin  *
98*d6eebaa4SHervé Poussineau  * Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
991734f297SVictor Perevertkin  */
magic_churnrand(char * rand_data,u32_t rand_len)100*d6eebaa4SHervé Poussineau static void magic_churnrand(char *rand_data, u32_t rand_len) {
101*d6eebaa4SHervé Poussineau   lwip_md5_context md5_ctx;
102*d6eebaa4SHervé Poussineau 
103*d6eebaa4SHervé Poussineau   /* LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: %u@%P\n", rand_len, rand_data)); */
104*d6eebaa4SHervé Poussineau   lwip_md5_init(&md5_ctx);
105*d6eebaa4SHervé Poussineau   lwip_md5_starts(&md5_ctx);
106*d6eebaa4SHervé Poussineau   lwip_md5_update(&md5_ctx, (u_char *)magic_randpool, sizeof(magic_randpool));
107*d6eebaa4SHervé Poussineau   if (rand_data) {
108*d6eebaa4SHervé Poussineau     lwip_md5_update(&md5_ctx, (u_char *)rand_data, rand_len);
109*d6eebaa4SHervé Poussineau   } else {
110*d6eebaa4SHervé Poussineau     struct {
111*d6eebaa4SHervé Poussineau       /* INCLUDE fields for any system sources of randomness */
112*d6eebaa4SHervé Poussineau       u32_t jiffies;
113*d6eebaa4SHervé Poussineau #ifdef LWIP_RAND
114*d6eebaa4SHervé Poussineau       u32_t rand;
115*d6eebaa4SHervé Poussineau #endif /* LWIP_RAND */
116*d6eebaa4SHervé Poussineau     } sys_data;
117*d6eebaa4SHervé Poussineau     /* Load sys_data fields here. */
118*d6eebaa4SHervé Poussineau     magic_randomseed += sys_jiffies();
119*d6eebaa4SHervé Poussineau     sys_data.jiffies = magic_randomseed;
120*d6eebaa4SHervé Poussineau #ifdef LWIP_RAND
121*d6eebaa4SHervé Poussineau     sys_data.rand = LWIP_RAND();
122*d6eebaa4SHervé Poussineau #endif /* LWIP_RAND */
123*d6eebaa4SHervé Poussineau     lwip_md5_update(&md5_ctx, (u_char *)&sys_data, sizeof(sys_data));
124*d6eebaa4SHervé Poussineau   }
125*d6eebaa4SHervé Poussineau   lwip_md5_finish(&md5_ctx, (u_char *)magic_randpool);
126*d6eebaa4SHervé Poussineau   lwip_md5_free(&md5_ctx);
127*d6eebaa4SHervé Poussineau /*  LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: -> 0\n")); */
1281734f297SVictor Perevertkin }
1291734f297SVictor Perevertkin 
1301734f297SVictor Perevertkin /*
131*d6eebaa4SHervé Poussineau  * Initialize the random number generator.
1321734f297SVictor Perevertkin  */
magic_init(void)133*d6eebaa4SHervé Poussineau void magic_init(void) {
134*d6eebaa4SHervé Poussineau   magic_churnrand(NULL, 0);
135*d6eebaa4SHervé Poussineau }
136*d6eebaa4SHervé Poussineau 
137*d6eebaa4SHervé Poussineau /*
138*d6eebaa4SHervé Poussineau  * Randomize our random seed value.
139*d6eebaa4SHervé Poussineau  */
magic_randomize(void)140*d6eebaa4SHervé Poussineau void magic_randomize(void) {
141*d6eebaa4SHervé Poussineau   magic_churnrand(NULL, 0);
142*d6eebaa4SHervé Poussineau }
143*d6eebaa4SHervé Poussineau 
144*d6eebaa4SHervé Poussineau /*
145*d6eebaa4SHervé Poussineau  * Fill a buffer with random bytes.
146*d6eebaa4SHervé Poussineau  *
147*d6eebaa4SHervé Poussineau  * Use the random pool to generate random data.  This degrades to pseudo
148*d6eebaa4SHervé Poussineau  *  random when used faster than randomness is supplied using magic_churnrand().
149*d6eebaa4SHervé Poussineau  * Note: It's important that there be sufficient randomness in magic_randpool
150*d6eebaa4SHervé Poussineau  *  before this is called for otherwise the range of the result may be
151*d6eebaa4SHervé Poussineau  *  narrow enough to make a search feasible.
152*d6eebaa4SHervé Poussineau  *
153*d6eebaa4SHervé Poussineau  * Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
154*d6eebaa4SHervé Poussineau  *
155*d6eebaa4SHervé Poussineau  * XXX Why does he not just call magic_churnrand() for each block?  Probably
156*d6eebaa4SHervé Poussineau  *  so that you don't ever publish the seed which could possibly help
157*d6eebaa4SHervé Poussineau  *  predict future values.
158*d6eebaa4SHervé Poussineau  * XXX Why don't we preserve md5 between blocks and just update it with
159*d6eebaa4SHervé Poussineau  *  magic_randcount each time?  Probably there is a weakness but I wish that
160*d6eebaa4SHervé Poussineau  *  it was documented.
161*d6eebaa4SHervé Poussineau  */
magic_random_bytes(unsigned char * buf,u32_t buf_len)162*d6eebaa4SHervé Poussineau void magic_random_bytes(unsigned char *buf, u32_t buf_len) {
163*d6eebaa4SHervé Poussineau   lwip_md5_context md5_ctx;
164*d6eebaa4SHervé Poussineau   u_char tmp[MD5_HASH_SIZE];
165*d6eebaa4SHervé Poussineau   u32_t n;
166*d6eebaa4SHervé Poussineau 
167*d6eebaa4SHervé Poussineau   while (buf_len > 0) {
168*d6eebaa4SHervé Poussineau     lwip_md5_init(&md5_ctx);
169*d6eebaa4SHervé Poussineau     lwip_md5_starts(&md5_ctx);
170*d6eebaa4SHervé Poussineau     lwip_md5_update(&md5_ctx, (u_char *)magic_randpool, sizeof(magic_randpool));
171*d6eebaa4SHervé Poussineau     lwip_md5_update(&md5_ctx, (u_char *)&magic_randcount, sizeof(magic_randcount));
172*d6eebaa4SHervé Poussineau     lwip_md5_finish(&md5_ctx, tmp);
173*d6eebaa4SHervé Poussineau     lwip_md5_free(&md5_ctx);
174*d6eebaa4SHervé Poussineau     magic_randcount++;
175*d6eebaa4SHervé Poussineau     n = LWIP_MIN(buf_len, MD5_HASH_SIZE);
176*d6eebaa4SHervé Poussineau     MEMCPY(buf, tmp, n);
177*d6eebaa4SHervé Poussineau     buf += n;
178*d6eebaa4SHervé Poussineau     buf_len -= n;
179*d6eebaa4SHervé Poussineau   }
180*d6eebaa4SHervé Poussineau }
181*d6eebaa4SHervé Poussineau 
182*d6eebaa4SHervé Poussineau /*
183*d6eebaa4SHervé Poussineau  * Return a new 32-bit random number.
184*d6eebaa4SHervé Poussineau  */
magic(void)185*d6eebaa4SHervé Poussineau u32_t magic(void) {
186*d6eebaa4SHervé Poussineau   u32_t new_rand;
187*d6eebaa4SHervé Poussineau 
188*d6eebaa4SHervé Poussineau   magic_random_bytes((unsigned char *)&new_rand, sizeof(new_rand));
189*d6eebaa4SHervé Poussineau   return new_rand;
190*d6eebaa4SHervé Poussineau }
191*d6eebaa4SHervé Poussineau 
192*d6eebaa4SHervé Poussineau #else /* PPP_MD5_RANDM */
193*d6eebaa4SHervé Poussineau 
194*d6eebaa4SHervé Poussineau #ifndef LWIP_RAND
195*d6eebaa4SHervé Poussineau static int  magic_randomized;       /* Set when truly randomized. */
196*d6eebaa4SHervé Poussineau #endif /* LWIP_RAND */
197*d6eebaa4SHervé Poussineau static u32_t magic_randomseed;      /* Seed used for random number generation. */
198*d6eebaa4SHervé Poussineau 
199*d6eebaa4SHervé Poussineau /*
200*d6eebaa4SHervé Poussineau  * Initialize the random number generator.
201*d6eebaa4SHervé Poussineau  *
202*d6eebaa4SHervé Poussineau  * Here we attempt to compute a random number seed but even if
203*d6eebaa4SHervé Poussineau  * it isn't random, we'll randomize it later.
204*d6eebaa4SHervé Poussineau  *
205*d6eebaa4SHervé Poussineau  * The current method uses the jiffies counter.  When this is
206*d6eebaa4SHervé Poussineau  * invoked at startup the jiffies counter value may repeat
207*d6eebaa4SHervé Poussineau  * after each boot.  Thus we call it again on the first
208*d6eebaa4SHervé Poussineau  * random event.
209*d6eebaa4SHervé Poussineau  *
210*d6eebaa4SHervé Poussineau  * If LWIP_RAND if available, we do not call srand() as we are
211*d6eebaa4SHervé Poussineau  * not going to call rand().
212*d6eebaa4SHervé Poussineau  */
magic_init(void)213*d6eebaa4SHervé Poussineau void magic_init(void) {
214*d6eebaa4SHervé Poussineau   magic_randomseed += sys_jiffies();
215*d6eebaa4SHervé Poussineau #ifndef LWIP_RAND
216*d6eebaa4SHervé Poussineau   /* Initialize the random number generator. */
217*d6eebaa4SHervé Poussineau   srand((unsigned)magic_randomseed);
218*d6eebaa4SHervé Poussineau #endif /* LWIP_RAND */
219*d6eebaa4SHervé Poussineau }
220*d6eebaa4SHervé Poussineau 
221*d6eebaa4SHervé Poussineau /*
222*d6eebaa4SHervé Poussineau  * Randomize our random seed value.  Here we use the fact that
223*d6eebaa4SHervé Poussineau  * this function is called at *truly random* times by the polling
224*d6eebaa4SHervé Poussineau  * and network functions.  Here we only get 16 bits of new random
225*d6eebaa4SHervé Poussineau  * value but we use the previous value to randomize the other 16
226*d6eebaa4SHervé Poussineau  * bits.
227*d6eebaa4SHervé Poussineau  */
magic_randomize(void)228*d6eebaa4SHervé Poussineau void magic_randomize(void) {
229*d6eebaa4SHervé Poussineau #ifndef LWIP_RAND
230*d6eebaa4SHervé Poussineau   if (!magic_randomized) {
231*d6eebaa4SHervé Poussineau     magic_randomized = !0;
232*d6eebaa4SHervé Poussineau     magic_init();
233*d6eebaa4SHervé Poussineau     /* The initialization function also updates the seed. */
234*d6eebaa4SHervé Poussineau     return;
235*d6eebaa4SHervé Poussineau   }
236*d6eebaa4SHervé Poussineau #endif /* LWIP_RAND */
237*d6eebaa4SHervé Poussineau   magic_randomseed += sys_jiffies();
238*d6eebaa4SHervé Poussineau }
239*d6eebaa4SHervé Poussineau 
240*d6eebaa4SHervé Poussineau /*
241*d6eebaa4SHervé Poussineau  * Return a new 32-bit random number.
242*d6eebaa4SHervé Poussineau  *
243*d6eebaa4SHervé Poussineau  * Here we use the rand() function to supply a pseudo random
244*d6eebaa4SHervé Poussineau  * number which we make truly random by combining it with our own
245*d6eebaa4SHervé Poussineau  * seed which is randomized by truly random events.
246*d6eebaa4SHervé Poussineau  * Thus the numbers will be truly random unless there have been no
247*d6eebaa4SHervé Poussineau  * operator or network events in which case it will be pseudo random
248*d6eebaa4SHervé Poussineau  * seeded by srand().
249*d6eebaa4SHervé Poussineau  *
250*d6eebaa4SHervé Poussineau  * Alternatively, use LWIP_RAND if available, but we do not assume
251*d6eebaa4SHervé Poussineau  * it is returning 32 bits of random data because it is probably
252*d6eebaa4SHervé Poussineau  * going to be defined to directly return the rand() value. For
253*d6eebaa4SHervé Poussineau  * example, LCP magic numbers are 32-bit random values.
254*d6eebaa4SHervé Poussineau  */
magic(void)255*d6eebaa4SHervé Poussineau u32_t magic(void) {
256*d6eebaa4SHervé Poussineau #ifdef LWIP_RAND
257*d6eebaa4SHervé Poussineau   return (LWIP_RAND() << 16) + LWIP_RAND() + magic_randomseed;
258*d6eebaa4SHervé Poussineau #else /* LWIP_RAND */
259*d6eebaa4SHervé Poussineau   return ((u32_t)rand() << 16) + (u32_t)rand() + magic_randomseed;
260*d6eebaa4SHervé Poussineau #endif /* LWIP_RAND */
261*d6eebaa4SHervé Poussineau }
262*d6eebaa4SHervé Poussineau 
263*d6eebaa4SHervé Poussineau /*
264*d6eebaa4SHervé Poussineau  * Fill a buffer with random bytes.
265*d6eebaa4SHervé Poussineau  */
magic_random_bytes(unsigned char * buf,u32_t buf_len)266*d6eebaa4SHervé Poussineau void magic_random_bytes(unsigned char *buf, u32_t buf_len) {
267*d6eebaa4SHervé Poussineau   u32_t new_rand, n;
268*d6eebaa4SHervé Poussineau 
269*d6eebaa4SHervé Poussineau   while (buf_len > 0) {
270*d6eebaa4SHervé Poussineau     new_rand = magic();
271*d6eebaa4SHervé Poussineau     n = LWIP_MIN(buf_len, sizeof(new_rand));
272*d6eebaa4SHervé Poussineau     MEMCPY(buf, &new_rand, n);
273*d6eebaa4SHervé Poussineau     buf += n;
274*d6eebaa4SHervé Poussineau     buf_len -= n;
275*d6eebaa4SHervé Poussineau   }
276*d6eebaa4SHervé Poussineau }
277*d6eebaa4SHervé Poussineau #endif /* PPP_MD5_RANDM */
278*d6eebaa4SHervé Poussineau 
279*d6eebaa4SHervé Poussineau /*
280*d6eebaa4SHervé Poussineau  * Return a new random number between 0 and (2^pow)-1 included.
281*d6eebaa4SHervé Poussineau  */
magic_pow(u8_t pow)282*d6eebaa4SHervé Poussineau u32_t magic_pow(u8_t pow) {
283*d6eebaa4SHervé Poussineau   return magic() & ~(~0UL<<pow);
2841734f297SVictor Perevertkin }
2851734f297SVictor Perevertkin 
2861734f297SVictor Perevertkin #endif /* PPP_SUPPORT */
287