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

     1  // NXP i.MX6UL 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 imx6ul
    11  
    12  import (
    13  	"encoding/binary"
    14  	"time"
    15  	_ "unsafe"
    16  
    17  	"github.com/usbarmory/tamago/dma"
    18  	"github.com/usbarmory/tamago/internal/rng"
    19  	"github.com/usbarmory/tamago/soc/nxp/caam"
    20  	"github.com/usbarmory/tamago/soc/nxp/rngb"
    21  )
    22  
    23  //go:linkname initRNG runtime.initRNG
    24  func initRNG() {
    25  	if !Native {
    26  		drbg := &rng.DRBG{}
    27  		binary.LittleEndian.PutUint64(drbg.Seed[:], uint64(time.Now().UnixNano()))
    28  		rng.GetRandomDataFn = drbg.GetRandomData
    29  		return
    30  	}
    31  
    32  	switch Model() {
    33  	case "i.MX6UL":
    34  		// Cryptographic Acceleration and Assurance Module
    35  		CAAM = &caam.CAAM{
    36  			Base:            CAAM_BASE,
    37  			CCGR:            CCM_CCGR0,
    38  			CG:              CCGRx_CG5,
    39  			DeriveKeyMemory: dma.Default(),
    40  		}
    41  		CAAM.Init()
    42  
    43  		// The CAAM TRNG is too slow for direct use, therefore
    44  		// we use it to seed an AES-CTR based DRBG.
    45  		drbg := &rng.DRBG{}
    46  		CAAM.GetRandomData(drbg.Seed[:])
    47  
    48  		rng.GetRandomDataFn = drbg.GetRandomData
    49  	case "i.MX6ULL", "i.MX6ULZ":
    50  		// True Random Number Generator
    51  		RNGB = &rngb.RNGB{
    52  			Base: RNGB_BASE,
    53  		}
    54  		RNGB.Init()
    55  
    56  		rng.GetRandomDataFn = RNGB.GetRandomData
    57  	}
    58  }