github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/sifive/fu540/rng.go (about)

     1  // SiFive FU540 RNG initialization
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright (c) WithSecure Corporation
     5  // https://foundry.withsecure.com
     6  //
     7  // Use of this source code is governed by the license
     8  // that can be found in the LICENSE file.
     9  
    10  package fu540
    11  
    12  import (
    13  	"encoding/binary"
    14  	"time"
    15  	_ "unsafe"
    16  
    17  	"github.com/usbarmory/tamago/internal/rng"
    18  )
    19  
    20  //go:linkname initRNG runtime.initRNG
    21  func initRNG() {
    22  	drbg := &rng.DRBG{}
    23  	binary.LittleEndian.PutUint64(drbg.Seed[:], uint64(time.Now().UnixNano()))
    24  	rng.GetRandomDataFn = drbg.GetRandomData
    25  
    26  }
    27  
    28  // SetRNG allows to override the internal random number generator function used
    29  // by TamaGo on the FU540 SoC.
    30  //
    31  // At runtime initialization the fu540 package selects a timer seeded with the
    32  // CPU timer as the FU540 lacks an entropy source. This is unsuitable for
    33  // secure random number generation and must therefore be overridden to ensure
    34  // safe operation of Go `crypto/rand`.
    35  func SetRNG(getRandomData func([]byte)) {
    36  	rng.GetRandomDataFn = getRandomData
    37  }