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