blumshub.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // blumshub.h - originally written and placed in the public domain by Wei Dai
  2. /// \file blumshub.h
  3. /// \brief Classes for Blum Blum Shub generator
  4. #ifndef CRYPTOPP_BLUMSHUB_H
  5. #define CRYPTOPP_BLUMSHUB_H
  6. #include "cryptlib.h"
  7. #include "modarith.h"
  8. #include "integer.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. /// \brief BlumBlumShub without factorization of the modulus
  11. /// \details You should reseed the generator after a fork() to avoid multiple generators
  12. /// with the same internal state.
  13. class PublicBlumBlumShub : public RandomNumberGenerator,
  14. public StreamTransformation
  15. {
  16. public:
  17. virtual ~PublicBlumBlumShub() {}
  18. /// \brief Construct a PublicBlumBlumShub
  19. /// \param n the modulus
  20. /// \param seed the seed for the generator
  21. /// \details seed is the secret key and should be about as large as n.
  22. PublicBlumBlumShub(const Integer &n, const Integer &seed);
  23. unsigned int GenerateBit();
  24. byte GenerateByte();
  25. void GenerateBlock(byte *output, size_t size);
  26. void ProcessData(byte *outString, const byte *inString, size_t length);
  27. bool IsSelfInverting() const {return true;}
  28. bool IsForwardTransformation() const {return true;}
  29. protected:
  30. ModularArithmetic modn;
  31. Integer current;
  32. word maxBits, bitsLeft;
  33. };
  34. /// \brief BlumBlumShub with factorization of the modulus
  35. /// \details You should reseed the generator after a fork() to avoid multiple generators
  36. /// with the same internal state.
  37. class BlumBlumShub : public PublicBlumBlumShub
  38. {
  39. public:
  40. virtual ~BlumBlumShub() {}
  41. /// \brief Construct a BlumBlumShub
  42. /// \param p the first prime factor
  43. /// \param q the second prime factor
  44. /// \param seed the seed for the generator
  45. /// \details Esure p and q are both primes congruent to 3 mod 4 and at least 512 bits long.
  46. /// seed is the secret key and should be about as large as p*q.
  47. BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed);
  48. bool IsRandomAccess() const {return true;}
  49. void Seek(lword index);
  50. protected:
  51. const Integer p, q;
  52. const Integer x0;
  53. };
  54. NAMESPACE_END
  55. #endif