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