Add pico_rand library (#1111)
Add a general purpose random number generator via pico_rand library. which tries to use as much real entropy as possible mixed into a PRNG Co-authored-by: graham sanderson <graham.sanderson@raspberrypi.com>
This commit is contained in:
@ -46,7 +46,6 @@ if (EXISTS ${PICO_LWIP_PATH}/${LWIP_TEST_PATH})
|
||||
${PICO_LWIP_PATH}/src/core/tcp_out.c
|
||||
${PICO_LWIP_PATH}/src/core/timeouts.c
|
||||
${PICO_LWIP_PATH}/src/core/udp.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/lwip_random.c
|
||||
)
|
||||
target_include_directories(pico_lwip_core INTERFACE
|
||||
${PICO_LWIP_PATH}/src/include)
|
||||
@ -274,7 +273,8 @@ if (EXISTS ${PICO_LWIP_PATH}/${LWIP_TEST_PATH})
|
||||
target_link_libraries(pico_lwip_nosys INTERFACE
|
||||
pico_async_context_base
|
||||
pico_lwip_arch
|
||||
pico_lwip)
|
||||
pico_lwip
|
||||
pico_rand)
|
||||
|
||||
if (NOT PICO_LWIP_CONTRIB_PATH)
|
||||
set(PICO_LWIP_CONTRIB_PATH ${PICO_LWIP_PATH}/contrib)
|
||||
|
@ -86,14 +86,8 @@ void pico_lwip_custom_unlock_tcpip_core(void);
|
||||
#endif
|
||||
|
||||
#ifndef LWIP_RAND
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
unsigned int pico_lwip_rand(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
// Use ROSC based random number generation, more for the fact that rand() may not be seeded, than anything else
|
||||
#define LWIP_RAND pico_lwip_rand
|
||||
#include "pico/rand.h"
|
||||
// Use the pico_rand library which goes to reasonable lengths to try to provide good entropy
|
||||
#define LWIP_RAND() get_rand_32()
|
||||
#endif
|
||||
#endif /* __CC_H__ */
|
||||
|
@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "pico.h"
|
||||
#include "hardware/structs/rosc.h"
|
||||
|
||||
static uint8_t pico_lwip_random_byte(int cycles) {
|
||||
static uint8_t byte;
|
||||
assert(cycles >= 8);
|
||||
assert(rosc_hw->status & ROSC_STATUS_ENABLED_BITS);
|
||||
for(int i=0;i<cycles;i++) {
|
||||
// picked a fairly arbitrary polynomial of 0x35u - this doesn't have to be crazily uniform.
|
||||
byte = ((byte << 1) | rosc_hw->randombit) ^ (byte & 0x80u ? 0x35u : 0);
|
||||
// delay a little because the random bit is a little slow
|
||||
busy_wait_at_least_cycles(30);
|
||||
}
|
||||
return byte;
|
||||
}
|
||||
|
||||
unsigned int pico_lwip_rand(void) {
|
||||
uint32_t value = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
value = (value << 8u) | pico_lwip_random_byte(32);
|
||||
}
|
||||
return value;
|
||||
}
|
Reference in New Issue
Block a user