-
Notifications
You must be signed in to change notification settings - Fork 43
/
sm2.go
176 lines (154 loc) · 4.76 KB
/
sm2.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
package codec
import (
"bytes"
"github.com/pkg/errors"
"github.com/yaklang/yaklang/common/gmsm/sm2"
"github.com/yaklang/yaklang/common/gmsm/x509"
)
import (
cryptoRand "crypto/rand"
)
func GenerateSM2PrivateKeyPEM() ([]byte, []byte, error) {
pkey, err := sm2.GenerateKey(cryptoRand.Reader)
if err != nil {
return nil, nil, errors.Wrap(err, "sm2.GenerateKey(cryptoRand.Reader)")
}
pKeyBytes, err := x509.WritePrivateKeyToPem(pkey, nil)
if err != nil {
return nil, nil, errors.Wrap(err, "write sm2.privateKey to pem")
}
pubKeyBytes, err := x509.WritePublicKeyToPem(pkey.Public().(*sm2.PublicKey))
if err != nil {
return nil, nil, errors.Wrap(err, "write sm2.publicKey to pem")
}
return pKeyBytes, pubKeyBytes, nil
}
func GenerateSM2PrivateKeyHEX() ([]byte, []byte, error) {
for i := 0; i < 10; i++ {
pkey, err := sm2.GenerateKey(cryptoRand.Reader)
if err != nil {
return nil, nil, errors.Wrap(err, "sm2.GenerateKey(cryptoRand.Reader)")
}
pKeyBytes := []byte(x509.WritePrivateKeyToHex(pkey))
pubKeyBytes := []byte(x509.WritePublicKeyToHex(pkey.Public().(*sm2.PublicKey)))
data, err := SM2EncryptC1C2C3(pubKeyBytes, []byte("abc"))
if err != nil {
continue
}
decData, err := SM2DecryptC1C2C3(pKeyBytes, data)
if err != nil {
continue
}
if string(decData) != "abc" {
continue
}
return pKeyBytes, pubKeyBytes, nil
}
return nil, nil, errors.New("generate sm2 private key failed")
}
func SM2EncryptC1C2C3(pubKey []byte, data []byte) ([]byte, error) {
//x509.ReadPublicKeyFromHex()
var pub *sm2.PublicKey
var err error
if bytes.HasPrefix(pubKey, []byte("---")) {
pub, err = x509.ReadPublicKeyFromPem(pubKey)
} else {
pub, err = x509.ReadPublicKeyFromHex(string(pubKey))
}
if err != nil {
return nil, errors.Wrap(err, "read sm2.publicKey")
}
results, err := sm2.Encrypt(pub, data, cryptoRand.Reader, sm2.C1C2C3)
if err != nil {
return nil, errors.Wrap(err, "sm2.Encrypt[C1C2C3] with pubkey")
}
return results, nil
}
func SM2DecryptC1C2C3(priKey []byte, data []byte) ([]byte, error) {
return SM2DecryptC1C2C3WithPassword(priKey, data, nil)
}
func SM2DecryptC1C2C3WithPassword(priKey []byte, data []byte, password []byte) ([]byte, error) {
var pri *sm2.PrivateKey
var err error
if bytes.HasPrefix(priKey, []byte("---")) {
pri, err = x509.ReadPrivateKeyFromPem(priKey, password)
} else {
pri, err = x509.ReadPrivateKeyFromHex(string(priKey))
}
if err != nil {
return nil, errors.Wrap(err, "read sm2.privateKey")
}
results, err := sm2.Decrypt(pri, data, sm2.C1C2C3)
if err != nil {
return nil, errors.Wrap(err, "sm2.Decrypt[C1C2C3] with prikey")
}
return results, nil
}
func SM2EncryptC1C3C2(pubKey []byte, data []byte) ([]byte, error) {
//x509.ReadPublicKeyFromHex()
pubKey = bytes.TrimSpace(pubKey)
var pub *sm2.PublicKey
var err error
if bytes.HasPrefix(pubKey, []byte("---")) {
pub, err = x509.ReadPublicKeyFromPem(pubKey)
} else {
pub, err = x509.ReadPublicKeyFromHex(string(pubKey))
}
if err != nil {
return nil, errors.Wrap(err, "read sm2.publicKey")
}
results, err := sm2.Encrypt(pub, data, cryptoRand.Reader, sm2.C1C3C2)
if err != nil {
return nil, errors.Wrap(err, "sm2.Encrypt[C1C3C2] with pubkey")
}
return results, nil
}
func SM2DecryptC1C3C2(priKey []byte, data []byte) ([]byte, error) {
return SM2DecryptC1C3C2WithPassword(priKey, data, nil)
}
func SM2DecryptC1C3C2WithPassword(priKey []byte, data []byte, password []byte) ([]byte, error) {
var pri *sm2.PrivateKey
var err error
if bytes.HasPrefix(priKey, []byte("---")) {
pri, err = x509.ReadPrivateKeyFromPem(priKey, password)
} else {
pri, err = x509.ReadPrivateKeyFromHex(string(priKey))
}
if err != nil {
return nil, errors.Wrap(err, "read sm2.privateKey")
}
results, err := sm2.Decrypt(pri, data, sm2.C1C3C2)
if err != nil {
return nil, errors.Wrap(err, "sm2.Decrypt[C1C3C2] with pubkey")
}
return results, nil
}
func SM2EncryptASN1(pubKey []byte, data []byte) ([]byte, error) {
var pub *sm2.PublicKey
var err error
if bytes.HasPrefix(pubKey, []byte("---")) {
pub, err = x509.ReadPublicKeyFromPem(pubKey)
} else {
pub, err = x509.ReadPublicKeyFromHex(string(pubKey))
}
if err != nil {
return nil, errors.Wrap(err, "read sm2.publicKey")
}
return sm2.EncryptAsn1(pub, data, cryptoRand.Reader)
}
func SM2DecryptASN1(priKey []byte, data []byte) ([]byte, error) {
return SM2DecryptASN1WithPassword(priKey, data, nil)
}
func SM2DecryptASN1WithPassword(priKey []byte, data []byte, password []byte) ([]byte, error) {
var pri *sm2.PrivateKey
var err error
if bytes.HasPrefix(priKey, []byte("---")) {
pri, err = x509.ReadPrivateKeyFromPem(priKey, password)
} else {
pri, err = x509.ReadPrivateKeyFromHex(string(priKey))
}
if err != nil {
return nil, errors.Wrap(err, "read sm2.privateKey")
}
return sm2.DecryptAsn1(pri, data)
}