00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 #ifndef CRYPTOPP_CRYPTLIB_H
00076 #define CRYPTOPP_CRYPTLIB_H
00077
00078 #include "config.h"
00079 #include "stdcpp.h"
00080
00081 NAMESPACE_BEGIN(CryptoPP)
00082
00083
00084 class Integer;
00085
00086
00087 enum CipherDir {ENCRYPTION, DECRYPTION};
00088
00089
00090 const unsigned long INFINITE_TIME = ULONG_MAX;
00091
00092
00093 template <typename ENUM_TYPE, int VALUE>
00094 struct EnumToType
00095 {
00096 static ENUM_TYPE ToEnum() {return (ENUM_TYPE)VALUE;}
00097 };
00098
00099 enum ByteOrder {LITTLE_ENDIAN_ORDER = 0, BIG_ENDIAN_ORDER = 1};
00100 typedef EnumToType<ByteOrder, LITTLE_ENDIAN_ORDER> LittleEndian;
00101 typedef EnumToType<ByteOrder, BIG_ENDIAN_ORDER> BigEndian;
00102
00103
00104 class CRYPTOPP_DLL Exception : public std::exception
00105 {
00106 public:
00107
00108 enum ErrorType {
00109
00110 NOT_IMPLEMENTED,
00111
00112 INVALID_ARGUMENT,
00113
00114 CANNOT_FLUSH,
00115
00116 DATA_INTEGRITY_CHECK_FAILED,
00117
00118 INVALID_DATA_FORMAT,
00119
00120 IO_ERROR,
00121
00122 OTHER_ERROR
00123 };
00124
00125 explicit Exception(ErrorType errorType, const std::string &s) : m_errorType(errorType), m_what(s) {}
00126 virtual ~Exception() throw() {}
00127 const char *what() const throw() {return (m_what.c_str());}
00128 const std::string &GetWhat() const {return m_what;}
00129 void SetWhat(const std::string &s) {m_what = s;}
00130 ErrorType GetErrorType() const {return m_errorType;}
00131 void SetErrorType(ErrorType errorType) {m_errorType = errorType;}
00132
00133 private:
00134 ErrorType m_errorType;
00135 std::string m_what;
00136 };
00137
00138
00139 class CRYPTOPP_DLL InvalidArgument : public Exception
00140 {
00141 public:
00142 explicit InvalidArgument(const std::string &s) : Exception(INVALID_ARGUMENT, s) {}
00143 };
00144
00145
00146 class CRYPTOPP_DLL InvalidDataFormat : public Exception
00147 {
00148 public:
00149 explicit InvalidDataFormat(const std::string &s) : Exception(INVALID_DATA_FORMAT, s) {}
00150 };
00151
00152
00153 class CRYPTOPP_DLL InvalidCiphertext : public InvalidDataFormat
00154 {
00155 public:
00156 explicit InvalidCiphertext(const std::string &s) : InvalidDataFormat(s) {}
00157 };
00158
00159
00160 class CRYPTOPP_DLL NotImplemented : public Exception
00161 {
00162 public:
00163 explicit NotImplemented(const std::string &s) : Exception(NOT_IMPLEMENTED, s) {}
00164 };
00165
00166
00167 class CRYPTOPP_DLL CannotFlush : public Exception
00168 {
00169 public:
00170 explicit CannotFlush(const std::string &s) : Exception(CANNOT_FLUSH, s) {}
00171 };
00172
00173
00174 class CRYPTOPP_DLL OS_Error : public Exception
00175 {
00176 public:
00177 OS_Error(ErrorType errorType, const std::string &s, const std::string& operation, int errorCode)
00178 : Exception(errorType, s), m_operation(operation), m_errorCode(errorCode) {}
00179 ~OS_Error() throw() {}
00180
00181
00182 const std::string & GetOperation() const {return m_operation;}
00183
00184 int GetErrorCode() const {return m_errorCode;}
00185
00186 protected:
00187 std::string m_operation;
00188 int m_errorCode;
00189 };
00190
00191
00192 struct CRYPTOPP_DLL DecodingResult
00193 {
00194 explicit DecodingResult() : isValidCoding(false), messageLength(0) {}
00195 explicit DecodingResult(unsigned int len) : isValidCoding(true), messageLength(len) {}
00196
00197 bool operator==(const DecodingResult &rhs) const {return isValidCoding == rhs.isValidCoding && messageLength == rhs.messageLength;}
00198 bool operator!=(const DecodingResult &rhs) const {return !operator==(rhs);}
00199
00200 bool isValidCoding;
00201 unsigned int messageLength;
00202
00203 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
00204 operator unsigned int() const {return isValidCoding ? messageLength : 0;}
00205 #endif
00206 };
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 class CRYPTOPP_NO_VTABLE NameValuePairs
00220 {
00221 public:
00222 virtual ~NameValuePairs() {}
00223
00224
00225 class CRYPTOPP_DLL ValueTypeMismatch : public InvalidArgument
00226 {
00227 public:
00228 ValueTypeMismatch(const std::string &name, const std::type_info &stored, const std::type_info &retrieving)
00229 : InvalidArgument("NameValuePairs: type mismatch for '" + name + "', stored '" + stored.name() + "', trying to retrieve '" + retrieving.name() + "'")
00230 , m_stored(stored), m_retrieving(retrieving) {}
00231
00232 const std::type_info & GetStoredTypeInfo() const {return m_stored;}
00233 const std::type_info & GetRetrievingTypeInfo() const {return m_retrieving;}
00234
00235 private:
00236 const std::type_info &m_stored;
00237 const std::type_info &m_retrieving;
00238 };
00239
00240
00241 template <class T>
00242 bool GetThisObject(T &object) const
00243 {
00244 return GetValue((std::string("ThisObject:")+typeid(T).name()).c_str(), object);
00245 }
00246
00247
00248 template <class T>
00249 bool GetThisPointer(T *&p) const
00250 {
00251 return GetValue((std::string("ThisPointer:")+typeid(T).name()).c_str(), p);
00252 }
00253
00254
00255 template <class T>
00256 bool GetValue(const char *name, T &value) const
00257 {
00258 return GetVoidValue(name, typeid(T), &value);
00259 }
00260
00261
00262 template <class T>
00263 T GetValueWithDefault(const char *name, T defaultValue) const
00264 {
00265 GetValue(name, defaultValue);
00266 return defaultValue;
00267 }
00268
00269
00270 CRYPTOPP_DLL std::string GetValueNames() const
00271 {std::string result; GetValue("ValueNames", result); return result;}
00272
00273
00274
00275
00276 CRYPTOPP_DLL bool GetIntValue(const char *name, int &value) const
00277 {return GetValue(name, value);}
00278
00279
00280 CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
00281 {return GetValueWithDefault(name, defaultValue);}
00282
00283
00284 CRYPTOPP_DLL static void ThrowIfTypeMismatch(const char *name, const std::type_info &stored, const std::type_info &retrieving)
00285 {if (stored != retrieving) throw ValueTypeMismatch(name, stored, retrieving);}
00286
00287 template <class T>
00288 void GetRequiredParameter(const char *className, const char *name, T &value) const
00289 {
00290 if (!GetValue(name, value))
00291 throw InvalidArgument(std::string(className) + ": missing required parameter '" + name + "'");
00292 }
00293
00294 CRYPTOPP_DLL void GetRequiredIntParameter(const char *className, const char *name, int &value) const
00295 {
00296 if (!GetIntValue(name, value))
00297 throw InvalidArgument(std::string(className) + ": missing required parameter '" + name + "'");
00298 }
00299
00300
00301 CRYPTOPP_DLL virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
00302 };
00303
00304
00305
00306
00307
00308
00309
00310 DOCUMENTED_NAMESPACE_BEGIN(Name)
00311
00312 DOCUMENTED_NAMESPACE_END
00313
00314
00315 class CRYPTOPP_DLL NullNameValuePairs : public NameValuePairs
00316 {
00317 public:
00318 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const {return false;}
00319 };
00320
00321
00322 extern CRYPTOPP_DLL const NullNameValuePairs g_nullNameValuePairs;
00323
00324
00325
00326
00327 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Clonable
00328 {
00329 public:
00330 virtual ~Clonable() {}
00331
00332 virtual Clonable* Clone() const {throw NotImplemented("Clone() is not implemented yet.");}
00333 };
00334
00335
00336
00337 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Algorithm : public Clonable
00338 {
00339 public:
00340
00341
00342 Algorithm(bool checkSelfTestStatus = true);
00343
00344 virtual std::string AlgorithmName() const {return "unknown";}
00345 };
00346
00347
00348
00349 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE SimpleKeyingInterface
00350 {
00351 public:
00352
00353 virtual unsigned int MinKeyLength() const =0;
00354
00355 virtual unsigned int MaxKeyLength() const =0;
00356
00357 virtual unsigned int DefaultKeyLength() const =0;
00358
00359
00360 virtual unsigned int GetValidKeyLength(unsigned int n) const =0;
00361
00362
00363 virtual bool IsValidKeyLength(unsigned int n) const
00364 {return n == GetValidKeyLength(n);}
00365
00366
00367
00368 virtual void SetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms = g_nullNameValuePairs) =0;
00369
00370
00371 void SetKeyWithRounds(const byte *key, unsigned int length, int rounds);
00372
00373
00374 void SetKeyWithIV(const byte *key, unsigned int length, const byte *iv);
00375
00376 enum IV_Requirement {STRUCTURED_IV = 0, RANDOM_IV, UNPREDICTABLE_RANDOM_IV, INTERNALLY_GENERATED_IV, NOT_RESYNCHRONIZABLE};
00377
00378 virtual IV_Requirement IVRequirement() const =0;
00379
00380
00381
00382 bool IsResynchronizable() const {return IVRequirement() < NOT_RESYNCHRONIZABLE;}
00383
00384 bool CanUseRandomIVs() const {return IVRequirement() <= UNPREDICTABLE_RANDOM_IV;}
00385
00386 bool CanUsePredictableIVs() const {return IVRequirement() <= RANDOM_IV;}
00387
00388 bool CanUseStructuredIVs() const {return IVRequirement() <= STRUCTURED_IV;}
00389
00390
00391 virtual unsigned int IVSize() const {throw NotImplemented("SimpleKeyingInterface: this object doesn't support resynchronization");}
00392
00393 virtual void Resynchronize(const byte *IV) {throw NotImplemented("SimpleKeyingInterface: this object doesn't support resynchronization");}
00394
00395
00396
00397
00398 virtual void GetNextIV(byte *IV) {throw NotImplemented("SimpleKeyingInterface: this object doesn't support GetNextIV()");}
00399
00400 protected:
00401 void ThrowIfInvalidKeyLength(const Algorithm &algorithm, unsigned int length);
00402 void ThrowIfResynchronizable();
00403 void ThrowIfInvalidIV(const byte *iv);
00404 const byte * GetIVAndThrowIfInvalid(const NameValuePairs ¶ms);
00405
00406 inline void AssertValidKeyLength(unsigned int length) const
00407 {
00408 assert(IsValidKeyLength(length));
00409 }
00410 };
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockTransformation : public Algorithm
00421 {
00422 public:
00423
00424 virtual void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const =0;
00425
00426
00427
00428 void ProcessBlock(const byte *inBlock, byte *outBlock) const
00429 {ProcessAndXorBlock(inBlock, NULL, outBlock);}
00430
00431
00432 void ProcessBlock(byte *inoutBlock) const
00433 {ProcessAndXorBlock(inoutBlock, NULL, inoutBlock);}
00434
00435
00436 virtual unsigned int BlockSize() const =0;
00437
00438
00439 virtual unsigned int BlockAlignment() const {return 4;}
00440
00441
00442 virtual bool IsPermutation() const {return true;}
00443
00444
00445 virtual bool IsForwardTransformation() const =0;
00446
00447
00448 virtual unsigned int OptimalNumberOfParallelBlocks() const {return 1;}
00449
00450
00451 virtual void ProcessAndXorMultipleBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, unsigned int numberOfBlocks) const;
00452 };
00453
00454
00455
00456 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE StreamTransformation : public Algorithm
00457 {
00458 public:
00459
00460
00461
00462 StreamTransformation& Ref() {return *this;}
00463
00464
00465 virtual unsigned int MandatoryBlockSize() const {return 1;}
00466
00467
00468
00469 virtual unsigned int OptimalBlockSize() const {return MandatoryBlockSize();}
00470
00471 virtual unsigned int GetOptimalBlockSizeUsed() const {return 0;}
00472
00473
00474 virtual unsigned int OptimalDataAlignment() const {return 1;}
00475
00476
00477
00478 virtual void ProcessData(byte *outString, const byte *inString, unsigned int length) =0;
00479
00480
00481
00482 virtual void ProcessLastBlock(byte *outString, const byte *inString, unsigned int length);
00483
00484 virtual unsigned int MinLastBlockSize() const {return 0;}
00485
00486
00487 inline void ProcessString(byte *inoutString, unsigned int length)
00488 {ProcessData(inoutString, inoutString, length);}
00489
00490 inline void ProcessString(byte *outString, const byte *inString, unsigned int length)
00491 {ProcessData(outString, inString, length);}
00492
00493 inline byte ProcessByte(byte input)
00494 {ProcessData(&input, &input, 1); return input;}
00495
00496
00497 virtual bool IsRandomAccess() const =0;
00498
00499 virtual void Seek(lword n)
00500 {
00501 assert(!IsRandomAccess());
00502 throw NotImplemented("StreamTransformation: this object doesn't support random access");
00503 }
00504
00505
00506 virtual bool IsSelfInverting() const =0;
00507
00508 virtual bool IsForwardTransformation() const =0;
00509 };
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE HashTransformation : public Algorithm
00520 {
00521 public:
00522
00523 virtual void Update(const byte *input, unsigned int length) =0;
00524
00525
00526 virtual byte * CreateUpdateSpace(unsigned int &size) {size=0; return NULL;}
00527
00528
00529
00530 virtual void Final(byte *digest)
00531 {TruncatedFinal(digest, DigestSize());}
00532
00533
00534 virtual void Restart()
00535 {TruncatedFinal(NULL, 0);}
00536
00537
00538 virtual unsigned int DigestSize() const =0;
00539
00540
00541 virtual unsigned int BlockSize() const {return 0;}
00542
00543
00544 virtual unsigned int OptimalBlockSize() const {return 1;}
00545
00546
00547 virtual unsigned int OptimalDataAlignment() const {return 1;}
00548
00549
00550 virtual void CalculateDigest(byte *digest, const byte *input, unsigned int length)
00551 {Update(input, length); Final(digest);}
00552
00553
00554
00555
00556 virtual bool Verify(const byte *digest)
00557 {return TruncatedVerify(digest, DigestSize());}
00558
00559
00560 virtual bool VerifyDigest(const byte *digest, const byte *input, unsigned int length)
00561 {Update(input, length); return Verify(digest);}
00562
00563
00564 virtual void TruncatedFinal(byte *digest, unsigned int digestSize) =0;
00565
00566
00567 virtual void CalculateTruncatedDigest(byte *digest, unsigned int digestSize, const byte *input, unsigned int length)
00568 {Update(input, length); TruncatedFinal(digest, digestSize);}
00569
00570
00571 virtual bool TruncatedVerify(const byte *digest, unsigned int digestLength);
00572
00573
00574 virtual bool VerifyTruncatedDigest(const byte *digest, unsigned int digestLength, const byte *input, unsigned int length)
00575 {Update(input, length); return TruncatedVerify(digest, digestLength);}
00576
00577 protected:
00578 void ThrowIfInvalidTruncatedSize(unsigned int size) const;
00579 };
00580
00581
00582 template <class T>
00583 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE SimpleKeyedTransformation : public T, public SimpleKeyingInterface
00584 {
00585 public:
00586 void ThrowIfInvalidKeyLength(unsigned int length)
00587 {SimpleKeyingInterface::ThrowIfInvalidKeyLength(*this, length);}
00588 };
00589
00590
00591 typedef HashTransformation HashFunction;
00592 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
00593
00594 class BlockCipher : public BlockTransformation, public SimpleKeyingInterface {};
00595
00596 class SymmetricCipher : public StreamTransformation, public SimpleKeyingInterface {};
00597
00598 class MessageAuthenticationCode : public HashTransformation, public SimpleKeyingInterface {};
00599 #else
00600 typedef SimpleKeyedTransformation<BlockTransformation> BlockCipher;
00601 typedef SimpleKeyedTransformation<StreamTransformation> SymmetricCipher;
00602 typedef SimpleKeyedTransformation<HashTransformation> MessageAuthenticationCode;
00603
00604 CRYPTOPP_DLL_TEMPLATE_CLASS SimpleKeyedTransformation<BlockTransformation>;
00605 CRYPTOPP_DLL_TEMPLATE_CLASS SimpleKeyedTransformation<StreamTransformation>;
00606 CRYPTOPP_DLL_TEMPLATE_CLASS SimpleKeyedTransformation<HashTransformation>;
00607 #endif
00608
00609 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
00610 typedef SymmetricCipher StreamCipher;
00611 #endif
00612
00613
00614
00615
00616 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE RandomNumberGenerator : public Algorithm
00617 {
00618 public:
00619
00620 virtual byte GenerateByte() =0;
00621
00622
00623
00624 virtual unsigned int GenerateBit();
00625
00626
00627 virtual word32 GenerateWord32(word32 a=0, word32 b=0xffffffffL);
00628
00629
00630
00631 virtual void GenerateBlock(byte *output, unsigned int size);
00632
00633
00634
00635 virtual void DiscardBytes(unsigned int n);
00636
00637
00638 template <class IT> void Shuffle(IT begin, IT end)
00639 {
00640 for (; begin != end; ++begin)
00641 std::iter_swap(begin, begin + GenerateWord32(0, end-begin-1));
00642 }
00643
00644 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
00645 byte GetByte() {return GenerateByte();}
00646 unsigned int GetBit() {return GenerateBit();}
00647 word32 GetLong(word32 a=0, word32 b=0xffffffffL) {return GenerateWord32(a, b);}
00648 word16 GetShort(word16 a=0, word16 b=0xffff) {return (word16)GenerateWord32(a, b);}
00649 void GetBlock(byte *output, unsigned int size) {GenerateBlock(output, size);}
00650 #endif
00651 };
00652
00653
00654 CRYPTOPP_DLL RandomNumberGenerator & NullRNG();
00655
00656 class WaitObjectContainer;
00657
00658
00659
00660 class CRYPTOPP_NO_VTABLE Waitable
00661 {
00662 public:
00663
00664 virtual unsigned int GetMaxWaitObjectCount() const =0;
00665
00666 virtual void GetWaitObjects(WaitObjectContainer &container) =0;
00667
00668
00669 bool Wait(unsigned long milliseconds);
00670 };
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BufferedTransformation : public Algorithm, public Waitable
00699 {
00700 public:
00701
00702 static const std::string NULL_CHANNEL;
00703
00704 BufferedTransformation() : Algorithm(false) {}
00705
00706
00707
00708
00709 BufferedTransformation& Ref() {return *this;}
00710
00711
00712
00713
00714 unsigned int Put(byte inByte, bool blocking=true)
00715 {return Put(&inByte, 1, blocking);}
00716
00717 unsigned int Put(const byte *inString, unsigned int length, bool blocking=true)
00718 {return Put2(inString, length, 0, blocking);}
00719
00720
00721 unsigned int PutWord16(word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
00722
00723 unsigned int PutWord32(word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
00724
00725
00726
00727
00728 virtual byte * CreatePutSpace(unsigned int &size) {size=0; return NULL;}
00729
00730 virtual bool CanModifyInput() const {return false;}
00731
00732
00733 unsigned int PutModifiable(byte *inString, unsigned int length, bool blocking=true)
00734 {return PutModifiable2(inString, length, 0, blocking);}
00735
00736 bool MessageEnd(int propagation=-1, bool blocking=true)
00737 {return !!Put2(NULL, 0, propagation < 0 ? -1 : propagation+1, blocking);}
00738 unsigned int PutMessageEnd(const byte *inString, unsigned int length, int propagation=-1, bool blocking=true)
00739 {return Put2(inString, length, propagation < 0 ? -1 : propagation+1, blocking);}
00740
00741
00742
00743 virtual unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking) =0;
00744
00745
00746 virtual unsigned int PutModifiable2(byte *inString, unsigned int length, int messageEnd, bool blocking)
00747 {return Put2(inString, length, messageEnd, blocking);}
00748
00749
00750 struct BlockingInputOnly : public NotImplemented
00751 {BlockingInputOnly(const std::string &s) : NotImplemented(s + ": Nonblocking input is not implemented by this object.") {}};
00752
00753
00754
00755
00756 unsigned int GetMaxWaitObjectCount() const;
00757 void GetWaitObjects(WaitObjectContainer &container);
00758
00759
00760
00761
00762 virtual void IsolatedInitialize(const NameValuePairs ¶meters) {throw NotImplemented("BufferedTransformation: this object can't be reinitialized");}
00763 virtual bool IsolatedFlush(bool hardFlush, bool blocking) =0;
00764 virtual bool IsolatedMessageSeriesEnd(bool blocking) {return false;}
00765
00766
00767 virtual void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1);
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779 virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true);
00780
00781
00782 virtual bool MessageSeriesEnd(int propagation=-1, bool blocking=true);
00783
00784
00785
00786 virtual void SetAutoSignalPropagation(int propagation) {}
00787
00788
00789 virtual int GetAutoSignalPropagation() const {return 0;}
00790 public:
00791
00792 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
00793 void Close() {MessageEnd();}
00794 #endif
00795
00796
00797
00798
00799
00800
00801
00802
00803 virtual unsigned long MaxRetrievable() const;
00804
00805
00806 virtual bool AnyRetrievable() const;
00807
00808
00809 virtual unsigned int Get(byte &outByte);
00810
00811 virtual unsigned int Get(byte *outString, unsigned int getMax);
00812
00813
00814 virtual unsigned int Peek(byte &outByte) const;
00815
00816 virtual unsigned int Peek(byte *outString, unsigned int peekMax) const;
00817
00818
00819 unsigned int GetWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER);
00820
00821 unsigned int GetWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER);
00822
00823
00824 unsigned int PeekWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER);
00825
00826 unsigned int PeekWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER);
00827
00828
00829 unsigned long TransferTo(BufferedTransformation &target, unsigned long transferMax=ULONG_MAX, const std::string &channel=NULL_CHANNEL)
00830 {TransferTo2(target, transferMax, channel); return transferMax;}
00831
00832
00833 virtual unsigned long Skip(unsigned long skipMax=ULONG_MAX);
00834
00835
00836 unsigned long CopyTo(BufferedTransformation &target, unsigned long copyMax=ULONG_MAX, const std::string &channel=NULL_CHANNEL) const
00837 {return CopyRangeTo(target, 0, copyMax, channel);}
00838
00839
00840 unsigned long CopyRangeTo(BufferedTransformation &target, unsigned long position, unsigned long copyMax=ULONG_MAX, const std::string &channel=NULL_CHANNEL) const
00841 {unsigned long i = position; CopyRangeTo2(target, i, i+copyMax, channel); return i-position;}
00842
00843 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
00844 unsigned long MaxRetrieveable() const {return MaxRetrievable();}
00845 #endif
00846
00847
00848
00849
00850
00851 virtual unsigned long TotalBytesRetrievable() const;
00852
00853 virtual unsigned int NumberOfMessages() const;
00854
00855 virtual bool AnyMessages() const;
00856
00857
00858
00859
00860
00861 virtual bool GetNextMessage();
00862
00863 virtual unsigned int SkipMessages(unsigned int count=UINT_MAX);
00864
00865 unsigned int TransferMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL)
00866 {TransferMessagesTo2(target, count, channel); return count;}
00867
00868 unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL) const;
00869
00870
00871 virtual void SkipAll();
00872
00873 void TransferAllTo(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL)
00874 {TransferAllTo2(target, channel);}
00875
00876 void CopyAllTo(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL) const;
00877
00878 virtual bool GetNextMessageSeries() {return false;}
00879 virtual unsigned int NumberOfMessagesInThisSeries() const {return NumberOfMessages();}
00880 virtual unsigned int NumberOfMessageSeries() const {return 0;}
00881
00882
00883
00884
00885
00886 virtual unsigned int TransferTo2(BufferedTransformation &target, unsigned long &byteCount, const std::string &channel=NULL_CHANNEL, bool blocking=true) =0;
00887 virtual unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const =0;
00888 unsigned int TransferMessagesTo2(BufferedTransformation &target, unsigned int &messageCount, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00889 unsigned int TransferAllTo2(BufferedTransformation &target, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00890
00891
00892
00893
00894 struct NoChannelSupport : public NotImplemented
00895 {NoChannelSupport() : NotImplemented("BufferedTransformation: this object doesn't support multiple channels") {}};
00896
00897 unsigned int ChannelPut(const std::string &channel, byte inByte, bool blocking=true)
00898 {return ChannelPut(channel, &inByte, 1, blocking);}
00899 unsigned int ChannelPut(const std::string &channel, const byte *inString, unsigned int length, bool blocking=true)
00900 {return ChannelPut2(channel, inString, length, 0, blocking);}
00901
00902 unsigned int ChannelPutModifiable(const std::string &channel, byte *inString, unsigned int length, bool blocking=true)
00903 {return ChannelPutModifiable2(channel, inString, length, 0, blocking);}
00904
00905 unsigned int ChannelPutWord16(const std::string &channel, word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
00906 unsigned int ChannelPutWord32(const std::string &channel, word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true);
00907
00908 bool ChannelMessageEnd(const std::string &channel, int propagation=-1, bool blocking=true)
00909 {return !!ChannelPut2(channel, NULL, 0, propagation < 0 ? -1 : propagation+1, blocking);}
00910 unsigned int ChannelPutMessageEnd(const std::string &channel, const byte *inString, unsigned int length, int propagation=-1, bool blocking=true)
00911 {return ChannelPut2(channel, inString, length, propagation < 0 ? -1 : propagation+1, blocking);}
00912
00913 virtual byte * ChannelCreatePutSpace(const std::string &channel, unsigned int &size);
00914
00915 virtual unsigned int ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking);
00916 virtual unsigned int ChannelPutModifiable2(const std::string &channel, byte *begin, unsigned int length, int messageEnd, bool blocking);
00917
00918 virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true);
00919 virtual bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);
00920
00921 virtual void SetRetrievalChannel(const std::string &channel);
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933 virtual bool Attachable() {return false;}
00934
00935 virtual BufferedTransformation *AttachedTransformation() {assert(!Attachable()); return 0;}
00936
00937 virtual const BufferedTransformation *AttachedTransformation() const
00938 {return const_cast<BufferedTransformation *>(this)->AttachedTransformation();}
00939
00940 virtual void Detach(BufferedTransformation *newAttachment = 0)
00941 {assert(!Attachable()); throw NotImplemented("BufferedTransformation: this object is not attachable");}
00942
00943 virtual void Attach(BufferedTransformation *newAttachment);
00944
00945
00946 protected:
00947 static int DecrementPropagation(int propagation)
00948 {return propagation != 0 ? propagation - 1 : 0;}
00949 };
00950
00951
00952 BufferedTransformation & TheBitBucket();
00953
00954
00955
00956 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CryptoMaterial : public NameValuePairs
00957 {
00958 public:
00959
00960 class CRYPTOPP_DLL InvalidMaterial : public InvalidDataFormat
00961 {
00962 public:
00963 explicit InvalidMaterial(const std::string &s) : InvalidDataFormat(s) {}
00964 };
00965
00966
00967
00968 virtual void AssignFrom(const NameValuePairs &source) =0;
00969
00970
00971
00972
00973
00974
00975
00976
00977 virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0;
00978
00979
00980 virtual void ThrowIfInvalid(RandomNumberGenerator &rng, unsigned int level) const
00981 {if (!Validate(rng, level)) throw InvalidMaterial("CryptoMaterial: this object contains invalid values");}
00982
00983
00984
00985
00986 virtual void Save(BufferedTransformation &bt) const
00987 {throw NotImplemented("CryptoMaterial: this object does not support saving");}
00988
00989
00990
00991
00992
00993 virtual void Load(BufferedTransformation &bt)
00994 {throw NotImplemented("CryptoMaterial: this object does not support loading");}
00995
00996
00997 virtual bool SupportsPrecomputation() const {return false;}
00998
00999
01000
01001
01002 virtual void Precompute(unsigned int n)
01003 {assert(!SupportsPrecomputation()); throw NotImplemented("CryptoMaterial: this object does not support precomputation");}
01004
01005 virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation)
01006 {assert(!SupportsPrecomputation()); throw NotImplemented("CryptoMaterial: this object does not support precomputation");}
01007
01008 virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const
01009 {assert(!SupportsPrecomputation()); throw NotImplemented("CryptoMaterial: this object does not support precomputation");}
01010
01011
01012 void DoQuickSanityCheck() const {ThrowIfInvalid(NullRNG(), 0);}
01013 };
01014
01015
01016
01017 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE GeneratableCryptoMaterial : virtual public CryptoMaterial
01018 {
01019 public:
01020
01021
01022
01023 virtual void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms = g_nullNameValuePairs)
01024 {throw NotImplemented("GeneratableCryptoMaterial: this object does not support key/parameter generation");}
01025
01026
01027 void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize);
01028 };
01029
01030
01031
01032 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PublicKey : virtual public CryptoMaterial
01033 {
01034 };
01035
01036
01037
01038 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PrivateKey : public GeneratableCryptoMaterial
01039 {
01040 };
01041
01042
01043
01044 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CryptoParameters : public GeneratableCryptoMaterial
01045 {
01046 };
01047
01048
01049
01050 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AsymmetricAlgorithm : public Algorithm
01051 {
01052 public:
01053
01054 virtual CryptoMaterial & AccessMaterial() =0;
01055
01056 virtual const CryptoMaterial & GetMaterial() const =0;
01057
01058
01059 void BERDecode(BufferedTransformation &bt)
01060 {AccessMaterial().Load(bt);}
01061
01062 void DEREncode(BufferedTransformation &bt) const
01063 {GetMaterial().Save(bt);}
01064 };
01065
01066
01067
01068 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PublicKeyAlgorithm : public AsymmetricAlgorithm
01069 {
01070 public:
01071
01072 CryptoMaterial & AccessMaterial() {return AccessPublicKey();}
01073 const CryptoMaterial & GetMaterial() const {return GetPublicKey();}
01074
01075 virtual PublicKey & AccessPublicKey() =0;
01076 virtual const PublicKey & GetPublicKey() const {return const_cast<PublicKeyAlgorithm *>(this)->AccessPublicKey();}
01077 };
01078
01079
01080
01081 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PrivateKeyAlgorithm : public AsymmetricAlgorithm
01082 {
01083 public:
01084 CryptoMaterial & AccessMaterial() {return AccessPrivateKey();}
01085 const CryptoMaterial & GetMaterial() const {return GetPrivateKey();}
01086
01087 virtual PrivateKey & AccessPrivateKey() =0;
01088 virtual const PrivateKey & GetPrivateKey() const {return const_cast<PrivateKeyAlgorithm *>(this)->AccessPrivateKey();}
01089 };
01090
01091
01092
01093 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE KeyAgreementAlgorithm : public AsymmetricAlgorithm
01094 {
01095 public:
01096 CryptoMaterial & AccessMaterial() {return AccessCryptoParameters();}
01097 const CryptoMaterial & GetMaterial() const {return GetCryptoParameters();}
01098
01099 virtual CryptoParameters & AccessCryptoParameters() =0;
01100 virtual const CryptoParameters & GetCryptoParameters() const {return const_cast<KeyAgreementAlgorithm *>(this)->AccessCryptoParameters();}
01101 };
01102
01103
01104
01105
01106
01107
01108 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_CryptoSystem
01109 {
01110 public:
01111 virtual ~PK_CryptoSystem() {}
01112
01113
01114
01115 virtual unsigned int MaxPlaintextLength(unsigned int ciphertextLength) const =0;
01116
01117
01118
01119 virtual unsigned int CiphertextLength(unsigned int plaintextLength) const =0;
01120
01121
01122
01123 virtual bool ParameterSupported(const char *name) const =0;
01124
01125
01126
01127
01128 virtual unsigned int FixedCiphertextLength() const {return 0;}
01129
01130
01131 virtual unsigned int FixedMaxPlaintextLength() const {return 0;}
01132
01133 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
01134 unsigned int MaxPlainTextLength(unsigned int cipherTextLength) const {return MaxPlaintextLength(cipherTextLength);}
01135 unsigned int CipherTextLength(unsigned int plainTextLength) const {return CiphertextLength(plainTextLength);}
01136 #endif
01137 };
01138
01139
01140 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_Encryptor : virtual public PK_CryptoSystem, public PublicKeyAlgorithm
01141 {
01142 public:
01143
01144 class CRYPTOPP_DLL InvalidPlaintextLength : public Exception
01145 {
01146 public:
01147 InvalidPlaintextLength() : Exception(OTHER_ERROR, "PK_Encryptor: invalid plaintext length") {}
01148 };
01149
01150
01151
01152
01153
01154 virtual void Encrypt(RandomNumberGenerator &rng,
01155 const byte *plaintext, unsigned int plaintextLength,
01156 byte *ciphertext, const NameValuePairs ¶meters = g_nullNameValuePairs) const =0;
01157
01158
01159
01160
01161
01162 virtual BufferedTransformation * CreateEncryptionFilter(RandomNumberGenerator &rng,
01163 BufferedTransformation *attachment=NULL, const NameValuePairs ¶meters = g_nullNameValuePairs) const;
01164 };
01165
01166
01167
01168 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_Decryptor : virtual public PK_CryptoSystem, public PrivateKeyAlgorithm
01169 {
01170 public:
01171
01172
01173
01174
01175 virtual DecodingResult Decrypt(RandomNumberGenerator &rng,
01176 const byte *ciphertext, unsigned int ciphertextLength,
01177 byte *plaintext, const NameValuePairs ¶meters = g_nullNameValuePairs) const =0;
01178
01179
01180
01181
01182 virtual BufferedTransformation * CreateDecryptionFilter(RandomNumberGenerator &rng,
01183 BufferedTransformation *attachment=NULL, const NameValuePairs ¶meters = g_nullNameValuePairs) const;
01184
01185
01186 DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *ciphertext, byte *plaintext, const NameValuePairs ¶meters = g_nullNameValuePairs) const
01187 {return Decrypt(rng, ciphertext, FixedCiphertextLength(), plaintext, parameters);}
01188 };
01189
01190 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
01191 typedef PK_CryptoSystem PK_FixedLengthCryptoSystem;
01192 typedef PK_Encryptor PK_FixedLengthEncryptor;
01193 typedef PK_Decryptor PK_FixedLengthDecryptor;
01194 #endif
01195
01196
01197
01198
01199
01200
01201 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_SignatureScheme
01202 {
01203 public:
01204
01205 class CRYPTOPP_DLL InvalidKeyLength : public Exception
01206 {
01207 public:
01208 InvalidKeyLength(const std::string &message) : Exception(OTHER_ERROR, message) {}
01209 };
01210
01211
01212 class CRYPTOPP_DLL KeyTooShort : public InvalidKeyLength
01213 {
01214 public:
01215 KeyTooShort() : InvalidKeyLength("PK_Signer: key too short for this signature scheme") {}
01216 };
01217
01218 virtual ~PK_SignatureScheme() {}
01219
01220
01221 virtual unsigned int SignatureLength() const =0;
01222
01223
01224 virtual unsigned int MaxSignatureLength(unsigned int recoverablePartLength = 0) const {return SignatureLength();}
01225
01226
01227 virtual unsigned int MaxRecoverableLength() const =0;
01228
01229
01230 virtual unsigned int MaxRecoverableLengthFromSignatureLength(unsigned int signatureLength) const =0;
01231
01232
01233
01234 virtual bool IsProbabilistic() const =0;
01235
01236
01237 virtual bool AllowNonrecoverablePart() const =0;
01238
01239
01240 virtual bool SignatureUpfront() const {return false;}
01241
01242
01243 virtual bool RecoverablePartFirst() const =0;
01244 };
01245
01246
01247
01248
01249
01250 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_MessageAccumulator : public HashTransformation
01251 {
01252 public:
01253
01254 unsigned int DigestSize() const
01255 {throw NotImplemented("PK_MessageAccumulator: DigestSize() should not be called");}
01256
01257 void TruncatedFinal(byte *digest, unsigned int digestSize)
01258 {throw NotImplemented("PK_MessageAccumulator: TruncatedFinal() should not be called");}
01259 };
01260
01261
01262
01263 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_Signer : public PK_SignatureScheme, public PrivateKeyAlgorithm
01264 {
01265 public:
01266
01267 virtual PK_MessageAccumulator * NewSignatureAccumulator(RandomNumberGenerator &rng) const =0;
01268
01269 virtual void InputRecoverableMessage(PK_MessageAccumulator &messageAccumulator, const byte *recoverableMessage, unsigned int recoverableMessageLength) const =0;
01270
01271
01272
01273
01274
01275 virtual unsigned int Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const;
01276
01277
01278
01279
01280
01281 virtual unsigned int SignAndRestart(RandomNumberGenerator &rng, PK_MessageAccumulator &messageAccumulator, byte *signature, bool restart=true) const =0;
01282
01283
01284
01285
01286
01287 virtual unsigned int SignMessage(RandomNumberGenerator &rng, const byte *message, unsigned int messageLen, byte *signature) const;
01288
01289
01290
01291
01292
01293 virtual unsigned int SignMessageWithRecovery(RandomNumberGenerator &rng, const byte *recoverableMessage, unsigned int recoverableMessageLength,
01294 const byte *nonrecoverableMessage, unsigned int nonrecoverableMessageLength, byte *signature) const;
01295 };
01296
01297
01298
01299
01300
01301
01302
01303
01304 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE PK_Verifier : public PK_SignatureScheme, public PublicKeyAlgorithm
01305 {
01306 public:
01307
01308 virtual PK_MessageAccumulator * NewVerificationAccumulator() const =0;
01309
01310
01311 virtual void InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, unsigned int signatureLength) const =0;
01312
01313
01314 virtual bool Verify(PK_MessageAccumulator *messageAccumulator) const;
01315
01316
01317 virtual bool VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const =0;
01318
01319
01320 virtual bool VerifyMessage(const byte *message, unsigned int messageLen,
01321 const byte *signature, unsigned int signatureLength) const;
01322
01323
01324
01325
01326 virtual DecodingResult Recover(byte *recoveredMessage, PK_MessageAccumulator *messageAccumulator) const;
01327
01328
01329
01330
01331 virtual DecodingResult RecoverAndRestart(byte *recoveredMessage, PK_MessageAccumulator &messageAccumulator) const =0;
01332
01333
01334
01335
01336 virtual DecodingResult RecoverMessage(byte *recoveredMessage,
01337 const byte *nonrecoverableMessage, unsigned int nonrecoverableMessageLength,
01338 const byte *signature, unsigned int signatureLength) const;
01339 };
01340
01341
01342
01343
01344
01345
01346
01347 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE SimpleKeyAgreementDomain : public KeyAgreementAlgorithm
01348 {
01349 public:
01350
01351 virtual unsigned int AgreedValueLength() const =0;
01352
01353 virtual unsigned int PrivateKeyLength() const =0;
01354
01355 virtual unsigned int PublicKeyLength() const =0;
01356
01357
01358 virtual void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const =0;
01359
01360
01361 virtual void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const =0;
01362
01363
01364 virtual void GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const;
01365
01366
01367
01368
01369
01370
01371 virtual bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const =0;
01372
01373 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
01374 bool ValidateDomainParameters(RandomNumberGenerator &rng) const
01375 {return GetCryptoParameters().Validate(rng, 2);}
01376 #endif
01377 };
01378
01379
01380
01381
01382
01383
01384
01385 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AuthenticatedKeyAgreementDomain : public KeyAgreementAlgorithm
01386 {
01387 public:
01388
01389 virtual unsigned int AgreedValueLength() const =0;
01390
01391
01392 virtual unsigned int StaticPrivateKeyLength() const =0;
01393
01394 virtual unsigned int StaticPublicKeyLength() const =0;
01395
01396
01397 virtual void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const =0;
01398
01399
01400 virtual void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const =0;
01401
01402
01403 virtual void GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const;
01404
01405
01406 virtual unsigned int EphemeralPrivateKeyLength() const =0;
01407
01408 virtual unsigned int EphemeralPublicKeyLength() const =0;
01409
01410
01411 virtual void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const =0;
01412
01413
01414 virtual void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const =0;
01415
01416
01417 virtual void GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const;
01418
01419
01420
01421
01422
01423
01424
01425
01426
01427
01428 virtual bool Agree(byte *agreedValue,
01429 const byte *staticPrivateKey, const byte *ephemeralPrivateKey,
01430 const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
01431 bool validateStaticOtherPublicKey=true) const =0;
01432
01433 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
01434 bool ValidateDomainParameters(RandomNumberGenerator &rng) const
01435 {return GetCryptoParameters().Validate(rng, 2);}
01436 #endif
01437 };
01438
01439
01440 #if 0
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462 class ProtocolSession
01463 {
01464 public:
01465
01466 class ProtocolError : public Exception
01467 {
01468 public:
01469 ProtocolError(ErrorType errorType, const std::string &s) : Exception(errorType, s) {}
01470 };
01471
01472
01473
01474 class UnexpectedMethodCall : public Exception
01475 {
01476 public:
01477 UnexpectedMethodCall(const std::string &s) : Exception(OTHER_ERROR, s) {}
01478 };
01479
01480 ProtocolSession() : m_rng(NULL), m_throwOnProtocolError(true), m_validState(false) {}
01481 virtual ~ProtocolSession() {}
01482
01483 virtual void InitializeSession(RandomNumberGenerator &rng, const NameValuePairs ¶meters) =0;
01484
01485 bool GetThrowOnProtocolError() const {return m_throwOnProtocolError;}
01486 void SetThrowOnProtocolError(bool throwOnProtocolError) {m_throwOnProtocolError = throwOnProtocolError;}
01487
01488 bool HasValidState() const {return m_validState;}
01489
01490 virtual bool OutgoingMessageAvailable() const =0;
01491 virtual unsigned int GetOutgoingMessageLength() const =0;
01492 virtual void GetOutgoingMessage(byte *message) =0;
01493
01494 virtual bool LastMessageProcessed() const =0;
01495 virtual void ProcessIncomingMessage(const byte *message, unsigned int messageLength) =0;
01496
01497 protected:
01498 void HandleProtocolError(Exception::ErrorType errorType, const std::string &s) const;
01499 void CheckAndHandleInvalidState() const;
01500 void SetValidState(bool valid) {m_validState = valid;}
01501
01502 RandomNumberGenerator *m_rng;
01503
01504 private:
01505 bool m_throwOnProtocolError, m_validState;
01506 };
01507
01508 class KeyAgreementSession : public ProtocolSession
01509 {
01510 public:
01511 virtual unsigned int GetAgreedValueLength() const =0;
01512 virtual void GetAgreedValue(byte *agreedValue) const =0;
01513 };
01514
01515 class PasswordAuthenticatedKeyAgreementSession : public KeyAgreementSession
01516 {
01517 public:
01518 void InitializePasswordAuthenticatedKeyAgreementSession(RandomNumberGenerator &rng,
01519 const byte *myId, unsigned int myIdLength,
01520 const byte *counterPartyId, unsigned int counterPartyIdLength,
01521 const byte *passwordOrVerifier, unsigned int passwordOrVerifierLength);
01522 };
01523
01524 class PasswordAuthenticatedKeyAgreementDomain : public KeyAgreementAlgorithm
01525 {
01526 public:
01527
01528 virtual bool ValidateDomainParameters(RandomNumberGenerator &rng) const
01529 {return GetCryptoParameters().Validate(rng, 2);}
01530
01531 virtual unsigned int GetPasswordVerifierLength(const byte *password, unsigned int passwordLength) const =0;
01532 virtual void GeneratePasswordVerifier(RandomNumberGenerator &rng, const byte *userId, unsigned int userIdLength, const byte *password, unsigned int passwordLength, byte *verifier) const =0;
01533
01534 enum RoleFlags {CLIENT=1, SERVER=2, INITIATOR=4, RESPONDER=8};
01535
01536 virtual bool IsValidRole(unsigned int role) =0;
01537 virtual PasswordAuthenticatedKeyAgreementSession * CreateProtocolSession(unsigned int role) const =0;
01538 };
01539 #endif
01540
01541
01542 class CRYPTOPP_DLL BERDecodeErr : public InvalidArgument
01543 {
01544 public:
01545 BERDecodeErr() : InvalidArgument("BER decode error") {}
01546 BERDecodeErr(const std::string &s) : InvalidArgument(s) {}
01547 };
01548
01549
01550 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ASN1Object
01551 {
01552 public:
01553 virtual ~ASN1Object() {}
01554
01555 virtual void BERDecode(BufferedTransformation &bt) =0;
01556
01557 virtual void DEREncode(BufferedTransformation &bt) const =0;
01558
01559
01560 virtual void BEREncode(BufferedTransformation &bt) const {DEREncode(bt);}
01561 };
01562
01563 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
01564 typedef PK_SignatureScheme PK_SignatureSystem;
01565 typedef SimpleKeyAgreementDomain PK_SimpleKeyAgreementDomain;
01566 typedef AuthenticatedKeyAgreementDomain PK_AuthenticatedKeyAgreementDomain;
01567 #endif
01568
01569 NAMESPACE_END
01570
01571 #endif