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

simulator: add support for GetRNG #4247

Merged
merged 1 commit into from Apr 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/machine/machine_generic.go
Expand Up @@ -2,6 +2,10 @@

package machine

import (
"crypto/rand"
)

// Dummy machine package that calls out to external functions.

const deviceName = "generic"
Expand Down Expand Up @@ -243,3 +247,13 @@ var (
sercomSPIM6 = SPI{6}
sercomSPIM7 = SPI{7}
)

// GetRNG returns 32 bits of random data from the WASI random source.
func GetRNG() (uint32, error) {
var buf [4]byte
_, err := rand.Read(buf[:])
if err != nil {
return 0, err
}
return uint32(buf[0])<<0 | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24, nil
}