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