luc.h
Go to the documentation of this file.
// luc.h - originally written and placed in the public domain by Wei Dai
/// \file luc.h
/// \brief Classes for the LUC cryptosystem
/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
/// defined later in this .h file may be of more practical interest.
/// \since Crypto++ 2.1
#ifndef CRYPTOPP_LUC_H
#define CRYPTOPP_LUC_H
#include "cryptlib.h"
#include "gfpcrypt.h"
#include "integer.h"
#include "algebra.h"
#include "secblock.h"
#if CRYPTOPP_MSC_VERSION
# pragma warning(push)
# pragma warning(disable: 4127 4189)
#endif
#include "pkcspad.h"
#include "integer.h"
#include "oaep.h"
#include "dh.h"
#include <limits.h>
NAMESPACE_BEGIN(CryptoPP)
/// \brief The LUC function.
/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
/// defined later in this .h file may be of more practical interest.
/// \since Crypto++ 2.1
class LUCFunction : public TrapdoorFunction, public PublicKey
{
typedef LUCFunction ThisClass;
public:
virtual ~LUCFunction() {}
/// \brief Initialize a LUC public key with {n,e}
/// \param n the modulus
/// \param e the public exponent
void Initialize(const Integer &n, const Integer &e)
{m_n = n; m_e = e;}
void BERDecode(BufferedTransformation &bt);
void DEREncode(BufferedTransformation &bt) const;
Integer ApplyFunction(const Integer &x) const;
Integer PreimageBound() const {return m_n;}
Integer ImageBound() const {return m_n;}
bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
void AssignFrom(const NameValuePairs &source);
// non-derived interface
const Integer & GetModulus() const {return m_n;}
const Integer & GetPublicExponent() const {return m_e;}
void SetModulus(const Integer &n) {m_n = n;}
void SetPublicExponent(const Integer &e) {m_e = e;}
protected:
Integer m_n, m_e;
};
/// \brief The LUC inverse function.
/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
/// defined later in this .h file may be of more practical interest.
/// \since Crypto++ 2.1
class InvertibleLUCFunction : public LUCFunction, public TrapdoorFunctionInverse, public PrivateKey
{
typedef InvertibleLUCFunction ThisClass;
public:
virtual ~InvertibleLUCFunction() {}
/// \brief Create a LUC private key
/// \param rng a RandomNumberGenerator derived class
/// \param modulusBits the size of the modulus, in bits
/// \param eStart the desired starting public exponent
/// \details Initialize() creates a new keypair using a starting public exponent of 17.
/// \details This function overload of Initialize() creates a new keypair because it
/// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
/// then use one of the other Initialize() overloads.
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17);
/// \brief Initialize a LUC private key with {n,e,p,q,dp,dq,u}
/// \param n modulus
/// \param e public exponent
/// \param p first prime factor
/// \param q second prime factor
/// \param u q<sup>-1</sup> mod p
/// \details This Initialize() function overload initializes a private key from existing parameters.
void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
{m_n = n; m_e = e; m_p = p; m_q = q; m_u = u;}
void BERDecode(BufferedTransformation &bt);
void DEREncode(BufferedTransformation &bt) const;
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
void AssignFrom(const NameValuePairs &source);
/*! parameters: (ModulusSize, PublicExponent (default 17)) */
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
// non-derived interface
const Integer& GetPrime1() const {return m_p;}
const Integer& GetPrime2() const {return m_q;}
const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
void SetPrime1(const Integer &p) {m_p = p;}
void SetPrime2(const Integer &q) {m_q = q;}
void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
protected:
Integer m_p, m_q, m_u;
};
/// \brief LUC cryptosystem
/// \since Crypto++ 2.1
struct LUC
{
static std::string StaticAlgorithmName() {return "LUC";}
typedef LUCFunction PublicKey;
typedef InvertibleLUCFunction PrivateKey;
};
/// \brief LUC encryption scheme
/// \tparam STANDARD signature standard
/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
/// defined later in this .h file may be of more practical interest.
/// \since Crypto++ 2.1
template <class STANDARD>
struct LUCES : public TF_ES<LUC, STANDARD>
{
};
/// \brief LUC signature scheme with appendix
/// \tparam STANDARD signature standard
/// \tparam H hash transformation
/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
/// defined later in this .h file may be of more practical interest.
/// \since Crypto++ 2.1
template <class STANDARD, class H>
struct LUCSS : public TF_SS<LUC, STANDARD, H>
{
};
// analogous to the RSA schemes defined in PKCS #1 v2.0
typedef LUCES<OAEP<SHA1> >::Decryptor LUCES_OAEP_SHA_Decryptor;
typedef LUCES<OAEP<SHA1> >::Encryptor LUCES_OAEP_SHA_Encryptor;
typedef LUCSS<PKCS1v15, SHA1>::Signer LUCSSA_PKCS1v15_SHA_Signer;
typedef LUCSS<PKCS1v15, SHA1>::Verifier LUCSSA_PKCS1v15_SHA_Verifier;
// ********************************************************
/// \brief LUC GroupParameters precomputation
/// \details No actual precomputation is performed
/// \since Crypto++ 2.1
class DL_GroupPrecomputation_LUC : public DL_GroupPrecomputation<Integer>
{
public:
virtual ~DL_GroupPrecomputation_LUC() {}
const AbstractGroup<Element> & GetGroup() const {CRYPTOPP_ASSERT(false); throw 0;}
Element BERDecodeElement(BufferedTransformation &bt) const {return Integer(bt);}
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {v.DEREncode(bt);}
// non-inherited
void SetModulus(const Integer &v) {m_p = v;}
const Integer & GetModulus() const {return m_p;}
private:
Integer m_p;
};
/// \brief LUC Precomputation
/// \since Crypto++ 2.1
class DL_BasePrecomputation_LUC : public DL_FixedBasePrecomputation<Integer>
{
public:
virtual ~DL_BasePrecomputation_LUC() {}
// DL_FixedBasePrecomputation
bool IsInitialized() const {return m_g.NotZero();}
void SetBase(const DL_GroupPrecomputation<Element> &group, const Integer &base)
{CRYPTOPP_UNUSED(group); m_g = base;}
const Integer & GetBase(const DL_GroupPrecomputation<Element> &group) const
{CRYPTOPP_UNUSED(group); return m_g;}
void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
{CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(maxExpBits); CRYPTOPP_UNUSED(storage);}
void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation)
{CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const
{CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
Integer Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
Integer CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Integer> &pc2, const Integer &exponent2) const
{
CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(exponent); CRYPTOPP_UNUSED(pc2); CRYPTOPP_UNUSED(exponent2);
// shouldn't be called
throw NotImplemented("DL_BasePrecomputation_LUC: CascadeExponentiate not implemented");
}
private:
Integer m_g;
};
/// \brief LUC GroupParameters specialization
/// \since Crypto++ 2.1
class DL_GroupParameters_LUC : public DL_GroupParameters_IntegerBasedImpl<DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC>
{
public:
virtual ~DL_GroupParameters_LUC() {}
// DL_GroupParameters
bool IsIdentity(const Integer &element) const {return element == Integer::Two();}
void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
Element MultiplyElements(const Element &a, const Element &b) const
{
CRYPTOPP_UNUSED(a); CRYPTOPP_UNUSED(b);
throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
}
Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
{
CRYPTOPP_UNUSED(element1); CRYPTOPP_UNUSED(exponent1); CRYPTOPP_UNUSED(element2); CRYPTOPP_UNUSED(exponent2);
throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
}
// NameValuePairs interface
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
}
private:
int GetFieldType() const {return 2;}
};
/// \brief GF(p) group parameters that default to safe primes
/// \since Crypto++ 2.1
class DL_GroupParameters_LUC_DefaultSafePrime : public DL_GroupParameters_LUC
{
public:
typedef NoCofactorMultiplication DefaultCofactorOption;
protected:
unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
};
/// \brief LUC HMP signature algorithm
/// \since Crypto++ 2.1
class DL_Algorithm_LUC_HMP : public DL_ElgamalLikeSignatureAlgorithm<Integer>
{
public:
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-HMP";}
virtual ~DL_Algorithm_LUC_HMP() {}
void Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const;
bool Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const;
size_t RLen(const DL_GroupParameters<Integer> &params) const
{return params.GetGroupOrder().ByteCount();}
};
/// \brief LUC signature keys
/// \since Crypto++ 2.1
struct DL_SignatureKeys_LUC
{
typedef DL_GroupParameters_LUC GroupParameters;
typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
};
/// \brief LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster, Markus Michels, Holger Petersen
/// \tparam H hash transformation
/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
/// defined later in this .h file may be of more practical interest.
/// \since Crypto++ 2.1
template <class H>
struct LUC_HMP : public DL_SS<DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, DL_SignatureMessageEncodingMethod_DSA, H>
{
};
/// \brief LUC encryption keys
/// \since Crypto++ 2.1
struct DL_CryptoKeys_LUC
{
typedef DL_GroupParameters_LUC_DefaultSafePrime GroupParameters;
typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
};
/// \brief LUC Integrated Encryption Scheme
/// \tparam COFACTOR_OPTION cofactor multiplication option
/// \tparam HASH HashTransformation derived class used for key drivation and MAC computation
/// \tparam DHAES_MODE flag indicating if the MAC includes additional context parameters such as <em>u·V</em>, <em>v·U</em> and label
/// \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits
/// \sa CofactorMultiplicationOption
/// \since Crypto++ 2.1, Crypto++ 5.7 for Bouncy Castle and Botan compatibility
template <class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS = false>
struct LUC_IES
: public DL_ES<
DL_CryptoKeys_LUC,
DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
LUC_IES<> >
{
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-IES";} // non-standard name
};
// ********************************************************
/// \brief LUC-DH
typedef DH_Domain<DL_GroupParameters_LUC_DefaultSafePrime> LUC_DH;
NAMESPACE_END
#if CRYPTOPP_MSC_VERSION
# pragma warning(pop)
#endif
#endif
algebra.h
Classes for performing mathematics over different fields.
AbstractGroup
Abstract group.
Definition algebra.h:27
BufferedTransformation
Interface for buffered transformations.
Definition cryptlib.h:1657
DH_Domain
Diffie-Hellman domain.
Definition dh.h:26
DL_Algorithm_LUC_HMP
LUC HMP signature algorithm.
Definition luc.h:266
DL_Algorithm_LUC_HMP::Verify
bool Verify(const DL_GroupParameters< Integer > &params, const DL_PublicKey< Integer > &publicKey, const Integer &e, const Integer &r, const Integer &s) const
Verify a message using a public key.
Definition luc.cpp:30
DL_Algorithm_LUC_HMP::RLen
size_t RLen(const DL_GroupParameters< Integer > &params) const
Retrieve R length.
Definition luc.h:275
DL_Algorithm_LUC_HMP::Sign
void Sign(const DL_GroupParameters< Integer > &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
Sign a message using a private key.
Definition luc.cpp:23
Element
DL_BasePrecomputation_LUC
LUC Precomputation.
Definition luc.h:193
DL_BasePrecomputation_LUC::IsInitialized
bool IsInitialized() const
Determines whether this object is initialized.
Definition luc.h:198
DL_ES
Discrete Log (DL) encryption scheme.
Definition pubkey.h:2362
DL_ElgamalLikeSignatureAlgorithm
Interface for Elgamal-like signature algorithms.
Definition pubkey.h:1407
DL_FixedBasePrecomputation
DL_FixedBasePrecomputation interface.
Definition eprecomp.h:61
DL_GroupParameters_IntegerBasedImpl
Integer-based GroupParameters default implementation.
Definition gfpcrypt.h:183
DL_GroupParameters_LUC_DefaultSafePrime
GF(p) group parameters that default to safe primes.
Definition luc.h:255
DL_GroupParameters_LUC
LUC GroupParameters specialization.
Definition luc.h:224
DL_GroupParameters_LUC::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition luc.h:243
DL_GroupParameters_LUC::IsIdentity
bool IsIdentity(const Integer &element) const
Determines if an element is an identity.
Definition luc.h:229
DL_GroupParameters
Interface for Discrete Log (DL) group parameters.
Definition pubkey.h:782
DL_GroupParameters::GetGroupOrder
virtual Integer GetGroupOrder() const
Retrieves the order of the group.
Definition pubkey.h:909
DL_GroupPrecomputation_LUC
LUC GroupParameters precomputation.
Definition luc.h:174
DL_GroupPrecomputation_LUC::GetGroup
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition luc.h:178
DL_GroupPrecomputation_LUC::DEREncodeElement
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition luc.h:180
DL_GroupPrecomputation_LUC::BERDecodeElement
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition luc.h:179
DL_GroupPrecomputation
DL_GroupPrecomputation interface.
Definition eprecomp.h:20
DL_PrivateKey_GFP
Discrete Log (DL) private key in GF(p) groups.
Definition gfpcrypt.h:614
DL_PublicKey_GFP
Discrete Log (DL) public key in GF(p) groups.
Definition gfpcrypt.h:578
DL_PublicKey
Interface for Discrete Log (DL) public keys.
Definition pubkey.h:1059
DL_SS
Discrete Log (DL) signature scheme.
Definition pubkey.h:2342
Integer
Multiple precision integer with arithmetic operations.
Definition integer.h:50
Integer::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Integer::Two
static const Integer & Two()
Integer representing 2.
Integer::ByteCount
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
InvertibleLUCFunction
The LUC inverse function.
Definition luc.h:79
InvertibleLUCFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition luc.cpp:213
InvertibleLUCFunction::Initialize
void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
Initialize a LUC private key with {n,e,p,q,dp,dq,u}.
Definition luc.h:102
InvertibleLUCFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition luc.cpp:222
InvertibleLUCFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition luc.cpp:180
InvertibleLUCFunction::CalculateInverse
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition luc.cpp:172
InvertibleLUCFunction::Initialize
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17)
Create a LUC private key.
Definition luc.cpp:137
InvertibleLUCFunction::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition luc.cpp:114
LUCFunction
The LUC function.
Definition luc.h:39
LUCFunction::PreimageBound
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition luc.h:55
LUCFunction::Initialize
void Initialize(const Integer &n, const Integer &e)
Initialize a LUC public key with {n,e}.
Definition luc.h:48
LUCFunction::ImageBound
Integer ImageBound() const
Returns the maximum size of a representation after the trapdoor function is applied.
Definition luc.h:56
NameValuePairs
Interface for retrieving values given their names.
Definition cryptlib.h:327
NotImplemented
A method was called which was not implemented.
Definition cryptlib.h:238
PrivateKey
Interface for private keys.
Definition cryptlib.h:2546
PublicKey
Interface for public keys.
Definition cryptlib.h:2541
RandomNumberGenerator
Interface for random number generators.
Definition cryptlib.h:1440
TF_ES
Trapdoor Function (TF) encryption scheme.
Definition pubkey.h:2290
TF_SS
Trapdoor Function (TF) Signature Scheme.
Definition pubkey.h:2316
TF_SS< LUC, STANDARD, H >::Signer
PK_FinalTemplate< TF_SignerImpl< SchemeOptions > > Signer
Definition pubkey.h:2326
TF_SS< LUC, STANDARD, H >::Verifier
PK_FinalTemplate< TF_VerifierImpl< SchemeOptions > > Verifier
Definition pubkey.h:2328
TrapdoorFunction
Applies the trapdoor function.
Definition pubkey.h:126
TrapdoorFunctionInverse
Applies the inverse of the trapdoor function.
Definition pubkey.h:179
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
dh.h
Classes for Diffie-Hellman key exchange.
gfpcrypt.h
Classes and functions for schemes based on Discrete Logs (DL) over GF(p).
integer.h
Multiple precision integer with arithmetic operations.
LUC_DH
DH_Domain< DL_GroupParameters_LUC_DefaultSafePrime > LUC_DH
LUC-DH.
Definition luc.h:330
CryptoPP
Crypto++ library namespace.
oaep.h
Classes for optimal asymmetric encryption padding.
pkcspad.h
Classes for PKCS padding schemes.
secblock.h
Classes and functions for secure memory allocations.
DL_CryptoKeys_LUC
LUC encryption keys.
Definition luc.h:302
DL_SignatureKeys_LUC
LUC signature keys.
Definition luc.h:282
LUC_HMP
LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster,...
Definition luc.h:296
LUC_IES
LUC Integrated Encryption Scheme.
Definition luc.h:323
LUCES
LUC encryption scheme.
Definition luc.h:146
LUC
LUC cryptosystem.
Definition luc.h:132
LUCSS
LUC signature scheme with appendix.
Definition luc.h:158
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition trap.h:68