/* Generate high-quality entropy source using a TRNG or through RDRAND. */
EFI_STATUS EFIAPI GenerateEntropy(IN UINTN Length, OUT UINT8 *Entropy)
/* Generate high-quality entropy source using a TRNG or through RDRAND. */
EFI_STATUS EFIAPI GenerateEntropy(IN UINTN Length, OUT UINT8 *Entropy)
{
  EFI_STATUS  Status;
  UINTN       CollectedEntropyBits;
  UINTN       RequiredEntropyBits;
  UINTN       EntropyBits;
  UINTN       Index;
  UINTN       MaxBits;
  ZeroMem (Entropy, Length);
  RequiredEntropyBits  = (Length << 3);
  Index                = 0;
  CollectedEntropyBits = 0;
  MaxBits              = GetArmTrngMaxSupportedEntropyBits ();
  while (CollectedEntropyBits < RequiredEntropyBits) {
    EntropyBits = MIN ((RequiredEntropyBits - CollectedEntropyBits), MaxBits);
    Status      = GetArmTrngEntropy (
                    EntropyBits,
                    (Length - Index),
                    &Entropy[Index]
                    );
    if (EFI_ERROR (Status)) {
      ZeroMem (Entropy, Length);
      return Status;
    }
    CollectedEntropyBits += EntropyBits;
    Index                += (EntropyBits >> 3);
  }
  return Status;
}