1
2
3
4
5
6
7
8
9
10
11 package ecdsa
12
13
14
15
16
17
18
19
20
21
22 import (
23 "crypto"
24 "crypto/aes"
25 "crypto/cipher"
26 "crypto/elliptic"
27 "crypto/internal/randutil"
28 "crypto/sha512"
29 "errors"
30 "io"
31 "math/big"
32
33 "golang.org/x/crypto/cryptobyte"
34 "golang.org/x/crypto/cryptobyte/asn1"
35 )
36
37
38 type invertible interface {
39
40 Inverse(k *big.Int) *big.Int
41 }
42
43
44 type combinedMult interface {
45
46 CombinedMult(Px, Py *big.Int, s1, s2 []byte) (x, y *big.Int)
47 }
48
49 const (
50 aesIV = "IV for ECDSA CTR"
51 )
52
53
54 type PublicKey struct {
55 elliptic.Curve
56 X, Y *big.Int
57 }
58
59
60
61
62
63
64
65
66
67 func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
68 xx, ok := x.(*PublicKey)
69 if !ok {
70 return false
71 }
72 return pub.X.Cmp(xx.X) == 0 && pub.Y.Cmp(xx.Y) == 0 &&
73
74
75
76
77 pub.Curve == xx.Curve
78 }
79
80
81 type PrivateKey struct {
82 PublicKey
83 D *big.Int
84 }
85
86
87 func (priv *PrivateKey) Public() crypto.PublicKey {
88 return &priv.PublicKey
89 }
90
91
92
93
94 func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool {
95 xx, ok := x.(*PrivateKey)
96 if !ok {
97 return false
98 }
99 return priv.PublicKey.Equal(&xx.PublicKey) && priv.D.Cmp(xx.D) == 0
100 }
101
102
103
104
105
106
107
108
109 func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
110 r, s, err := Sign(rand, priv, digest)
111 if err != nil {
112 return nil, err
113 }
114
115 var b cryptobyte.Builder
116 b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
117 b.AddASN1BigInt(r)
118 b.AddASN1BigInt(s)
119 })
120 return b.Bytes()
121 }
122
123 var one = new(big.Int).SetInt64(1)
124
125
126
127 func randFieldElement(c elliptic.Curve, rand io.Reader) (k *big.Int, err error) {
128 params := c.Params()
129
130
131 b := make([]byte, params.BitSize/8+8)
132 _, err = io.ReadFull(rand, b)
133 if err != nil {
134 return
135 }
136
137 k = new(big.Int).SetBytes(b)
138 n := new(big.Int).Sub(params.N, one)
139 k.Mod(k, n)
140 k.Add(k, one)
141 return
142 }
143
144
145 func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error) {
146 k, err := randFieldElement(c, rand)
147 if err != nil {
148 return nil, err
149 }
150
151 priv := new(PrivateKey)
152 priv.PublicKey.Curve = c
153 priv.D = k
154 priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
155 return priv, nil
156 }
157
158
159
160
161 func hashToInt(hash []byte, c elliptic.Curve) *big.Int {
162 orderBits := c.Params().N.BitLen()
163 orderBytes := (orderBits + 7) / 8
164 if len(hash) > orderBytes {
165 hash = hash[:orderBytes]
166 }
167
168 ret := new(big.Int).SetBytes(hash)
169 excess := len(hash)*8 - orderBits
170 if excess > 0 {
171 ret.Rsh(ret, uint(excess))
172 }
173 return ret
174 }
175
176
177
178
179
180
181 func fermatInverse(k, N *big.Int) *big.Int {
182 two := big.NewInt(2)
183 nMinus2 := new(big.Int).Sub(N, two)
184 return new(big.Int).Exp(k, nMinus2, N)
185 }
186
187 var errZeroParam = errors.New("zero parameter")
188
189
190
191
192
193
194 func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) {
195 randutil.MaybeReadByte(rand)
196
197
198
199
200
201
202
203
204
205
206
207
208
209 entropy := make([]byte, 32)
210 _, err = io.ReadFull(rand, entropy)
211 if err != nil {
212 return
213 }
214
215
216 md := sha512.New()
217 md.Write(priv.D.Bytes())
218 md.Write(entropy)
219 md.Write(hash)
220 key := md.Sum(nil)[:32]
221
222
223
224 block, err := aes.NewCipher(key)
225 if err != nil {
226 return nil, nil, err
227 }
228
229
230
231 csprng := cipher.StreamReader{
232 R: zeroReader,
233 S: cipher.NewCTR(block, []byte(aesIV)),
234 }
235
236 c := priv.PublicKey.Curve
237 return sign(priv, &csprng, c, hash)
238 }
239
240 func signGeneric(priv *PrivateKey, csprng *cipher.StreamReader, c elliptic.Curve, hash []byte) (r, s *big.Int, err error) {
241
242 N := c.Params().N
243 if N.Sign() == 0 {
244 return nil, nil, errZeroParam
245 }
246 var k, kInv *big.Int
247 for {
248 for {
249 k, err = randFieldElement(c, *csprng)
250 if err != nil {
251 r = nil
252 return
253 }
254
255 if in, ok := priv.Curve.(invertible); ok {
256 kInv = in.Inverse(k)
257 } else {
258 kInv = fermatInverse(k, N)
259 }
260
261 r, _ = priv.Curve.ScalarBaseMult(k.Bytes())
262 r.Mod(r, N)
263 if r.Sign() != 0 {
264 break
265 }
266 }
267
268 e := hashToInt(hash, c)
269 s = new(big.Int).Mul(priv.D, r)
270 s.Add(s, e)
271 s.Mul(s, kInv)
272 s.Mod(s, N)
273 if s.Sign() != 0 {
274 break
275 }
276 }
277
278 return
279 }
280
281
282
283
284
285 func SignASN1(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, error) {
286 return priv.Sign(rand, hash, nil)
287 }
288
289
290
291
292 func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
293 c := pub.Curve
294 N := c.Params().N
295
296 if r.Sign() <= 0 || s.Sign() <= 0 {
297 return false
298 }
299 if r.Cmp(N) >= 0 || s.Cmp(N) >= 0 {
300 return false
301 }
302 return verify(pub, c, hash, r, s)
303 }
304
305 func verifyGeneric(pub *PublicKey, c elliptic.Curve, hash []byte, r, s *big.Int) bool {
306
307 e := hashToInt(hash, c)
308 var w *big.Int
309 N := c.Params().N
310 if in, ok := c.(invertible); ok {
311 w = in.Inverse(s)
312 } else {
313 w = new(big.Int).ModInverse(s, N)
314 }
315
316 u1 := e.Mul(e, w)
317 u1.Mod(u1, N)
318 u2 := w.Mul(r, w)
319 u2.Mod(u2, N)
320
321
322 var x, y *big.Int
323 if opt, ok := c.(combinedMult); ok {
324 x, y = opt.CombinedMult(pub.X, pub.Y, u1.Bytes(), u2.Bytes())
325 } else {
326 x1, y1 := c.ScalarBaseMult(u1.Bytes())
327 x2, y2 := c.ScalarMult(pub.X, pub.Y, u2.Bytes())
328 x, y = c.Add(x1, y1, x2, y2)
329 }
330
331 if x.Sign() == 0 && y.Sign() == 0 {
332 return false
333 }
334 x.Mod(x, N)
335 return x.Cmp(r) == 0
336 }
337
338
339
340 func VerifyASN1(pub *PublicKey, hash, sig []byte) bool {
341 var (
342 r, s = &big.Int{}, &big.Int{}
343 inner cryptobyte.String
344 )
345 input := cryptobyte.String(sig)
346 if !input.ReadASN1(&inner, asn1.SEQUENCE) ||
347 !input.Empty() ||
348 !inner.ReadASN1Integer(r) ||
349 !inner.ReadASN1Integer(s) ||
350 !inner.Empty() {
351 return false
352 }
353 return Verify(pub, hash, r, s)
354 }
355
356 type zr struct {
357 io.Reader
358 }
359
360
361 func (z *zr) Read(dst []byte) (n int, err error) {
362 for i := range dst {
363 dst[i] = 0
364 }
365 return len(dst), nil
366 }
367
368 var zeroReader = &zr{}
369
View as plain text