mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 20:43:40 +00:00
stm32wl: STM32WL TRNG implementation in crypto/rand
This commit is contained in:
committed by
Ron Evans
parent
3e6410f323
commit
b4503c1e37
@@ -3,6 +3,7 @@ package machine
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrTimeoutRNG = errors.New("machine: RNG Timeout")
|
||||
ErrInvalidInputPin = errors.New("machine: invalid input pin")
|
||||
ErrInvalidOutputPin = errors.New("machine: invalid output pin")
|
||||
ErrInvalidClockPin = errors.New("machine: invalid clock pin")
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var rngInitDone = false
|
||||
|
||||
const (
|
||||
AF0_SYSTEM = 0
|
||||
AF1_TIM1_2_LPTIM1 = 1
|
||||
@@ -30,9 +32,10 @@ const (
|
||||
)
|
||||
|
||||
const (
|
||||
SYSCLK = 48e6
|
||||
APB1_TIM_FREQ = SYSCLK
|
||||
APB2_TIM_FREQ = SYSCLK
|
||||
SYSCLK = 48e6
|
||||
APB1_TIM_FREQ = SYSCLK
|
||||
APB2_TIM_FREQ = SYSCLK
|
||||
RNG_MAX_READ_RETRIES = 1000
|
||||
)
|
||||
|
||||
func CPUFrequency() uint32 {
|
||||
@@ -396,6 +399,27 @@ func (t *TIM) enableMainOutput() {
|
||||
t.Device.BDTR.SetBits(stm32.TIM_BDTR_MOE)
|
||||
}
|
||||
|
||||
// GetRNG returns 32 bits of cryptographically secure random data
|
||||
func GetRNG() (uint32, error) {
|
||||
|
||||
if !rngInitDone {
|
||||
stm32.RCC.AHB3ENR.SetBits(stm32.RCC_AHB3ENR_RNGEN)
|
||||
stm32.RNG.CR.SetBits(stm32.RNG_CR_RNGEN)
|
||||
rngInitDone = true
|
||||
}
|
||||
cnt := RNG_MAX_READ_RETRIES
|
||||
for cnt > 0 && !stm32.RNG.SR.HasBits(stm32.RNG_SR_DRDY) {
|
||||
cnt--
|
||||
}
|
||||
if cnt == 0 {
|
||||
return 0, ErrTimeoutRNG
|
||||
}
|
||||
ret := stm32.RNG.DR.Get()
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
//----------
|
||||
|
||||
type arrtype = uint32
|
||||
type arrRegType = volatile.Register32
|
||||
|
||||
|
||||
Reference in New Issue
Block a user