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