Skip to content

Commit 69c5d98

Browse files
committed
fixed ecdh
1 parent 7bb9e47 commit 69c5d98

File tree

7 files changed

+76
-84
lines changed

7 files changed

+76
-84
lines changed

ecdh/ecdh.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ func (k *PublicKey) SM2ZA(h func() hash.Hash, uid []byte) ([]byte, error) {
113113
return c.SM2ZA(h, k, uid)
114114
}
115115

116-
return nil, errors.New("crypto/ecdh: public key do not support SM2ZA")
116+
return nil, errors.New("go-cryptobin/ecdh: public key do not support SM2ZA")
117117
}
118118

119119
// SM2SharedKey performs SM2 key derivation to generate shared keying data, the k was generated by SM2MQV.
120120
func (k *PublicKey) SM2SharedKey(sPub, sRemote *PublicKey, uid, remoteUID []byte, kenLen int) ([]byte, error) {
121121
if _, ok := k.NamedCurve.(SM2ZACurve); !ok {
122-
return nil, errors.New("crypto/ecdh: public key do not support SM2ZA")
122+
return nil, errors.New("go-cryptobin/ecdh: public key do not support SM2ZA")
123123
}
124124

125125
var buffer [128]byte
@@ -166,7 +166,7 @@ type PrivateKey struct {
166166
// the result is the all-zero value, ECDH returns an error.
167167
func (k *PrivateKey) ECDH(remote *PublicKey) ([]byte, error) {
168168
if k.NamedCurve != remote.NamedCurve {
169-
return nil, errors.New("crypto/ecdh: private key and public key curves do not match")
169+
return nil, errors.New("go-cryptobin/ecdh: private key and public key curves do not match")
170170
}
171171

172172
return k.NamedCurve.ECDH(k, remote)
@@ -178,7 +178,7 @@ func (k *PrivateKey) ECMQV(eLocal *PrivateKey, sRemote, eRemote *PublicKey) (*Pu
178178
return c.ECMQV(k, eLocal, sRemote, eRemote)
179179
}
180180

181-
return nil, errors.New("crypto/ecdh: private key do not support ECMQV")
181+
return nil, errors.New("go-cryptobin/ecdh: private key do not support ECMQV")
182182
}
183183

184184
// Bytes returns a copy of the encoding of the private key.

ecdh/gmsm2.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ func (c *gmsm2Curve) GenerateKey(rand io.Reader) (*PrivateKey, error) {
3232

3333
size := (key.Curve.Params().N.BitLen() + 7) / 8
3434
if key.D.BitLen() > size*8 {
35-
return nil, errors.New("crypto/ecdh: invalid private key")
35+
return nil, errors.New("go-cryptobin/ecdh: invalid private key")
3636
}
3737

3838
return c.NewPrivateKey(key.D.FillBytes(make([]byte, size)))
3939
}
4040

4141
func (c *gmsm2Curve) NewPrivateKey(key []byte) (*PrivateKey, error) {
4242
if len(key) != 32 {
43-
return nil, errors.New("crypto/ecdh: invalid private key size")
43+
return nil, errors.New("go-cryptobin/ecdh: invalid private key size")
4444
}
4545

4646
if isZero(key) {
47-
return nil, errors.New("crypto/ecdh: invalid private key")
47+
return nil, errors.New("go-cryptobin/ecdh: invalid private key")
4848
}
4949

5050
return &PrivateKey{
@@ -55,7 +55,7 @@ func (c *gmsm2Curve) NewPrivateKey(key []byte) (*PrivateKey, error) {
5555

5656
func (c *gmsm2Curve) PrivateKeyToPublicKey(key *PrivateKey) *PublicKey {
5757
if key.NamedCurve != c {
58-
panic("crypto/ecdh: converting the wrong key type")
58+
panic("go-cryptobin/ecdh: converting the wrong key type")
5959
}
6060

6161
curve := sm2.P256()
@@ -64,7 +64,7 @@ func (c *gmsm2Curve) PrivateKeyToPublicKey(key *PrivateKey) *PublicKey {
6464

6565
publicKey := elliptic.Marshal(curve, x, y)
6666
if len(publicKey) == 1 {
67-
panic("crypto/ecdh: nistec ScalarBaseMult returned the identity")
67+
panic("go-cryptobin/ecdh: nistec ScalarBaseMult returned the identity")
6868
}
6969

7070
k := &PublicKey{
@@ -77,7 +77,7 @@ func (c *gmsm2Curve) PrivateKeyToPublicKey(key *PrivateKey) *PublicKey {
7777

7878
func (c *gmsm2Curve) NewPublicKey(key []byte) (*PublicKey, error) {
7979
if len(key) == 0 || key[0] != 4 {
80-
return nil, errors.New("crypto/ecdh: invalid public key")
80+
return nil, errors.New("go-cryptobin/ecdh: invalid public key")
8181
}
8282

8383
return &PublicKey{
@@ -92,7 +92,7 @@ func (c *gmsm2Curve) ECDH(local *PrivateKey, remote *PublicKey) ([]byte, error)
9292
// 公钥
9393
xx, yy := elliptic.Unmarshal(curve, remote.Bytes())
9494
if xx == nil {
95-
return nil, errors.New("crypto/ecdh: failed to unmarshal elliptic curve point")
95+
return nil, errors.New("go-cryptobin/ecdh: failed to unmarshal elliptic curve point")
9696
}
9797

9898
x, _ := curve.ScalarMult(xx, yy, local.Bytes())
@@ -170,7 +170,7 @@ func isZero(a []byte) bool {
170170
func SM2PublicKeyToECDH(pub *sm2.PublicKey) (*PublicKey, error) {
171171
publicKey := elliptic.Marshal(sm2.P256(), pub.X, pub.Y)
172172
if len(publicKey) == 1 {
173-
return nil, errors.New("crypto/ecdh: sm2 PublicKey error")
173+
return nil, errors.New("go-cryptobin/ecdh: sm2 PublicKey error")
174174
}
175175

176176
return GmSM2().NewPublicKey(publicKey)
@@ -180,7 +180,7 @@ func SM2PublicKeyToECDH(pub *sm2.PublicKey) (*PublicKey, error) {
180180
func SM2PrivateKeyToECDH(pri *sm2.PrivateKey) (*PrivateKey, error) {
181181
size := (pri.Curve.Params().N.BitLen() + 7) / 8
182182
if pri.D.BitLen() > size*8 {
183-
return nil, errors.New("crypto/ecdh: invalid private key")
183+
return nil, errors.New("go-cryptobin/ecdh: invalid private key")
184184
}
185185

186186
key := pri.D.FillBytes(make([]byte, size))

ecdh/key.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ var (
2222
oidPublicKeyGmSM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301}
2323
)
2424

25-
// 私钥 - 包装
25+
// PrivateKey info
2626
type pkcs8 struct {
2727
Version int
2828
Algo pkix.AlgorithmIdentifier
2929
PrivateKey []byte
3030
Attributes []asn1.RawValue `asn1:"optional,tag:0"`
3131
}
3232

33-
// 公钥 - 包装
33+
// pkix PublicKey info
3434
type pkixPublicKey struct {
3535
Algo pkix.AlgorithmIdentifier
3636
BitString asn1.BitString
3737
}
3838

39-
// 公钥信息 - 解析
39+
// publicKey Info
4040
type publicKeyInfo struct {
4141
Raw asn1.RawContent
4242
Algorithm pkix.AlgorithmIdentifier
@@ -50,7 +50,7 @@ type ecPrivateKey struct {
5050
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
5151
}
5252

53-
// 包装公钥
53+
// Marshal PublicKey
5454
func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
5555
switch pub.Curve() {
5656
case X448():
@@ -79,55 +79,53 @@ func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
7979

8080
pubkey, err := sm2.MarshalPublicKey(pubblic)
8181
if err != nil {
82-
return nil, errors.New("ecdsa: failed to marshal algo param: " + err.Error())
82+
return nil, errors.New("go-cryptobin/ecdh: failed to marshal algo param: " + err.Error())
8383
}
8484

8585
return pubkey, nil
8686
default:
8787
pubkey, err := ToPublicKey(pub)
8888
if err != nil {
89-
return nil, fmt.Errorf("ecdh: failed to marshal public key: %v", err)
89+
return nil, fmt.Errorf("go-cryptobin/ecdh: failed to marshal public key: %v", err)
9090
}
9191

9292
public, err := x509.MarshalPKIXPublicKey(pubkey)
9393
if err != nil {
94-
return nil, fmt.Errorf("ecdh: failed to marshal public key: %v", err)
94+
return nil, fmt.Errorf("go-cryptobin/ecdh: failed to marshal public key: %v", err)
9595
}
9696

9797
return public, nil
9898
}
9999
}
100100

101-
// 解析公钥
101+
// Parse PublicKey
102102
func ParsePublicKey(derBytes []byte) (*PublicKey, error) {
103103
var pki publicKeyInfo
104104
if rest, err := asn1.Unmarshal(derBytes, &pki); err != nil {
105105
return nil, err
106106
} else if len(rest) != 0 {
107-
return nil, errors.New("ecdh: trailing data after ASN.1 of public-key")
107+
return nil, errors.New("go-cryptobin/ecdh: trailing data after ASN.1 of public-key")
108108
}
109109

110-
keyData := &pki
111-
112-
oid := keyData.Algorithm.Algorithm
113-
params := keyData.Algorithm.Parameters
114-
der := cryptobyte.String(keyData.PublicKey.RightAlign())
110+
oid := pki.Algorithm.Algorithm
111+
params := pki.Algorithm.Parameters
112+
der := cryptobyte.String(pki.PublicKey.RightAlign())
115113

116114
switch {
117115
case oid.Equal(oidPublicKeyX448):
118116
if len(params.FullBytes) != 0 {
119-
return nil, errors.New("ecdh: X25519 key encoded with illegal parameters")
117+
return nil, errors.New("go-cryptobin/ecdh: X25519 key encoded with illegal parameters")
120118
}
121119

122120
return X448().NewPublicKey(der)
123121
default:
124-
// 先判断 SM2 证书
122+
// check SM2 cert
125123
if oid.Equal(oidPublicKeyECDSA) {
126124
paramsDer := cryptobyte.String(params.FullBytes)
127125

128126
namedCurveOID := new(asn1.ObjectIdentifier)
129127
if !paramsDer.ReadASN1ObjectIdentifier(namedCurveOID) {
130-
return nil, errors.New("ecdh: invalid ECDH parameters")
128+
return nil, errors.New("go-cryptobin/ecdh: invalid ECDH parameters")
131129
}
132130

133131
if oidPublicKeyGmSM2.Equal(*namedCurveOID) {
@@ -137,7 +135,7 @@ func ParsePublicKey(derBytes []byte) (*PublicKey, error) {
137135
}
138136
}
139137

140-
// 其他 EC 曲线
138+
// other EC curve
141139
key, err := x509.ParsePKIXPublicKey(derBytes)
142140
if err != nil {
143141
return nil, err
@@ -158,14 +156,14 @@ func ParsePublicKey(derBytes []byte) (*PublicKey, error) {
158156
}
159157
}
160158

161-
return nil, errors.New("ecdh: unknown public key algorithm")
159+
return nil, errors.New("go-cryptobin/ecdh: unknown public key algorithm")
162160
}
163161

164162
}
165163

166164
// ====================
167165

168-
// 包装私钥
166+
// Marshal PrivateKey
169167
func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
170168
var privKey pkcs8
171169

@@ -176,7 +174,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
176174
}
177175
var err error
178176
if privKey.PrivateKey, err = asn1.Marshal(key.Bytes()); err != nil {
179-
return nil, fmt.Errorf("ecdh: failed to marshal private key: %v", err)
177+
return nil, fmt.Errorf("go-cryptobin/ecdh: failed to marshal private key: %v", err)
180178
}
181179
case GmSM2():
182180
c := sm2.P256()
@@ -190,19 +188,19 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
190188

191189
private, err := sm2.MarshalPrivateKey(pri)
192190
if err != nil {
193-
return nil, errors.New("ecdsa: failed to marshal algo param: " + err.Error())
191+
return nil, errors.New("go-cryptobin/ecdh: failed to marshal algo param: " + err.Error())
194192
}
195193

196194
return private, nil
197195
default:
198196
prikey, err := ToPrivateKey(key)
199197
if err != nil {
200-
return nil, fmt.Errorf("ecdh: failed to marshal private key: %v", err)
198+
return nil, fmt.Errorf("go-cryptobin/ecdh: failed to marshal private key: %v", err)
201199
}
202200

203201
private, err := x509.MarshalPKCS8PrivateKey(prikey)
204202
if err != nil {
205-
return nil, fmt.Errorf("ecdh: failed to marshal private key: %v", err)
203+
return nil, fmt.Errorf("go-cryptobin/ecdh: failed to marshal private key: %v", err)
206204
}
207205

208206
return private, nil
@@ -211,7 +209,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
211209
return asn1.Marshal(privKey)
212210
}
213211

214-
// 解析私钥
212+
// Parse PrivateKey
215213
func ParsePrivateKey(der []byte) (*PrivateKey, error) {
216214
var privKey pkcs8
217215
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
@@ -221,17 +219,17 @@ func ParsePrivateKey(der []byte) (*PrivateKey, error) {
221219
switch {
222220
case privKey.Algo.Algorithm.Equal(oidPublicKeyX448):
223221
if l := len(privKey.Algo.Parameters.FullBytes); l != 0 {
224-
return nil, errors.New("ecdh: invalid X448 private key parameters")
222+
return nil, errors.New("go-cryptobin/ecdh: invalid X448 private key parameters")
225223
}
226224

227225
var curvePrivateKey []byte
228226
if _, err := asn1.Unmarshal(privKey.PrivateKey, &curvePrivateKey); err != nil {
229-
return nil, fmt.Errorf("ecdh: invalid X448 private key: %v", err)
227+
return nil, fmt.Errorf("go-cryptobin/ecdh: invalid X448 private key: %v", err)
230228
}
231229

232230
return X448().NewPrivateKey(curvePrivateKey)
233231
default:
234-
// 先判断 SM2 证书
232+
// check SM2 cert
235233
if privKey.Algo.Algorithm.Equal(oidPublicKeyECDSA) {
236234
bytes := privKey.Algo.Parameters.FullBytes
237235

@@ -247,7 +245,7 @@ func ParsePrivateKey(der []byte) (*PrivateKey, error) {
247245
}
248246
}
249247

250-
// 其他 EC 曲线
248+
// other EC curve
251249
key, err := x509.ParsePKCS8PrivateKey(der)
252250
if err != nil {
253251
return nil, err
@@ -267,13 +265,12 @@ func ParsePrivateKey(der []byte) (*PrivateKey, error) {
267265
}
268266
}
269267

270-
return nil, fmt.Errorf("ecdh: PKCS#8 wrapping contained private key with unknown algorithm: %v", privKey.Algo.Algorithm)
268+
return nil, fmt.Errorf("go-cryptobin/ecdh: PKCS#8 wrapping contained private key with unknown algorithm: %v", privKey.Algo.Algorithm)
271269
}
272270
}
273271

274272
// ================
275273

276-
// 格式转换
277274
func FromPrivateKey(key *crypto_ecdh.PrivateKey) (*PrivateKey, error) {
278275
switch key.Curve() {
279276
case crypto_ecdh.P256():
@@ -286,7 +283,7 @@ func FromPrivateKey(key *crypto_ecdh.PrivateKey) (*PrivateKey, error) {
286283
return X25519().NewPrivateKey(key.Bytes())
287284
}
288285

289-
return nil, fmt.Errorf("ecdh: PrivateKey is not support")
286+
return nil, fmt.Errorf("go-cryptobin/ecdh: PrivateKey is not support")
290287
}
291288

292289
func ToPrivateKey(key *PrivateKey) (*crypto_ecdh.PrivateKey, error) {
@@ -302,12 +299,12 @@ func ToPrivateKey(key *PrivateKey) (*crypto_ecdh.PrivateKey, error) {
302299
case X25519():
303300
newCurve = crypto_ecdh.X25519()
304301
default:
305-
return nil, fmt.Errorf("ecdh: unknown key type while marshaling PKCS#8: %T", key)
302+
return nil, fmt.Errorf("go-cryptobin/ecdh: unknown key type while marshaling PKCS#8: %T", key)
306303
}
307304

308305
prikey, err := newCurve.NewPrivateKey(key.Bytes())
309306
if err != nil {
310-
return nil, fmt.Errorf("ecdh: failed to marshal private key: %v", err)
307+
return nil, fmt.Errorf("go-cryptobin/ecdh: failed to marshal private key: %v", err)
311308
}
312309

313310
return prikey, nil
@@ -325,7 +322,7 @@ func FromPublicKey(pub *crypto_ecdh.PublicKey) (*PublicKey, error) {
325322
return X25519().NewPublicKey(pub.Bytes())
326323
}
327324

328-
return nil, fmt.Errorf("ecdh: PublicKey is not support")
325+
return nil, fmt.Errorf("go-cryptobin/ecdh: PublicKey is not support")
329326
}
330327

331328
func ToPublicKey(pub *PublicKey) (*crypto_ecdh.PublicKey, error) {
@@ -341,12 +338,12 @@ func ToPublicKey(pub *PublicKey) (*crypto_ecdh.PublicKey, error) {
341338
case X25519():
342339
newCurve = crypto_ecdh.X25519()
343340
default:
344-
return nil, fmt.Errorf("ecdh: unsupported public key type: %T", pub)
341+
return nil, fmt.Errorf("go-cryptobin/ecdh: unsupported public key type: %T", pub)
345342
}
346343

347344
pubkey, err := newCurve.NewPublicKey(pub.Bytes())
348345
if err != nil {
349-
return nil, fmt.Errorf("ecdh: failed to marshal public key: %v", err)
346+
return nil, fmt.Errorf("go-cryptobin/ecdh: failed to marshal public key: %v", err)
350347
}
351348

352349
return pubkey, nil

0 commit comments

Comments
 (0)