Skip to content

Commit

Permalink
added support for using encoding parameters and key derivation parame…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
weidai11 committed Jul 16, 2003
1 parent 8cd6a92 commit 38b49e4
Show file tree
Hide file tree
Showing 29 changed files with 406 additions and 396 deletions.
4 changes: 3 additions & 1 deletion Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,6 @@ History
- fixed a number of compiler warnings, minor bugs, and portability problems
- removed Sapphire

5.2 - Merged in changes for 5.01 - 5.04
5.2 - Merged in changes for 5.01 - 5.0.4
- added support for using encoding parameters and key derivation parameters
with public key encryption (implemented by OAEP and DLIES)
27 changes: 27 additions & 0 deletions algparam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ NAMESPACE_BEGIN(CryptoPP)

bool (*AssignIntToInteger)(const std::type_info &valueType, void *pInteger, const void *pInt) = NULL;

bool CombinedNameValuePairs::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
if (strcmp(name, "ValueNames") == 0)
return m_pairs1.GetVoidValue(name, valueType, pValue) && m_pairs2.GetVoidValue(name, valueType, pValue);
else
return m_pairs1.GetVoidValue(name, valueType, pValue) || m_pairs2.GetVoidValue(name, valueType, pValue);
}

bool AlgorithmParametersBase::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
if (strcmp(name, "ValueNames") == 0)
{
ThrowIfTypeMismatch(name, typeid(std::string), valueType);
GetParent().GetVoidValue(name, valueType, pValue);
(*reinterpret_cast<std::string *>(pValue) += m_name) += ";";
return true;
}
else if (strcmp(name, m_name) == 0)
{
AssignValue(name, valueType, pValue);
m_used = true;
return true;
}
else
return GetParent().GetVoidValue(name, valueType, pValue);
}

NAMESPACE_END

#endif
132 changes: 77 additions & 55 deletions algparam.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,9 @@ class CombinedNameValuePairs : public NameValuePairs
CombinedNameValuePairs(const NameValuePairs &pairs1, const NameValuePairs &pairs2)
: m_pairs1(pairs1), m_pairs2(pairs2) {}

bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
if (strcmp(name, "ValueNames") == 0)
return m_pairs1.GetVoidValue(name, valueType, pValue) && m_pairs2.GetVoidValue(name, valueType, pValue);
else
return m_pairs1.GetVoidValue(name, valueType, pValue) || m_pairs2.GetVoidValue(name, valueType, pValue);
}
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;

private:
const NameValuePairs &m_pairs1, &m_pairs2;
};

Expand Down Expand Up @@ -247,73 +242,100 @@ CRYPTOPP_DLL extern bool (*AssignIntToInteger)(const std::type_info &valueType,

CRYPTOPP_DLL const std::type_info & IntegerTypeId();

template <class BASE, class T>
class AlgorithmParameters : public NameValuePairs
class CRYPTOPP_DLL AlgorithmParametersBase : public NameValuePairs
{
public:
AlgorithmParameters(const BASE &base, const char *name, const T &value)
: m_base(base), m_name(name), m_value(value)
#ifndef NDEBUG
, m_used(false)
class ParameterNotUsed : public Exception
{
public:
ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
};

AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
: m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}

~AlgorithmParametersBase()
{
#ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
if (!std::uncaught_exception())
#else
try
#endif
{}
{
if (m_throwIfNotUsed && !m_used)
throw ParameterNotUsed(m_name);
}
#ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
catch(...)
{
}
#endif
}

#ifndef NDEBUG
AlgorithmParameters(const AlgorithmParameters &copy)
: m_base(copy.m_base), m_name(copy.m_name), m_value(copy.m_value), m_used(false)
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;

protected:
virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
virtual const NameValuePairs & GetParent() const =0;

bool m_throwIfNotUsed;
mutable bool m_used;
const char *m_name;
};

template <class T>
class AlgorithmParametersBase2 : public AlgorithmParametersBase
{
public:
AlgorithmParametersBase2(const char *name, const T &value, bool throwIfNotUsed) : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value) {}

void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
{
copy.m_used = true;
// special case for retrieving an Integer parameter when an int was passed in
if (!(AssignIntToInteger != NULL && typeid(T) == typeid(int) && AssignIntToInteger(valueType, pValue, &m_value)))
{
ThrowIfTypeMismatch(name, typeid(T), valueType);
*reinterpret_cast<T *>(pValue) = m_value;
}
}

// TODO: revisit after implementing some tracing mechanism, this won't work because of exceptions
// ~AlgorithmParameters() {assert(m_used);} // use assert here because we don't want to throw out of a destructor
#endif
protected:
T m_value;
};

