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