-
Notifications
You must be signed in to change notification settings - Fork 929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for esp32c3 on-chip random generator to crypto/rand package. #2123
Draft
zdima
wants to merge
5
commits into
tinygo-org:dev
Choose a base branch
from
zdima:esp32c3-rnd
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa56f53
add support for esp32c3 on-chip random generator to crypto/rand package.
zdima b4bbf07
enable High-speed ADC
zdima b5c43e4
added helper functions. need to move to appropriate place.
zdima 5de1e20
use recommended logic to improve entropy of random number by looking …
zdima c23068c
keep minimalCPUTicks with the reader to fetch this once per Read call.
zdima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// +build esp32c3 | ||
|
||
// This implementation of crypto/rand uses on-chip random generator | ||
// to generate random numbers. | ||
// | ||
|
||
package rand | ||
|
||
import ( | ||
"device/esp" | ||
"device/riscv" | ||
"machine" | ||
"unsafe" | ||
) | ||
|
||
const MHZ = 1000000 | ||
|
||
func init() { | ||
Reader = &esp32c3RndReader{} | ||
|
||
// When using the random number generator, make sure at least either the SAR ADC, | ||
// high-speed ADC1, or RTC20M_CLK2 is enabled. Otherwise, pseudo-random numbers will be returned. | ||
// • SAR ADC can be enabled by using the DIG ADC controller. For details, | ||
// please refer to Chapter 6 On-Chip Sensors and Analog Signal Processing [to be added later]. | ||
// • High-speed ADC is enabled automatically when the Wi-Fi or Bluetooth modules | ||
// is enabled. | ||
// • RTC20M_CLK is enabled by setting the RTC_CNTL_DIG_CLK20M_EN bit in | ||
// the RTC_CNTL_CLK_CONF_REG register. | ||
// Note: | ||
// 1. Note that, when the Wi-Fi module is enabled, the value read from the high-speed | ||
// ADC can be saturated in some extreme cases, which lowers the entropy. Thus, it | ||
// is advisable to also enable the SAR ADC as the noise source for the random | ||
// number generator for such cases. | ||
// 2. Enabling RTC20M_CLK increases the RNG entropy. However, to ensure maximum entropy, | ||
// it’s recommended to always enable an ADC source as well. | ||
|
||
// Enable SAR ADC | ||
esp.SYSTEM.PERIP_CLK_EN0.SetBits(esp.SYSTEM_PERIP_CLK_EN0_APB_SARADC_CLK_EN) | ||
|
||
// High-speed ADC | ||
esp.SYSTEM.PERIP_CLK_EN0.SetBits(esp.SYSTEM_PERIP_CLK_EN0_ADC2_ARB_CLK_EN) | ||
|
||
// Enable RTC20M_CLK2 | ||
// Unfortunately, the technical reference document from where the above note is taken | ||
// has no information on RTC_CNTL_DIG_CLK20M_EN, nither the SVD have such information. | ||
/* | ||
|
||
https://github.com/espressif/esp-idf/blob/5f38b766a83d18f78167d1d0dd8c8427ea1a36cb/components/hal/esp32c3/include/hal/i2c_ll.h#L823 | ||
|
||
// rtc_clk needs to switch on. | ||
if (src_clk == I2C_SCLK_RTC) { | ||
SET_PERI_REG_MASK(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_DIG_CLK8M_EN_M); | ||
esp_rom_delay_us(DELAY_RTC_CLK_SWITCH); | ||
} | ||
|
||
esp.RTC_CLK_CONF | ||
|
||
|
||
*/ | ||
// esp.RTC_CNTL.RTC_CLK_CONF.SetBits( ??? ) | ||
|
||
} | ||
|
||
type esp32c3RndReader struct { | ||
lastCpuTick uint32 | ||
minimalCPUTicks uint32 | ||
} | ||
|
||
func getApbFreqHZ() uint32 { | ||
v := esp.RTC_CNTL.RTC_STORE5.Get() & 0xffff | ||
v = v << 12 | ||
v += MHZ / 2 | ||
r := v % MHZ | ||
return v - r | ||
} | ||
|
||
const CSR_CPU_COUNTER riscv.CSR = 0x7e2 | ||
|
||
func getCpuTickCount() uint32 { | ||
count := CSR_CPU_COUNTER.Get() | ||
return uint32(count) | ||
} | ||
|
||
func (r *esp32c3RndReader) hw_rand() uint32 { | ||
currentCPUTick := getCpuTickCount() | ||
result := esp.APB_CTRL.RND_DATA.Get() | ||
for (currentCPUTick - r.lastCpuTick) < r.minimalCPUTicks { | ||
currentCPUTick = getCpuTickCount() | ||
result ^= esp.APB_CTRL.RND_DATA.Get() | ||
} | ||
r.lastCpuTick = currentCPUTick | ||
return result ^ esp.APB_CTRL.RND_DATA.Get() | ||
} | ||
|
||
func (r *esp32c3RndReader) Read(b []byte) (n int, err error) { | ||
if len(b) != 0 { | ||
// update minimalCPUTicks in case the APB frequency has changed | ||
r.minimalCPUTicks = 16 * (machine.CPUFrequency() / getApbFreqHZ()) | ||
for i := 0; i < len(b); { | ||
nextRandom := r.hw_rand() | ||
byteArray := (*[4]byte)(unsafe.Pointer(&nextRandom))[:] | ||
for k := 0; k < 4 && i < len(b); { | ||
b[i] = byteArray[k] | ||
k++ | ||
i++ | ||
} | ||
} | ||
} | ||
return len(b), nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that enabling random number generation is a bit more complicated than this:
https://github.com/espressif/esp-idf/blob/v4.3/components/bootloader_support/src/bootloader_random_esp32c3.c
I'm worried that it is not truly random without this complete initialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will move it to draft until have move details on initialization and the #2325 with #2333 are move so I would have better access to register's fields.