template <class R>
AlgorithmParameters<AlgorithmParameters<BASE,T>, R> operator()(const char *name, const R &value) const
template <class PARENT, class T>
class AlgorithmParameters : public AlgorithmParametersBase2<T>
{
public:
AlgorithmParameters(const PARENT &parent, const char *name, const T &value, bool throwIfNotUsed)
: AlgorithmParametersBase2<T>(name, value, throwIfNotUsed), m_parent(parent)
{}

AlgorithmParameters(const AlgorithmParameters &copy)
: AlgorithmParametersBase2<T>(copy), m_parent(copy.m_parent)
{
return AlgorithmParameters<AlgorithmParameters<BASE,T>, R>(*this, name, value);
copy.m_used = true;
}

bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
template <class R>
AlgorithmParameters<AlgorithmParameters<PARENT,T>, R> operator()(const char *name, const R &value) const
{
if (strcmp(name, "ValueNames") == 0)
{
ThrowIfTypeMismatch(name, typeid(std::string), valueType);
m_base.GetVoidValue(name, valueType, pValue);
(*reinterpret_cast<std::string *>(pValue) += m_name) += ";";
return true;
}
else if (strcmp(name, m_name) == 0)
{
// special case for retrieving an Integer parameter when an int was passed in
if (!(AssignIntToInteger != NULL && typeid(T) == typeid(int) && AssignIntToInteger(valueType, pValue, &m_value)))
{
ThrowIfTypeMismatch(name, typeid(T), valueType);
*reinterpret_cast<T *>(pValue) = m_value;
}
#ifndef NDEBUG
m_used = true;
#endif
return true;
}
else
return m_base.GetVoidValue(name, valueType, pValue);
return AlgorithmParameters<AlgorithmParameters<PARENT,T>, R>(*this, name, value, m_throwIfNotUsed);
}

private:
BASE m_base;
const char *m_name;
T m_value;
#ifndef NDEBUG
mutable bool m_used;
#endif
const NameValuePairs & GetParent() const {return m_parent;}
PARENT m_parent;
};

//! Create an object that implements NameValuePairs for passing parameters
/*! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
\note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
such as MSVC 7.0 and earlier. */
template <class T>
AlgorithmParameters<NullNameValuePairs,T> MakeParameters(const char *name, const T &value)
AlgorithmParameters<NullNameValuePairs,T> MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
{
return AlgorithmParameters<NullNameValuePairs,T>(g_nullNameValuePairs, name, value);
return AlgorithmParameters<NullNameValuePairs,T>(g_nullNameValuePairs, name, value, throwIfNotUsed);
}

#define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
Expand Down
3 changes: 3 additions & 0 deletions argnames.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ CRYPTOPP_DEFINE_NAME_STRING(Cofactor) //!< Integer
CRYPTOPP_DEFINE_NAME_STRING(SubgroupGenerator) //!< Integer, ECP::Point, or EC2N::Point
CRYPTOPP_DEFINE_NAME_STRING(Curve) //!< ECP or EC2N
CRYPTOPP_DEFINE_NAME_STRING(GroupOID) //!< OID
CRYPTOPP_DEFINE_NAME_STRING(PointerToPrimeSelector) //!< const PrimeSelector *
CRYPTOPP_DEFINE_NAME_STRING(Prime1) //!< Integer
CRYPTOPP_DEFINE_NAME_STRING(Prime2) //!< Integer
CRYPTOPP_DEFINE_NAME_STRING(ModPrime1PrivateExponent) //!< Integer
Expand All @@ -52,6 +53,8 @@ CRYPTOPP_DEFINE_NAME_STRING(InputBinaryMode) //!< bool
CRYPTOPP_DEFINE_NAME_STRING(OutputFileName) //!< const char *
CRYPTOPP_DEFINE_NAME_STRING(OutputStreamPointer) //!< std::ostream *
CRYPTOPP_DEFINE_NAME_STRING(OutputBinaryMode) //!< bool
CRYPTOPP_DEFINE_NAME_STRING(EncodingParameters) //!< ConstByteArrayParameter
CRYPTOPP_DEFINE_NAME_STRING(KeyDerivationParameters) //!< ConstByteArrayParameter

DOCUMENTED_NAMESPACE_END

Expand Down
2 changes: 2 additions & 0 deletions basecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ void Grouper::IsolatedInitialize(const NameValuePairs &parameters)
ConstByteArrayParameter separator, terminator;
if (m_groupSize)
parameters.GetRequiredParameter("Grouper", "Separator", separator);
else
parameters.GetValue("Separator", separator);
parameters.GetValue("Terminator", terminator);

m_separator.Assign(separator.begin(), separator.size());
Expand Down
18 changes: 4 additions & 14 deletions channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,11 @@ unsigned int ChannelSwitch::ChannelPut2(const std::string &channel, const byte *
return 0;
}

void ChannelSwitch::ChannelInitialize(const std::string &channel, const NameValuePairs &parameters/* =g_nullNameValuePairs */, int propagation/* =-1 */)
void ChannelSwitch::IsolatedInitialize(const NameValuePairs &parameters/* =g_nullNameValuePairs */)
{
if (channel.empty())
{
m_routeMap.clear();
m_defaultRoutes.clear();
}

m_it.Reset(channel);

while (!m_it.End())
{
m_it.Destination().ChannelInitialize(m_it.Channel(), parameters, propagation);
m_it.Next();
}
m_routeMap.clear();
m_defaultRoutes.clear();
m_blocked = false;
}

bool ChannelSwitch::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking)
Expand Down
3 changes: 2 additions & 1 deletion channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ class CRYPTOPP_DLL ChannelSwitch : public Multichannel<Sink>, public ChannelSwit
AddDefaultRoute(destination, outChannel);
}

void IsolatedInitialize(const NameValuePairs &parameters=g_nullNameValuePairs);

unsigned int ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking);
unsigned int ChannelPutModifiable2(const std::string &channel, byte *begin, unsigned int length, int messageEnd, bool blocking);

void ChannelInitialize(const std::string &channel, const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1);
bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true);
bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true);

Expand Down
4 changes: 4 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ NAMESPACE_END
# pragma warning(disable: 4231 4250 4251 4275 4660 4661 4786 4355)
#endif

#if !(defined(_MSC_VER) && _MSC_VER <= 1300)
#define CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
#endif

// ***************** determine availability of OS features ********************

#ifndef NO_OS_DEPENDENCE
Expand Down
4 changes: 2 additions & 2 deletions cryptdll.dsp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 38b49e4

Please sign in to comment.