00001 // blumshub.h - written and placed in the public domain by Wei Dai 00002 00003 //! \file 00004 //! \headerfile blumshub.h 00005 //! \brief Classes for Blum Blum Shub generator 00006 00007 #ifndef CRYPTOPP_BLUMSHUB_H 00008 #define CRYPTOPP_BLUMSHUB_H 00009 00010 #include "cryptlib.h" 00011 #include "modarith.h" 00012 #include "integer.h" 00013 00014 NAMESPACE_BEGIN(CryptoPP) 00015 00016 //! BlumBlumShub without factorization of the modulus 00017 class PublicBlumBlumShub : public RandomNumberGenerator, 00018 public StreamTransformation 00019 { 00020 public: 00021 PublicBlumBlumShub(const Integer &n, const Integer &seed); 00022 00023 unsigned int GenerateBit(); 00024 byte GenerateByte(); 00025 void GenerateBlock(byte *output, size_t size); 00026 void ProcessData(byte *outString, const byte *inString, size_t length); 00027 00028 bool IsSelfInverting() const {return true;} 00029 bool IsForwardTransformation() const {return true;} 00030 00031 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 00032 virtual ~PublicBlumBlumShub() {} 00033 #endif 00034 00035 protected: 00036 ModularArithmetic modn; 00037 Integer current; 00038 word maxBits, bitsLeft; 00039 }; 00040 00041 //! BlumBlumShub with factorization of the modulus 00042 class BlumBlumShub : public PublicBlumBlumShub 00043 { 00044 public: 00045 // Make sure p and q are both primes congruent to 3 mod 4 and at least 512 bits long, 00046 // seed is the secret key and should be about as big as p*q 00047 BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed); 00048 00049 bool IsRandomAccess() const {return true;} 00050 void Seek(lword index); 00051 00052 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 00053 virtual ~BlumBlumShub() {} 00054 #endif 00055 00056 protected: 00057 const Integer p, q; 00058 const Integer x0; 00059 }; 00060 00061 NAMESPACE_END 00062 00063 #endif