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