Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:54

0001 // blumshub.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file blumshub.h

0004 /// \brief Classes for Blum Blum Shub generator

0005 
0006 #ifndef CRYPTOPP_BLUMSHUB_H
0007 #define CRYPTOPP_BLUMSHUB_H
0008 
0009 #include "cryptlib.h"
0010 #include "modarith.h"
0011 #include "integer.h"
0012 
0013 NAMESPACE_BEGIN(CryptoPP)
0014 
0015 /// \brief BlumBlumShub without factorization of the modulus

0016 /// \details You should reseed the generator after a fork() to avoid multiple generators

0017 ///  with the same internal state.

0018 class PublicBlumBlumShub : public RandomNumberGenerator,
0019                            public StreamTransformation
0020 {
0021 public:
0022     virtual ~PublicBlumBlumShub() {}
0023 
0024     /// \brief Construct a PublicBlumBlumShub

0025     /// \param n the modulus

0026     /// \param seed the seed for the generator

0027     /// \details seed is the secret key and should be about as large as n.

0028     PublicBlumBlumShub(const Integer &n, const Integer &seed);
0029 
0030     unsigned int GenerateBit();
0031     byte GenerateByte();
0032     void GenerateBlock(byte *output, size_t size);
0033     void ProcessData(byte *outString, const byte *inString, size_t length);
0034 
0035     bool IsSelfInverting() const {return true;}
0036     bool IsForwardTransformation() const {return true;}
0037 
0038 protected:
0039     ModularArithmetic modn;
0040     Integer current;
0041     word maxBits, bitsLeft;
0042 };
0043 
0044 /// \brief BlumBlumShub with factorization of the modulus

0045 /// \details You should reseed the generator after a fork() to avoid multiple generators

0046 ///  with the same internal state.

0047 class BlumBlumShub : public PublicBlumBlumShub
0048 {
0049 public:
0050     virtual ~BlumBlumShub() {}
0051 
0052     /// \brief Construct a BlumBlumShub

0053     /// \param p the first prime factor

0054     /// \param q the second prime factor

0055     /// \param seed the seed for the generator

0056     /// \details Esure p and q are both primes congruent to 3 mod 4 and at least 512 bits long.

0057     ///  seed is the secret key and should be about as large as p*q.

0058     BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed);
0059 
0060     bool IsRandomAccess() const {return true;}
0061     void Seek(lword index);
0062 
0063 protected:
0064     const Integer p, q;
0065     const Integer x0;
0066 };
0067 
0068 NAMESPACE_END
0069 
0070 #endif