@@ -27,13 +27,17 @@ import (
27
27
"io"
28
28
"math/big"
29
29
30
+ "golang.org/x/crypto/cryptobyte"
31
+ cbasn1 "golang.org/x/crypto/cryptobyte/asn1"
32
+
30
33
"github.com/tjfoc/gmsm/sm3"
31
34
)
32
35
33
36
var (
34
37
default_uid = []byte {0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 }
35
- C1C3C2 = 0
36
- C1C2C3 = 1
38
+
39
+ C1C3C2 = 0
40
+ C1C2C3 = 1
37
41
)
38
42
39
43
type PublicKey struct {
@@ -46,9 +50,6 @@ type PrivateKey struct {
46
50
D * big.Int
47
51
}
48
52
49
- type sm2Signature struct {
50
- R , S * big.Int
51
- }
52
53
type sm2Cipher struct {
53
54
XCoordinate * big.Int
54
55
YCoordinate * big.Int
@@ -71,16 +72,28 @@ func (priv *PrivateKey) Sign(random io.Reader, msg []byte, signer crypto.SignerO
71
72
if err != nil {
72
73
return nil , err
73
74
}
74
- return asn1 .Marshal (sm2Signature {r , s })
75
- }
76
-
77
- func (pub * PublicKey ) Verify (msg []byte , sign []byte ) bool {
78
- var sm2Sign sm2Signature
79
- _ , err := asn1 .Unmarshal (sign , & sm2Sign )
80
- if err != nil {
75
+ var b cryptobyte.Builder
76
+ b .AddASN1 (cbasn1 .SEQUENCE , func (b * cryptobyte.Builder ) {
77
+ b .AddASN1BigInt (r )
78
+ b .AddASN1BigInt (s )
79
+ })
80
+ return b .Bytes ()
81
+ }
82
+
83
+ func (pub * PublicKey ) Verify (msg []byte , sig []byte ) bool {
84
+ var (
85
+ r , s = & big.Int {}, & big.Int {}
86
+ inner cryptobyte.String
87
+ )
88
+ input := cryptobyte .String (sig )
89
+ if ! input .ReadASN1 (& inner , cbasn1 .SEQUENCE ) ||
90
+ ! input .Empty () ||
91
+ ! inner .ReadASN1Integer (r ) ||
92
+ ! inner .ReadASN1Integer (s ) ||
93
+ ! inner .Empty () {
81
94
return false
82
95
}
83
- return Sm2Verify (pub , msg , default_uid , sm2Sign . R , sm2Sign . S )
96
+ return Sm2Verify (pub , msg , default_uid , r , s )
84
97
}
85
98
86
99
func (pub * PublicKey ) Sm3Digest (msg , uid []byte ) ([]byte , error ) {
@@ -244,7 +257,7 @@ func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
244
257
* hash
245
258
* CipherText
246
259
*/
247
- func Encrypt (pub * PublicKey , data []byte , random io.Reader ,mode int ) ([]byte , error ) {
260
+ func Encrypt (pub * PublicKey , data []byte , random io.Reader , mode int ) ([]byte , error ) {
248
261
length := len (data )
249
262
for {
250
263
c := []byte {}
@@ -287,42 +300,40 @@ func Encrypt(pub *PublicKey, data []byte, random io.Reader,mode int) ([]byte, er
287
300
for i := 0 ; i < length ; i ++ {
288
301
c [96 + i ] ^= data [i ]
289
302
}
290
- switch mode {
291
-
303
+ switch mode {
304
+
292
305
case C1C3C2 :
293
306
return append ([]byte {0x04 }, c ... ), nil
294
307
case C1C2C3 :
295
308
c1 := make ([]byte , 64 )
296
- c2 := make ([]byte , len (c ) - 96 )
309
+ c2 := make ([]byte , len (c )- 96 )
297
310
c3 := make ([]byte , 32 )
298
- copy (c1 , c [:64 ])//x1,y1
299
- copy (c3 , c [64 :96 ])//hash
300
- copy (c2 , c [96 :])//密文
311
+ copy (c1 , c [:64 ]) //x1,y1
312
+ copy (c3 , c [64 :96 ]) //hash
313
+ copy (c2 , c [96 :]) //密文
301
314
ciphertext := []byte {}
302
315
ciphertext = append (ciphertext , c1 ... )
303
316
ciphertext = append (ciphertext , c2 ... )
304
317
ciphertext = append (ciphertext , c3 ... )
305
318
return append ([]byte {0x04 }, ciphertext ... ), nil
306
- default :
319
+ default :
307
320
return append ([]byte {0x04 }, c ... ), nil
321
+ }
308
322
}
309
323
}
310
- }
311
-
312
324
313
-
314
- func Decrypt (priv * PrivateKey , data []byte ,mode int ) ([]byte , error ) {
325
+ func Decrypt (priv * PrivateKey , data []byte , mode int ) ([]byte , error ) {
315
326
switch mode {
316
327
case C1C3C2 :
317
328
data = data [1 :]
318
- case C1C2C3 :
329
+ case C1C2C3 :
319
330
data = data [1 :]
320
331
c1 := make ([]byte , 64 )
321
- c2 := make ([]byte , len (data ) - 96 )
332
+ c2 := make ([]byte , len (data )- 96 )
322
333
c3 := make ([]byte , 32 )
323
- copy (c1 , data [:64 ])//x1,y1
324
- copy (c2 , data [64 :len (data ) - 32 ])//密文
325
- copy (c3 , data [len (data ) - 32 :])//hash
334
+ copy (c1 , data [:64 ]) //x1,y1
335
+ copy (c2 , data [64 :len (data )- 32 ]) //密文
336
+ copy (c3 , data [len (data )- 32 :]) //hash
326
337
c := []byte {}
327
338
c = append (c , c1 ... )
328
339
c = append (c , c3 ... )
@@ -362,8 +373,6 @@ func Decrypt(priv *PrivateKey, data []byte,mode int) ([]byte, error) {
362
373
return c , nil
363
374
}
364
375
365
-
366
-
367
376
// keyExchange 为SM2密钥交换算法的第二部和第三步复用部分,协商的双方均调用此函数计算共同的字节串
368
377
// klen: 密钥长度
369
378
// ida, idb: 协商双方的标识,ida为密钥协商算法发起方标识,idb为响应方标识
@@ -479,7 +488,7 @@ func zeroByteSlice() []byte {
479
488
sm2加密,返回asn.1编码格式的密文内容
480
489
*/
481
490
func EncryptAsn1 (pub * PublicKey , data []byte , rand io.Reader ) ([]byte , error ) {
482
- cipher , err := Encrypt (pub , data , rand ,C1C3C2 )
491
+ cipher , err := Encrypt (pub , data , rand , C1C3C2 )
483
492
if err != nil {
484
493
return nil , err
485
494
}
@@ -494,7 +503,7 @@ func DecryptAsn1(pub *PrivateKey, data []byte) ([]byte, error) {
494
503
if err != nil {
495
504
return nil , err
496
505
}
497
- return Decrypt (pub , cipher ,C1C3C2 )
506
+ return Decrypt (pub , cipher , C1C3C2 )
498
507
}
499
508
500
509
/*
@@ -670,5 +679,5 @@ func getLastBit(a *big.Int) uint {
670
679
671
680
// crypto.Decrypter
672
681
func (priv * PrivateKey ) Decrypt (_ io.Reader , msg []byte , _ crypto.DecrypterOpts ) (plaintext []byte , err error ) {
673
- return Decrypt (priv , msg ,C1C3C2 )
682
+ return Decrypt (priv , msg , C1C3C2 )
674
683
}
0 commit comments