void DH::GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const { Integer x(rng, ExponentBitLength()); Integer y = gpc.Exponentiate(x); x.Encode(privateKey, PrivateKeyLength()); y.Encode(publicKey, PublicKeyLength()); }
bool DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const { Integer w(otherPublicKey, PublicKeyLength()); if (validateOtherPublicKey && !(w > 1 && w < p && Jacobi(w, p) == 1)) return false; Integer s(privateKey, PrivateKeyLength()); Integer z = a_exp_b_mod_c(w, s, p); z.Encode(agreedValue, AgreedValueLength()); return true; }
bool XTR_DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const { GFP2Element w(otherPublicKey, PublicKeyLength()); if (validateOtherPublicKey) { GFP2_ONB<ModularArithmetic> gfp2(m_p); GFP2Element three = gfp2.ConvertIn(3); if (w.c1.IsNegative() || w.c2.IsNegative() || w.c1 >= m_p || w.c2 >= m_p || w == three) return false; if (XTR_Exponentiate(w, m_q, m_p) != three) return false; } Integer s(privateKey, PrivateKeyLength()); GFP2Element z = XTR_Exponentiate(w, s, m_p); z.Encode(agreedValue, AgreedValueLength()); return true; }
void XTR_DH::GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const { Integer x(privateKey, PrivateKeyLength()); GFP2Element y = XTR_Exponentiate(m_g, x, m_p); y.Encode(publicKey, PublicKeyLength()); }