17#define RISCV_CPU_FEATURE_ZKR_BITMASK 0x8
19#define SEED_RETRY_LOOPS 100
29#define TWIST_MASK 0xb5026f5aa96619e9ULL
30#define INIT_FACT 6364136223846793005ULL
32#define MASK1 0x5555555555555555ULL
34#define MASK2 0x71d67fffeda60000ULL
36#define MASK3 0xfff7eee000000000ULL
39#define LOWER_MASK 0x7fffffff
40#define UPPER_MASK (~(UINT64)LOWER_MASK)
42static UINT64 mState[STATE_SIZE];
43static UINTN mIndex = STATE_SIZE + 1;
61 for (I = 1; I < STATE_SIZE; I++) {
62 mState[I] = (INIT_FACT * (mState[I - 1] ^ (mState[I - 1] >> INIT_SHIFT))) + I;
82 for (I = 0; I < STATE_SIZE; I++) {
83 X = (mState[I] & UPPER_MASK) | (mState[(I + 1) % STATE_SIZE] & LOWER_MASK);
84 X = (X >> 1) ^ (X & 1 ? TWIST_MASK : 0);
85 mState[I] = mState[(I + MIDDLE) % STATE_SIZE] ^ X;
117 Retry = SEED_RETRY_LOOPS;
118 Entropy = (UINT16 *)Out;
119 NeededSeeds =
sizeof (UINT64) /
sizeof (UINT16);
123 DEBUG ((DEBUG_ERROR,
"Get64BitSeed: HW not supported!\n"));
130 switch (Seed & SEED_OPST_MASK) {
132 Entropy[ValidSeeds++] = Seed & SEED_ENTROPY_MASK;
133 if (ValidSeeds == NeededSeeds) {
140 DEBUG ((DEBUG_ERROR,
"Get64BitSeed: Unrecoverable error!\n"));
172 return EFI_UNSUPPORTED;
194 *
Rand = Rand64 & MAX_UINT16;
219 *
Rand = Rand64 & MAX_UINT32;
244 if (mIndex > STATE_SIZE) {
249 if (mIndex == STATE_SIZE) {
254 Y ^= (Y >> SHIFT1) & MASK1;
255 Y ^= (Y << SHIFT2) & MASK2;
256 Y ^= (Y << SHIFT3) & MASK3;
276 return ((
PcdGet64 (PcdRiscVFeatureOverride) & RISCV_CPU_FEATURE_ZKR_BITMASK) != 0);
#define DEBUG(Expression)
#define PcdGet64(TokenName)
BOOLEAN EFIAPI ArchGetRandomNumber16(OUT UINT16 *Rand)
EFI_STATUS EFIAPI BaseRngLibConstructor(VOID)
BOOLEAN EFIAPI ArchGetRandomNumber64(OUT UINT64 *Rand)
BOOLEAN EFIAPI ArchGetRandomNumber32(OUT UINT32 *Rand)
STATIC BOOLEAN Get64BitSeed(OUT UINT64 *Out)
BOOLEAN EFIAPI ArchIsRngSupported(VOID)
STATIC VOID TwistRng(VOID)
STATIC VOID SeedRng(IN UINT64 S)