-
Notifications
You must be signed in to change notification settings - Fork 56
/
sm9_key.go
438 lines (385 loc) · 13.5 KB
/
sm9_key.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package sm9
import (
"errors"
"io"
"math/big"
"sync"
"github.com/emmansun/gmsm/sm9/bn256"
"golang.org/x/crypto/cryptobyte"
)
// SignMasterPrivateKey master private key for sign, generated by KGC
type SignMasterPrivateKey struct {
SignMasterPublicKey // master public key
D *big.Int // master private key
}
// SignMasterPublicKey master public key for sign, generated by KGC
type SignMasterPublicKey struct {
MasterPublicKey *bn256.G2 // master public key
pairOnce sync.Once
basePoint *bn256.GT // the result of Pair(Gen1, pub.MasterPublicKey)
tableGenOnce sync.Once
table *[32 * 2]bn256.GTFieldTable // precomputed basePoint^n
}
// SignPrivateKey user private key for sign, generated by KGC
type SignPrivateKey struct {
PrivateKey *bn256.G1 // user private key
SignMasterPublicKey // master public key
}
// EncryptMasterPrivateKey master private key for encryption, generated by KGC
type EncryptMasterPrivateKey struct {
EncryptMasterPublicKey // master public key
D *big.Int // master private key
}
// EncryptMasterPublicKey master private key for encryption, generated by KGC
type EncryptMasterPublicKey struct {
MasterPublicKey *bn256.G1 // public key
pairOnce sync.Once
basePoint *bn256.GT // the result of Pair(pub.MasterPublicKey, Gen2)
tableGenOnce sync.Once
table *[32 * 2]bn256.GTFieldTable // precomputed basePoint^n
}
// EncryptPrivateKey user private key for encryption, generated by KGC
type EncryptPrivateKey struct {
PrivateKey *bn256.G2 // user private key
EncryptMasterPublicKey // master public key
}
// GenerateSignMasterKey generates a master public and private key pair for DSA usage.
func GenerateSignMasterKey(rand io.Reader) (*SignMasterPrivateKey, error) {
k, err := randFieldElement(rand)
if err != nil {
return nil, err
}
priv := new(SignMasterPrivateKey)
priv.D = k
priv.MasterPublicKey = new(bn256.G2).ScalarBaseMult(k)
return priv, nil
}
// MarshalASN1 marshal sign master private key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (master *SignMasterPrivateKey) MarshalASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BigInt(master.D)
return b.Bytes()
}
// UnmarshalASN1 unmarsal der data to sign master private key
func (master *SignMasterPrivateKey) UnmarshalASN1(der []byte) error {
input := cryptobyte.String(der)
d := &big.Int{}
if !input.ReadASN1Integer(d) || !input.Empty() {
return errors.New("sm9: invalid sign master key asn1 data")
}
master.D = d
master.MasterPublicKey = new(bn256.G2).ScalarBaseMult(d)
return nil
}
// GenerateUserKey generate an user dsa key.
func (master *SignMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*SignPrivateKey, error) {
var id []byte
id = append(id, uid...)
id = append(id, hid)
t1 := hashH1(id)
t1.Add(t1, master.D)
if t1.Sign() == 0 {
return nil, errors.New("sm9: need to re-generate sign master private key")
}
t1 = fermatInverse(t1, bn256.Order)
t2 := new(big.Int).Mul(t1, master.D)
t2.Mod(t2, bn256.Order)
priv := new(SignPrivateKey)
priv.SignMasterPublicKey = master.SignMasterPublicKey
priv.PrivateKey = new(bn256.G1).ScalarBaseMult(t2)
return priv, nil
}
// Public returns the public key corresponding to priv.
func (master *SignMasterPrivateKey) Public() *SignMasterPublicKey {
return &master.SignMasterPublicKey
}
// pair generate the basepoint once
func (pub *SignMasterPublicKey) pair() *bn256.GT {
pub.pairOnce.Do(func() {
pub.basePoint = bn256.Pair(bn256.Gen1, pub.MasterPublicKey)
})
return pub.basePoint
}
func (pub *SignMasterPublicKey) generatorTable() *[32 * 2]bn256.GTFieldTable {
pub.tableGenOnce.Do(func() {
pub.table = bn256.GenerateGTFieldTable(pub.pair())
})
return pub.table
}
// ScalarBaseMult compute basepoint^r with precomputed table
// The base point = pair(Gen1, <master public key>)
func (pub *SignMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT {
tables := pub.generatorTable()
return bn256.ScalarBaseMultGT(tables, r)
}
// GenerateUserPublicKey generate user sign public key
func (pub *SignMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G2 {
var buffer []byte
buffer = append(buffer, uid...)
buffer = append(buffer, hid)
h1 := hashH1(buffer)
p := new(bn256.G2).ScalarBaseMult(h1)
p.Add(p, pub.MasterPublicKey)
return p
}
// MarshalASN1 marshal sign master public key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (pub *SignMasterPublicKey) MarshalASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(pub.MasterPublicKey.MarshalUncompressed())
return b.Bytes()
}
// MarshalCompressedASN1 marshal sign master public key to asn.1 format data according
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (pub *SignMasterPublicKey) MarshalCompressedASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(pub.MasterPublicKey.MarshalCompressed())
return b.Bytes()
}
func unmarshalG2(bytes []byte) (*bn256.G2, error) {
g2 := new(bn256.G2)
switch bytes[0] {
case 4:
_, err := g2.Unmarshal(bytes[1:])
if err != nil {
return nil, err
}
case 2, 3:
_, err := g2.UnmarshalCompressed(bytes)
if err != nil {
return nil, err
}
default:
return nil, errors.New("sm9: invalid point identity byte")
}
return g2, nil
}
// UnmarshalASN1 unmarsal der data to sign master public key
func (pub *SignMasterPublicKey) UnmarshalASN1(der []byte) error {
var bytes []byte
input := cryptobyte.String(der)
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
return errors.New("sm9: invalid sign master public key asn1 data")
}
g2, err := unmarshalG2(bytes)
if err != nil {
return err
}
pub.MasterPublicKey = g2
return nil
}
// MasterPublic returns the master public key corresponding to priv.
func (priv *SignPrivateKey) MasterPublic() *SignMasterPublicKey {
return &priv.SignMasterPublicKey
}
// SetMasterPublicKey bind the sign master public key to it.
func (priv *SignPrivateKey) SetMasterPublicKey(pub *SignMasterPublicKey) {
if priv.SignMasterPublicKey.MasterPublicKey == nil {
priv.SignMasterPublicKey = *pub
}
}
// MarshalASN1 marshal sign user private key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (priv *SignPrivateKey) MarshalASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(priv.PrivateKey.MarshalUncompressed())
return b.Bytes()
}
// MarshalCompressedASN1 marshal sign user private key to asn.1 format data according
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (priv *SignPrivateKey) MarshalCompressedASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(priv.PrivateKey.MarshalCompressed())
return b.Bytes()
}
func unmarshalG1(bytes []byte) (*bn256.G1, error) {
g := new(bn256.G1)
switch bytes[0] {
case 4:
_, err := g.Unmarshal(bytes[1:])
if err != nil {
return nil, err
}
case 2, 3:
_, err := g.UnmarshalCompressed(bytes)
if err != nil {
return nil, err
}
default:
return nil, errors.New("sm9: invalid point identity byte")
}
return g, nil
}
// UnmarshalASN1 unmarsal der data to sign user private key
// Note, priv's SignMasterPublicKey should be handled separately.
func (priv *SignPrivateKey) UnmarshalASN1(der []byte) error {
var bytes []byte
input := cryptobyte.String(der)
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
return errors.New("sm9: invalid sign user private key asn1 data")
}
g, err := unmarshalG1(bytes)
if err != nil {
return err
}
priv.PrivateKey = g
return nil
}
// GenerateEncryptMasterKey generates a master public and private key pair for encryption usage.
func GenerateEncryptMasterKey(rand io.Reader) (*EncryptMasterPrivateKey, error) {
k, err := randFieldElement(rand)
if err != nil {
return nil, err
}
priv := new(EncryptMasterPrivateKey)
priv.D = k
priv.MasterPublicKey = new(bn256.G1).ScalarBaseMult(k)
return priv, nil
}
// GenerateUserKey generate an user key for encryption.
func (master *EncryptMasterPrivateKey) GenerateUserKey(uid []byte, hid byte) (*EncryptPrivateKey, error) {
var id []byte
id = append(id, uid...)
id = append(id, hid)
t1 := hashH1(id)
t1.Add(t1, master.D)
if t1.Sign() == 0 {
return nil, errors.New("sm9: need to re-generate encrypt master private key")
}
t1 = fermatInverse(t1, bn256.Order)
t2 := new(big.Int).Mul(t1, master.D)
t2.Mod(t2, bn256.Order)
priv := new(EncryptPrivateKey)
priv.EncryptMasterPublicKey = master.EncryptMasterPublicKey
priv.PrivateKey = new(bn256.G2).ScalarBaseMult(t2)
return priv, nil
}
// Public returns the public key corresponding to priv.
func (master *EncryptMasterPrivateKey) Public() *EncryptMasterPublicKey {
return &master.EncryptMasterPublicKey
}
// MarshalASN1 marshal encrypt master private key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (master *EncryptMasterPrivateKey) MarshalASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BigInt(master.D)
return b.Bytes()
}
// UnmarshalASN1 unmarsal der data to encrpt master private key
func (master *EncryptMasterPrivateKey) UnmarshalASN1(der []byte) error {
input := cryptobyte.String(der)
d := &big.Int{}
if !input.ReadASN1Integer(d) || !input.Empty() {
return errors.New("sm9: invalid encrpt master key asn1 data")
}
master.D = d
master.MasterPublicKey = new(bn256.G1).ScalarBaseMult(d)
return nil
}
// pair generate the basepoint once
func (pub *EncryptMasterPublicKey) pair() *bn256.GT {
pub.pairOnce.Do(func() {
pub.basePoint = bn256.Pair(pub.MasterPublicKey, bn256.Gen2)
})
return pub.basePoint
}
func (pub *EncryptMasterPublicKey) generatorTable() *[32 * 2]bn256.GTFieldTable {
pub.tableGenOnce.Do(func() {
pub.table = bn256.GenerateGTFieldTable(pub.pair())
})
return pub.table
}
// ScalarBaseMult compute basepoint^r with precomputed table.
// The base point = pair(<master public key>, Gen2)
func (pub *EncryptMasterPublicKey) ScalarBaseMult(r *big.Int) *bn256.GT {
tables := pub.generatorTable()
return bn256.ScalarBaseMultGT(tables, r)
}
// GenerateUserPublicKey generate user encrypt public key
func (pub *EncryptMasterPublicKey) GenerateUserPublicKey(uid []byte, hid byte) *bn256.G1 {
var buffer []byte
buffer = append(buffer, uid...)
buffer = append(buffer, hid)
h1 := hashH1(buffer)
p := new(bn256.G1).ScalarBaseMult(h1)
p.Add(p, pub.MasterPublicKey)
return p
}
// MarshalASN1 marshal encrypt master public key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (pub *EncryptMasterPublicKey) MarshalASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(pub.MasterPublicKey.MarshalUncompressed())
return b.Bytes()
}
// MarshalCompressedASN1 marshal encrypt master public key to asn.1 format data according
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (pub *EncryptMasterPublicKey) MarshalCompressedASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(pub.MasterPublicKey.MarshalCompressed())
return b.Bytes()
}
// UnmarshalASN1 unmarsal der data to encrypt master public key
func (pub *EncryptMasterPublicKey) UnmarshalASN1(der []byte) error {
var bytes []byte
input := cryptobyte.String(der)
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
return errors.New("sm9: invalid encrypt master public key asn1 data")
}
g, err := unmarshalG1(bytes)
if err != nil {
return err
}
pub.MasterPublicKey = g
return nil
}
// MasterPublic returns the master public key corresponding to priv.
func (priv *EncryptPrivateKey) MasterPublic() *EncryptMasterPublicKey {
return &priv.EncryptMasterPublicKey
}
// SetMasterPublicKey bind the encrypt master public key to it.
func (priv *EncryptPrivateKey) SetMasterPublicKey(pub *EncryptMasterPublicKey) {
if priv.EncryptMasterPublicKey.MasterPublicKey == nil {
priv.EncryptMasterPublicKey = *pub
}
}
// MarshalASN1 marshal encrypt user private key to asn.1 format data according
// SM9 cryptographic algorithm application specification
func (priv *EncryptPrivateKey) MarshalASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(priv.PrivateKey.MarshalUncompressed())
return b.Bytes()
}
// MarshalCompressedASN1 marshal encrypt user private key to asn.1 format data according
// SM9 cryptographic algorithm application specification, the curve point is in compressed form.
func (priv *EncryptPrivateKey) MarshalCompressedASN1() ([]byte, error) {
var b cryptobyte.Builder
b.AddASN1BitString(priv.PrivateKey.MarshalCompressed())
return b.Bytes()
}
// UnmarshalASN1 unmarsal der data to encrypt user private key
// Note, priv's EncryptMasterPublicKey should be handled separately.
func (priv *EncryptPrivateKey) UnmarshalASN1(der []byte) error {
var bytes []byte
input := cryptobyte.String(der)
if !input.ReadASN1BitStringAsBytes(&bytes) || !input.Empty() {
return errors.New("sm9: invalid encrypt user private key asn1 data")
}
g, err := unmarshalG2(bytes)
if err != nil {
return err
}
priv.PrivateKey = g
return nil
}
// fermatInverse calculates the inverse of k in GF(P) using Fermat's method
// (exponentiation modulo P - 2, per Euler's theorem). This has better
// constant-time properties than Euclid's method (implemented in
// math/big.Int.ModInverse and FIPS 186-4, Appendix C.1) although math/big
// itself isn't strictly constant-time so it's not perfect.
func fermatInverse(k, N *big.Int) *big.Int {
two := big.NewInt(2)
nMinus2 := new(big.Int).Sub(N, two)
return new(big.Int).Exp(k, nMinus2, N)
}