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

# rabin.cpp

    00001 // rabin.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004 #include "rabin.h"
    00005 #include "nbtheory.h"
    00006 #include "asn.h"
    00007 #include "sha.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 void RabinFunction::BERDecode(BufferedTransformation &bt)
    00012 {
    00013         BERSequenceDecoder seq(bt);
    00014         m_n.BERDecode(seq);
    00015         m_r.BERDecode(seq);
    00016         m_s.BERDecode(seq);
    00017         seq.MessageEnd();
    00018 }
    00019
    00020 void RabinFunction::DEREncode(BufferedTransformation &bt) const
    00021 {
    00022         DERSequenceEncoder seq(bt);
    00023         m_n.DEREncode(seq);
    00024         m_r.DEREncode(seq);
    00025         m_s.DEREncode(seq);
    00026         seq.MessageEnd();
    00027 }
    00028
    00029 Integer RabinFunction::ApplyFunction(const Integer &in) const
    00030 {
    00031         DoQuickSanityCheck();
    00032
    00033         Integer out = in.Squared()%m_n;
    00034         if (in.IsOdd())
    00035                 out = out*m_r%m_n;
    00036         if (Jacobi(in, m_n)==-1)
    00037                 out = out*m_s%m_n;
    00038         return out;
    00039 }
    00040
    00041 bool RabinFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
    00042 {
    00043         bool pass = true;
    00044         pass = pass && m_n > Integer::One() && m_n%4 == 1;
    00045         pass = pass && m_r > Integer::One() && m_r < m_n;
    00046         pass = pass && m_s > Integer::One() && m_s < m_n;
    00047         if (level >= 1)
    00048                 pass = pass && Jacobi(m_r, m_n) == -1 && Jacobi(m_s, m_n) == -1;
    00049         return pass;
    00050 }
    00051
    00052 bool RabinFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
    00053 {
    00054         return GetValueHelper(this, name, valueType, pValue).Assignable()
    00055                 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
    00056                 CRYPTOPP_GET_FUNCTION_ENTRY(QuadraticResidueModPrime1)
    00057                 CRYPTOPP_GET_FUNCTION_ENTRY(QuadraticResidueModPrime2)
    00058                 ;
    00059 }
    00060
    00061 void RabinFunction::AssignFrom(const NameValuePairs &source)
    00062 {
    00063         AssignFromHelper(this, source)
    00064                 CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
    00065                 CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime1)
    00066                 CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime2)
    00067                 ;
    00068 }
    00069
    00070 // *****************************************************************************
    00071 // private key operations:
    00072
    00073 // generate a random private key
    00074 void InvertibleRabinFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
    00075 {
    00076         int modulusSize = 2048;
    00077         alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
    00078
    00079         if (modulusSize < 16)
    00080                 throw InvalidArgument("InvertibleRabinFunction: specified modulus size is too small");
    00081
    00082         // VC70 workaround: putting these after primeParam causes overlapped stack allocation
    00083         bool rFound=false, sFound=false;
    00084         Integer t=2;
    00085
    00086         const NameValuePairs &primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
    00087                 ("EquivalentTo", 3)("Mod", 4);
    00088         m_p.GenerateRandom(rng, primeParam);
    00089         m_q.GenerateRandom(rng, primeParam);
    00090
    00091         while (!(rFound && sFound))
    00092         {
    00093                 int jp = Jacobi(t, m_p);
    00094                 int jq = Jacobi(t, m_q);
    00095
    00096                 if (!rFound && jp==1 && jq==-1)
    00097                 {
    00098                         m_r = t;
    00099                         rFound = true;
    00100                 }
    00101
    00102                 if (!sFound && jp==-1 && jq==1)
    00103                 {
    00104                         m_s = t;
    00105                         sFound = true;
    00106                 }
    00107
    00108                 ++t;
    00109         }
    00110
    00111         m_n = m_p * m_q;
    00112         m_u = m_q.InverseMod(m_p);
    00113 }
    00114
    00115 void InvertibleRabinFunction::BERDecode(BufferedTransformation &bt)
    00116 {
    00117         BERSequenceDecoder seq(bt);
    00118         m_n.BERDecode(seq);
    00119         m_r.BERDecode(seq);
    00120         m_s.BERDecode(seq);
    00121         m_p.BERDecode(seq);
    00122         m_q.BERDecode(seq);
    00123         m_u.BERDecode(seq);
    00124         seq.MessageEnd();
    00125 }
    00126
    00127 void InvertibleRabinFunction::DEREncode(BufferedTransformation &bt) const
    00128 {
    00129         DERSequenceEncoder seq(bt);
    00130         m_n.DEREncode(seq);
    00131         m_r.DEREncode(seq);
    00132         m_s.DEREncode(seq);
    00133         m_p.DEREncode(seq);
    00134         m_q.DEREncode(seq);
    00135         m_u.DEREncode(seq);
    00136         seq.MessageEnd();
    00137 }
    00138
    00139 Integer InvertibleRabinFunction::CalculateInverse(const Integer &in) const
    00140 {
    00141         DoQuickSanityCheck();
    00142
    00143         Integer cp=in%m_p, cq=in%m_q;
    00144
    00145         int jp = Jacobi(cp, m_p);
    00146         int jq = Jacobi(cq, m_q);
    00147
    00148         if (jq==-1)
    00149         {
    00150                 cp = cp*EuclideanMultiplicativeInverse(m_r, m_p)%m_p;
    00151                 cq = cq*EuclideanMultiplicativeInverse(m_r, m_q)%m_q;
    00152         }
    00153
    00154         if (jp==-1)
    00155         {
    00156                 cp = cp*EuclideanMultiplicativeInverse(m_s, m_p)%m_p;
    00157                 cq = cq*EuclideanMultiplicativeInverse(m_s, m_q)%m_q;
    00158         }
    00159
    00160         cp = ModularSquareRoot(cp, m_p);
    00161         cq = ModularSquareRoot(cq, m_q);
    00162
    00163         if (jp==-1)
    00164                 cp = m_p-cp;
    00165
    00166         Integer out = CRT(cq, m_q, cp, m_p, m_u);
    00167
    00168         if ((jq==-1 && out.IsEven()) || (jq==1 && out.IsOdd()))
    00169                 out = m_n-out;
    00170
    00171         return out;
    00172 }
    00173
    00174 bool InvertibleRabinFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
    00175 {
    00176         bool pass = RabinFunction::Validate(rng, level);
    00177         pass = pass && m_p > Integer::One() && m_p%4 == 3 && m_p < m_n;
    00178         pass = pass && m_q > Integer::One() && m_q%4 == 3 && m_q < m_n;
    00179         pass = pass && m_u.IsPositive() && m_u < m_p;
    00180         if (level >= 1)
    00181         {
    00182                 pass = pass && m_p * m_q == m_n;
    00183                 pass = pass && m_u * m_q % m_p == 1;
    00184                 pass = pass && Jacobi(m_r, m_p) == 1;
    00185                 pass = pass && Jacobi(m_r, m_q) == -1;
    00186                 pass = pass && Jacobi(m_s, m_p) == -1;
    00187                 pass = pass && Jacobi(m_s, m_q) == 1;
    00188         }
    00189         if (level >= 2)
    00190                 pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
    00191         return pass;
    00192 }
    00193
    00194 bool InvertibleRabinFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
    00195 {
    00196         return GetValueHelper<RabinFunction>(this, name, valueType, pValue).Assignable()
    00197                 CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
    00198                 CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
    00199                 CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
    00200                 ;
    00201 }
    00202
    00203 void InvertibleRabinFunction::AssignFrom(const NameValuePairs &source)
    00204 {
    00205         AssignFromHelper<RabinFunction>(this, source)
    00206                 CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
    00207                 CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
    00208                 CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
    00209                 ;
    00210 }
    00211
    00212 NAMESPACE_END