/* This function ignores the byte order suggestion in RFC3414 * since it simply doesn't influence the effectiveness of an IV. * * Implementing RFC3826 priv param algorithm if LWIP_RAND is available. * * @todo: This is a potential thread safety issue. */ err_t snmpv3_build_priv_param(u8_t *priv_param) { #ifdef LWIP_RAND /* Based on RFC3826 */ static u8_t init; static u32_t priv1, priv2; /* Lazy initialisation */ if (init == 0) { init = 1; priv1 = LWIP_RAND(); priv2 = LWIP_RAND(); } SMEMCPY(&priv_param[0], &priv1, sizeof(priv1)); SMEMCPY(&priv_param[4], &priv2, sizeof(priv2)); /* Emulate 64bit increment */ priv1++; if (!priv1) { /* Overflow */ priv2++; } #else /* Based on RFC3414 */ static u32_t ctr; u32_t boots = snmpv3_get_engine_boots_internal(); SMEMCPY(&priv_param[0], &boots, 4); SMEMCPY(&priv_param[4], &ctr, 4); ctr++; #endif return ERR_OK; }
/* * Return a new random number. * * Here we use the Borland rand() function to supply a pseudo random * number which we make truely random by combining it with our own * seed which is randomized by truely random events. * Thus the numbers will be truely random unless there have been no * operator or network events in which case it will be pseudo random * seeded by the real time clock. */ u32_t magic(void) { #ifdef LWIP_RAND return LWIP_RAND() + magic_randomseed; #else /* LWIP_RAND */ return ((u32_t)rand() << 16) + (u32_t)rand() + magic_randomseed; #endif /* LWIP_RAND */ }
/* * 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. * * Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427 */ static void magic_churnrand(char *rand_data, u32_t rand_len) { md5_context md5_ctx; /* LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: %u@%P\n", rand_len, rand_data)); */ md5_starts(&md5_ctx); md5_update(&md5_ctx, (u_char *)magic_randpool, sizeof(magic_randpool)); if (rand_data) { md5_update(&md5_ctx, (u_char *)rand_data, rand_len); } else { struct { /* INCLUDE fields for any system sources of randomness */ u32_t jiffies; #ifdef LWIP_RAND u32_t rand; #endif /* LWIP_RAND */ } sys_data; magic_randomseed += sys_jiffies(); sys_data.jiffies = magic_randomseed; #ifdef LWIP_RAND sys_data.rand = LWIP_RAND(); #endif /* LWIP_RAND */ /* Load sys_data fields here. */ md5_update(&md5_ctx, (u_char *)&sys_data, sizeof(sys_data)); } md5_finish(&md5_ctx, (u_char *)magic_randpool); /* LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: -> 0\n")); */ }
/** * Initialize this module. */ void udp_init(void) { #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND()); #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */ }
void netif_create_ip4_linklocal_address(struct netif * netif) { #if 1 ip_addr_t linklocal; ip_addr_t linklocal_mask; ip4_addr_t addr = {0}; /* Link-local prefix and mask. */ IP4_ADDR(ip_2_ip4(&linklocal), 169, 254, 0, 0); IP4_ADDR(ip_2_ip4(&linklocal_mask), 255, 255, 0, 0); if (!ip4_addr_netcmp( ip_2_ip4(&linklocal), ip_2_ip4(&netif->link_local_addr), ip_2_ip4(&linklocal_mask) ) && !ip4_addr_isany(ip_2_ip4(&netif->ip_addr)) ) { IP4_ADDR( ip_2_ip4(&netif->link_local_addr), 169, 254, ip4_addr3( ip_2_ip4(&netif->ip_addr) ) , ip4_addr4( ip_2_ip4(&netif->ip_addr) ) ); return; } while ( !(addr.addr) || !ip4_addr4(&addr) ) //os_get_random((unsigned char *)&addr, sizeof(addr)); addr.addr = LWIP_RAND(); if ( ip_2_ip4(&netif->netmask)->addr > IP_CLASSB_NET && !ip4_addr_isany( ip_2_ip4(&netif->ip_addr) )) { // random host address IP4_ADDR( ip_2_ip4(&netif->link_local_addr), 169, 254, ip4_addr3( ip_2_ip4(&netif->ip_addr)) , ip4_addr4(&addr)); } else { IP4_ADDR( ip_2_ip4(&netif->link_local_addr), 169, 254, ip4_addr3(&addr), ip4_addr4(&addr) ); } #endif }
static void mbed_lwip_core_init(void) { // Check if we've already brought up lwip if (!lwip_inited) { // Seed lwip random lwip_seed_random(); // Initialise TCP sequence number uint32_t tcp_isn_secret[4]; for (int i = 0; i < 4; i++) { tcp_isn_secret[i] = LWIP_RAND(); } lwip_init_tcp_isn(0, (u8_t *) &tcp_isn_secret); sys_sem_new(&lwip_tcpip_inited, 0); sys_sem_new(&lwip_netif_linked, 0); sys_sem_new(&lwip_netif_unlinked, 0); sys_sem_new(&lwip_netif_has_any_addr, 0); #if PREF_ADDR_TIMEOUT sys_sem_new(&lwip_netif_has_pref_addr, 0); #endif #if BOTH_ADDR_TIMEOUT sys_sem_new(&lwip_netif_has_both_addr, 0); #endif tcpip_init(mbed_lwip_tcpip_init_irq, NULL); sys_arch_sem_wait(&lwip_tcpip_inited, 0); lwip_inited = true; } }
/** * Schedule a delayed membership report for a group * * @param group the mld_group for which "delaying" membership report * should be sent * @param maxresp the max resp delay provided in the query */ static void mld6_delayed_report(struct mld_group *group, u16_t maxresp) { /* Convert maxresp from milliseconds to tmr ticks */ maxresp = maxresp / MLD6_TMR_INTERVAL; if (maxresp == 0) { maxresp = 1; } #ifdef LWIP_RAND /* Randomize maxresp. (if LWIP_RAND is supported) */ maxresp = LWIP_RAND() % maxresp; if (maxresp == 0) { maxresp = 1; } #endif /* LWIP_RAND */ /* Apply timer value if no report has been scheduled already. */ if ((group->group_state == MLD6_GROUP_IDLE_MEMBER) || ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) && ((group->timer == 0) || (maxresp < group->timer)))) { group->timer = maxresp; group->group_state = MLD6_GROUP_DELAYING_MEMBER; } }
/****************************************************************************** * FunctionName : espconn_port * Description : access port value for client so that we don't end up bouncing * all connections at the same time . * Parameters : none * Returns : access port value *******************************************************************************/ uint32 ICACHE_FLASH_ATTR espconn_port(void) { uint32 port = 0; static uint32 randnum = 0; do { port = system_get_time(); if (port < 0) { port = LWIP_RAND() - port; } port %= 0xc350; if (port < 0x400) { port += 0x400; } } while (port == randnum); randnum = port; return port; }
/* LWIP network stack implementation */ LWIP::LWIP() { default_interface = NULL; // Seed lwip random lwip_seed_random(); // Initialise TCP sequence number uint32_t tcp_isn_secret[4]; for (int i = 0; i < 4; i++) { tcp_isn_secret[i] = LWIP_RAND(); } lwip_init_tcp_isn(0, (u8_t *) &tcp_isn_secret); tcpip_init(&LWIP::tcpip_init_irq, this); tcpip_inited.wait(0); // Zero out socket set arena_init(); }
/** * Schedule a delayed membership report for a group * * @param group the mld_group for which "delaying" membership report * should be sent * @param maxresp the max resp delay provided in the query */ static void mld6_delayed_report(struct mld_group *group, u16_t maxresp) { /* Convert maxresp from milliseconds to tmr ticks */ maxresp = maxresp / MLD6_TMR_INTERVAL; if (maxresp == 0) { maxresp = 1; } #ifdef LWIP_RAND /* Randomize maxresp. (if LWIP_RAND is supported) */ maxresp = LWIP_RAND() % maxresp; if (maxresp == 0) { maxresp = 1; } #endif /* LWIP_RAND */ /* Apply timer value if no report has been scheduled already. */ if ((group->group_state == MLD6_GROUP_IDLE_MEMBER) || ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) && ((group->timer == 0) || (maxresp < group->timer)))) { group->timer = maxresp; unsigned long curr_ticks = sys_arch_get_os_ticks() / MLD_TIMER_OFFSET; group->timer = group->timer + curr_ticks; group->group_state = MLD6_GROUP_DELAYING_MEMBER; struct mld_group* min_timeout_grp = lwip_get_min_timeout_mld_grp(); if (min_timeout_grp && (min_timeout_grp->timer) > (curr_ticks)) { if (group->timer <= min_timeout_grp->timer) { unsigned int new_timer_val = (min_timeout_grp->timer) - curr_ticks; sys_untimeout(mld6_timer, NULL); sys_timeout(new_timer_val, mld6_timer, NULL); } } } }