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