mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 21:43:40 +00:00
stm32wl: STM32WL TRNG implementation in crypto/rand
This commit is contained in:
committed by
Ron Evans
parent
3e6410f323
commit
b4503c1e37
@@ -0,0 +1,36 @@
|
||||
//go:build stm32wle5
|
||||
// +build stm32wle5
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user