mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 13:33:39 +00:00
cab3834bc9
This implements machine.GetRNG() using VirtIO. This gets the tests to pass for crypto/md5 and crypto/sha1 that use crypto/rand in their tests.
39 lines
665 B
Go
39 lines
665 B
Go
//go:build nrf || (stm32 && !(stm32f103 || stm32l0x1)) || (sam && atsamd51) || (sam && atsame5x) || esp32c3 || tkey || (tinygo.riscv32 && virt)
|
|
|
|
// If you update the above build constraint, you'll probably also need to update
|
|
// src/runtime/rand_hwrng.go.
|
|
|
|
package rand
|
|
|
|
import (
|
|
"machine"
|
|
)
|
|
|
|
func init() {
|
|
Reader = &reader{}
|
|
}
|
|
|
|
type reader struct {
|
|
}
|
|
|
|
func (r *reader) Read(b []byte) (n int, err error) {
|
|
if len(b) == 0 {
|
|
return
|
|
}
|
|
|
|
var randomByte uint32
|
|
for i := range b {
|
|
if i%4 == 0 {
|
|
randomByte, err = machine.GetRNG()
|
|
if err != nil {
|
|
return n, err
|
|
}
|
|
} else {
|
|
randomByte >>= 8
|
|
}
|
|
b[i] = byte(randomByte)
|
|
}
|
|
|
|
return len(b), nil
|
|
}
|