mirror of
https://git.savannah.nongnu.org/git/lwip.git
synced 2025-08-06 22:44:38 +08:00
reworked magic/randm
This commit is contained in:
parent
bcba806ef0
commit
1a1deb5d58
@ -74,7 +74,6 @@
|
|||||||
|
|
||||||
#include "lwip/opt.h"
|
#include "lwip/opt.h"
|
||||||
|
|
||||||
#define PPP_SUPPORT 1
|
|
||||||
#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
|
#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
|
||||||
|
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
@ -82,50 +81,21 @@
|
|||||||
#include "pppd.h"
|
#include "pppd.h"
|
||||||
#include "pppmy.h"
|
#include "pppmy.h"
|
||||||
|
|
||||||
/*
|
|
||||||
* magic_init - Initialize the magic number generator.
|
|
||||||
*
|
|
||||||
* Attempts to compute a random number seed which will not repeat.
|
|
||||||
* The current method uses the current hostid, current process ID
|
|
||||||
* and current time, currently.
|
|
||||||
*/
|
|
||||||
void magic_init() {
|
|
||||||
avRandomInit();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
#if MD5_SUPPORT /* Using MD5 for better randomness if MD5 support is enabled */
|
||||||
* magic - Returns the next magic number.
|
|
||||||
*/
|
|
||||||
u_int32_t magic() {
|
|
||||||
return (u_int32_t)avRandom();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if MD5_SUPPORT /* this module depends on MD5 */
|
#define MAGIC_RANDPOOLSIZE 16 /* Bytes stored in the pool of randomness. */
|
||||||
#define RANDPOOLSZ 16 /* Bytes stored in the pool of randomness. */
|
|
||||||
|
|
||||||
/*****************************/
|
/*****************************/
|
||||||
/*** LOCAL DATA STRUCTURES ***/
|
/*** LOCAL DATA STRUCTURES ***/
|
||||||
/*****************************/
|
/*****************************/
|
||||||
static char randPool[RANDPOOLSZ]; /* Pool of randomness. */
|
static char magic_randpool[MAGIC_RANDPOOLSIZE]; /* Pool of randomness. */
|
||||||
static long randCount = 0; /* Pseudo-random incrementer */
|
static long magic_randcount = 0; /* Pseudo-random incrementer */
|
||||||
|
|
||||||
|
|
||||||
/***********************************/
|
/***********************************/
|
||||||
/*** PUBLIC FUNCTION DEFINITIONS ***/
|
/*** PUBLIC FUNCTION DEFINITIONS ***/
|
||||||
/***********************************/
|
/***********************************/
|
||||||
/*
|
|
||||||
* Initialize the random number generator.
|
|
||||||
*
|
|
||||||
* Since this is to be called on power up, we don't have much
|
|
||||||
* system randomess to work with. Here all we use is the
|
|
||||||
* real-time clock. We'll accumulate more randomness as soon
|
|
||||||
* as things start happening.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
avRandomInit()
|
|
||||||
{
|
|
||||||
avChurnRand(NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Churn the randomness pool on a random event. Call this early and often
|
* Churn the randomness pool on a random event. Call this early and often
|
||||||
@ -137,33 +107,47 @@ avRandomInit()
|
|||||||
*
|
*
|
||||||
* Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
|
* Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
|
||||||
*/
|
*/
|
||||||
void
|
void magic_churnrand(char *rand_data, u32_t rand_len) {
|
||||||
avChurnRand(char *randData, u32_t randLen)
|
|
||||||
{
|
|
||||||
MD5_CTX md5;
|
MD5_CTX md5;
|
||||||
|
|
||||||
/* LWIP_DEBUGF(LOG_INFO, ("churnRand: %u@%P\n", randLen, randData)); */
|
/* LWIP_DEBUGF(LOG_INFO, ("churnRand: %u@%P\n", rand_len, rand_data)); */
|
||||||
MD5_Init(&md5);
|
MD5_Init(&md5);
|
||||||
MD5_Update(&md5, (u_char *)randPool, sizeof(randPool));
|
MD5_Update(&md5, (u_char *)magic_randpool, sizeof(magic_randpool));
|
||||||
if (randData) {
|
if (rand_data) {
|
||||||
MD5_Update(&md5, (u_char *)randData, randLen);
|
MD5_Update(&md5, (u_char *)rand_data, rand_len);
|
||||||
} else {
|
} else {
|
||||||
struct {
|
struct {
|
||||||
/* INCLUDE fields for any system sources of randomness */
|
/* INCLUDE fields for any system sources of randomness */
|
||||||
char foobar;
|
char foobar;
|
||||||
} sysData;
|
} sys_data;
|
||||||
|
|
||||||
/* Load sysData fields here. */
|
/* Load sys_data fields here. */
|
||||||
MD5_Update(&md5, (u_char *)&sysData, sizeof(sysData));
|
MD5_Update(&md5, (u_char *)&sys_data, sizeof(sys_data));
|
||||||
}
|
}
|
||||||
MD5_Final((u_char *)randPool, &md5);
|
MD5_Final((u_char *)magic_randpool, &md5);
|
||||||
/* LWIP_DEBUGF(LOG_INFO, ("churnRand: -> 0\n")); */
|
/* LWIP_DEBUGF(LOG_INFO, ("churnRand: -> 0\n")); */
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* Initialize the random number generator.
|
||||||
|
*/
|
||||||
|
void magic_init() {
|
||||||
|
magic_churnrand(NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Randomize our random seed value.
|
||||||
|
*/
|
||||||
|
void magic_randomize(void) {
|
||||||
|
magic_churnrand(NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* random_bytes - Fill a buffer with random bytes.
|
||||||
|
*
|
||||||
* Use the random pool to generate random data. This degrades to pseudo
|
* Use the random pool to generate random data. This degrades to pseudo
|
||||||
* random when used faster than randomness is supplied using churnRand().
|
* random when used faster than randomness is supplied using churnRand().
|
||||||
* Note: It's important that there be sufficient randomness in randPool
|
* Note: It's important that there be sufficient randomness in magic_randpool
|
||||||
* before this is called for otherwise the range of the result may be
|
* before this is called for otherwise the range of the result may be
|
||||||
* narrow enough to make a search feasible.
|
* narrow enough to make a search feasible.
|
||||||
*
|
*
|
||||||
@ -173,49 +157,36 @@ avChurnRand(char *randData, u32_t randLen)
|
|||||||
* so that you don't ever publish the seed which could possibly help
|
* so that you don't ever publish the seed which could possibly help
|
||||||
* predict future values.
|
* predict future values.
|
||||||
* XXX Why don't we preserve md5 between blocks and just update it with
|
* XXX Why don't we preserve md5 between blocks and just update it with
|
||||||
* randCount each time? Probably there is a weakness but I wish that
|
* magic_randcount each time? Probably there is a weakness but I wish that
|
||||||
* it was documented.
|
* it was documented.
|
||||||
*/
|
*/
|
||||||
void
|
void random_bytes(unsigned char *buf, u32_t buf_len) {
|
||||||
avGenRand(char *buf, u32_t bufLen)
|
|
||||||
{
|
|
||||||
MD5_CTX md5;
|
MD5_CTX md5;
|
||||||
u_char tmp[16];
|
u_char tmp[16];
|
||||||
u32_t n;
|
u32_t n;
|
||||||
|
|
||||||
while (bufLen > 0) {
|
while (buf_len > 0) {
|
||||||
n = LWIP_MIN(bufLen, RANDPOOLSZ);
|
n = LWIP_MIN(buf_len, MAGIC_RANDPOOLSIZE);
|
||||||
MD5_Init(&md5);
|
MD5_Init(&md5);
|
||||||
MD5_Update(&md5, (u_char *)randPool, sizeof(randPool));
|
MD5_Update(&md5, (u_char *)magic_randpool, sizeof(magic_randpool));
|
||||||
MD5_Update(&md5, (u_char *)&randCount, sizeof(randCount));
|
MD5_Update(&md5, (u_char *)&magic_randcount, sizeof(magic_randcount));
|
||||||
MD5_Final(tmp, &md5);
|
MD5_Final(tmp, &md5);
|
||||||
randCount++;
|
magic_randcount++;
|
||||||
MEMCPY(buf, tmp, n);
|
MEMCPY(buf, tmp, n);
|
||||||
buf += n;
|
buf += n;
|
||||||
bufLen -= n;
|
buf_len -= n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return a new random number.
|
* Return a new random number.
|
||||||
*/
|
*/
|
||||||
u32_t
|
u32_t magic() {
|
||||||
avRandom()
|
u32_t new_rand;
|
||||||
{
|
|
||||||
u32_t newRand;
|
|
||||||
|
|
||||||
avGenRand((char *)&newRand, sizeof(newRand));
|
random_bytes((char *)&new_rand, sizeof(new_rand));
|
||||||
|
|
||||||
return newRand;
|
return new_rand;
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* random_bytes - Fill a buffer with random bytes.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
random_bytes(unsigned char *buf, int len)
|
|
||||||
{
|
|
||||||
avGenRand(buf, len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* MD5_SUPPORT */
|
#else /* MD5_SUPPORT */
|
||||||
@ -223,13 +194,14 @@ random_bytes(unsigned char *buf, int len)
|
|||||||
/*****************************/
|
/*****************************/
|
||||||
/*** LOCAL DATA STRUCTURES ***/
|
/*** LOCAL DATA STRUCTURES ***/
|
||||||
/*****************************/
|
/*****************************/
|
||||||
static int avRandomized = 0; /* Set when truely randomized. */
|
static int magic_randomized = 0; /* Set when truely randomized. */
|
||||||
static u32_t avRandomSeed = 0; /* Seed used for random number generation. */
|
static u32_t magic_randomseed = 0; /* Seed used for random number generation. */
|
||||||
|
|
||||||
|
|
||||||
/***********************************/
|
/***********************************/
|
||||||
/*** PUBLIC FUNCTION DEFINITIONS ***/
|
/*** PUBLIC FUNCTION DEFINITIONS ***/
|
||||||
/***********************************/
|
/***********************************/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize the random number generator.
|
* Initialize the random number generator.
|
||||||
*
|
*
|
||||||
@ -244,59 +216,39 @@ static u32_t avRandomSeed = 0; /* Seed used for random number generation. *
|
|||||||
* operational. Thus we call it again on the first random
|
* operational. Thus we call it again on the first random
|
||||||
* event.
|
* event.
|
||||||
*/
|
*/
|
||||||
void
|
void magic_init() {
|
||||||
avRandomInit()
|
magic_randomseed += sys_jiffies();
|
||||||
{
|
|
||||||
#if 0
|
|
||||||
/* Get a pointer into the last 4 bytes of clockBuf. */
|
|
||||||
u32_t *lptr1 = (u32_t *)((char *)&clockBuf[3]);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Initialize our seed using the real-time clock, the idle
|
|
||||||
* counter, the millisecond timer, and the hardware timer
|
|
||||||
* tick counter. The real-time clock and the hardware
|
|
||||||
* tick counter are the best sources of randomness but
|
|
||||||
* since the tick counter is only 16 bit (and truncated
|
|
||||||
* at that), the idle counter and millisecond timer
|
|
||||||
* (which may be small values) are added to help
|
|
||||||
* randomize the lower 16 bits of the seed.
|
|
||||||
*/
|
|
||||||
readClk();
|
|
||||||
avRandomSeed += *(u32_t *)clockBuf + *lptr1 + OSIdleCtr
|
|
||||||
+ ppp_mtime() + ((u32_t)TM1 << 16) + TM1;
|
|
||||||
#else
|
|
||||||
avRandomSeed += sys_jiffies(); /* XXX */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Initialize the Borland random number generator. */
|
/* Initialize the Borland random number generator. */
|
||||||
srand((unsigned)avRandomSeed);
|
srand((unsigned)magic_randomseed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* magic_init - Initialize the magic number generator.
|
||||||
|
*
|
||||||
* Randomize our random seed value. Here we use the fact that
|
* Randomize our random seed value. Here we use the fact that
|
||||||
* this function is called at *truely random* times by the polling
|
* this function is called at *truely random* times by the polling
|
||||||
* and network functions. Here we only get 16 bits of new random
|
* and network functions. Here we only get 16 bits of new random
|
||||||
* value but we use the previous value to randomize the other 16
|
* value but we use the previous value to randomize the other 16
|
||||||
* bits.
|
* bits.
|
||||||
*/
|
*/
|
||||||
void
|
void magic_randomize(void) {
|
||||||
avRandomize(void)
|
|
||||||
{
|
|
||||||
static u32_t last_jiffies;
|
static u32_t last_jiffies;
|
||||||
|
|
||||||
if (!avRandomized) {
|
if (!magic_randomized) {
|
||||||
avRandomized = !0;
|
magic_randomized = !0;
|
||||||
avRandomInit();
|
magic_randominit();
|
||||||
/* The initialization function also updates the seed. */
|
/* The initialization function also updates the seed. */
|
||||||
} else {
|
} else {
|
||||||
/* avRandomSeed += (avRandomSeed << 16) + TM1; */
|
/* magic_randomseed += (magic_randomseed << 16) + TM1; */
|
||||||
avRandomSeed += (sys_jiffies() - last_jiffies); /* XXX */
|
magic_randomseed += (sys_jiffies() - last_jiffies); /* XXX */
|
||||||
}
|
}
|
||||||
last_jiffies = sys_jiffies();
|
last_jiffies = sys_jiffies();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return a new random number.
|
* Return a new random number.
|
||||||
|
*
|
||||||
* Here we use the Borland rand() function to supply a pseudo random
|
* Here we use the Borland rand() function to supply a pseudo random
|
||||||
* number which we make truely random by combining it with our own
|
* number which we make truely random by combining it with our own
|
||||||
* seed which is randomized by truely random events.
|
* seed which is randomized by truely random events.
|
||||||
@ -304,10 +256,8 @@ avRandomize(void)
|
|||||||
* operator or network events in which case it will be pseudo random
|
* operator or network events in which case it will be pseudo random
|
||||||
* seeded by the real time clock.
|
* seeded by the real time clock.
|
||||||
*/
|
*/
|
||||||
u32_t
|
u32_t magic() {
|
||||||
avRandom()
|
return ((((u32_t)rand() << 16) + rand()) + magic_randomseed);
|
||||||
{
|
|
||||||
return ((((u32_t)rand() << 16) + rand()) + avRandomSeed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MD5_SUPPORT */
|
#endif /* MD5_SUPPORT */
|
||||||
|
@ -74,38 +74,37 @@
|
|||||||
* Extracted from avos.
|
* Extracted from avos.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#ifndef RANDM_H
|
#ifndef MAGIC_H
|
||||||
#define RANDM_H
|
#define MAGIC_H
|
||||||
|
|
||||||
|
#include "lwip/opt.h"
|
||||||
|
|
||||||
|
#if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
|
||||||
|
|
||||||
/***********************
|
/***********************
|
||||||
*** PUBLIC FUNCTIONS ***
|
*** PUBLIC FUNCTIONS ***
|
||||||
***********************/
|
***********************/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize the random number generator.
|
* Initialize the random number generator.
|
||||||
*/
|
*/
|
||||||
void avRandomInit(void);
|
void magic_init(void);
|
||||||
|
|
||||||
/*
|
|
||||||
* Churn the randomness pool on a random event. Call this early and often
|
|
||||||
* on random and semi-random system events to build randomness in time for
|
|
||||||
* usage. For randomly timed events, pass a null pointer and a zero length
|
|
||||||
* and this will use the system timer and other sources to add randomness.
|
|
||||||
* If new random data is available, pass a pointer to that and it will be
|
|
||||||
* included.
|
|
||||||
*/
|
|
||||||
void avChurnRand(char *randData, u32_t randLen);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Randomize our random seed value. To be called for truely random events
|
* Randomize our random seed value. To be called for truely random events
|
||||||
* such as user operations and network traffic.
|
* such as user operations and network traffic.
|
||||||
*/
|
*/
|
||||||
#if MD5_SUPPORT
|
void magic_randomize(void);
|
||||||
#define avRandomize() avChurnRand(NULL, 0)
|
|
||||||
#else /* MD5_SUPPORT */
|
|
||||||
void avRandomize(void);
|
|
||||||
#endif /* MD5_SUPPORT */
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* Return a new random number.
|
||||||
|
*/
|
||||||
|
u32_t magic(void); /* Returns the next magic number */
|
||||||
|
|
||||||
|
#if MD5_SUPPORT
|
||||||
|
/*
|
||||||
|
* Fill buffer with random bytes
|
||||||
|
*
|
||||||
* Use the random pool to generate random data. This degrades to pseudo
|
* Use the random pool to generate random data. This degrades to pseudo
|
||||||
* random when used faster than randomness is supplied using churnRand().
|
* random when used faster than randomness is supplied using churnRand().
|
||||||
* Thus it's important to make sure that the results of this are not
|
* Thus it's important to make sure that the results of this are not
|
||||||
@ -113,18 +112,8 @@ void avRandomize(void);
|
|||||||
* least some degree. Also, it's important to get a good seed before
|
* least some degree. Also, it's important to get a good seed before
|
||||||
* the first use.
|
* the first use.
|
||||||
*/
|
*/
|
||||||
void avGenRand(char *buf, u32_t bufLen);
|
void random_bytes(unsigned char *buf, u32_t len);
|
||||||
|
#endif /* MD5_SUPPORT */
|
||||||
|
|
||||||
/*
|
#endif /* PPP_SUPPORT */
|
||||||
* Return a new random number.
|
#endif /* MAGIC_H */
|
||||||
*/
|
|
||||||
u32_t avRandom(void);
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* RANDM_H */
|
|
||||||
|
|
||||||
void magic_init __P((void)); /* Initialize the magic number generator */
|
|
||||||
u_int32_t magic __P((void)); /* Returns the next magic number */
|
|
||||||
|
|
||||||
/* Fill buffer with random bytes */
|
|
||||||
void random_bytes __P((unsigned char *buf, int len));
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user