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 class sapphire 00022 { 00023 00024 // These variables comprise the state of the state machine. 00025 unsigned char cards[256]; // A permutation of 0-255. 00026 unsigned char rotor, // Index that rotates smoothly 00027 ratchet, // Index that moves erratically 00028 avalanche, // Index heavily data dependent 00029 last_plain, // Last plain text byte 00030 last_cipher; // Last cipher text byte 00031 00032 // This function is used by initialize(), which is called by the 00033 // constructor. 00034 unsigned char keyrand (int limit, unsigned char *user_key, 00035 unsigned char keysize, unsigned char *rsum, 00036 unsigned *keypos); public:sapphire (unsigned char 00037 *key = NULL, // Calls initialize if a real 00038 unsigned char keysize = 0); // key is provided. If none 00039 // is provided, call initialize 00040 // before encrypt or decrypt. 00041 ~sapphire (); // Destroy cipher state information. 00042 void initialize (unsigned char *key, // User key is used to set 00043 unsigned char keysize); // up state information. 00044 void hash_init (void); // Set up default hash. 00045 unsigned char encrypt (unsigned char b = 0); // Encrypt byte 00046 // or get a random byte. 00047 unsigned char decrypt (unsigned char b); // Decrypt byte. 00048 void hash_final (unsigned char *hash, // Copy hash value to hash 00049 unsigned char hashlength = 20); // Hash length (16-32) 00050 void burn (void); // Destroy cipher state information. 00051 }; 00052 00053