mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / extra / yassl / taocrypt / src / dsa.cpp
blobbf116d3e48d12b268940c7cb981bf1621d829124
1 /*
2 Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to the
15 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16 MA 02110-1301 USA.
20 #include "runtime.hpp"
21 #include "dsa.hpp"
22 #include "sha.hpp"
23 #include "asn.hpp"
24 #include "modarith.hpp"
27 namespace TaoCrypt {
30 void DSA_PublicKey::Swap(DSA_PublicKey& other)
32 p_.Swap(other.p_);
33 q_.Swap(other.q_);
34 g_.Swap(other.g_);
35 y_.Swap(other.y_);
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);
47 Swap(tmp);
48 return *this;
52 DSA_PublicKey::DSA_PublicKey(Source& source)
54 Initialize(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)
68 p_ = p;
69 q_ = q;
70 g_ = g;
71 y_ = y;
75 const Integer& DSA_PublicKey::GetModulus() const
77 return p_;
80 const Integer& DSA_PublicKey::GetSubGroupOrder() const
82 return q_;
86 const Integer& DSA_PublicKey::GetSubGroupGenerator() const
88 return g_;
92 const Integer& DSA_PublicKey::GetPublicPart() const
94 return y_;
98 void DSA_PublicKey::SetModulus(const Integer& p)
100 p_ = p;
104 void DSA_PublicKey::SetSubGroupOrder(const Integer& q)
106 q_ = q;
110 void DSA_PublicKey::SetSubGroupGenerator(const Integer& g)
112 g_ = g;
116 void DSA_PublicKey::SetPublicPart(const Integer& y)
118 y_ = y;
122 word32 DSA_PublicKey::SignatureLength() const
124 return GetSubGroupOrder().ByteCount() * 2; // r and s
129 DSA_PrivateKey::DSA_PrivateKey(Source& source)
131 Initialize(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,
144 const Integer& x)
146 DSA_PublicKey::Initialize(p, q, g, y);
147 x_ = x;
151 const Integer& DSA_PrivateKey::GetPrivatePart() const
153 return x_;
157 void DSA_PrivateKey::SetPrivatePart(const Integer& x)
159 x_ = x;
163 DSA_Signer::DSA_Signer(const DSA_PrivateKey& key)
164 : key_(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);
179 r_ %= q;
181 Integer H(sha_digest, SHA::DIGEST_SIZE); // sha Hash(m)
183 Integer kInv = k.InverseMod(q);
184 s_ = (kInv * (H + x*r_)) % q;
186 if (!(!!r_ && !!s_))
187 return -1;
189 int rSz = r_.ByteCount();
191 if (rSz == 19) {
192 sig[0] = 0;
193 sig++;
196 r_.Encode(sig, rSz);
198 int sSz = s_.ByteCount();
200 if (sSz == 19) {
201 sig[rSz] = 0;
202 sig++;
205 s_.Encode(sig + rSz, sSz);
207 return 40;
211 DSA_Verifier::DSA_Verifier(const DSA_PublicKey& key)
212 : key_(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();
225 r_.Decode(sig, sz);
226 s_.Decode(sig + sz, sz);
228 if (r_ >= q || r_ < 1 || s_ >= q || s_ < 1)
229 return false;
231 Integer H(sha_digest, SHA::DIGEST_SIZE); // sha Hash(m)
233 Integer w = s_.InverseMod(q);
234 Integer u1 = (H * w) % q;
235 Integer u2 = (r_ * w) % q;
237 // verify r == ((g^u1 * y^u2) mod p) mod q
238 ModularArithmetic ma(p);
239 Integer v = ma.CascadeExponentiate(g, u1, y, u2);
240 v %= q;
242 return r_ == v;
248 const Integer& DSA_Signer::GetR() const
250 return r_;
254 const Integer& DSA_Signer::GetS() const
256 return s_;
260 const Integer& DSA_Verifier::GetR() const
262 return r_;
266 const Integer& DSA_Verifier::GetS() const
268 return s_;
272 } // namespace