Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

luc.cpp

00001 // luc.cpp - written and placed in the public domain by Wei Dai
00002 
00003 #include "pch.h"
00004 #include "luc.h"
00005 #include "asn.h"
00006 #include "nbtheory.h"
00007 #include "sha.h"
00008 #include "algparam.h"
00009 
00010 NAMESPACE_BEGIN(CryptoPP)
00011 
00012 void LUC_TestInstantiations()
00013 {
00014         LUC_HMP<SHA>::Signer t1;
00015         LUCFunction t2;
00016         InvertibleLUCFunction t3;
00017 }
00018 
00019 bool DL_Algorithm_LUC_HMP::Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
00020 {
00021         const Integer &q = params.GetSubgroupOrder();
00022         r = params.ExponentiateBase(k);
00023         s = (k + x*(r+e)) % q;
00024         return true;
00025 }
00026 
00027 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
00028 {
00029         Integer p = params.GetGroupOrder()-1;
00030         const Integer &q = params.GetSubgroupOrder();
00031 
00032         Integer Vsg = params.ExponentiateBase(s);
00033         Integer Vry = publicKey.ExponentiatePublicElement((r+e)%q);
00034         return (Vsg*Vsg + Vry*Vry + r*r) % p == (Vsg * Vry * r + 4) % p;
00035 }
00036 
00037 Integer DL_BasePrecomputation_LUC::Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const
00038 {
00039         return Lucas(exponent, m_g, static_cast<const DL_GroupPrecomputation_LUC &>(group).GetModulus());
00040 }
00041 
00042 void DL_GroupParameters_LUC::SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
00043 {
00044         for (unsigned int i=0; i<exponentsCount; i++)
00045                 results[i] = Lucas(exponents[i], base, GetModulus());
00046 }
00047 
00048 void LUCFunction::BERDecode(BufferedTransformation &bt)
00049 {
00050         BERSequenceDecoder seq(bt);
00051         m_n.BERDecode(seq);
00052         m_e.BERDecode(seq);
00053         seq.MessageEnd();
00054 }
00055 
00056 void LUCFunction::DEREncode(BufferedTransformation &bt) const
00057 {
00058         DERSequenceEncoder seq(bt);
00059         m_n.DEREncode(seq);
00060         m_e.DEREncode(seq);
00061         seq.MessageEnd();
00062 }
00063 
00064 Integer LUCFunction::ApplyFunction(const Integer &x) const
00065 {
00066         DoQuickSanityCheck();
00067         return Lucas(m_e, x, m_n);
00068 }
00069 
00070 bool LUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
00071 {
00072         bool pass = true;
00073         pass = pass && m_n > Integer::One() && m_n.IsOdd();
00074         pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
00075         return pass;
00076 }
00077 
00078 bool LUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
00079 {
00080         return GetValueHelper(this, name, valueType, pValue).Assignable()
00081                 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
00082                 CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
00083                 ;
00084 }
00085 
00086 void LUCFunction::AssignFrom(const NameValuePairs &source)
00087 {
00088         AssignFromHelper(this, source)
00089                 CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
00090                 CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
00091                 ;
00092 }
00093 
00094 // *****************************************************************************
00095 // private key operations:
00096 
00097 class LUCPrimeSelector : public PrimeSelector
00098 {
00099 public:
00100         LUCPrimeSelector(const Integer &e) : m_e(e) {}
00101         bool IsAcceptable(const Integer &candidate) const
00102         {
00103                 return RelativelyPrime(m_e, candidate+1) && RelativelyPrime(m_e, candidate-1);
00104         }
00105         Integer m_e;
00106 };
00107 
00108 void InvertibleLUCFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
00109 {
00110         int modulusSize = 2048;
00111         alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
00112 
00113         if (modulusSize < 16)
00114                 throw InvalidArgument("InvertibleLUCFunction: specified modulus size is too small");
00115 
00116         m_e = alg.GetValueWithDefault("PublicExponent", Integer(17));
00117 
00118         if (m_e < 5 || m_e.IsEven())
00119                 throw InvalidArgument("InvertibleLUCFunction: invalid public exponent");
00120 
00121         LUCPrimeSelector selector(m_e);
00122         const NameValuePairs &primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
00123                 ("PointerToPrimeSelector", selector.GetSelectorPointer());
00124         m_p.GenerateRandom(rng, primeParam);
00125         m_q.GenerateRandom(rng, primeParam);
00126 
00127         m_n = m_p * m_q;
00128         m_u = m_q.InverseMod(m_p);
00129 }
00130 
00131 void InvertibleLUCFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
00132 {
00133         GenerateRandom(rng, MakeParameters("ModulusSize", (int)keybits)("PublicExponent", e));
00134 }
00135 
00136 void InvertibleLUCFunction::BERDecode(BufferedTransformation &bt)
00137 {
00138         BERSequenceDecoder seq(bt);
00139 
00140         Integer version(seq);
00141         if (!!version)  // make sure version is 0
00142                 BERDecodeError();
00143 
00144         m_n.BERDecode(seq);
00145         m_e.BERDecode(seq);
00146         m_p.BERDecode(seq);
00147         m_q.BERDecode(seq);
00148         m_u.BERDecode(seq);
00149         seq.MessageEnd();
00150 }
00151 
00152 void InvertibleLUCFunction::DEREncode(BufferedTransformation &bt) const
00153 {
00154         DERSequenceEncoder seq(bt);
00155 
00156         const byte version[] = {INTEGER, 1, 0};
00157         seq.Put(version, sizeof(version));
00158         m_n.DEREncode(seq);
00159         m_e.DEREncode(seq);
00160         m_p.DEREncode(seq);
00161         m_q.DEREncode(seq);
00162         m_u.DEREncode(seq);
00163         seq.MessageEnd();
00164 }
00165 
00166 Integer InvertibleLUCFunction::CalculateInverse(const Integer &x) const
00167 {
00168         DoQuickSanityCheck();
00169         return InverseLucas(m_e, x, m_q, m_p, m_u);
00170 }
00171 
00172 bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
00173 {
00174         bool pass = LUCFunction::Validate(rng, level);
00175         pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
00176         pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
00177         pass = pass && m_u.IsPositive() && m_u < m_p;
00178         if (level >= 1)
00179         {
00180                 pass = pass && m_p * m_q == m_n;
00181                 pass = pass && RelativelyPrime(m_e, m_p+1);
00182                 pass = pass && RelativelyPrime(m_e, m_p-1);
00183                 pass = pass && RelativelyPrime(m_e, m_q+1);
00184                 pass = pass && RelativelyPrime(m_e, m_q-1);
00185                 pass = pass && m_u * m_q % m_p == 1;
00186         }
00187         if (level >= 2)
00188                 pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
00189         return pass;
00190 }
00191 
00192 bool InvertibleLUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
00193 {
00194         return GetValueHelper<LUCFunction>(this, name, valueType, pValue).Assignable()
00195                 CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
00196                 CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
00197                 CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
00198                 ;
00199 }
00200 
00201 void InvertibleLUCFunction::AssignFrom(const NameValuePairs &source)
00202 {
00203         AssignFromHelper<LUCFunction>(this, source)
00204                 CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
00205                 CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
00206                 CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
00207                 ;
00208 }
00209 
00210 NAMESPACE_END

Generated on Tue Jul 8 23:34:19 2003 for Crypto++ by doxygen 1.3.2