machine/stm32f4, stm32f7, stm32l4: implement TRNG for randomness

Signed-off-by: deadprogram <ron@hybridgroup.com>
Этот коммит содержится в:
deadprogram 2021-12-14 18:17:33 +01:00 коммит произвёл Ron Evans
родитель 2ebaa9f520
коммит e1df2510d4
6 изменённых файлов: 54 добавлений и 2 удалений

Просмотреть файл

@ -1,5 +1,5 @@
//go:build stm32wlx || (sam && atsamd51) || (sam && atsame5x)
// +build stm32wlx sam,atsamd51 sam,atsame5x
//go:build stm32wlx || stm32f4 || stm32f7 || stm32l4 || (sam && atsamd51) || (sam && atsame5x)
// +build stm32wlx stm32f4 stm32f7 stm32l4 sam,atsamd51 sam,atsame5x
package rand

29
src/machine/machine_stm32_rng.go Обычный файл
Просмотреть файл

@ -0,0 +1,29 @@
//go:build stm32f4 || stm32f7 || stm32l4
// +build stm32f4 stm32f7 stm32l4
package machine
import "device/stm32"
var rngInitDone = false
const RNG_MAX_READ_RETRIES = 1000
// GetRNG returns 32 bits of cryptographically secure random data
func GetRNG() (uint32, error) {
if !rngInitDone {
initRNG()
rngInitDone = true
}
cnt := RNG_MAX_READ_RETRIES
for !stm32.RNG.SR.HasBits(stm32.RNG_SR_DRDY) {
cnt--
if cnt == 0 {
return 0, ErrTimeoutRNG
}
}
ret := stm32.RNG.DR.Get()
return ret, nil
}

Просмотреть файл

@ -1,3 +1,4 @@
//go:build stm32f4
// +build stm32f4
package machine
@ -586,3 +587,8 @@ const (
ARR_MAX = 0x10000
PSC_MAX = 0x10000
)
func initRNG() {
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_RNGEN)
stm32.RNG.CR.SetBits(stm32.RNG_CR_RNGEN)
}

Просмотреть файл

@ -1,3 +1,4 @@
//go:build stm32f7
// +build stm32f7
package machine
@ -716,3 +717,8 @@ const (
ARR_MAX = 0x10000
PSC_MAX = 0x10000
)
func initRNG() {
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_RNGEN)
stm32.RNG.CR.SetBits(stm32.RNG_CR_RNGEN)
}

Просмотреть файл

@ -1,3 +1,4 @@
//go:build stm32l0
// +build stm32l0
package machine

Просмотреть файл

@ -1,3 +1,4 @@
//go:build stm32l4
// +build stm32l4
package machine
@ -494,3 +495,12 @@ const (
ARR_MAX = 0x10000
PSC_MAX = 0x10000
)
func initRNG() {
stm32.RCC.CRRCR.SetBits(stm32.RCC_CRRCR_HSI48ON)
for !stm32.RCC.CRRCR.HasBits(stm32.RCC_CRRCR_HSI48RDY) {
}
stm32.RCC.AHB2ENR.SetBits(stm32.RCC_AHB2ENR_RNGEN)
stm32.RNG.CR.SetBits(stm32.RNG_CR_RNGEN)
}