75 #include "netif/ppp/ppp_opts.h" 78 #include "netif/ppp/ppp_impl.h" 79 #include "netif/ppp/magic.h" 83 #include "netif/ppp/pppcrypt.h" 85 #define MD5_HASH_SIZE 16 86 static char magic_randpool[MD5_HASH_SIZE];
87 static long magic_randcount;
88 static u32_t magic_randomseed;
100 static void magic_churnrand(
char *rand_data, u32_t rand_len) {
101 lwip_md5_context md5_ctx;
104 lwip_md5_init(&md5_ctx);
105 lwip_md5_starts(&md5_ctx);
106 lwip_md5_update(&md5_ctx, (u_char *)magic_randpool,
sizeof(magic_randpool));
108 lwip_md5_update(&md5_ctx, (u_char *)rand_data, rand_len);
118 sys_data.jiffies = magic_randomseed;
120 sys_data.rand = LWIP_RAND();
123 lwip_md5_update(&md5_ctx, (u_char *)&sys_data,
sizeof(sys_data));
125 lwip_md5_finish(&md5_ctx, (u_char *)magic_randpool);
126 lwip_md5_free(&md5_ctx);
133 void magic_init(
void) {
134 magic_churnrand(NULL, 0);
140 void magic_randomize(
void) {
141 magic_churnrand(NULL, 0);
162 void magic_random_bytes(
unsigned char *buf, u32_t buf_len) {
163 lwip_md5_context md5_ctx;
164 u_char tmp[MD5_HASH_SIZE];
167 while (buf_len > 0) {
168 lwip_md5_init(&md5_ctx);
169 lwip_md5_starts(&md5_ctx);
170 lwip_md5_update(&md5_ctx, (u_char *)magic_randpool,
sizeof(magic_randpool));
171 lwip_md5_update(&md5_ctx, (u_char *)&magic_randcount,
sizeof(magic_randcount));
172 lwip_md5_finish(&md5_ctx, tmp);
173 lwip_md5_free(&md5_ctx);
175 n = LWIP_MIN(buf_len, MD5_HASH_SIZE);
188 magic_random_bytes((
unsigned char *)&new_rand,
sizeof(new_rand));
199 static int magic_randomized;
201 static u32_t magic_randomseed;
222 void magic_init(
void) {
226 srand((
unsigned)magic_randomseed);
239 void magic_randomize(
void) {
241 if (!magic_randomized) {
242 magic_randomized = !0;
265 return LWIP_RAND() + magic_randomseed;
267 return ((u32_t)rand() << 16) + (u32_t)rand() + magic_randomseed;
274 void magic_random_bytes(
unsigned char *buf, u32_t buf_len) {
277 while (buf_len > 0) {
279 n = LWIP_MIN(buf_len,
sizeof(new_rand));
280 MEMCPY(buf, &new_rand, n);
290 u32_t magic_pow(u8_t pow) {
291 return magic() & ~(~0UL<<pow);