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