Skip to content
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

fix: correctly handle calls for GetRNG() when being made from nrf devices with SoftDevice enabled #4692

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/machine/machine_nrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ func (i2c *I2C) signalStop() error {

var rngStarted = false

// GetRNG returns 32 bits of non-deterministic random data based on internal thermal noise.
// getRNG returns 32 bits of non-deterministic random data based on internal thermal noise.
// According to Nordic's documentation, the random output is suitable for cryptographic purposes.
func GetRNG() (ret uint32, err error) {
func getRNG() (ret uint32, err error) {
// There's no apparent way to check the status of the RNG peripheral's task, so simply start it
// to avoid deadlocking while waiting for output.
if !rngStarted {
Expand Down
9 changes: 9 additions & 0 deletions src/machine/machine_nrf_bare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build nrf && !softdevice

package machine

// GetRNG returns 32 bits of non-deterministic random data based on internal thermal noise.
// According to Nordic's documentation, the random output is suitable for cryptographic purposes.
func GetRNG() (ret uint32, err error) {
return getRNG()
}
59 changes: 59 additions & 0 deletions src/machine/machine_nrf_sd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build nrf && softdevice

package machine

import (
"device/arm"
"device/nrf"

"errors"
)

// avoid a heap allocation in GetRNG.
var (
softdeviceEnabled uint8
bytesAvailable uint8
buf [4]uint8

errNoSoftDeviceSupport = errors.New("rng: softdevice not supported on this device")
errNotEnoughRandomData = errors.New("rng: not enough random data available")
)

// GetRNG returns 32 bits of non-deterministic random data based on internal thermal noise.
// According to Nordic's documentation, the random output is suitable for cryptographic purposes.
func GetRNG() (ret uint32, err error) {
// First check whether the SoftDevice is enabled.
// sd_rand_application_bytes_available_get cannot be called when the SoftDevice is not enabled.
arm.SVCall1(0x12, &softdeviceEnabled) // sd_softdevice_is_enabled

if softdeviceEnabled == 0 {
return getRNG()
}

// call into the SoftDevice to get random data bytes available
switch nrf.Device {
case "nrf51":
// sd_rand_application_bytes_available_get: SOC_SVC_BASE_NOT_AVAILABLE + 4
arm.SVCall1(0x2B+4, &bytesAvailable)
case "nrf52", "nrf52840", "nrf52833":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case when we have "nrf52840", "nrf52833" and not "nrf52"?
I mean this can probably be dropped: ", nrf52840", "nrf52833"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// sd_rand_application_bytes_available_get: SOC_SVC_BASE_NOT_AVAILABLE + 4
arm.SVCall1(0x2C+4, &bytesAvailable)
default:
return 0, errNoSoftDeviceSupport
}

if bytesAvailable < 4 {
return 0, errNotEnoughRandomData
}

switch nrf.Device {
case "nrf51":
// sd_rand_application_vector_get: SOC_SVC_BASE_NOT_AVAILABLE + 5
arm.SVCall2(0x2B+5, &buf, 4)
case "nrf52", "nrf52840", "nrf52833":
// sd_rand_application_vector_get: SOC_SVC_BASE_NOT_AVAILABLE + 5
arm.SVCall2(0x2C+5, &buf, 4)
}

return uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24, nil
}
Loading