darn.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // darn.h - written and placed in public domain by Jeffrey Walton
  2. // DARN requires POWER9/ISA 3.0.
  3. // At the moment only GCC 7.0 (and above) seems to support __builtin_darn()
  4. // and __builtin_darn_32(). However, GCC generates incorrect code. Clang 7.0
  5. // does not provide them, but it does support assembly instructions. XLC is
  6. // unknown, but there are no hits when searching IBM's site. To cover more
  7. // platforms we provide GCC inline assembly like we do with RDRAND and RDSEED.
  8. // Platforms that don't support GCC inline assembly or the builtin will fail
  9. // to compile. Also see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91481 and
  10. // https://gcc.gnu.org/onlinedocs/gcc/Basic-PowerPC-Built-in-Functions-Available-on-ISA-3_002e0.html
  11. /// \file darn.h
  12. /// \brief Classes for DARN RNG
  13. /// \sa <A HREF="https://openpowerfoundation.org/?resource_lib=power-isa-version-3-0">Power
  14. /// ISA Version 3.0B</A>
  15. /// \since Crypto++ 8.0
  16. #ifndef CRYPTOPP_DARN_H
  17. #define CRYPTOPP_DARN_H
  18. #include "cryptlib.h"
  19. NAMESPACE_BEGIN(CryptoPP)
  20. /// \brief Exception thrown when a DARN generator encounters
  21. /// a generator related error.
  22. /// \since Crypto++ 8.0
  23. class DARN_Err : public Exception
  24. {
  25. public:
  26. DARN_Err(const std::string &operation)
  27. : Exception(OTHER_ERROR, "DARN: " + operation + " operation failed") {}
  28. };
  29. /// \brief Hardware generated random numbers using DARN instruction
  30. /// \details DARN() provides access to Power9's random number generator. The
  31. /// Crypto++ implementation provides conditioned random numbers from the
  32. /// generator as opposed to raw random numbers. According to Power ISA 3.0B
  33. /// manual, a conditioned random number has been processed by hardware to
  34. /// reduce bias. A raw random number is unconditioned noise source output.
  35. /// \details According to Power ISA 3.0B manual, the random number generator
  36. /// provided by the <tt>darn</tt> instruction is NIST SP800-90B and SP800-90C
  37. /// compliant to the extent possible given the completeness of the standards
  38. /// at the time the hardware is designed. The random number generator provides
  39. /// a minimum of 0.5 bits of entropy per bit.
  40. /// \par Wraps
  41. /// darn instruction
  42. /// \sa <A HREF="https://openpowerfoundation.org/?resource_lib=power-isa-version-3-0">Power
  43. /// ISA Version 3.0B</A>, MaurerRandomnessTest() for random bit generators
  44. /// \since Crypto++ 8.0
  45. class DARN : public RandomNumberGenerator
  46. {
  47. public:
  48. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() { return "DARN"; }
  49. virtual ~DARN() {}
  50. /// \brief Construct a DARN generator
  51. /// \throw DARN_Err if the random number generator is not available
  52. DARN();
  53. /// \brief Generate random array of bytes
  54. /// \param output the byte buffer
  55. /// \param size the length of the buffer, in bytes
  56. virtual void GenerateBlock(byte *output, size_t size);
  57. /// \brief Generate and discard n bytes
  58. /// \param n the number of bytes to generate and discard
  59. /// \details the RDSEED generator discards words, not bytes. If n is
  60. /// not a multiple of a machine word, then it is rounded up to
  61. /// that size.
  62. virtual void DiscardBytes(size_t n);
  63. /// \brief Update RNG state with additional unpredictable values
  64. /// \param input unused
  65. /// \param length unused
  66. /// \details The operation is a nop for this generator.
  67. virtual void IncorporateEntropy(const byte *input, size_t length)
  68. {
  69. // Override to avoid the base class' throw.
  70. CRYPTOPP_UNUSED(input); CRYPTOPP_UNUSED(length);
  71. }
  72. std::string AlgorithmProvider() const {
  73. return "Power9";
  74. }
  75. private:
  76. SecBlock<byte, AllocatorWithCleanup<byte, true> > m_temp;
  77. };
  78. NAMESPACE_END
  79. #endif // CRYPTOPP_DARN_H