20 #include "runtime.hpp" 
   24 #include "modarith.hpp" 
   30 void DSA_PublicKey::Swap(DSA_PublicKey& other)
 
   39 DSA_PublicKey::DSA_PublicKey(
const DSA_PublicKey& other)
 
   40     : p_(other.p_), q_(other.q_), g_(other.g_), y_(other.y_)
 
   44 DSA_PublicKey& DSA_PublicKey::operator=(
const DSA_PublicKey& that)
 
   46     DSA_PublicKey tmp(that);
 
   52 DSA_PublicKey::DSA_PublicKey(Source& source)
 
   58 void DSA_PublicKey::Initialize(Source& source)
 
   60     DSA_Public_Decoder decoder(source);
 
   61     decoder.Decode(*
this);
 
   65 void DSA_PublicKey::Initialize(
const Integer& p, 
const Integer& q,
 
   66                                const Integer& g, 
const Integer& y)
 
   75 const Integer& DSA_PublicKey::GetModulus()
 const 
   80 const Integer& DSA_PublicKey::GetSubGroupOrder()
 const 
   86 const Integer& DSA_PublicKey::GetSubGroupGenerator()
 const 
   92 const Integer& DSA_PublicKey::GetPublicPart()
 const 
   98 void DSA_PublicKey::SetModulus(
const Integer& p)
 
  104 void DSA_PublicKey::SetSubGroupOrder(
const Integer& q)
 
  110 void DSA_PublicKey::SetSubGroupGenerator(
const Integer& g)
 
  116 void DSA_PublicKey::SetPublicPart(
const Integer& y)
 
  122 word32 DSA_PublicKey::SignatureLength()
 const 
  124     return GetSubGroupOrder().ByteCount() * 2;  
 
  129 DSA_PrivateKey::DSA_PrivateKey(Source& source)
 
  135 void DSA_PrivateKey::Initialize(Source& source)
 
  137     DSA_Private_Decoder decoder(source);
 
  138     decoder.Decode(*
this);
 
  142 void DSA_PrivateKey::Initialize(
const Integer& p, 
const Integer& q,
 
  143                                 const Integer& g, 
const Integer& y,
 
  146     DSA_PublicKey::Initialize(p, q, g, y);
 
  151 const Integer& DSA_PrivateKey::GetPrivatePart()
 const 
  157 void DSA_PrivateKey::SetPrivatePart(
const Integer& x)
 
  163 DSA_Signer::DSA_Signer(
const DSA_PrivateKey& key)
 
  168 word32 DSA_Signer::Sign(
const byte* sha_digest, byte* sig,
 
  169                         RandomNumberGenerator& rng)
 
  171     const Integer& p = key_.GetModulus();
 
  172     const Integer& q = key_.GetSubGroupOrder();
 
  173     const Integer& g = key_.GetSubGroupGenerator();
 
  174     const Integer& x = key_.GetPrivatePart();
 
  176     Integer k(rng, 1, q - 1);
 
  178     r_ =  a_exp_b_mod_c(g, k, p);
 
  181     Integer H(sha_digest, SHA::DIGEST_SIZE);  
 
  183     Integer kInv = k.InverseMod(q);
 
  184     s_ = (kInv * (H + x*r_)) % q;
 
  189     int rSz = r_.ByteCount();
 
  198     int sSz = s_.ByteCount();
 
  205     s_.Encode(sig + rSz, sSz);
 
  211 DSA_Verifier::DSA_Verifier(
const DSA_PublicKey& key)
 
  216 bool DSA_Verifier::Verify(
const byte* sha_digest, 
const byte* sig)
 
  218     const Integer& p = key_.GetModulus();
 
  219     const Integer& q = key_.GetSubGroupOrder();
 
  220     const Integer& g = key_.GetSubGroupGenerator();
 
  221     const Integer& y = key_.GetPublicPart();
 
  223     int sz = q.ByteCount();
 
  226     s_.Decode(sig + sz, sz);
 
  228     if (r_ >= q || r_ < 1 || s_ >= q || s_ < 1)
 
  231     Integer H(sha_digest, SHA::DIGEST_SIZE);  
 
  233     Integer w = s_.InverseMod(q);
 
  234     Integer u1 = (H  * w) % q;
 
  235     Integer u2 = (r_ * w) % q;
 
  238     ModularArithmetic ma(p);
 
  239     Integer v = ma.CascadeExponentiate(g, u1, y, u2);
 
  248 const Integer& DSA_Signer::GetR()
 const 
  254 const Integer& DSA_Signer::GetS()
 const 
  260 const Integer& DSA_Verifier::GetR()
 const 
  266 const Integer& DSA_Verifier::GetS()
 const