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