sapphire.h

00001 /* sapphire.h -- Interface for the Saphire II stream cipher.
00002 
00003    Dedicated to the Public Domain the author and inventor
00004    (Michael Paul Johnson).  This code comes with no warranty.
00005    Use it at your own risk.
00006    Ported from the Pascal implementation of the Sapphire Stream
00007    Cipher 9 December 1994.
00008    Added hash-specific functions 27 December 1994.
00009    Made index variable initialization key-dependent,
00010    made the output function more resistant to cryptanalysis,
00011    and renamed to Sapphire II Stream Cipher 2 January 1995.
00012 
00013    unsigned char is assumed to be 8 bits.  If it is not, the
00014    results of assignments need to be reduced to 8 bits with
00015    & 0xFF or % 0x100, whichever is faster.
00016 */  
00017   
00018 #ifndef NULL
00019 #define NULL 0
00020 #endif  /*  */
00021 
00022 #include <defs.h>
00023 
00024 SWORD_NAMESPACE_START
00025 
00026   class sapphire 
00027 {
00028   
00029     // These variables comprise the state of the state machine.
00030   unsigned char cards[256];     // A permutation of 0-255.
00031   unsigned char rotor,          // Index that rotates smoothly
00032     ratchet,                    // Index that moves erratically
00033     avalanche,                  // Index heavily data dependent
00034     last_plain,                 // Last plain text byte
00035     last_cipher;                // Last cipher text byte
00036   
00037     // This function is used by initialize(), which is called by the
00038     // constructor.
00039   unsigned char keyrand (int limit, unsigned char *user_key,
00040                           unsigned char keysize, unsigned char *rsum,
00041 unsigned *keypos); public:sapphire (unsigned char
00042                                       *key = NULL,      // Calls initialize if a real
00043                                       unsigned char keysize = 0);       // key is provided.  If none
00044   // is provided, call initialize
00045   // before encrypt or decrypt.
00046   ~sapphire ();                 // Destroy cipher state information.
00047   void initialize (unsigned char *key,  // User key is used to set
00048                    unsigned char keysize);      // up state information.
00049   void hash_init (void);        // Set up default hash.
00050   unsigned char encrypt (unsigned char b = 0);  // Encrypt byte
00051   // or get a random byte.
00052   unsigned char decrypt (unsigned char b);      // Decrypt byte.
00053   void hash_final (unsigned char *hash, // Copy hash value to hash
00054                    unsigned char hashlength = 20);      // Hash length (16-32)
00055   void burn (void);             // Destroy cipher state information.
00056 };
00057 
00058 
00059 SWORD_NAMESPACE_END