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