|
@@ -0,0 +1,350 @@
|
|
1
|
+package tencentsig
|
|
2
|
+
|
|
3
|
+// @see https://github.com/ThePiachu/Golang-Koblitz-elliptic-curve-DSA-library/blob/master/bitelliptic/bitelliptic.go
|
|
4
|
+
|
|
5
|
+import (
|
|
6
|
+ "crypto/elliptic"
|
|
7
|
+ "io"
|
|
8
|
+ "math/big"
|
|
9
|
+ "sync"
|
|
10
|
+)
|
|
11
|
+
|
|
12
|
+// A BitCurve represents a Koblitz Curve with a=0.
|
|
13
|
+// See http://www.hyperelliptic.org/EFD/g1p/auto-shortw.html
|
|
14
|
+type BitCurve struct {
|
|
15
|
+ P *big.Int // the order of the underlying field
|
|
16
|
+ N *big.Int // the order of the base point
|
|
17
|
+ B *big.Int // the constant of the BitCurve equation
|
|
18
|
+ Gx, Gy *big.Int // (x,y) of the base point
|
|
19
|
+ BitSize int // the size of the underlying field
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+func (BitCurve *BitCurve) Params() *elliptic.CurveParams {
|
|
23
|
+ return &elliptic.CurveParams{P: BitCurve.P, N: BitCurve.N, B: BitCurve.B, Gx: BitCurve.Gx, Gy: BitCurve.Gy, BitSize: BitCurve.BitSize}
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+// IsOnBitCurve returns true if the given (x,y) lies on the BitCurve.
|
|
27
|
+func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
|
|
28
|
+ // y² = x³ + b
|
|
29
|
+ y2 := new(big.Int).Mul(y, y) //y²
|
|
30
|
+ y2.Mod(y2, BitCurve.P) //y²%P
|
|
31
|
+
|
|
32
|
+ x3 := new(big.Int).Mul(x, x) //x²
|
|
33
|
+ x3.Mul(x3, x) //x³
|
|
34
|
+
|
|
35
|
+ x3.Add(x3, BitCurve.B) //x³+B
|
|
36
|
+ x3.Mod(x3, BitCurve.P) //(x³+B)%P
|
|
37
|
+
|
|
38
|
+ return x3.Cmp(y2) == 0
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+//TODO: double check if the function is okay
|
|
42
|
+// affineFromJacobian reverses the Jacobian transform. See the comment at the
|
|
43
|
+// top of the file.
|
|
44
|
+func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
|
|
45
|
+ zinv := new(big.Int).ModInverse(z, BitCurve.P)
|
|
46
|
+ zinvsq := new(big.Int).Mul(zinv, zinv)
|
|
47
|
+
|
|
48
|
+ xOut = new(big.Int).Mul(x, zinvsq)
|
|
49
|
+ xOut.Mod(xOut, BitCurve.P)
|
|
50
|
+ zinvsq.Mul(zinvsq, zinv)
|
|
51
|
+ yOut = new(big.Int).Mul(y, zinvsq)
|
|
52
|
+ yOut.Mod(yOut, BitCurve.P)
|
|
53
|
+ return
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+// Add returns the sum of (x1,y1) and (x2,y2)
|
|
57
|
+func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
|
|
58
|
+ z := new(big.Int).SetInt64(1)
|
|
59
|
+ return BitCurve.affineFromJacobian(BitCurve.addJacobian(x1, y1, z, x2, y2, z))
|
|
60
|
+}
|
|
61
|
+
|
|
62
|
+// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
|
|
63
|
+// (x2, y2, z2) and returns their sum, also in Jacobian form.
|
|
64
|
+func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
|
|
65
|
+ // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
|
|
66
|
+ z1z1 := new(big.Int).Mul(z1, z1)
|
|
67
|
+ z1z1.Mod(z1z1, BitCurve.P)
|
|
68
|
+ z2z2 := new(big.Int).Mul(z2, z2)
|
|
69
|
+ z2z2.Mod(z2z2, BitCurve.P)
|
|
70
|
+
|
|
71
|
+ u1 := new(big.Int).Mul(x1, z2z2)
|
|
72
|
+ u1.Mod(u1, BitCurve.P)
|
|
73
|
+ u2 := new(big.Int).Mul(x2, z1z1)
|
|
74
|
+ u2.Mod(u2, BitCurve.P)
|
|
75
|
+ h := new(big.Int).Sub(u2, u1)
|
|
76
|
+ if h.Sign() == -1 {
|
|
77
|
+ h.Add(h, BitCurve.P)
|
|
78
|
+ }
|
|
79
|
+ i := new(big.Int).Lsh(h, 1)
|
|
80
|
+ i.Mul(i, i)
|
|
81
|
+ j := new(big.Int).Mul(h, i)
|
|
82
|
+
|
|
83
|
+ s1 := new(big.Int).Mul(y1, z2)
|
|
84
|
+ s1.Mul(s1, z2z2)
|
|
85
|
+ s1.Mod(s1, BitCurve.P)
|
|
86
|
+ s2 := new(big.Int).Mul(y2, z1)
|
|
87
|
+ s2.Mul(s2, z1z1)
|
|
88
|
+ s2.Mod(s2, BitCurve.P)
|
|
89
|
+ r := new(big.Int).Sub(s2, s1)
|
|
90
|
+ if r.Sign() == -1 {
|
|
91
|
+ r.Add(r, BitCurve.P)
|
|
92
|
+ }
|
|
93
|
+ r.Lsh(r, 1)
|
|
94
|
+ v := new(big.Int).Mul(u1, i)
|
|
95
|
+
|
|
96
|
+ x3 := new(big.Int).Set(r)
|
|
97
|
+ x3.Mul(x3, x3)
|
|
98
|
+ x3.Sub(x3, j)
|
|
99
|
+ x3.Sub(x3, v)
|
|
100
|
+ x3.Sub(x3, v)
|
|
101
|
+ x3.Mod(x3, BitCurve.P)
|
|
102
|
+
|
|
103
|
+ y3 := new(big.Int).Set(r)
|
|
104
|
+ v.Sub(v, x3)
|
|
105
|
+ y3.Mul(y3, v)
|
|
106
|
+ s1.Mul(s1, j)
|
|
107
|
+ s1.Lsh(s1, 1)
|
|
108
|
+ y3.Sub(y3, s1)
|
|
109
|
+ y3.Mod(y3, BitCurve.P)
|
|
110
|
+
|
|
111
|
+ z3 := new(big.Int).Add(z1, z2)
|
|
112
|
+ z3.Mul(z3, z3)
|
|
113
|
+ z3.Sub(z3, z1z1)
|
|
114
|
+ if z3.Sign() == -1 {
|
|
115
|
+ z3.Add(z3, BitCurve.P)
|
|
116
|
+ }
|
|
117
|
+ z3.Sub(z3, z2z2)
|
|
118
|
+ if z3.Sign() == -1 {
|
|
119
|
+ z3.Add(z3, BitCurve.P)
|
|
120
|
+ }
|
|
121
|
+ z3.Mul(z3, h)
|
|
122
|
+ z3.Mod(z3, BitCurve.P)
|
|
123
|
+
|
|
124
|
+ return x3, y3, z3
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+// Double returns 2*(x,y)
|
|
128
|
+func (BitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
|
|
129
|
+ z1 := new(big.Int).SetInt64(1)
|
|
130
|
+ return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z1))
|
|
131
|
+}
|
|
132
|
+
|
|
133
|
+// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
|
|
134
|
+// returns its double, also in Jacobian form.
|
|
135
|
+func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
|
|
136
|
+ // See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
|
|
137
|
+
|
|
138
|
+ a := new(big.Int).Mul(x, x) //X1²
|
|
139
|
+ b := new(big.Int).Mul(y, y) //Y1²
|
|
140
|
+ c := new(big.Int).Mul(b, b) //B²
|
|
141
|
+
|
|
142
|
+ d := new(big.Int).Add(x, b) //X1+B
|
|
143
|
+ d.Mul(d, d) //(X1+B)²
|
|
144
|
+ d.Sub(d, a) //(X1+B)²-A
|
|
145
|
+ d.Sub(d, c) //(X1+B)²-A-C
|
|
146
|
+ d.Mul(d, big.NewInt(2)) //2*((X1+B)²-A-C)
|
|
147
|
+
|
|
148
|
+ e := new(big.Int).Mul(big.NewInt(3), a) //3*A
|
|
149
|
+ f := new(big.Int).Mul(e, e) //E²
|
|
150
|
+
|
|
151
|
+ x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D
|
|
152
|
+ x3.Sub(f, x3) //F-2*D
|
|
153
|
+ x3.Mod(x3, BitCurve.P)
|
|
154
|
+
|
|
155
|
+ y3 := new(big.Int).Sub(d, x3) //D-X3
|
|
156
|
+ y3.Mul(e, y3) //E*(D-X3)
|
|
157
|
+ y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C
|
|
158
|
+ y3.Mod(y3, BitCurve.P)
|
|
159
|
+
|
|
160
|
+ z3 := new(big.Int).Mul(y, z) //Y1*Z1
|
|
161
|
+ z3.Mul(big.NewInt(2), z3) //3*Y1*Z1
|
|
162
|
+ z3.Mod(z3, BitCurve.P)
|
|
163
|
+
|
|
164
|
+ return x3, y3, z3
|
|
165
|
+}
|
|
166
|
+
|
|
167
|
+//TODO: double check if it is okay
|
|
168
|
+// ScalarMult returns k*(Bx,By) where k is a number in big-endian form.
|
|
169
|
+func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
|
|
170
|
+ // We have a slight problem in that the identity of the group (the
|
|
171
|
+ // point at infinity) cannot be represented in (x, y) form on a finite
|
|
172
|
+ // machine. Thus the standard add/double algorithm has to be tweaked
|
|
173
|
+ // slightly: our initial state is not the identity, but x, and we
|
|
174
|
+ // ignore the first true bit in |k|. If we don't find any true bits in
|
|
175
|
+ // |k|, then we return nil, nil, because we cannot return the identity
|
|
176
|
+ // element.
|
|
177
|
+
|
|
178
|
+ Bz := new(big.Int).SetInt64(1)
|
|
179
|
+ x := Bx
|
|
180
|
+ y := By
|
|
181
|
+ z := Bz
|
|
182
|
+
|
|
183
|
+ seenFirstTrue := false
|
|
184
|
+ for _, b := range k {
|
|
185
|
+ for bitNum := 0; bitNum < 8; bitNum++ {
|
|
186
|
+ if seenFirstTrue {
|
|
187
|
+ x, y, z = BitCurve.doubleJacobian(x, y, z)
|
|
188
|
+ }
|
|
189
|
+ if b&0x80 == 0x80 {
|
|
190
|
+ if !seenFirstTrue {
|
|
191
|
+ seenFirstTrue = true
|
|
192
|
+ } else {
|
|
193
|
+ x, y, z = BitCurve.addJacobian(Bx, By, Bz, x, y, z)
|
|
194
|
+ }
|
|
195
|
+ }
|
|
196
|
+ b <<= 1
|
|
197
|
+ }
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ if !seenFirstTrue {
|
|
201
|
+ return nil, nil
|
|
202
|
+ }
|
|
203
|
+
|
|
204
|
+ return BitCurve.affineFromJacobian(x, y, z)
|
|
205
|
+}
|
|
206
|
+
|
|
207
|
+// ScalarBaseMult returns k*G, where G is the base point of the group and k is
|
|
208
|
+// an integer in big-endian form.
|
|
209
|
+func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
|
|
210
|
+ return BitCurve.ScalarMult(BitCurve.Gx, BitCurve.Gy, k)
|
|
211
|
+}
|
|
212
|
+
|
|
213
|
+var mask = []byte{0xff, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f}
|
|
214
|
+
|
|
215
|
+//TODO: double check if it is okay
|
|
216
|
+// GenerateKey returns a public/private key pair. The private key is generated
|
|
217
|
+// using the given reader, which must return random data.
|
|
218
|
+func (BitCurve *BitCurve) GenerateKey(rand io.Reader) (priv []byte, x, y *big.Int, err error) {
|
|
219
|
+ byteLen := (BitCurve.BitSize + 7) >> 3
|
|
220
|
+ priv = make([]byte, byteLen)
|
|
221
|
+
|
|
222
|
+ for x == nil {
|
|
223
|
+ _, err = io.ReadFull(rand, priv)
|
|
224
|
+ if err != nil {
|
|
225
|
+ return
|
|
226
|
+ }
|
|
227
|
+ // We have to mask off any excess bits in the case that the size of the
|
|
228
|
+ // underlying field is not a whole number of bytes.
|
|
229
|
+ priv[0] &= mask[BitCurve.BitSize%8]
|
|
230
|
+ // This is because, in tests, rand will return all zeros and we don't
|
|
231
|
+ // want to get the point at infinity and loop forever.
|
|
232
|
+ priv[1] ^= 0x42
|
|
233
|
+ x, y = BitCurve.ScalarBaseMult(priv)
|
|
234
|
+ }
|
|
235
|
+ return
|
|
236
|
+}
|
|
237
|
+
|
|
238
|
+// Marshal converts a point into the form specified in section 4.3.6 of ANSI
|
|
239
|
+// X9.62.
|
|
240
|
+func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte {
|
|
241
|
+ byteLen := (BitCurve.BitSize + 7) >> 3
|
|
242
|
+
|
|
243
|
+ ret := make([]byte, 1+2*byteLen)
|
|
244
|
+ ret[0] = 4 // uncompressed point
|
|
245
|
+
|
|
246
|
+ xBytes := x.Bytes()
|
|
247
|
+ copy(ret[1+byteLen-len(xBytes):], xBytes)
|
|
248
|
+ yBytes := y.Bytes()
|
|
249
|
+ copy(ret[1+2*byteLen-len(yBytes):], yBytes)
|
|
250
|
+ return ret
|
|
251
|
+}
|
|
252
|
+
|
|
253
|
+// Unmarshal converts a point, serialised by Marshal, into an x, y pair. On
|
|
254
|
+// error, x = nil.
|
|
255
|
+func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) {
|
|
256
|
+ byteLen := (BitCurve.BitSize + 7) >> 3
|
|
257
|
+ if len(data) != 1+2*byteLen {
|
|
258
|
+ return
|
|
259
|
+ }
|
|
260
|
+ if data[0] != 4 { // uncompressed form
|
|
261
|
+ return
|
|
262
|
+ }
|
|
263
|
+ x = new(big.Int).SetBytes(data[1 : 1+byteLen])
|
|
264
|
+ y = new(big.Int).SetBytes(data[1+byteLen:])
|
|
265
|
+ return
|
|
266
|
+}
|
|
267
|
+
|
|
268
|
+//curve parameters taken from:
|
|
269
|
+//http://www.secg.org/collateral/sec2_final.pdf
|
|
270
|
+
|
|
271
|
+var initonce sync.Once
|
|
272
|
+var secp160k1 *BitCurve
|
|
273
|
+var secp192k1 *BitCurve
|
|
274
|
+var secp224k1 *BitCurve
|
|
275
|
+var secp256k1 *BitCurve
|
|
276
|
+
|
|
277
|
+func initAll() {
|
|
278
|
+ initS160()
|
|
279
|
+ initS192()
|
|
280
|
+ initS224()
|
|
281
|
+ initS256()
|
|
282
|
+}
|
|
283
|
+
|
|
284
|
+func initS160() {
|
|
285
|
+ // See SEC 2 section 2.4.1
|
|
286
|
+ secp160k1 = new(BitCurve)
|
|
287
|
+ secp160k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", 16)
|
|
288
|
+ secp160k1.N, _ = new(big.Int).SetString("0100000000000000000001B8FA16DFAB9ACA16B6B3", 16)
|
|
289
|
+ secp160k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000007", 16)
|
|
290
|
+ secp160k1.Gx, _ = new(big.Int).SetString("3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", 16)
|
|
291
|
+ secp160k1.Gy, _ = new(big.Int).SetString("938CF935318FDCED6BC28286531733C3F03C4FEE", 16)
|
|
292
|
+ secp160k1.BitSize = 160
|
|
293
|
+}
|
|
294
|
+
|
|
295
|
+func initS192() {
|
|
296
|
+ // See SEC 2 section 2.5.1
|
|
297
|
+ secp192k1 = new(BitCurve)
|
|
298
|
+ secp192k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", 16)
|
|
299
|
+ secp192k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", 16)
|
|
300
|
+ secp192k1.B, _ = new(big.Int).SetString("000000000000000000000000000000000000000000000003", 16)
|
|
301
|
+ secp192k1.Gx, _ = new(big.Int).SetString("DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", 16)
|
|
302
|
+ secp192k1.Gy, _ = new(big.Int).SetString("9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", 16)
|
|
303
|
+ secp192k1.BitSize = 192
|
|
304
|
+}
|
|
305
|
+
|
|
306
|
+func initS224() {
|
|
307
|
+ // See SEC 2 section 2.6.1
|
|
308
|
+ secp224k1 = new(BitCurve)
|
|
309
|
+ secp224k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D", 16)
|
|
310
|
+ secp224k1.N, _ = new(big.Int).SetString("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7", 16)
|
|
311
|
+ secp224k1.B, _ = new(big.Int).SetString("00000000000000000000000000000000000000000000000000000005", 16)
|
|
312
|
+ secp224k1.Gx, _ = new(big.Int).SetString("A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C", 16)
|
|
313
|
+ secp224k1.Gy, _ = new(big.Int).SetString("7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5", 16)
|
|
314
|
+ secp224k1.BitSize = 224
|
|
315
|
+}
|
|
316
|
+
|
|
317
|
+func initS256() {
|
|
318
|
+ // See SEC 2 section 2.7.1
|
|
319
|
+ secp256k1 = new(BitCurve)
|
|
320
|
+ secp256k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16)
|
|
321
|
+ secp256k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
|
|
322
|
+ secp256k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16)
|
|
323
|
+ secp256k1.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16)
|
|
324
|
+ secp256k1.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16)
|
|
325
|
+ secp256k1.BitSize = 256
|
|
326
|
+}
|
|
327
|
+
|
|
328
|
+// S160 returns a BitCurve which implements secp160k1 (see SEC 2 section 2.4.1)
|
|
329
|
+func S160() *BitCurve {
|
|
330
|
+ initonce.Do(initAll)
|
|
331
|
+ return secp160k1
|
|
332
|
+}
|
|
333
|
+
|
|
334
|
+// S192 returns a BitCurve which implements secp192k1 (see SEC 2 section 2.5.1)
|
|
335
|
+func S192() *BitCurve {
|
|
336
|
+ initonce.Do(initAll)
|
|
337
|
+ return secp192k1
|
|
338
|
+}
|
|
339
|
+
|
|
340
|
+// S224 returns a BitCurve which implements secp224k1 (see SEC 2 section 2.6.1)
|
|
341
|
+func S224() *BitCurve {
|
|
342
|
+ initonce.Do(initAll)
|
|
343
|
+ return secp224k1
|
|
344
|
+}
|
|
345
|
+
|
|
346
|
+// S256 returns a BitCurve which implements secp256k1 (see SEC 2 section 2.7.1)
|
|
347
|
+func S256() *BitCurve {
|
|
348
|
+ initonce.Do(initAll)
|
|
349
|
+ return secp256k1
|
|
350
|
+}
|