Skip to content

Commit 36b992c

Browse files
authored
Merge pull request #148 from easyops-cn/cryptobyte
chore: use golang.org/x/crypto/cryptobyte
2 parents 7c7d82b + 3c94660 commit 36b992c

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

sm2/sm2.go

+44-35
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ import (
2727
"io"
2828
"math/big"
2929

30+
"golang.org/x/crypto/cryptobyte"
31+
cbasn1 "golang.org/x/crypto/cryptobyte/asn1"
32+
3033
"github.com/tjfoc/gmsm/sm3"
3134
)
3235

3336
var (
3437
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
3741
)
3842

3943
type PublicKey struct {
@@ -46,9 +50,6 @@ type PrivateKey struct {
4650
D *big.Int
4751
}
4852

49-
type sm2Signature struct {
50-
R, S *big.Int
51-
}
5253
type sm2Cipher struct {
5354
XCoordinate *big.Int
5455
YCoordinate *big.Int
@@ -71,16 +72,28 @@ func (priv *PrivateKey) Sign(random io.Reader, msg []byte, signer crypto.SignerO
7172
if err != nil {
7273
return nil, err
7374
}
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() {
8194
return false
8295
}
83-
return Sm2Verify(pub, msg, default_uid, sm2Sign.R, sm2Sign.S)
96+
return Sm2Verify(pub, msg, default_uid, r, s)
8497
}
8598

8699
func (pub *PublicKey) Sm3Digest(msg, uid []byte) ([]byte, error) {
@@ -244,7 +257,7 @@ func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
244257
* hash
245258
* CipherText
246259
*/
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) {
248261
length := len(data)
249262
for {
250263
c := []byte{}
@@ -287,42 +300,40 @@ func Encrypt(pub *PublicKey, data []byte, random io.Reader,mode int) ([]byte, er
287300
for i := 0; i < length; i++ {
288301
c[96+i] ^= data[i]
289302
}
290-
switch mode{
291-
303+
switch mode {
304+
292305
case C1C3C2:
293306
return append([]byte{0x04}, c...), nil
294307
case C1C2C3:
295308
c1 := make([]byte, 64)
296-
c2 := make([]byte, len(c) - 96)
309+
c2 := make([]byte, len(c)-96)
297310
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:]) //密文
301314
ciphertext := []byte{}
302315
ciphertext = append(ciphertext, c1...)
303316
ciphertext = append(ciphertext, c2...)
304317
ciphertext = append(ciphertext, c3...)
305318
return append([]byte{0x04}, ciphertext...), nil
306-
default:
319+
default:
307320
return append([]byte{0x04}, c...), nil
321+
}
308322
}
309323
}
310-
}
311-
312324

313-
314-
func Decrypt(priv *PrivateKey, data []byte,mode int) ([]byte, error) {
325+
func Decrypt(priv *PrivateKey, data []byte, mode int) ([]byte, error) {
315326
switch mode {
316327
case C1C3C2:
317328
data = data[1:]
318-
case C1C2C3:
329+
case C1C2C3:
319330
data = data[1:]
320331
c1 := make([]byte, 64)
321-
c2 := make([]byte, len(data) - 96)
332+
c2 := make([]byte, len(data)-96)
322333
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
326337
c := []byte{}
327338
c = append(c, c1...)
328339
c = append(c, c3...)
@@ -362,8 +373,6 @@ func Decrypt(priv *PrivateKey, data []byte,mode int) ([]byte, error) {
362373
return c, nil
363374
}
364375

365-
366-
367376
// keyExchange 为SM2密钥交换算法的第二部和第三步复用部分,协商的双方均调用此函数计算共同的字节串
368377
// klen: 密钥长度
369378
// ida, idb: 协商双方的标识,ida为密钥协商算法发起方标识,idb为响应方标识
@@ -479,7 +488,7 @@ func zeroByteSlice() []byte {
479488
sm2加密,返回asn.1编码格式的密文内容
480489
*/
481490
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)
483492
if err != nil {
484493
return nil, err
485494
}
@@ -494,7 +503,7 @@ func DecryptAsn1(pub *PrivateKey, data []byte) ([]byte, error) {
494503
if err != nil {
495504
return nil, err
496505
}
497-
return Decrypt(pub, cipher,C1C3C2)
506+
return Decrypt(pub, cipher, C1C3C2)
498507
}
499508

500509
/*
@@ -670,5 +679,5 @@ func getLastBit(a *big.Int) uint {
670679

671680
// crypto.Decrypter
672681
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)
674683
}

sm2/utils.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Decompress(a []byte) *PublicKey {
2020

2121
y2 := sm2P256ToBig(&xx3)
2222
y := new(big.Int).ModSqrt(y2, sm2P256.P)
23-
if getLastBit(y)!= uint(a[0]) {
23+
if getLastBit(y) != uint(a[0]) {
2424
y.Sub(sm2P256.P, y)
2525
}
2626
return &PublicKey{
@@ -41,7 +41,9 @@ func Compress(a *PublicKey) []byte {
4141
return buf
4242
}
4343

44-
44+
type sm2Signature struct {
45+
R, S *big.Int
46+
}
4547

4648
func SignDigitToSignData(r, s *big.Int) ([]byte, error) {
4749
return asn1.Marshal(sm2Signature{r, s})
@@ -56,4 +58,3 @@ func SignDataToSignDigit(sign []byte) (*big.Int, *big.Int, error) {
5658
}
5759
return sm2Sign.R, sm2Sign.S, nil
5860
}
59-

0 commit comments

Comments
 (0)