1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package dsa implements the Digital Signature Algorithm, as defined in FIPS 186-3.
7 // The DSA operations in this package are not implemented using constant-time algorithms.
15 "crypto/internal/randutil"
18 // Parameters represents the domain parameters for a key. These parameters can
19 // be shared across many keys. The bit length of Q must be a multiple of 8.
20 type Parameters
struct {
24 // PublicKey represents a DSA public key.
25 type PublicKey
struct {
30 // PrivateKey represents a DSA private key.
31 type PrivateKey
struct {
36 // ErrInvalidPublicKey results when a public key is not usable by this code.
37 // FIPS is quite strict about the format of DSA keys, but other code may be
38 // less so. Thus, when using keys which may have been generated by other code,
39 // this error must be handled.
40 var ErrInvalidPublicKey
= errors
.New("crypto/dsa: invalid public key")
42 // ParameterSizes is an enumeration of the acceptable bit lengths of the primes
43 // in a set of DSA parameters. See FIPS 186-3, section 4.2.
44 type ParameterSizes
int
47 L1024N160 ParameterSizes
= iota
53 // numMRTests is the number of Miller-Rabin primality tests that we perform. We
54 // pick the largest recommended number from table C.1 of FIPS 186-3.
57 // GenerateParameters puts a random, valid set of DSA parameters into params.
58 // This function can take many seconds, even on fast machines.
59 func GenerateParameters(params
*Parameters
, rand io
.Reader
, sizes ParameterSizes
) error
{
60 // This function doesn't follow FIPS 186-3 exactly in that it doesn't
61 // use a verification seed to generate the primes. The verification
62 // seed doesn't appear to be exported or used by other code and
63 // omitting it makes the code cleaner.
80 return errors
.New("crypto/dsa: invalid ParameterSizes")
83 qBytes
:= make([]byte, N
/8)
84 pBytes
:= make([]byte, L
/8)
94 if _
, err
:= io
.ReadFull(rand
, qBytes
); err
!= nil {
98 qBytes
[len(qBytes
)-1] |
= 1
102 if !q
.ProbablyPrime(numMRTests
) {
106 for i
:= 0; i
< 4*L
; i
++ {
107 if _
, err
:= io
.ReadFull(rand
, pBytes
); err
!= nil {
111 pBytes
[len(pBytes
)-1] |
= 1
122 if !p
.ProbablyPrime(numMRTests
) {
136 pm1
:= new(big
.Int
).Sub(p
, one
)
137 e
:= new(big
.Int
).Div(pm1
, q
)
151 // GenerateKey generates a public&private key pair. The Parameters of the
152 // PrivateKey must already be valid (see GenerateParameters).
153 func GenerateKey(priv
*PrivateKey
, rand io
.Reader
) error
{
154 if priv
.P
== nil || priv
.Q
== nil || priv
.G
== nil {
155 return errors
.New("crypto/dsa: parameters not set up before generating key")
159 xBytes
:= make([]byte, priv
.Q
.BitLen()/8)
162 _
, err
:= io
.ReadFull(rand
, xBytes
)
167 if x
.Sign() != 0 && x
.Cmp(priv
.Q
) < 0 {
173 priv
.Y
= new(big
.Int
)
174 priv
.Y
.Exp(priv
.G
, x
, priv
.P
)
178 // fermatInverse calculates the inverse of k in GF(P) using Fermat's method.
179 // This has better constant-time properties than Euclid's method (implemented
180 // in math/big.Int.ModInverse) although math/big itself isn't strictly
181 // constant-time so it's not perfect.
182 func fermatInverse(k
, P
*big
.Int
) *big
.Int
{
184 pMinus2
:= new(big
.Int
).Sub(P
, two
)
185 return new(big
.Int
).Exp(k
, pMinus2
, P
)
188 // Sign signs an arbitrary length hash (which should be the result of hashing a
189 // larger message) using the private key, priv. It returns the signature as a
190 // pair of integers. The security of the private key depends on the entropy of
193 // Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated
194 // to the byte-length of the subgroup. This function does not perform that
195 // truncation itself.
197 // Be aware that calling Sign with an attacker-controlled PrivateKey may
198 // require an arbitrary amount of CPU.
199 func Sign(rand io
.Reader
, priv
*PrivateKey
, hash
[]byte) (r
, s
*big
.Int
, err error
) {
200 randutil
.MaybeReadByte(rand
)
202 // FIPS 186-3, section 4.6
205 if priv
.Q
.Sign() <= 0 || priv
.P
.Sign() <= 0 || priv
.G
.Sign() <= 0 || priv
.X
.Sign() <= 0 || n
&7 != 0 {
206 err
= ErrInvalidPublicKey
212 for attempts
= 10; attempts
> 0; attempts
-- {
214 buf
:= make([]byte, n
)
216 _
, err
= io
.ReadFull(rand
, buf
)
221 // priv.Q must be >= 128 because the test above
222 // requires it to be > 0 and that
223 // ceil(log_2(Q)) mod 8 = 0
224 // Thus this loop will quickly terminate.
225 if k
.Sign() > 0 && k
.Cmp(priv
.Q
) < 0 {
230 kInv
:= fermatInverse(k
, priv
.Q
)
232 r
= new(big
.Int
).Exp(priv
.G
, k
, priv
.P
)
239 z
:= k
.SetBytes(hash
)
241 s
= new(big
.Int
).Mul(priv
.X
, r
)
252 // Only degenerate private keys will require more than a handful of
255 return nil, nil, ErrInvalidPublicKey
261 // Verify verifies the signature in r, s of hash using the public key, pub. It
262 // reports whether the signature is valid.
264 // Note that FIPS 186-3 section 4.6 specifies that the hash should be truncated
265 // to the byte-length of the subgroup. This function does not perform that
266 // truncation itself.
267 func Verify(pub
*PublicKey
, hash
[]byte, r
, s
*big
.Int
) bool {
268 // FIPS 186-3, section 4.7
270 if pub
.P
.Sign() == 0 {
274 if r
.Sign() < 1 || r
.Cmp(pub
.Q
) >= 0 {
277 if s
.Sign() < 1 || s
.Cmp(pub
.Q
) >= 0 {
281 w
:= new(big
.Int
).ModInverse(s
, pub
.Q
)
287 z
:= new(big
.Int
).SetBytes(hash
)
289 u1
:= new(big
.Int
).Mul(z
, w
)
293 v
:= u1
.Exp(pub
.G
, u1
, pub
.P
)
294 u2
.Exp(pub
.Y
, u2
, pub
.P
)