//============================================================================= // drt/sys/slfsr.hpp // Pseudo-random linear feedback shift register (LFSR) number generating // functions. // // History // 0.01, 1998-08-22, David R Tribble. // First cut. // // 0.02, 1998-08-23, David R Tribble. // Added protected members to class DrtLFSR. // // 0.03, 1998-09-25, David R Tribble. // Combined class DrtLFSR with the local class DrtLFSR96 and renamed it to // class DrtLFSR96. // Renamed class DrtLFSRKey to DrtLFSRKey96. // // 1.00, 1999-03-09, David R Tribble. // Moved from drt/scm/ to drt/sys/. // // Copyright ©1998-1999, by David R. Tribble, all rights reserved. // See "drt/sys/copyr.txt" for more information. //----------------------------------------------------------------------------- #ifndef drt_sys_slfsr_hpp #define drt_sys_slfsr_hpp 100 // Identification #ifndef NO_H_IDENT static const char drt_sys_slfsr_hpp_id[] = "@(#)drt/sys/slfsr.hpp 1.00"; #endif // Special includes #ifndef drt_sys_sdefs_hpp #include "sdefs.hpp" #endif // Local includes #ifndef drt_sys_sdebug_hpp #include "sdebug.hpp" #endif // Local wrappers #include "slib1.hpp" drt_namespace_begin // Forward declarations class DrtChar; //#include "schar.hpp" //----------------------------------------------------------------------------- // Class DrtLFSRKey96 // Digital key for a 96-bit linear feedback shift register (LFSR). // // Notes // Implemented as a 96-bit array of characters. // // History // 100, 1998-08-25, David R Tribble. // First cut. // // 200, 1998-09-25, David R Tribble. // Renamed this class from DrtLFSRKey to DrtLFSRKey96. //----------------------------------------------------------------------------- #define DrtLFSRKey96_VS 200 // Class version class DRTEXPORT DrtLFSRKey96 { public: // Shared constants static const int VS; // Class version static const unsigned int MAGIC; // Class magic number public: // Shared variables static DrtTraceGroup s_grp; // Class debugging group public: // Functions /*void*/ ~DrtLFSRKey96(); // Destructor /*void*/ DrtLFSRKey96(); // Default constructor void validate() const; // Validate this object virtual void dump() const; // Dump object to debugs void set(const char *s, int len); // Initialize key value void set(const unsigned char k[96/8]); // Initialize key value void get(unsigned char k[96/8]) const; // Retrieve key value protected: // Variables unsigned int m_magic; // Class magic number int m_vers; // Class version number unsigned char m_key[96/8]; // Key value private: // Functions // Constructors and destructors not provided /*void*/ DrtLFSRKey96(const DrtLFSRKey96 &r); // Copy constructor const DrtLFSRKey96 & operator =(const DrtLFSRKey96 &r); // Assignment operator }; //----------------------------------------------------------------------------- // Class DrtLFSR96 // 96-bit linear feedback shift register (LFSR), which is capable of // generating pseudo-random numbers. // // Notes // Implemented as a 96-bit LFSR in a modified Galois configuration. // By itself, this is probably not very cryptographically secure. // // History // 100, 1998-08-22, David R Tribble. // First cut. // // 101, 1998-08-23, David R Tribble. // Added protected members to class DrtLFSR. // Added class DrtLFSRKey. // // 200, 1998-09-25, David R Tribble. // Combined this class DrtLFSR with the local class DrtLFSR96 and renamed // it to class DrtLFSR96. //----------------------------------------------------------------------------- #define DrtLFSR96_VS 200 // Class version class DRTEXPORT DrtLFSR96 { public: // Shared constants static const int VS; // Class version static const unsigned int MAGIC; // Class magic number public: // Shared variables static DrtTraceGroup s_grp; // Class debugging group public: // Shared functions static DrtLFSR96 & self(); // Singleton object public: // Functions /*void*/ ~DrtLFSR96(); // Destructor /*void*/ DrtLFSR96(); // Default constructor void validate() const; // Validate this object virtual void dump() const; // Dump object to debugs int genrand8(); // Generate an 8-bit number unsigned int genrand16(); // Generate a 16-bit number unsigned long genrand32(); // Generate a 32-bit number unsigned long genrand(int n); // Generate an n-bit value bool crc32(const void *blk, int len); // Generate a CRC-32 for block bool crc96(const void *blk, int len); // Generate a CRC-96 for block void setKey(const DrtLFSRKey96 &key); // Initialize state vector void getKey(DrtLFSRKey96 *key) const; // Retrieve current state void setState(const unsigned char st[96/8]); // Initialize state vector void getState(unsigned char st[96/8]) const; // Retrieve state vector unsigned long hash(const unsigned char *b, int len); // Hash a block of chars unsigned long hash(const DrtChar *b, int len); // Hash a block of chars protected: // Variables unsigned int m_magic; // Class magic number int m_vers; // Class version number drt_uint32_t m_lfsr[96/32]; // LFSR bits protected: // Functions void setState(const drt_uint32_t st[96/32]); // Initialize state vector void getState(drt_uint32_t st[96/32]) const; // Retrieve current state private: // Shared constants static const drt_uint32_t MASK2; // LFSR bitmasks (96 bits) static const drt_uint32_t MASK1; static const drt_uint32_t MASK0; static const drt_uint32_t SMASK[0x100][96/32]; // Optimized 8-bit lookup table static const unsigned char SRESULT[0x100]; // Optimized 8-bit result table private: // Functions // Constructors and destructors not provided /*void*/ DrtLFSR96(const DrtLFSR96 &r); // Copy constructor const DrtLFSR96 & operator =(const DrtLFSR96 &r); // Assignment operator }; #include "slib2.hpp" drt_namespace_end #endif // drt_sys_slfsr_hpp // End slfsr.hpp