Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

sapphire.h

00001 #ifndef CRYPTOPP_SAPPHIRE_H
00002 #define CRYPTOPP_SAPPHIRE_H
00003 
00004 #include "seckey.h"
00005 #include "secblock.h"
00006 
00007 NAMESPACE_BEGIN(CryptoPP)
00008 
00009 /// base class, do not use directly
00010 class SapphireBase : public VariableKeyLength<16, 1, 255>
00011 {
00012 protected:
00013         SapphireBase();
00014         SapphireBase(const byte *userKey, unsigned int keyLength);
00015         ~SapphireBase();
00016 
00017         inline void ShuffleCards()
00018         {
00019                 ratchet += cards[rotor++];
00020                 byte swaptemp = cards[last_cipher];
00021                 cards[last_cipher] = cards[ratchet];
00022                 cards[ratchet] = cards[last_plain];
00023                 cards[last_plain] = cards[rotor];
00024                 cards[rotor] = swaptemp;
00025                 avalanche += cards[swaptemp];
00026         }
00027 
00028         // These variables comprise the state of the state machine.
00029 
00030         SecByteBlock cards;             // A permutation of 0-255.
00031         byte 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 private:
00038         byte keyrand(unsigned int limit, const byte *user_key, byte keysize, byte *rsum, unsigned *keypos);
00039 };
00040 
00041 /// <a href="http://www.weidai.com/scan-mirror/cs.html#Sapphire-II">Sapphire-II Cipher</a>
00042 class SapphireEncryption : public StreamTransformation, public SapphireBase
00043 {
00044 public:
00045         SapphireEncryption(const byte *userKey, unsigned int keyLength=DEFAULT_KEYLENGTH)
00046                 : SapphireBase(userKey, keyLength) {}
00047 
00048         inline byte ProcessByte(byte b)
00049         {
00050                 ShuffleCards();
00051                 last_cipher = b^cards[(cards[ratchet] + cards[rotor]) & 0xFF] ^
00052                                           cards[cards[(cards[last_plain] +
00053                                                                    cards[last_cipher] +
00054                                                                    cards[avalanche])&0xFF]];
00055                 last_plain = b;
00056                 return last_cipher;
00057         }
00058 
00059         void ProcessString(byte *outString, const byte *inString, unsigned int length);
00060         void ProcessString(byte *inoutString, unsigned int length);
00061 
00062 protected:
00063         SapphireEncryption() {}     // for SapphireHash
00064 };
00065 
00066 /// <a href="http://www.weidai.com/scan-mirror/cs.html#Sapphire-II">Sapphire-II cipher</a>
00067 class SapphireDecryption : public StreamTransformation, public SapphireBase
00068 {
00069 public:
00070         SapphireDecryption(const byte *userKey, unsigned int keyLength=DEFAULT_KEYLENGTH)
00071                 : SapphireBase(userKey, keyLength) {}
00072 
00073         inline byte ProcessByte(byte b)
00074         {
00075                 ShuffleCards();
00076                 last_plain = b^cards[(cards[ratchet] + cards[rotor]) & 0xFF] ^
00077                                            cards[cards[(cards[last_plain] +
00078                                                                         cards[last_cipher] +
00079                                                                         cards[avalanche])&0xFF]];
00080                 last_cipher = b;
00081                 return last_plain;
00082         }
00083 
00084         void ProcessString(byte *outString, const byte *inString, unsigned int length);
00085         void ProcessString(byte *inoutString, unsigned int length);
00086 };
00087 
00088 /// Sapphire Random Number Generator
00089 class SapphireRNG : public RandomNumberGenerator, private SapphireEncryption
00090 {
00091 public:
00092         SapphireRNG(const byte *seed, unsigned int seedLength)
00093                 : SapphireEncryption(seed, seedLength) {}
00094 
00095         inline byte GetByte() {return SapphireEncryption::ProcessByte(0);}
00096 };
00097 
00098 //! Sapphire Hash
00099 /*! Digest Length = 160 bits */
00100 class SapphireHash : public HashTransformation, private SapphireEncryption
00101 {
00102 public:
00103         SapphireHash(unsigned int hashLength=20);
00104         void Update(const byte *input, unsigned int length);
00105         void TruncatedFinal(byte *hash, unsigned int size);
00106         unsigned int DigestSize() const {return hashLength;}
00107 
00108 private:
00109         void Init();
00110         const unsigned int hashLength;
00111 };
00112 
00113 NAMESPACE_END
00114 
00115 #endif

Generated on Mon Apr 19 18:12:32 2004 for Crypto++ by doxygen 1.3.6-20040222