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