TianoCore EDK2 master
Loading...
Searching...
No Matches
Rng.c File Reference

Go to the source code of this file.

Macros

#define RISCV_CPU_FEATURE_ZKR_BITMASK   0x8
 
#define SEED_RETRY_LOOPS   100
 
#define STATE_SIZE   312
 
#define MIDDLE   156
 
#define INIT_SHIFT   62
 
#define TWIST_MASK   0xb5026f5aa96619e9ULL
 
#define INIT_FACT   6364136223846793005ULL
 
#define SHIFT1   29
 
#define MASK1   0x5555555555555555ULL
 
#define SHIFT2   17
 
#define MASK2   0x71d67fffeda60000ULL
 
#define SHIFT3   37
 
#define MASK3   0xfff7eee000000000ULL
 
#define SHIFT4   43
 
#define LOWER_MASK   0x7fffffff
 
#define UPPER_MASK   (~(UINT64)LOWER_MASK)
 

Functions

STATIC VOID SeedRng (IN UINT64 S)
 
STATIC VOID TwistRng (VOID)
 
UINT64 ReadSeed (VOID)
 
STATIC BOOLEAN Get64BitSeed (OUT UINT64 *Out)
 
EFI_STATUS EFIAPI BaseRngLibConstructor (VOID)
 
BOOLEAN EFIAPI ArchGetRandomNumber16 (OUT UINT16 *Rand)
 
BOOLEAN EFIAPI ArchGetRandomNumber32 (OUT UINT32 *Rand)
 
BOOLEAN EFIAPI ArchGetRandomNumber64 (OUT UINT64 *Rand)
 
BOOLEAN EFIAPI ArchIsRngSupported (VOID)
 

Detailed Description

Random number generator service that uses the SEED instruction to provide pseudorandom numbers.

Copyright (c) 2024, Rivos, Inc.

SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file Rng.c.

Macro Definition Documentation

◆ INIT_FACT

#define INIT_FACT   6364136223846793005ULL

Definition at line 30 of file Rng.c.

◆ INIT_SHIFT

#define INIT_SHIFT   62

Definition at line 28 of file Rng.c.

◆ LOWER_MASK

#define LOWER_MASK   0x7fffffff

Definition at line 39 of file Rng.c.

◆ MASK1

#define MASK1   0x5555555555555555ULL

Definition at line 32 of file Rng.c.

◆ MASK2

#define MASK2   0x71d67fffeda60000ULL

Definition at line 34 of file Rng.c.

◆ MASK3

#define MASK3   0xfff7eee000000000ULL

Definition at line 36 of file Rng.c.

◆ MIDDLE

#define MIDDLE   156

Definition at line 27 of file Rng.c.

◆ RISCV_CPU_FEATURE_ZKR_BITMASK

#define RISCV_CPU_FEATURE_ZKR_BITMASK   0x8

Definition at line 17 of file Rng.c.

◆ SEED_RETRY_LOOPS

#define SEED_RETRY_LOOPS   100

Definition at line 19 of file Rng.c.

◆ SHIFT1

#define SHIFT1   29

Definition at line 31 of file Rng.c.

◆ SHIFT2

#define SHIFT2   17

Definition at line 33 of file Rng.c.

◆ SHIFT3

#define SHIFT3   37

Definition at line 35 of file Rng.c.

◆ SHIFT4

#define SHIFT4   43

Definition at line 37 of file Rng.c.

◆ STATE_SIZE

#define STATE_SIZE   312

Definition at line 26 of file Rng.c.

◆ TWIST_MASK

#define TWIST_MASK   0xb5026f5aa96619e9ULL

Definition at line 29 of file Rng.c.

◆ UPPER_MASK

#define UPPER_MASK   (~(UINT64)LOWER_MASK)

Definition at line 40 of file Rng.c.

Function Documentation

◆ ArchGetRandomNumber16()

BOOLEAN EFIAPI ArchGetRandomNumber16 ( OUT UINT16 *  Rand)

Generates a 16-bit random number.

Parameters
[out]RandBuffer pointer to store the 16-bit random value.
Return values
TRUERandom number generated successfully.
FALSEFailed to generate the random number.

Definition at line 187 of file Rng.c.

◆ ArchGetRandomNumber32()

BOOLEAN EFIAPI ArchGetRandomNumber32 ( OUT UINT32 *  Rand)

Generates a 32-bit random number.

Parameters
[out]RandBuffer pointer to store the 32-bit random value.
Return values
TRUERandom number generated successfully.
FALSEFailed to generate the random number.

Definition at line 212 of file Rng.c.

◆ ArchGetRandomNumber64()

BOOLEAN EFIAPI ArchGetRandomNumber64 ( OUT UINT64 *  Rand)

Generates a 64-bit random number.

Parameters
[out]RandBuffer pointer to store the 64-bit random value.
Return values
TRUERandom number generated successfully.
FALSEFailed to generate the random number.

Definition at line 237 of file Rng.c.

◆ ArchIsRngSupported()

BOOLEAN EFIAPI ArchIsRngSupported ( VOID  )

Checks whether SEED is supported.

Return values
TRUESEED is supported.

Definition at line 272 of file Rng.c.

◆ BaseRngLibConstructor()

EFI_STATUS EFIAPI BaseRngLibConstructor ( VOID  )

Constructor library which initializes Seeds and mStatus array.

Return values
EFI_SUCCESSIntialization was successful.
EFI_UNSUPPORTEDFeature not supported.

Definition at line 162 of file Rng.c.

◆ Get64BitSeed()

STATIC BOOLEAN Get64BitSeed ( OUT UINT64 *  Out)

Gets seed value by executing trng instruction (CSR 0x15) amd returns the see to the caller 64bit value.

Parameters
[out]OutBuffer pointer to store the 64-bit random value.
Return values
TRUERandom number generated successfully.
FALSEFailed to generate the random number.

Definition at line 107 of file Rng.c.

◆ SeedRng()

STATIC VOID SeedRng ( IN UINT64  S)

Initialize mState to defualt state.

Parameters
[in]SInput seed value

Definition at line 52 of file Rng.c.

◆ TwistRng()

STATIC VOID TwistRng ( VOID  )

Initializes mState with entropy values. The initialization is based on the Seed value populated in mState[0] which then influences all the other values in the mState array. Later values are retrieved from the same array instead of calling trng instruction every time.

Definition at line 75 of file Rng.c.