1
2
3
4
5
6
7
8
9
10
11
12
13
14 package dsa
15
16 import (
17 "errors"
18 "io"
19 "math/big"
20
21 "crypto/internal/fips140only"
22 "crypto/internal/randutil"
23 )
24
25
26
27 type Parameters struct {
28 P, Q, G *big.Int
29 }
30
31
32 type PublicKey struct {
33 Parameters
34 Y *big.Int
35 }
36
37
38 type PrivateKey struct {
39 PublicKey
40 X *big.Int
41 }
42
43
44
45
46
47 var ErrInvalidPublicKey = errors.New("crypto/dsa: invalid public key")
48
49
50
51 type ParameterSizes int
52
53 const (
54 L1024N160 ParameterSizes = iota
55 L2048N224
56 L2048N256
57 L3072N256
58 )
59
60
61
62 const numMRTests = 64
63
64
65
66 func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) error {
67 if fips140only.Enabled {
68 return errors.New("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
69 }
70
71
72
73
74
75
76 var L, N int
77 switch sizes {
78 case L1024N160:
79 L = 1024
80 N = 160
81 case L2048N224:
82 L = 2048
83 N = 224
84 case L2048N256:
85 L = 2048
86 N = 256
87 case L3072N256:
88 L = 3072
89 N = 256
90 default:
91 return errors.New("crypto/dsa: invalid ParameterSizes")
92 }
93
94 qBytes := make([]byte, N/8)
95 pBytes := make([]byte, L/8)
96
97 q := new(big.Int)
98 p := new(big.Int)
99 rem := new(big.Int)
100 one := new(big.Int)
101 one.SetInt64(1)
102
103 GeneratePrimes:
104 for {
105 if _, err := io.ReadFull(rand, qBytes); err != nil {
106 return err
107 }
108
109 qBytes[len(qBytes)-1] |= 1
110 qBytes[0] |= 0x80
111 q.SetBytes(qBytes)
112
113 if !q.ProbablyPrime(numMRTests) {
114 continue
115 }
116
117 for i := 0; i < 4*L; i++ {
118 if _, err := io.ReadFull(rand, pBytes); err != nil {
119 return err
120 }
121
122 pBytes[len(pBytes)-1] |= 1
123 pBytes[0] |= 0x80
124
125 p.SetBytes(pBytes)
126 rem.Mod(p, q)
127 rem.Sub(rem, one)
128 p.Sub(p, rem)
129 if p.BitLen() < L {
130 continue
131 }
132
133 if !p.ProbablyPrime(numMRTests) {
134 continue
135 }
136
137 params.P = p
138 params.Q = q
139 break GeneratePrimes
140 }
141 }
142
143 h := new(big.Int)
144 h.SetInt64(2)
145 g := new(big.Int)
146
147 pm1 := new(big.Int).Sub(p, one)
148 e := new(big.Int).Div(pm1, q)
149
150 for {
151 g.Exp(h, e, p)
152 if g.Cmp(one) == 0 {
153 h.Add(h, one)
154 continue
155 }
156
157 params.G = g
158 return nil
159 }
160 }
161
162
163
164 func GenerateKey(priv *PrivateKey, rand io.Reader) error {
165 if fips140only.Enabled {
166 return errors.New("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
167 }
168
169 if priv.P == nil || priv.Q == nil || priv.G == nil {
170 return errors.New("crypto/dsa: parameters not set up before generating key")
171 }
172
173 x := new(big.Int)
174 xBytes := make([]byte, priv.Q.BitLen()/8)
175
176 for {
177 _, err := io.ReadFull(rand, xBytes)
178 if err != nil {
179 return err
180 }
181 x.SetBytes(xBytes)
182 if x.Sign() != 0 && x.Cmp(priv.Q) < 0 {
183 break
184 }
185 }
186
187 priv.X = x
188 priv.Y = new(big.Int)
189 priv.Y.Exp(priv.G, x, priv.P)
190 return nil
191 }
192
193
194
195
196
197 func fermatInverse(k, P *big.Int) *big.Int {
198 two := big.NewInt(2)
199 pMinus2 := new(big.Int).Sub(P, two)
200 return new(big.Int).Exp(k, pMinus2, P)
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214 func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) {
215 if fips140only.Enabled {
216 return nil, nil, errors.New("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
217 }
218
219 randutil.MaybeReadByte(rand)
220
221
222
223 n := priv.Q.BitLen()
224 if priv.Q.Sign() <= 0 || priv.P.Sign() <= 0 || priv.G.Sign() <= 0 || priv.X.Sign() <= 0 || n%8 != 0 {
225 err = ErrInvalidPublicKey
226 return
227 }
228 n >>= 3
229
230 var attempts int
231 for attempts = 10; attempts > 0; attempts-- {
232 k := new(big.Int)
233 buf := make([]byte, n)
234 for {
235 _, err = io.ReadFull(rand, buf)
236 if err != nil {
237 return
238 }
239 k.SetBytes(buf)
240
241
242
243
244 if k.Sign() > 0 && k.Cmp(priv.Q) < 0 {
245 break
246 }
247 }
248
249 kInv := fermatInverse(k, priv.Q)
250
251 r = new(big.Int).Exp(priv.G, k, priv.P)
252 r.Mod(r, priv.Q)
253
254 if r.Sign() == 0 {
255 continue
256 }
257
258 z := k.SetBytes(hash)
259
260 s = new(big.Int).Mul(priv.X, r)
261 s.Add(s, z)
262 s.Mod(s, priv.Q)
263 s.Mul(s, kInv)
264 s.Mod(s, priv.Q)
265
266 if s.Sign() != 0 {
267 break
268 }
269 }
270
271
272
273 if attempts == 0 {
274 return nil, nil, ErrInvalidPublicKey
275 }
276
277 return
278 }
279
280
281
282
283
284
285
286 func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
287 if fips140only.Enabled {
288 panic("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
289 }
290
291
292
293 if pub.P.Sign() == 0 {
294 return false
295 }
296
297 if r.Sign() < 1 || r.Cmp(pub.Q) >= 0 {
298 return false
299 }
300 if s.Sign() < 1 || s.Cmp(pub.Q) >= 0 {
301 return false
302 }
303
304 w := new(big.Int).ModInverse(s, pub.Q)
305 if w == nil {
306 return false
307 }
308
309 n := pub.Q.BitLen()
310 if n%8 != 0 {
311 return false
312 }
313 z := new(big.Int).SetBytes(hash)
314
315 u1 := new(big.Int).Mul(z, w)
316 u1.Mod(u1, pub.Q)
317 u2 := w.Mul(r, w)
318 u2.Mod(u2, pub.Q)
319 v := u1.Exp(pub.G, u1, pub.P)
320 u2.Exp(pub.Y, u2, pub.P)
321 v.Mul(v, u2)
322 v.Mod(v, pub.P)
323 v.Mod(v, pub.Q)
324
325 return v.Cmp(r) == 0
326 }
327
View as plain text