-
Notifications
You must be signed in to change notification settings - Fork 22
/
sign.go
398 lines (302 loc) · 8.96 KB
/
sign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package sm9
import (
"io"
"fmt"
"errors"
"crypto"
"math/big"
"encoding/asn1"
"github.com/deatil/go-cryptobin/gm/sm9/sm9curve"
)
// 默认 HID
const DefaultSignHid byte = 0x01
type hashMode int
const (
// hashmode used in h1: 0x01
H1 hashMode = iota + 1
// hashmode used in h2: 0x02
H2
)
// G2Bytes = G2.Marshal()
type SignMasterPublicKey struct {
Mpk *sm9curve.G2
}
// Equal reports whether pub and x have the same value.
func (pub *SignMasterPublicKey) Equal(x crypto.PublicKey) bool {
xx, ok := x.(*SignMasterPublicKey)
if !ok {
return false
}
return pub.Mpk.Equal(xx.Mpk)
}
func (pub *SignMasterPublicKey) Verify(uid []byte, hid byte, hash, sig []byte) bool {
return VerifyASN1(pub, uid, hid, hash, sig)
}
func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) (*sm9curve.G2, error) {
n := sm9curve.Order
uidh := append(uid, hid)
h := hash(uidh, n, H1)
qb, err := new(sm9curve.G2).ScalarBaseMult(sm9curve.NormalizeScalar(h.Bytes()))
if err != nil {
return nil, err
}
qb.Add(qb, pub.Mpk)
return qb, nil
}
func (pub *SignMasterPublicKey) Marshal() []byte {
return pub.Mpk.MarshalUncompressed()
}
func (pub *SignMasterPublicKey) Unmarshal(bytes []byte) (err error) {
g := new(sm9curve.G2)
_, err = g.UnmarshalUncompressed(bytes)
if err != nil {
return err
}
pub.Mpk = g
return
}
// 压缩明文
func (pub *SignMasterPublicKey) MarshalCompress() []byte {
return pub.Mpk.MarshalCompressed()
}
// 解压缩明文
func (pub *SignMasterPublicKey) UnmarshalCompress(bytes []byte) (err error) {
g := new(sm9curve.G2)
_, err = g.UnmarshalCompressed(bytes)
if err != nil {
return err
}
pub.Mpk = g
return
}
// SignMasterPrivateKey contains a master secret key and a master public key.
type SignMasterPrivateKey struct {
SignMasterPublicKey
D *big.Int
}
// Equal reports whether priv and x have the same value.
func (priv *SignMasterPrivateKey) Equal(x crypto.PrivateKey) bool {
xx, ok := x.(*SignMasterPrivateKey)
if !ok {
return false
}
return priv.SignMasterPublicKey.Equal(&xx.SignMasterPublicKey) &&
bigIntEqual(priv.D, xx.D)
}
func (priv *SignMasterPrivateKey) PublicKey() *SignMasterPublicKey {
return &priv.SignMasterPublicKey
}
// Public returns the public key corresponding to priv.
func (priv *SignMasterPrivateKey) Public() crypto.PublicKey {
return priv.PublicKey()
}
// generate user's secret key.
func (priv *SignMasterPrivateKey) GenerateUserKey(id []byte, hid byte) (uk *SignPrivateKey, err error) {
id = append(id, hid)
n := sm9curve.Order
t1 := hash(id, n, H1)
t1.Add(t1, priv.D)
// if t1 = 0, we need to regenerate the master key.
if t1.BitLen() == 0 || t1.Cmp(n) == 0 {
return nil, errors.New("need to regen MasterPrivateKey!")
}
t1.ModInverse(t1, n)
// t2 = s*t1^-1
t2 := new(big.Int).Mul(priv.D, t1)
uk = new(SignPrivateKey)
uk.Sk, err = new(sm9curve.G1).ScalarBaseMult(sm9curve.NormalizeScalar(t2.Bytes()))
uk.Mpk = priv.Mpk
return
}
func (priv *SignMasterPrivateKey) Marshal() []byte {
return priv.D.Bytes()
}
func (priv *SignMasterPrivateKey) Unmarshal(bytes []byte) (err error) {
priv.D = new(big.Int).SetBytes(bytes)
d := new(big.Int).SetBytes(sm9curve.NormalizeScalar(bytes))
priv.Mpk, err = new(sm9curve.G2).ScalarBaseMult(sm9curve.NormalizeScalar(d.Bytes()))
return
}
// SignPrivateKey contains a secret key.
// G1Bytes = G1.Marshal()
type SignPrivateKey struct {
Sk *sm9curve.G1
SignMasterPublicKey
}
// Equal reports whether priv and x have the same value.
func (priv *SignPrivateKey) Equal(x crypto.PrivateKey) bool {
xx, ok := x.(*SignPrivateKey)
if !ok {
return false
}
return priv.Sk.Equal(xx.Sk)
}
func (priv *SignPrivateKey) PublicKey() *SignMasterPublicKey {
return &priv.SignMasterPublicKey
}
// Public returns the public key corresponding to priv.
func (priv *SignPrivateKey) Public() crypto.PublicKey {
return priv.PublicKey()
}
// Sign
func (priv *SignPrivateKey) Sign(rand io.Reader, hash []byte) ([]byte, error) {
return SignASN1(rand, priv, hash)
}
func (priv *SignPrivateKey) Marshal() []byte {
if priv.Mpk == nil {
return nil
}
pub := priv.Mpk.MarshalUncompressed()
return append(priv.Sk.MarshalUncompressed(), pub...)
}
func (priv *SignPrivateKey) Unmarshal(bytes []byte) (err error) {
var pub []byte
g1 := new(sm9curve.G1)
pub, err = g1.UnmarshalUncompressed(bytes)
if err != nil {
return err
}
if len(pub) == 0 {
return errors.New("private key need publickey bytes")
}
priv.Sk = g1
g2 := new(sm9curve.G2)
_, err = g2.UnmarshalUncompressed(pub)
if err != nil {
return err
}
priv.Mpk = g2
return
}
// generate master key for KGC(Key Generate Center).
func GenerateSignMasterKey(rand io.Reader) (mk *SignMasterPrivateKey, err error) {
s, err := randFieldElement(rand, sm9curve.Order)
if err != nil {
return nil, errors.New(fmt.Sprintf("gen rand num err: %s", err))
}
mk = new(SignMasterPrivateKey)
mk.D = new(big.Int).Set(s)
mk.Mpk, err = new(sm9curve.G2).ScalarBaseMult(sm9curve.NormalizeScalar(s.Bytes()))
return
}
// generate user's secret key.
func GenerateSignUserKey(mk *SignMasterPrivateKey, id []byte, hid byte) (*SignPrivateKey, error) {
return mk.GenerateUserKey(id, hid)
}
// 解析签名主公钥明文
func NewSignMasterPublicKey(bytes []byte) (pub *SignMasterPublicKey, err error) {
pub = new(SignMasterPublicKey)
err = pub.Unmarshal(bytes)
return
}
// 输出签名主公钥明文
func ToSignMasterPublicKey(pub *SignMasterPublicKey) []byte {
return pub.Marshal()
}
// 解析签名主私钥明文
func NewSignMasterPrivateKey(bytes []byte) (priv *SignMasterPrivateKey, err error) {
priv = new(SignMasterPrivateKey)
err = priv.Unmarshal(bytes)
return
}
// 输出签名主私钥明文
func ToSignMasterPrivateKey(priv *SignMasterPrivateKey) []byte {
return priv.Marshal()
}
// 解析签名私钥明文
func NewSignPrivateKey(bytes []byte) (priv *SignPrivateKey, err error) {
priv = new(SignPrivateKey)
err = priv.Unmarshal(bytes)
return
}
// 输出签名私钥明文
func ToSignPrivateKey(priv *SignPrivateKey) []byte {
return priv.Marshal()
}
// sm9 sign algorithm:
// A1:compute g = e(P1,Ppub);
// A2:choose random num r in [1,n-1];
// A3:compute w = g^r;
// A4:compute h = H2(M||w,n);
// A5:compute l = (r-h) mod n, if l = 0 goto A2;
// A6:compute S = l·sk.
func Sign(rand io.Reader, pri *SignPrivateKey, msg []byte) (h *big.Int, s *sm9curve.G1, err error) {
n := sm9curve.Order
g := sm9curve.Pair(sm9curve.Gen1, pri.Mpk)
Regen:
r, err := randFieldElement(rand, n)
if err != nil {
return nil, nil, errors.New(fmt.Sprintf("gen rand num failed: %s", err))
}
w := new(sm9curve.GT).ScalarMult(g, r)
wBytes := w.Marshal()
msg = append(msg, wBytes...)
hashed := hash(msg, n, H2)
h = new(big.Int).Set(hashed)
l := new(big.Int).Sub(r, hashed)
l.Mod(l, n)
if l.BitLen() == 0 {
goto Regen
}
s, err = new(sm9curve.G1).ScalarMult(pri.Sk, l.Bytes())
return
}
// sm9 verify algorithm(given h',S', message M' and user's id):
// B1:compute g = e(P1,Ppub);
// B2:compute t = g^h';
// B3:compute h1 = H1(id||hid,n);
// B4:compute P = h1·P2+Ppub;
// B5:compute u = e(S',P);
// B6:compute w' = u·t;
// B7:compute h2 = H2(M'||w',n), check if h2 = h'.
func Verify(pub *SignMasterPublicKey, id []byte, hid byte, msg []byte, h *big.Int, s *sm9curve.G1) bool {
n := sm9curve.Order
g := sm9curve.Pair(sm9curve.Gen1, pub.Mpk)
t := new(sm9curve.GT).ScalarMult(g, h)
id = append(id, hid)
h1 := hash(id, n, H1)
P, err := new(sm9curve.G2).ScalarBaseMult(sm9curve.NormalizeScalar(h1.Bytes()))
if err != nil {
return false
}
P.Add(P, pub.Mpk)
u := sm9curve.Pair(s, P)
w := new(sm9curve.GT).Add(u, t)
wBytes := w.Marshal()
msg = append(msg, wBytes...)
h2 := hash(msg, n, H2)
if h2.Cmp(h) != 0 {
return false
}
return true
}
type sigData struct {
H []byte
S asn1.BitString
}
func SignASN1(rand io.Reader, priv *SignPrivateKey, hash []byte) ([]byte, error) {
h, s, err := Sign(rand, priv, hash)
if err != nil {
return nil, err
}
r := sigData{
H: h.Bytes(),
S: asn1.BitString{
Bytes: s.MarshalUncompressed(),
},
}
return asn1.Marshal(r)
}
func VerifyASN1(pub *SignMasterPublicKey, uid []byte, hid byte, hash, sig []byte) bool {
var data sigData
if _, err := asn1.Unmarshal(sig, &data); err != nil {
return false
}
s := new(sm9curve.G1)
_, err := s.UnmarshalUncompressed(data.S.Bytes)
if err != nil {
return false
}
h := new(big.Int).SetBytes(data.H)
return Verify(pub, uid, hid, hash, h, s)
}