luc.cpp
// luc.cpp - originally written and placed in the public domain by Wei Dai
#include "pch.h"
#include "luc.h"
#include "asn.h"
#include "sha.h"
#include "integer.h"
#include "nbtheory.h"
#include "algparam.h"
#include "pkcspad.h"
NAMESPACE_BEGIN(CryptoPP)
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
void LUC_TestInstantiations()
{
LUC_HMP<SHA1>::Signer t1;
LUCFunction t2;
InvertibleLUCFunction t3;
}
#endif
void DL_Algorithm_LUC_HMP::Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
{
const Integer &q = params.GetSubgroupOrder();
r = params.ExponentiateBase(k);
s = (k + x*(r+e)) % q;
}
bool DL_Algorithm_LUC_HMP::Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
{
const Integer p = params.GetGroupOrder()-1;
const Integer &q = params.GetSubgroupOrder();
Integer Vsg = params.ExponentiateBase(s);
Integer Vry = publicKey.ExponentiatePublicElement((r+e)%q);
return (Vsg*Vsg + Vry*Vry + r*r) % p == (Vsg * Vry * r + 4) % p;
}
Integer DL_BasePrecomputation_LUC::Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const
{
return Lucas(exponent, m_g, static_cast<const DL_GroupPrecomputation_LUC &>(group).GetModulus());
}
void DL_GroupParameters_LUC::SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
{
for (unsigned int i=0; i<exponentsCount; i++)
results[i] = Lucas(exponents[i], base, GetModulus());
}
void LUCFunction::BERDecode(BufferedTransformation &bt)
{
BERSequenceDecoder seq(bt);
m_n.BERDecode(seq);
m_e.BERDecode(seq);
seq.MessageEnd();
}
void LUCFunction::DEREncode(BufferedTransformation &bt) const
{
DERSequenceEncoder seq(bt);
m_n.DEREncode(seq);
m_e.DEREncode(seq);
seq.MessageEnd();
}
Integer LUCFunction::ApplyFunction(const Integer &x) const
{
DoQuickSanityCheck();
return Lucas(m_e, x, m_n);
}
bool LUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{
CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
bool pass = true;
pass = pass && m_n > Integer::One() && m_n.IsOdd();
CRYPTOPP_ASSERT(pass);
pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
CRYPTOPP_ASSERT(pass);
return pass;
}
bool LUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper(this, name, valueType, pValue).Assignable()
CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
;
}
void LUCFunction::AssignFrom(const NameValuePairs &source)
{
AssignFromHelper(this, source)
CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
;
}
// *****************************************************************************
// private key operations:
class LUCPrimeSelector : public PrimeSelector
{
public:
LUCPrimeSelector(const Integer &e) : m_e(e) {}
bool IsAcceptable(const Integer &candidate) const
{
return RelativelyPrime(m_e, candidate+1) && RelativelyPrime(m_e, candidate-1);
}
Integer m_e;
};
void InvertibleLUCFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
{
int modulusSize = 2048;
alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
if (modulusSize < 16)
throw InvalidArgument("InvertibleLUCFunction: specified modulus size is too small");
m_e = alg.GetValueWithDefault("PublicExponent", Integer(17));
if (m_e < 5 || m_e.IsEven())
throw InvalidArgument("InvertibleLUCFunction: invalid public exponent");
LUCPrimeSelector selector(m_e);
AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
("PointerToPrimeSelector", selector.GetSelectorPointer());
m_p.GenerateRandom(rng, primeParam);
m_q.GenerateRandom(rng, primeParam);
m_n = m_p * m_q;
m_u = m_q.InverseMod(m_p);
}
void InvertibleLUCFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
{
GenerateRandom(rng, MakeParameters("ModulusSize", (int)keybits)("PublicExponent", e));
}
void InvertibleLUCFunction::BERDecode(BufferedTransformation &bt)
{
BERSequenceDecoder seq(bt);
Integer version(seq);
if (!!version) // make sure version is 0
BERDecodeError();
m_n.BERDecode(seq);
m_e.BERDecode(seq);
m_p.BERDecode(seq);
m_q.BERDecode(seq);
m_u.BERDecode(seq);
seq.MessageEnd();
}
void InvertibleLUCFunction::DEREncode(BufferedTransformation &bt) const
{
DERSequenceEncoder seq(bt);
const byte version[] = {INTEGER, 1, 0};
seq.Put(version, sizeof(version));
m_n.DEREncode(seq);
m_e.DEREncode(seq);
m_p.DEREncode(seq);
m_q.DEREncode(seq);
m_u.DEREncode(seq);
seq.MessageEnd();
}
Integer InvertibleLUCFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
{
// not clear how to do blinding with LUC
CRYPTOPP_UNUSED(rng);
DoQuickSanityCheck();
return InverseLucas(m_e, x, m_q, m_p, m_u);
}
bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{
bool pass = LUCFunction::Validate(rng, level);
CRYPTOPP_ASSERT(pass);
pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u.IsPositive() && m_u < m_p;
CRYPTOPP_ASSERT(pass);
if (level >= 1)
{
pass = pass && m_p * m_q == m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_p+1);
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_p-1);
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_q+1);
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_q-1);
CRYPTOPP_ASSERT(pass);
pass = pass && m_u * m_q % m_p == 1;
CRYPTOPP_ASSERT(pass);
}
if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
}
return pass;
}
bool InvertibleLUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper<LUCFunction>(this, name, valueType, pValue).Assignable()
CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
;
}
void InvertibleLUCFunction::AssignFrom(const NameValuePairs &source)
{
AssignFromHelper<LUCFunction>(this, source)
CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
;
}
NAMESPACE_END
algparam.h
Classes for working with NameValuePairs.
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition algparam.h:508
asn.h
Classes and functions for working with ANS.1 objects.
INTEGER
@ INTEGER
ASN.1 Integer.
Definition asn.h:34
BERDecodeError
void BERDecodeError()
Raises a BERDecodeErr.
Definition asn.h:104
AlgorithmParameters
An object that implements NameValuePairs.
Definition algparam.h:426
BERSequenceDecoder
BER Sequence Decoder.
Definition asn.h:525
BufferedTransformation
Interface for buffered transformations.
Definition cryptlib.h:1657
CryptoMaterial::DoQuickSanityCheck
void DoQuickSanityCheck() const
Perform a quick sanity check.
Definition cryptlib.h:2498
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::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_GroupParameters_IntegerBasedImpl< DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC >::GetModulus
const Integer & GetModulus() const
Definition gfpcrypt.h:205
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_GroupParameters::GetSubgroupOrder
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.
DL_GroupParameters::ExponentiateBase
virtual Element ExponentiateBase(const Integer &exponent) const
Exponentiates the base.
Definition pubkey.h:869
DL_GroupPrecomputation_LUC
LUC GroupParameters precomputation.
Definition luc.h:174
DL_GroupPrecomputation
DL_GroupPrecomputation interface.
Definition eprecomp.h:20
DL_PublicKey
Interface for Discrete Log (DL) public keys.
Definition pubkey.h:1059
DL_PublicKey::ExponentiatePublicElement
virtual Element ExponentiatePublicElement(const Integer &exponent) const
Exponentiates this element.
Definition pubkey.h:1098
DL_SS< DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, DL_SignatureMessageEncodingMethod_DSA, H >::Signer
PK_FinalTemplate< DL_SignerImpl< SchemeOptions > > Signer
Definition pubkey.h:2349
Integer
Multiple precision integer with arithmetic operations.
Definition integer.h:50
Integer::BERDecode
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Integer::IsOdd
bool IsOdd() const
Determines if the Integer is odd parity.
Definition integer.h:356
Integer::One
static const Integer & One()
Integer representing 1.
InvalidArgument
An invalid argument was detected.
Definition cryptlib.h:208
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::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::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition luc.cpp:84
LUCFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition luc.cpp:73
LUCFunction::ApplyFunction
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition luc.cpp:67
LUCFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition luc.cpp:92
LUCPrimeSelector
Definition luc.cpp:104
NameValuePairs
Interface for retrieving values given their names.
Definition cryptlib.h:327
NameValuePairs::GetValueWithDefault
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition cryptlib.h:397
NameValuePairs::GetIntValue
CRYPTOPP_DLL bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition cryptlib.h:420
PrimeSelector
Application callback to signal suitability of a cabdidate prime.
Definition nbtheory.h:117
RandomNumberGenerator
Interface for random number generators.
Definition cryptlib.h:1440
integer.h
Multiple precision integer with arithmetic operations.
luc.h
Classes for the LUC cryptosystem.
CryptoPP
Crypto++ library namespace.
nbtheory.h
Classes and functions for number theoretic operations.
RelativelyPrime
bool RelativelyPrime(const Integer &a, const Integer &b)
Determine relative primality.
Definition nbtheory.h:153
Lucas
CRYPTOPP_DLL Integer Lucas(const Integer &e, const Integer &p, const Integer &n)
Calculate the Lucas value.
InverseLucas
CRYPTOPP_DLL Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u)
Calculate the inverse Lucas value.
VerifyPrime
CRYPTOPP_DLL bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a number is probably prime.
pch.h
Precompiled header file.
pkcspad.h
Classes for PKCS padding schemes.
sha.h
Classes for SHA-1 and SHA-2 family of message digests.
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition trap.h:68