Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

cryptlib.h

Go to the documentation of this file.
00001 // cryptlib.h - written and placed in the public domain by Wei Dai
00057 #ifndef CRYPTOPP_CRYPTLIB_H
00058 #define CRYPTOPP_CRYPTLIB_H
00059 
00060 #include "config.h"
00061 #include <limits.h>
00062 #include <exception>
00063 #include <string>
00064 
00065 NAMESPACE_BEGIN(CryptoPP)
00066 
00068 
00069 class Exception : public std::exception
00070 {
00071 public:
00072         explicit Exception(const std::string &s) : m_what(s) {}
00073         virtual ~Exception() throw() {}
00074         const char *what() const throw() {return (m_what.c_str());}
00075         const std::string &GetWhat() const {return m_what;}
00076         void SetWhat(const std::string &s) {m_what = s;}
00077 
00078 private:
00079         std::string m_what;
00080 };
00081 
00083 enum CipherDir {
00085         ENCRYPTION,
00087         DECRYPTION};
00088 
00089 
00091 
00100 class BlockTransformation
00101 {
00102 public:
00104         virtual ~BlockTransformation() {}
00105 
00107 
00108         virtual void ProcessBlock(byte *inoutBlock) const =0;
00109 
00111 
00112         virtual void ProcessBlock(const byte *inBlock, byte *outBlock) const =0;
00113 
00115         virtual unsigned int BlockSize() const =0;
00116 };
00117 
00119 template <unsigned int N>
00120 class FixedBlockSize : public BlockTransformation
00121 {
00122 public:
00123         enum {BLOCKSIZE = N};
00124         virtual unsigned int BlockSize() const {return BLOCKSIZE;}
00125 };
00126 
00128 
00129 class StreamCipher
00130 {
00131 public:
00133         virtual ~StreamCipher() {}
00134 
00136         virtual byte ProcessByte(byte input) =0;
00137 
00139         virtual void ProcessString(byte *inoutString, unsigned int length);
00141         virtual void ProcessString(byte *outString, const byte *inString, unsigned int length);
00142 };
00143 
00145 
00146 class RandomAccessStreamCipher : public virtual StreamCipher
00147 {
00148 public:
00150         virtual ~RandomAccessStreamCipher() {}
00151         /*/ specify that the next byte to be processed is at absolute position n
00152                 in the plaintext/ciphertext stream */
00153         virtual void Seek(unsigned long n) =0;
00154 };
00155 
00157 
00160 class RandomNumberGenerator
00161 {
00162 public:
00164         virtual ~RandomNumberGenerator() {}
00165 
00167         virtual byte GenerateByte() =0;
00168 
00170 
00171         virtual unsigned int GenerateBit();
00172 
00174         virtual word32 GenerateWord32(word32 a=0, word32 b=0xffffffffL);
00175 
00177         //* Default implementation is to call GenerateByte() size times.
00178         virtual void GenerateBlock(byte *output, unsigned int size);
00179 
00181         template <class IT> void Shuffle(IT begin, IT end)
00182         {
00183                 for (; begin != end; ++begin)
00184                         std::iter_swap(begin, begin + GenerateWord32(0, end-begin-1));
00185         }
00186 
00187         // for backwards compatibility, maybe be remove later
00188         byte GetByte() {return GenerateByte();}
00189         unsigned int GetBit() {return GenerateBit();}
00190         word32 GetLong(word32 a=0, word32 b=0xffffffffL) {return GenerateWord32(a, b);}
00191         word16 GetShort(word16 a=0, word16 b=0xffff) {return (word16)GenerateWord32(a, b);}
00192         void GetBlock(byte *output, unsigned int size) {GenerateBlock(output, size);}
00193 };
00194 
00196 
00203 class HashModule
00204 {
00205 public:
00207         virtual ~HashModule() {}
00208 
00210         virtual void Update(const byte *input, unsigned int length) =0;
00211 
00212         /*/ calculate hash for the current message (the concatenation of all
00213                 inputs passed in via Update()), then reinitialize the object */
00214         //* Precondition: size of digest == DigestSize().
00215         virtual void Final(byte *digest) =0;
00216 
00218         virtual unsigned int DigestSize() const =0;
00219 
00221         virtual void CalculateDigest(byte *digest, const byte *input, int length)
00222                 {Update(input, length); Final(digest);}
00223 
00225 
00227         virtual bool Verify(const byte *digest);
00228 
00230         virtual bool VerifyDigest(const byte *digest, const byte *input, int length)
00231                 {Update(input, length); return Verify(digest);}
00232 };
00233 
00235 
00242 class MessageAuthenticationCode : public virtual HashModule
00243 {
00244 public:
00246         virtual ~MessageAuthenticationCode() {}
00247 };
00248 
00250 
00264 class BufferedTransformation
00265 {
00266 public:
00268         virtual ~BufferedTransformation() {}
00269 
00271 
00272 
00273                 virtual void Put(byte inByte) =0;
00275                 virtual void Put(const byte *inString, unsigned int length) =0;
00276 
00278                 void PutWord16(word16 value, bool highFirst=true);
00280                 void PutWord32(word32 value, bool highFirst=true);
00282 
00284 
00285 
00286 
00288                 virtual void Flush(bool completeFlush, int propagation=-1);
00290 
00294                 virtual void MessageEnd(int propagation=-1);
00296                 virtual void PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1);
00298 
00299                 virtual void MessageSeriesEnd(int propagation=-1);
00300 
00302 
00303                 virtual void SetAutoSignalPropagation(int propagation) {}
00304 
00306                 virtual int GetAutoSignalPropagation() const {return 0;}
00307 
00308                 // for backwards compatibility
00309                 void Close() {MessageEnd();}
00311 
00313 
00314 
00315                 enum ErrorType {
00317                         CANNOT_FLUSH,
00319                         DATA_INTEGRITY_CHECK_FAILED,
00321                         INVALID_DATA_FORMAT,
00323                         INPUT_ERROR,
00325                         OUTPUT_ERROR,
00327                         OTHER_ERROR
00328                 };
00329 
00331                 class Err : public Exception
00332                 {
00333                 public:
00334                         Err(ErrorType errorType, const std::string &s="");
00335                         ErrorType GetErrorType() const {return m_errorType;}
00336                         void SetErrorType(ErrorType errorType) {m_errorType = errorType;}
00337                 private:
00338                         ErrorType m_errorType;
00339                 };
00341 
00343 
00344 
00345 
00348                 virtual unsigned long MaxRetrievable() const;
00349 
00350                 // old mispelled name
00351                 unsigned long MaxRetrieveable() const {return MaxRetrievable();}
00352 
00354                 virtual bool AnyRetrievable() const;
00355 
00357                 virtual unsigned int Get(byte &outByte);
00359                 virtual unsigned int Get(byte *outString, unsigned int getMax);
00360 
00362                 virtual unsigned int Peek(byte &outByte) const;
00364                 virtual unsigned int Peek(byte *outString, unsigned int peekMax) const;
00365 
00367                 unsigned int GetWord16(word16 &value, bool highFirst=true);
00369                 unsigned int GetWord32(word32 &value, bool highFirst=true);
00370 
00372                 unsigned int PeekWord16(word16 &value, bool highFirst=true);
00374                 unsigned int PeekWord32(word32 &value, bool highFirst=true);
00375 
00377                 virtual unsigned long TransferTo(BufferedTransformation &target, unsigned long transferMax=ULONG_MAX);
00378 
00380                 virtual unsigned long Skip(unsigned long skipMax=ULONG_MAX);
00381 
00383                 virtual unsigned long CopyTo(BufferedTransformation &target, unsigned long copyMax=ULONG_MAX) const;
00385 
00387 
00388 
00389                 virtual unsigned long TotalBytesRetrievable() const;
00391                 virtual unsigned int NumberOfMessages() const;
00393                 virtual bool AnyMessages() const;
00395 
00399                 virtual bool GetNextMessage();
00401                 virtual unsigned int SkipMessages(unsigned int count=UINT_MAX);
00403                 virtual unsigned int TransferMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX);
00405                 virtual unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX) const;
00406 
00408                 virtual void SkipAll();
00410                 virtual void TransferAllTo(BufferedTransformation &target);
00412                 virtual void CopyAllTo(BufferedTransformation &target) const;
00414 
00416 
00417                 virtual void ChannelPut(const std::string &channel, byte inByte);
00418                 virtual void ChannelPut(const std::string &channel, const byte *inString, unsigned int length);
00419 
00420                 void ChannelPutWord16(const std::string &channel, word16 value, bool highFirst=true);
00421                 void ChannelPutWord32(const std::string &channel, word32 value, bool highFirst=true);
00422 
00423                 virtual void ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1);
00424                 virtual void ChannelMessageEnd(const std::string &channel, int propagation=-1);
00425                 virtual void ChannelPutMessageEnd(const std::string &channel, const byte *inString, unsigned int length, int propagation=-1);
00426                 virtual void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1);
00427 
00428                 virtual void SetRetrievalChannel(const std::string &channel);
00429 
00430                 static const std::string NULL_CHANNEL;
00432 
00441 
00442                 virtual bool Attachable() {return false;}
00444                 virtual BufferedTransformation *AttachedTransformation() {return 0;}
00446                 virtual const BufferedTransformation *AttachedTransformation() const
00447                         {return const_cast<BufferedTransformation *>(this)->AttachedTransformation();}
00449                 virtual void Detach(BufferedTransformation *newAttachment = 0) {}
00451                 virtual void Attach(BufferedTransformation *newAttachment);
00453 };
00454 
00456 
00460 class PK_CryptoSystem
00461 {
00462 public:
00464         virtual ~PK_CryptoSystem() {}
00465 
00467         //* This function returns 0 if cipherTextLength is not valid (too long or too short).
00468         virtual unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const =0;
00469 
00471         //* This function returns 0 if plainTextLength is not valid (too long).
00472         virtual unsigned int CipherTextLength(unsigned int plainTextLength) const =0;
00473 };
00474 
00476 
00480 class PK_Encryptor : public virtual PK_CryptoSystem
00481 {
00482 public:
00484 
00490         virtual void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) =0;
00491 };
00492 
00494 
00498 class PK_Decryptor : public virtual PK_CryptoSystem
00499 {
00500 public:
00502 
00508         virtual unsigned int Decrypt(const byte *cipherText, unsigned int cipherTextLength, byte *plainText) =0;
00509 };
00510 
00512 
00519 class PK_FixedLengthCryptoSystem : public virtual PK_CryptoSystem
00520 {
00521 public:
00523         virtual unsigned int MaxPlainTextLength() const =0;
00525         virtual unsigned int CipherTextLength() const =0;
00526 
00527         unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const;
00528         unsigned int CipherTextLength(unsigned int plainTextLength) const;
00529 };
00530 
00532 
00533 class PK_FixedLengthEncryptor : public virtual PK_Encryptor, public virtual PK_FixedLengthCryptoSystem
00534 {
00535 };
00536 
00538 
00539 class PK_FixedLengthDecryptor : public virtual PK_Decryptor, public virtual PK_FixedLengthCryptoSystem
00540 {
00541 public:
00543 
00552         virtual unsigned int Decrypt(const byte *cipherText, byte *plainText) =0;
00553 
00554         unsigned int Decrypt(const byte *cipherText, unsigned int cipherTextLength, byte *plainText);
00555 };
00556 
00558 
00563 class PK_SignatureSystem
00564 {
00565 public:
00567         virtual ~PK_SignatureSystem() {};
00568 
00570         virtual unsigned int SignatureLength() const =0;
00571 
00573         virtual HashModule * NewMessageAccumulator() const =0;
00574 };
00575 
00577 
00581 class PK_Signer : public virtual PK_SignatureSystem
00582 {
00583 public:
00585         class KeyTooShort : public Exception
00586         {
00587         public:
00588                 KeyTooShort() : Exception("PK_Signer: key too short") {}
00589         };
00590 
00592 
00599         virtual void Sign(RandomNumberGenerator &rng, HashModule *messageAccumulator, byte *signature) const =0;
00600 
00602 
00603         virtual void SignMessage(RandomNumberGenerator &rng, const byte *message, unsigned int messageLen, byte *signature) const;
00604 };
00605 
00607 
00611 class PK_Verifier : public virtual PK_SignatureSystem
00612 {
00613 public:
00615 
00622         virtual bool Verify(HashModule *messageAccumulator, const byte *sig) const =0;
00623 
00625 
00626         virtual bool VerifyMessage(const byte *message, unsigned int messageLen, const byte *sig) const;
00627 };
00628 
00630 
00634 class PK_SignatureSystemWithRecovery : public virtual PK_SignatureSystem
00635 {
00636 public:
00638         virtual unsigned int MaximumRecoverableLength() const =0;
00639 
00641 
00645         virtual bool AllowLeftoverMessage() const =0;
00646 };
00647 
00649 
00650 class PK_SignerWithRecovery : public virtual PK_SignatureSystemWithRecovery, public PK_Signer
00651 {
00652 };
00653 
00655 
00660 class PK_VerifierWithRecovery : public virtual PK_SignatureSystemWithRecovery, public PK_Verifier
00661 {
00662 public:
00664         virtual HashModule * NewLeftoverMessageAccumulator(const byte *signature) const =0;
00665 
00667 
00675         virtual unsigned int PartialRecover(HashModule *leftoverMessageAccumulator, byte *recoveredMessage) const =0;
00676 
00678 
00685         virtual unsigned int Recover(const byte *signature, byte *recoveredMessage) const =0;
00686 };
00687 
00689 
00694 class PK_SimpleKeyAgreementDomain
00695 {
00696 public:
00697         virtual ~PK_SimpleKeyAgreementDomain() {}
00698 
00700         virtual bool ValidateDomainParameters(RandomNumberGenerator &rng) const =0;
00702         virtual unsigned int AgreedValueLength() const =0;
00704         virtual unsigned int PrivateKeyLength() const =0;
00706         virtual unsigned int PublicKeyLength() const =0;
00708 
00714         virtual void GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const =0;
00716 
00724 
00728 
00733 
00739 
00741 
00744 
00746 
00748 
00754 
00757 
00759 
00761 
00767 
00770 
00781 
00788 
00792 
00796 
00799 
00803 
00806 
00808 
00812 

Generated at Mon Jan 15 01:16:30 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000