79 #ifndef CRYPTOPP_CRYPTLIB_H
80 #define CRYPTOPP_CRYPTLIB_H
85 NAMESPACE_BEGIN(CryptoPP)
99 template <
typename ENUM_TYPE,
int VALUE>
102 static ENUM_TYPE ToEnum() {
return (ENUM_TYPE)VALUE;}
105 enum ByteOrder {LITTLE_ENDIAN_ORDER = 0, BIG_ENDIAN_ORDER = 1};
131 explicit Exception(ErrorType errorType,
const std::string &s) : m_errorType(errorType), m_what(s) {}
133 const char *what()
const throw() {
return (m_what.c_str());}
134 const std::string &GetWhat()
const {
return m_what;}
135 void SetWhat(
const std::string &s) {m_what = s;}
136 ErrorType GetErrorType()
const {
return m_errorType;}
137 void SetErrorType(ErrorType errorType) {m_errorType = errorType;}
140 ErrorType m_errorType;
183 OS_Error(
ErrorType errorType,
const std::string &s,
const std::string& operation,
int errorCode)
184 :
Exception(errorType, s), m_operation(operation), m_errorCode(errorCode) {}
188 const std::string & GetOperation()
const {
return m_operation;}
190 int GetErrorCode()
const {
return m_errorCode;}
193 std::string m_operation;
200 explicit DecodingResult() : isValidCoding(
false), messageLength(0) {}
201 explicit DecodingResult(
size_t len) : isValidCoding(
true), messageLength(len) {}
203 bool operator==(
const DecodingResult &rhs)
const {
return isValidCoding == rhs.isValidCoding && messageLength == rhs.messageLength;}
204 bool operator!=(
const DecodingResult &rhs)
const {
return !operator==(rhs);}
207 size_t messageLength;
209 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
210 operator size_t()
const {
return isValidCoding ? messageLength : 0;}
234 ValueTypeMismatch(
const std::string &name,
const std::type_info &stored,
const std::type_info &retrieving)
235 :
InvalidArgument(
"NameValuePairs: type mismatch for '" + name +
"', stored '" + stored.name() +
"', trying to retrieve '" + retrieving.name() +
"'")
236 , m_stored(stored), m_retrieving(retrieving) {}
238 const std::type_info & GetStoredTypeInfo()
const {
return m_stored;}
239 const std::type_info & GetRetrievingTypeInfo()
const {
return m_retrieving;}
242 const std::type_info &m_stored;
243 const std::type_info &m_retrieving;
250 return GetValue((std::string(
"ThisObject:")+
typeid(T).name()).c_str(),
object);
257 return GetValue((std::string(
"ThisPointer:")+
typeid(T).name()).c_str(), p);
264 return GetVoidValue(name,
typeid(T), &value);
271 GetValue(name, defaultValue);
277 {std::string result; GetValue(
"ValueNames", result);
return result;}
283 {
return GetValue(name, value);}
287 {
return GetValueWithDefault(name, defaultValue);}
290 CRYPTOPP_DLL
static void CRYPTOPP_API
ThrowIfTypeMismatch(
const char *name,
const std::type_info &stored,
const std::type_info &retrieving)
294 void GetRequiredParameter(
const char *className,
const char *name, T &value)
const
296 if (!GetValue(name, value))
297 throw InvalidArgument(std::string(className) +
": missing required parameter '" + name +
"'");
300 CRYPTOPP_DLL
void GetRequiredIntParameter(
const char *className,
const char *name,
int &value)
const
302 if (!GetIntValue(name, value))
303 throw InvalidArgument(std::string(className) +
": missing required parameter '" + name +
"'");
307 CRYPTOPP_DLL
virtual bool GetVoidValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const =0;
316 DOCUMENTED_NAMESPACE_BEGIN(Name)
318 DOCUMENTED_NAMESPACE_END
329 virtual ~Clonable() {}
341 Algorithm(
bool checkSelfTestStatus =
true);
353 virtual size_t MinKeyLength()
const =0;
355 virtual size_t MaxKeyLength()
const =0;
357 virtual size_t DefaultKeyLength()
const =0;
360 virtual size_t GetValidKeyLength(
size_t n)
const =0;
364 {
return n == GetValidKeyLength(n);}
371 void SetKeyWithRounds(
const byte *key,
size_t length,
int rounds);
374 void SetKeyWithIV(
const byte *key,
size_t length,
const byte *iv,
size_t ivLength);
378 {SetKeyWithIV(key, length, iv, IVSize());}
380 enum IV_Requirement {UNIQUE_IV = 0, RANDOM_IV, UNPREDICTABLE_RANDOM_IV, INTERNALLY_GENERATED_IV, NOT_RESYNCHRONIZABLE};
382 virtual IV_Requirement IVRequirement()
const =0;
394 virtual unsigned int IVSize()
const {
throw NotImplemented(GetAlgorithm().AlgorithmName() +
": this object doesn't support resynchronization");}
402 virtual void Resynchronize(
const byte *iv,
int ivLength=-1) {
throw NotImplemented(GetAlgorithm().AlgorithmName() +
": this object doesn't support resynchronization");}
410 virtual const Algorithm & GetAlgorithm()
const =0;
411 virtual void UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms) =0;
413 void ThrowIfInvalidKeyLength(
size_t length);
414 void ThrowIfResynchronizable();
415 void ThrowIfInvalidIV(
const byte *iv);
416 size_t ThrowIfInvalidIVLength(
int size);
417 const byte * GetIVAndThrowIfInvalid(
const NameValuePairs ¶ms,
size_t &size);
418 inline void AssertValidKeyLength(
size_t length)
const
419 {assert(IsValidKeyLength(length));}
433 virtual void ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock, byte *outBlock)
const =0;
438 {ProcessAndXorBlock(inBlock, NULL, outBlock);}
442 {ProcessAndXorBlock(inoutBlock, NULL, inoutBlock);}
445 virtual unsigned int BlockSize()
const =0;
448 virtual unsigned int OptimalDataAlignment()
const;
454 virtual bool IsForwardTransformation()
const =0;
459 enum {BT_InBlockIsCounter=1, BT_DontIncrementInOutPointers=2, BT_XorInput=4, BT_ReverseDirection=8, BT_AllowParallel=16} FlagsForAdvancedProcessBlocks;
463 virtual size_t AdvancedProcessBlocks(
const byte *inBlocks,
const byte *xorBlocks, byte *outBlocks,
size_t length, word32 flags)
const;
465 inline CipherDir GetCipherDirection()
const {
return IsForwardTransformation() ? ENCRYPTION : DECRYPTION;}
486 virtual unsigned int OptimalDataAlignment()
const;
490 virtual void ProcessData(byte *outString,
const byte *inString,
size_t length) =0;
494 virtual void ProcessLastBlock(byte *outString,
const byte *inString,
size_t length);
500 {ProcessData(inoutString, inoutString, length);}
502 inline void ProcessString(byte *outString,
const byte *inString,
size_t length)
503 {ProcessData(outString, inString, length);}
506 {ProcessData(&input, &input, 1);
return input;}
509 virtual bool IsRandomAccess()
const =0;
513 assert(!IsRandomAccess());
514 throw NotImplemented(
"StreamTransformation: this object doesn't support random access");
518 virtual bool IsSelfInverting()
const =0;
520 virtual bool IsForwardTransformation()
const =0;
538 virtual void Update(
const byte *input,
size_t length) =0;
546 {TruncatedFinal(digest, DigestSize());}
550 {TruncatedFinal(NULL, 0);}
553 virtual unsigned int DigestSize()
const =0;
556 unsigned int TagSize()
const {
return DigestSize();}
566 virtual unsigned int OptimalDataAlignment()
const;
570 {Update(input, length); Final(digest);}
576 {
return TruncatedVerify(digest, DigestSize());}
579 virtual bool VerifyDigest(
const byte *digest,
const byte *input,
size_t length)
580 {Update(input, length);
return Verify(digest);}
583 virtual void TruncatedFinal(byte *digest,
size_t digestSize) =0;
587 {Update(input, length); TruncatedFinal(digest, digestSize);}
590 virtual bool TruncatedVerify(
const byte *digest,
size_t digestLength);
594 {Update(input, length);
return TruncatedVerify(digest, digestLength);}
597 void ThrowIfInvalidTruncatedSize(
size_t size)
const;
607 const Algorithm & GetAlgorithm()
const {
return *
this;}
614 const Algorithm & GetAlgorithm()
const {
return *
this;}
621 const Algorithm & GetAlgorithm()
const {
return *
this;}
634 explicit BadState(
const std::string &name,
const char *message) :
Exception(OTHER_ERROR, name +
": " + message) {}
635 explicit BadState(
const std::string &name,
const char *
function,
const char *state) :
Exception(OTHER_ERROR, name +
": " +
function +
" was called before " + state) {}
639 virtual lword MaxHeaderLength()
const =0;
641 virtual lword MaxMessageLength()
const =0;
648 void SpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength=0);
650 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);
652 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);
659 virtual void UncheckedSpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength) {}
662 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
679 virtual byte GenerateByte();
683 virtual unsigned int GenerateBit();
686 virtual word32 GenerateWord32(word32 a=0, word32 b=0xffffffffL);
689 virtual void GenerateBlock(byte *output,
size_t size);
692 virtual void DiscardBytes(
size_t n);
695 virtual void GenerateIntoBufferedTransformation(
BufferedTransformation &target,
const std::string &channel, lword length);
698 template <
class IT>
void Shuffle(IT begin, IT end)
700 for (; begin != end; ++begin)
701 std::iter_swap(begin, begin + GenerateWord32(0, end-begin-1));
704 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
705 byte GetByte() {
return GenerateByte();}
706 unsigned int GetBit() {
return GenerateBit();}
707 word32 GetLong(word32 a=0, word32 b=0xffffffffL) {
return GenerateWord32(a, b);}
708 word16 GetShort(word16 a=0, word16 b=0xffff) {
return (word16)GenerateWord32(a, b);}
709 void GetBlock(byte *output,
size_t size) {GenerateBlock(output, size);}
727 virtual unsigned int GetMaxWaitObjectCount()
const =0;
736 bool Wait(
unsigned long milliseconds,
CallStack const& callStack);
775 static const std::string &NULL_CHANNEL;
785 size_t Put(byte inByte,
bool blocking=
true)
786 {
return Put(&inByte, 1, blocking);}
788 size_t Put(
const byte *inString,
size_t length,
bool blocking=
true)
789 {
return Put2(inString, length, 0, blocking);}
792 size_t PutWord16(word16 value, ByteOrder order=BIG_ENDIAN_ORDER,
bool blocking=
true);
794 size_t PutWord32(word32 value, ByteOrder order=BIG_ENDIAN_ORDER,
bool blocking=
true);
801 virtual bool CanModifyInput()
const {
return false;}
805 {
return PutModifiable2(inString, length, 0, blocking);}
807 bool MessageEnd(
int propagation=-1,
bool blocking=
true)
808 {
return !!Put2(NULL, 0, propagation < 0 ? -1 : propagation+1, blocking);}
809 size_t PutMessageEnd(
const byte *inString,
size_t length,
int propagation=-1,
bool blocking=
true)
810 {
return Put2(inString, length, propagation < 0 ? -1 : propagation+1, blocking);}
814 virtual size_t Put2(
const byte *inString,
size_t length,
int messageEnd,
bool blocking) =0;
817 virtual size_t PutModifiable2(byte *inString,
size_t length,
int messageEnd,
bool blocking)
818 {
return Put2(inString, length, messageEnd, blocking);}
833 virtual void IsolatedInitialize(
const NameValuePairs ¶meters) {
throw NotImplemented(
"BufferedTransformation: this object can't be reinitialized");}
834 virtual bool IsolatedFlush(
bool hardFlush,
bool blocking) =0;
835 virtual bool IsolatedMessageSeriesEnd(
bool blocking) {
return false;}
850 virtual bool Flush(
bool hardFlush,
int propagation=-1,
bool blocking=
true);
853 virtual bool MessageSeriesEnd(
int propagation=-1,
bool blocking=
true);
860 virtual int GetAutoSignalPropagation()
const {
return 0;}
863 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
864 void Close() {MessageEnd();}
874 virtual lword MaxRetrievable()
const;
877 virtual bool AnyRetrievable()
const;
880 virtual size_t Get(byte &outByte);
882 virtual size_t Get(byte *outString,
size_t getMax);
885 virtual size_t Peek(byte &outByte)
const;
887 virtual size_t Peek(byte *outString,
size_t peekMax)
const;
890 size_t GetWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER);
892 size_t GetWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER);
895 size_t PeekWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER)
const;
897 size_t PeekWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER)
const;
901 {TransferTo2(target, transferMax, channel);
return transferMax;}
904 virtual lword Skip(lword skipMax=LWORD_MAX);
908 {
return CopyRangeTo(target, 0, copyMax, channel);}
912 {lword i = position; CopyRangeTo2(target, i, i+copyMax, channel);
return i-position;}
914 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
915 unsigned long MaxRetrieveable()
const {
return MaxRetrievable();}
922 virtual lword TotalBytesRetrievable()
const;
924 virtual unsigned int NumberOfMessages()
const;
926 virtual bool AnyMessages()
const;
932 virtual bool GetNextMessage();
934 virtual unsigned int SkipMessages(
unsigned int count=UINT_MAX);
936 unsigned int TransferMessagesTo(
BufferedTransformation &target,
unsigned int count=UINT_MAX,
const std::string &channel=DEFAULT_CHANNEL)
937 {TransferMessagesTo2(target, count, channel);
return count;}
939 unsigned int CopyMessagesTo(
BufferedTransformation &target,
unsigned int count=UINT_MAX,
const std::string &channel=DEFAULT_CHANNEL)
const;
942 virtual void SkipAll();
945 {TransferAllTo2(target, channel);}
949 virtual bool GetNextMessageSeries() {
return false;}
950 virtual unsigned int NumberOfMessagesInThisSeries()
const {
return NumberOfMessages();}
951 virtual unsigned int NumberOfMessageSeries()
const {
return 0;}
957 virtual size_t TransferTo2(
BufferedTransformation &target, lword &byteCount,
const std::string &channel=DEFAULT_CHANNEL,
bool blocking=
true) =0;
959 virtual size_t CopyRangeTo2(
BufferedTransformation &target, lword &begin, lword end=LWORD_MAX,
const std::string &channel=DEFAULT_CHANNEL,
bool blocking=
true)
const =0;
961 size_t TransferMessagesTo2(
BufferedTransformation &target,
unsigned int &messageCount,
const std::string &channel=DEFAULT_CHANNEL,
bool blocking=
true);
963 size_t TransferAllTo2(
BufferedTransformation &target,
const std::string &channel=DEFAULT_CHANNEL,
bool blocking=
true);
973 size_t ChannelPut(
const std::string &channel, byte inByte,
bool blocking=
true)
974 {
return ChannelPut(channel, &inByte, 1, blocking);}
975 size_t ChannelPut(
const std::string &channel,
const byte *inString,
size_t length,
bool blocking=
true)
976 {
return ChannelPut2(channel, inString, length, 0, blocking);}
978 size_t ChannelPutModifiable(
const std::string &channel, byte *inString,
size_t length,
bool blocking=
true)
979 {
return ChannelPutModifiable2(channel, inString, length, 0, blocking);}
981 size_t ChannelPutWord16(
const std::string &channel, word16 value, ByteOrder order=BIG_ENDIAN_ORDER,
bool blocking=
true);
982 size_t ChannelPutWord32(
const std::string &channel, word32 value, ByteOrder order=BIG_ENDIAN_ORDER,
bool blocking=
true);
984 bool ChannelMessageEnd(
const std::string &channel,
int propagation=-1,
bool blocking=
true)
985 {
return !!ChannelPut2(channel, NULL, 0, propagation < 0 ? -1 : propagation+1, blocking);}
986 size_t ChannelPutMessageEnd(
const std::string &channel,
const byte *inString,
size_t length,
int propagation=-1,
bool blocking=
true)
987 {
return ChannelPut2(channel, inString, length, propagation < 0 ? -1 : propagation+1, blocking);}
989 virtual byte * ChannelCreatePutSpace(
const std::string &channel,
size_t &size);
991 virtual size_t ChannelPut2(
const std::string &channel,
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
992 virtual size_t ChannelPutModifiable2(
const std::string &channel, byte *begin,
size_t length,
int messageEnd,
bool blocking);
994 virtual bool ChannelFlush(
const std::string &channel,
bool hardFlush,
int propagation=-1,
bool blocking=
true);
995 virtual bool ChannelMessageSeriesEnd(
const std::string &channel,
int propagation=-1,
bool blocking=
true);
997 virtual void SetRetrievalChannel(
const std::string &channel);
1017 {assert(!Attachable());
throw NotImplemented(
"BufferedTransformation: this object is not attachable");}
1023 static int DecrementPropagation(
int propagation)
1024 {
return propagation != 0 ? propagation - 1 : 0;}
1060 {
if (!Validate(rng, level))
throw InvalidMaterial(
"CryptoMaterial: this object contains invalid values");}
1066 {
throw NotImplemented(
"CryptoMaterial: this object does not support saving");}
1073 {
throw NotImplemented(
"CryptoMaterial: this object does not support loading");}
1082 {assert(!SupportsPrecomputation());
throw NotImplemented(
"CryptoMaterial: this object does not support precomputation");}
1085 {assert(!SupportsPrecomputation());
throw NotImplemented(
"CryptoMaterial: this object does not support precomputation");}
1088 {assert(!SupportsPrecomputation());
throw NotImplemented(
"CryptoMaterial: this object does not support precomputation");}
1091 void DoQuickSanityCheck()
const {ThrowIfInvalid(
NullRNG(), 0);}
1093 #if (defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590)
1095 char m_sunCCworkaround;
1108 {
throw NotImplemented(
"GeneratableCryptoMaterial: this object does not support key/parameter generation");}
1144 {AccessMaterial().Load(bt);}
1147 {GetMaterial().Save(bt);}
1159 virtual PublicKey & AccessPublicKey() =0;
1199 virtual size_t MaxPlaintextLength(
size_t ciphertextLength)
const =0;
1203 virtual size_t CiphertextLength(
size_t plaintextLength)
const =0;
1207 virtual bool ParameterSupported(
const char *name)
const =0;
1217 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
1218 size_t MaxPlainTextLength(
size_t cipherTextLength)
const {
return MaxPlaintextLength(cipherTextLength);}
1219 size_t CipherTextLength(
size_t plainTextLength)
const {
return CiphertextLength(plainTextLength);}
1239 const byte *plaintext,
size_t plaintextLength,
1260 const byte *ciphertext,
size_t ciphertextLength,
1274 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
1339 {
throw NotImplemented(
"PK_MessageAccumulator: DigestSize() should not be called");}
1342 {
throw NotImplemented(
"PK_MessageAccumulator: TruncatedFinal() should not be called");}
1353 virtual void InputRecoverableMessage(
PK_MessageAccumulator &messageAccumulator,
const byte *recoverableMessage,
size_t recoverableMessageLength)
const =0;
1371 virtual size_t SignMessage(
RandomNumberGenerator &rng,
const byte *message,
size_t messageLen, byte *signature)
const;
1377 virtual size_t SignMessageWithRecovery(
RandomNumberGenerator &rng,
const byte *recoverableMessage,
size_t recoverableMessageLength,
1378 const byte *nonrecoverableMessage,
size_t nonrecoverableMessageLength, byte *signature)
const;
1395 virtual void InputSignature(
PK_MessageAccumulator &messageAccumulator,
const byte *signature,
size_t signatureLength)
const =0;
1404 virtual bool VerifyMessage(
const byte *message,
size_t messageLen,
1405 const byte *signature,
size_t signatureLength)
const;
1421 const byte *nonrecoverableMessage,
size_t nonrecoverableMessageLength,
1422 const byte *signature,
size_t signatureLength)
const;
1435 virtual unsigned int AgreedValueLength()
const =0;
1437 virtual unsigned int PrivateKeyLength()
const =0;
1439 virtual unsigned int PublicKeyLength()
const =0;
1445 virtual void GeneratePublicKey(
RandomNumberGenerator &rng,
const byte *privateKey, byte *publicKey)
const =0;
1455 virtual bool Agree(byte *agreedValue,
const byte *privateKey,
const byte *otherPublicKey,
bool validateOtherPublicKey=
true)
const =0;
1457 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
1459 {
return GetCryptoParameters().Validate(rng, 2);}
1473 virtual unsigned int AgreedValueLength()
const =0;
1476 virtual unsigned int StaticPrivateKeyLength()
const =0;
1478 virtual unsigned int StaticPublicKeyLength()
const =0;
1484 virtual void GenerateStaticPublicKey(
RandomNumberGenerator &rng,
const byte *privateKey, byte *publicKey)
const =0;
1487 virtual void GenerateStaticKeyPair(
RandomNumberGenerator &rng, byte *privateKey, byte *publicKey)
const;
1490 virtual unsigned int EphemeralPrivateKeyLength()
const =0;
1492 virtual unsigned int EphemeralPublicKeyLength()
const =0;
1498 virtual void GenerateEphemeralPublicKey(
RandomNumberGenerator &rng,
const byte *privateKey, byte *publicKey)
const =0;
1501 virtual void GenerateEphemeralKeyPair(
RandomNumberGenerator &rng, byte *privateKey, byte *publicKey)
const;
1512 virtual bool Agree(byte *agreedValue,
1513 const byte *staticPrivateKey,
const byte *ephemeralPrivateKey,
1514 const byte *staticOtherPublicKey,
const byte *ephemeralOtherPublicKey,
1515 bool validateStaticOtherPublicKey=
true)
const =0;
1517 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
1519 {
return GetCryptoParameters().Validate(rng, 2);}
1546 class ProtocolSession
1553 ProtocolError(ErrorType errorType,
const std::string &s) :
Exception(errorType, s) {}
1558 class UnexpectedMethodCall :
public Exception
1561 UnexpectedMethodCall(
const std::string &s) :
Exception(OTHER_ERROR, s) {}
1564 ProtocolSession() : m_rng(NULL), m_throwOnProtocolError(true), m_validState(false) {}
1565 virtual ~ProtocolSession() {}
1569 bool GetThrowOnProtocolError()
const {
return m_throwOnProtocolError;}
1570 void SetThrowOnProtocolError(
bool throwOnProtocolError) {m_throwOnProtocolError = throwOnProtocolError;}
1572 bool HasValidState()
const {
return m_validState;}
1574 virtual bool OutgoingMessageAvailable()
const =0;
1575 virtual unsigned int GetOutgoingMessageLength()
const =0;
1576 virtual void GetOutgoingMessage(byte *message) =0;
1578 virtual bool LastMessageProcessed()
const =0;
1579 virtual void ProcessIncomingMessage(
const byte *message,
unsigned int messageLength) =0;
1583 void CheckAndHandleInvalidState()
const;
1584 void SetValidState(
bool valid) {m_validState = valid;}
1589 bool m_throwOnProtocolError, m_validState;
1592 class KeyAgreementSession :
public ProtocolSession
1595 virtual unsigned int GetAgreedValueLength()
const =0;
1596 virtual void GetAgreedValue(byte *agreedValue)
const =0;
1599 class PasswordAuthenticatedKeyAgreementSession :
public KeyAgreementSession
1603 const byte *myId,
unsigned int myIdLength,
1604 const byte *counterPartyId,
unsigned int counterPartyIdLength,
1605 const byte *passwordOrVerifier,
unsigned int passwordOrVerifierLength);
1613 {
return GetCryptoParameters().Validate(rng, 2);}
1615 virtual unsigned int GetPasswordVerifierLength(
const byte *password,
unsigned int passwordLength)
const =0;
1616 virtual void GeneratePasswordVerifier(
RandomNumberGenerator &rng,
const byte *userId,
unsigned int userIdLength,
const byte *password,
unsigned int passwordLength, byte *verifier)
const =0;
1618 enum RoleFlags {CLIENT=1, SERVER=2, INITIATOR=4, RESPONDER=8};
1620 virtual bool IsValidRole(
unsigned int role) =0;
1621 virtual PasswordAuthenticatedKeyAgreementSession * CreateProtocolSession(
unsigned int role)
const =0;
1647 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
base class for all exceptions thrown by Crypto++
exception thrown when invalid crypto material is detected
exception thrown when an invalid argument is detected
void SetKeyWithIV(const byte *key, size_t length, const byte *iv)
calls SetKey() with an NameValuePairs object that just specifies "IV"
bool GetThisObject(T &object) const
get a copy of this object or a subobject of it
bool CanUseRandomIVs() const
returns whether this object can use random IVs (in addition to ones returned by GetNextIV) ...
interface for message authentication codes
container of wait objects
virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const
save precomputation for later use
interface for asymmetric algorithms
interface for public-key encryptors and decryptors
error reported by the operating system
interface for for one direction (encryption or decryption) of a stream cipher or block cipher mode wi...
T GetValueWithDefault(const char *name, T defaultValue) const
get a named value, returns the default if the name doesn't exist
virtual void Load(BufferedTransformation &bt)
load key from a BufferedTransformation
virtual bool NeedsPrespecifiedDataLengths() const
if this function returns true, SpecifyDataLengths() must be called before attempting to input data ...
this indicates that a member function was called in the wrong state, for example trying to encrypt a ...
interface for public-key signers
virtual void ThrowIfInvalid(RandomNumberGenerator &rng, unsigned int level) const
throws InvalidMaterial if this object fails Validate() test
interface for public-key encryptors
CipherDir
used to specify a direction for a cipher to operate in (encrypt or decrypt)
void BERDecode(BufferedTransformation &bt)
for backwards compatibility, calls AccessMaterial().Load(bt)
exception thrown by a class when Flush(true) is called but it can't completely flush its buffers ...
virtual void Save(BufferedTransformation &bt) const
save key into a BufferedTransformation
exception thrown when trying to retrieve a value using a different type than expected ...
CryptoMaterial & AccessMaterial()
returns a reference to the crypto material used by this object
interface for asymmetric algorithms using private keys
BufferedTransformation & TheBitBucket()
returns a reference to a BufferedTransformation object that discards all input
virtual bool IsProbabilistic() const =0
requires a random number generator to sign
virtual bool SupportsPrecomputation() const
virtual bool AllowNonrecoverablePart() const =0
whether or not a non-recoverable message part can be signed
virtual bool CanIncorporateEntropy() const
returns true if IncorporateEntropy is implemented
std::string GetValueNames() const
get a list of value names that can be retrieved
interface for random number generators
virtual size_t SignatureLength() const =0
signature length if it only depends on the key, otherwise 0
virtual lword MaxFooterLength() const
the maximum length of AAD that can be input after the encrypted data
interface for private keys
interface for cloning objects, this is not implemented by most classes yet
virtual unsigned int MinIVLength() const
returns minimal length of IVs accepted by this object
const CryptoMaterial & GetMaterial() const
returns a const reference to the crypto material used by this object
bool GetThisPointer(T *&p) const
get a pointer to this object, as a pointer to T
bool GetIntValue(const char *name, int &value) const
get a named value with type int
virtual bool IsValidKeyLength(size_t n) const
returns whether n is a valid key length
data integerity check (such as CRC or MAC) failed
interface for one direction (encryption or decryption) of a block cipher
interface for objects that you can wait for
virtual size_t MaxRecoverableLength() const =0
length of longest message that can be recovered, or 0 if this signature scheme does not support messa...
interface for domains of simple key agreement protocols
virtual unsigned int GetMaxWaitObjectCount() const =0
maximum number of wait objects that this object can return
used to return decoding results
virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation)
retrieve previously saved precomputation
Algorithm(bool checkSelfTestStatus=true)
exception thrown when trying to encrypt plaintext of invalid length
bool CanUsePredictableIVs() const
returns whether this object can use random but possibly predictable IVs (in addition to ones returned...
DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *ciphertext, byte *plaintext, const NameValuePairs ¶meters=g_nullNameValuePairs) const
decrypt a fixed size ciphertext
received input data that doesn't conform to expected format
bool GetValue(const char *name, T &value) const
get a named value, returns true if the name exists
interface for public-key decryptors
virtual void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)=0
put wait objects into container
int GetIntValueWithDefault(const char *name, int defaultValue) const
get a named value with type int, with default
exception thrown by a class if a non-implemented method is called
key too short exception, may be thrown by any function in this class if the private or public key is ...
virtual size_t FixedCiphertextLength() const
return fixed ciphertext length, if one exists, otherwise return 0
const std::string DEFAULT_CHANNEL
the default channel for BufferedTransformation, equal to the empty string
bool IsResynchronizable() const
returns whether this object can be resynchronized (i.e. supports initialization vectors) ...
const CryptoMaterial & GetMaterial() const
returns a const reference to the crypto material used by this object
interface for encoding and decoding ASN1 objects
virtual void Resynchronize(const byte *iv, int ivLength=-1)
resynchronize with an IV. ivLength=-1 means use IVSize()
virtual size_t MaxSignatureLength(size_t recoverablePartLength=0) const
maximum signature length produced for a given length of recoverable message part
bool CanUseStructuredIVs() const
returns whether this object can use structured IVs, for example a counter (in addition to ones return...
interface for one direction (encryption or decryption) of a stream cipher or cipher mode ...
multiple precision integer and basic arithmetics
invalid key exception, may be thrown by any function in this class if the private or public key has a...
keying interface for crypto algorithms that take byte strings as keys
virtual std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
interface for asymmetric algorithms using public keys
virtual size_t FixedMaxPlaintextLength() const
return maximum plaintext length given the fixed ciphertext length, if one exists, otherwise return 0 ...
const CryptoMaterial & GetMaterial() const
returns a const reference to the crypto material used by this object
virtual size_t MaxRecoverableLengthFromSignatureLength(size_t signatureLength) const =0
length of longest message that can be recovered from a signature of given length, or 0 if this signat...
const NameValuePairs & g_nullNameValuePairs
empty set of name-value pairs
unsigned int DigestSize() const
should not be called on PK_MessageAccumulator
interface for public-key signers and verifiers
RandomNumberGenerator & NullRNG()
returns a reference that can be passed to functions that ask for a RNG but doesn't actually use it ...
const unsigned long INFINITE_TIME
used to represent infinite time
virtual unsigned int MaxIVLength() const
returns maximal length of IVs accepted by this object
interface for all crypto algorithms
unsigned int DefaultIVLength() const
returns default length of IVs accepted by this object
virtual void BEREncode(BufferedTransformation &bt) const
encode this object into a BufferedTransformation, using BER
interface for accumulating messages to be signed or verified
virtual Clonable * Clone() const
this is not implemented by most classes yet
exception thrown by decryption filters when trying to decrypt an invalid ciphertext ...
interface for key agreement algorithms
static void ThrowIfTypeMismatch(const char *name, const std::type_info &stored, const std::type_info &retrieving)
used by derived classes to check for type mismatch
CryptoMaterial & AccessMaterial()
returns a reference to the crypto material used by this object
interface for public-key signature verifiers
void Shuffle(IT begin, IT end)
randomly shuffle the specified array, resulting permutation is uniformly distributed ...
virtual bool SignatureUpfront() const
if this function returns true, during verification you must input the signature before the message...
interface for crypto material, such as public and private keys, and crypto parameters ...
virtual void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms=g_nullNameValuePairs)
generate a random key or crypto parameters
void DEREncode(BufferedTransformation &bt) const
for backwards compatibility, calls GetMaterial().Save(bt)
CryptoMaterial & AccessMaterial()
returns a reference to the crypto material used by this object
invalid function argument
interface for generatable crypto material, such as private keys and crypto parameters ...
virtual bool RecoverablePartFirst() const =0
whether you must input the recoverable part before the non-recoverable part during signing ...
interface for crypto prameters
BufferedTransformation received a Flush(true) signal but can't flush buffers.
interface for public keys
interface for domains of authenticated key agreement protocols
void TruncatedFinal(byte *digest, size_t digestSize)
should not be called on PK_MessageAccumulator
a method is not implemented
const std::string AAD_CHANNEL
channel for additional authenticated data, equal to "AAD"
error reading from input device or writing to output device
virtual void Precompute(unsigned int n)
do precomputation
virtual void IncorporateEntropy(const byte *input, size_t length)
update RNG state with additional unpredictable values
interface for retrieving values given their names
BER Decode Exception Class, may be thrown during an ASN1 BER decode operation.