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