uint32_t AutoIPRand(NET_CONFIG* pNet) { LoadState(_TCPIPStackNetIx(pNet)); LFSRSeedRand(AutoIPClient.wRandSeed); AutoIPClient.wRandSeed = LFSRRand(); return AutoIPClient.wRandSeed; }
DWORD AutoIPRand (BYTE vInterface) { LoadState (vInterface); LFSRSeedRand(AutoIPClient.wRandSeed); AutoIPClient.wRandSeed = LFSRRand(); return AutoIPClient.wRandSeed; }
void EthernetInit(void) { // Initialize hardware InitBoard(); // Initialize stack-related hardware components that may be // required by the UART configuration routines TickInit(); // Initialize Stack and application related NV variables into AppConfig. InitAppConfig(); // Seed the LFSRRand() function. LFSRSeedRand(GenerateRandomDWORD()); // Initialize the MAC library. MACInit(); // Initialize the ARP library. ARPInit(); // Initialize UDP. UDPInit(); // Open up a socket for our UDP server. localServerSocket = UDPOpenEx(NULL, UDP_OPEN_SERVER, UDP_SERVER_PORT, UDP_SERVER_PORT); if (localServerSocket == INVALID_UDP_SOCKET) { FATAL_ERROR(); } // Open up a socket for our UDP client. remoteServerSocket = UDPOpenEx(NULL, UDP_OPEN_IP_ADDRESS, UDP_CLIENT_PORT, UDP_CLIENT_PORT); if (remoteServerSocket == INVALID_UDP_SOCKET) { FATAL_ERROR(); } }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); MACInit(); #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__18CXX) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif }
/***************************************************************************** Function: uint32_t SYS_GENERATE_RANDOM_DWORD(void) Summary: Generates a random uint32_t. Description: This function generates a random 32-bit integer. It collects randomness by comparing the A/D converter's internal R/C oscillator clock with our main system clock. By passing collected entropy to the LFSRSeedRand()/LFSRRand() functions, the output is normalized (deskewed) in the hopes of meeting statistical randomness tests. Precondition: None Parameters: None Returns: Random 32-bit number. Side Effects: This function uses the A/D converter (and so you must disable interrupts if you use the A/D converted in your ISR). The LFSRRand() function will be reseeded, and Timer1 (PIC24, dsPIC, and PIC32) will be used. TMR#H:TMR#L will have a new value. Note that this is the same timer used by the Tick module. Remarks: This function times out after 1 second of attempting to generate the random uint32_t. In such a case, the output may not be truly random. Typically, this function executes in around 500,000 instruction cycles. The intent of this function is to produce statistically random and cryptographically secure random number. Whether or not this is true on all (or any) devices/voltages/temperatures is not tested. ***************************************************************************/ uint32_t SYS_GENERATE_RANDOM_DWORD(void) { uint8_t vBitCount; uint16_t w, wTime, wLastValue; uint32_t dwTotalTime; union { uint32_t dw; uint16_t w[2]; } randomResult; uint16_t AD1CON1Save, AD1CON2Save, AD1CON3Save; uint16_t T1CONSave, PR1Save; // Save hardware SFRs AD1CON1Save = AD1CON1; AD1CON2Save = AD1CON2; AD1CON3Save = AD1CON3; T1CONSave = T1CON; PR1Save = PR1; // Set up Timer and A/D converter module AD1CON1 = 0x0000; // Turn off the ADC so we can write to it AD1CON3 = 0x9F00; // Frc A/D clock, 31 Tad acquisition AD1CON2 = 0x003F; // Interrupt after every 16th sample/convert AD1CON1 = 0x80E4; // Turn on the A/D module, auto-convert T1CON = 0x8000; // TON = 1, no prescalar PR1 = 0xFFFF; // Don't clear timer early vBitCount = 0; dwTotalTime = 0; wLastValue = 0; randomResult.dw = LFSRRand(); while(1) { SYS_WDT_Clear(); #if defined(__C30__) while(!IFS0bits.AD1IF); #else while(!IFS1bits.AD1IF); #endif wTime = TMR1; TMR1 = 0x0000; #if defined(__C30__) IFS0bits.AD1IF = 0; #else IFS1CLR = _IFS1_AD1IF_MASK; #endif w = LFSRRand(); // Wait no longer than 1 second obtaining entropy dwTotalTime += wTime; if(dwTotalTime >= SYS_CLK_ClockGet()) { randomResult.w[0] ^= LFSRRand(); randomResult.w[1] ^= LFSRRand(); break; } // Keep sampling if minimal entropy was likely obtained this round if(wLastValue == wTime) continue; // Add this entropy into the pseudo random number generator by reseeding LFSRSeedRand(w + (wLastValue - wTime)); wLastValue = wTime; // Accumulate at least 32 bits of randomness over time randomResult.dw <<= 1; if(LFSRRand() & 0x0080) randomResult.w[0] |= 0x1; // See if we've collected a fair amount of entropy and can quit early if(++vBitCount == 0u) break; } // Restore hardware SFRs AD1CON1 = 0x0000; // Turn off the ADC so we can write to it AD1CON3 = AD1CON3Save; AD1CON2 = AD1CON2Save; AD1CON1 = AD1CON1Save; T1CON = T1CONSave; PR1 = PR1Save; return randomResult.dw; }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { static BOOL once = FALSE; smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif #if defined (WF_CS_TRIS) && defined (STACK_USE_DHCP_CLIENT) g_DhcpRenew = FALSE; g_DhcpRetryTimer = 0; #endif if (!once) { // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); once = TRUE; } MACInit(); #if defined (WF_AGGRESSIVE_PS) && defined (WF_CS_TRIS) WFEnableAggressivePowerSave(); #endif #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__18CXX) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif #if defined(STACK_USE_CCS_SMTP) SMTPInit(); #endif #if defined(STACK_USE_CCS_SNTP_CLIENT) NTPInit(); #endif #if defined(STACK_USE_CCS_GRATUITOUS_ARP) GratArpInit(); #endif #if defined(STACK_USE_CCS_HTTP1_SERVER) || defined(STACK_USE_CCS_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_CCS_TFTP_SERVER) TFTPSInit(); #endif }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { static bool once = false; smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = true; #endif #if defined (WF_CS_TRIS) && defined (STACK_USE_DHCP_CLIENT) g_DhcpRenew = false; g_DhcpRetryTimer = 0; #endif if (!once) { // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); once = true; } MACInit(); #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__XC8) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_ANNOUNCE) AnnounceInit(); #endif #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif #if defined(STACK_USE_NBNS) NBNSInit(); #endif }