Skip to content

Commit 8372d74

Browse files
committed
fixed
1 parent cbf17be commit 8372d74

File tree

14 files changed

+564
-549
lines changed

14 files changed

+564
-549
lines changed

gm/sm2/key_pkcs1.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,3 @@ func parseSM2PrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *
121121

122122
return priv, nil
123123
}
124-
125-
var (
126-
oidNamedCurveP256SM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301}
127-
)
128-
129-
func namedCurveFromOID(oid asn1.ObjectIdentifier) elliptic.Curve {
130-
switch {
131-
case oid.Equal(oidNamedCurveP256SM2):
132-
return P256()
133-
}
134-
135-
return nil
136-
}
137-
138-
func oidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) {
139-
switch curve {
140-
case P256():
141-
return oidNamedCurveP256SM2, true
142-
}
143-
144-
return nil, false
145-
}

gm/sm2/key_pkcs8.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sm2
33
import (
44
"errors"
55
"encoding/asn1"
6+
"crypto/elliptic"
67
"crypto/x509/pkix"
78

89
"github.com/deatil/go-cryptobin/gm/sm2/sm2curve"
@@ -150,3 +151,25 @@ func MarshalPublicKey(key *PublicKey) ([]byte, error) {
150151

151152
return asn1.Marshal(r)
152153
}
154+
155+
var (
156+
oidNamedCurveP256SM2 = asn1.ObjectIdentifier{1, 2, 156, 10197, 1, 301}
157+
)
158+
159+
func namedCurveFromOID(oid asn1.ObjectIdentifier) elliptic.Curve {
160+
switch {
161+
case oid.Equal(oidNamedCurveP256SM2):
162+
return P256()
163+
}
164+
165+
return nil
166+
}
167+
168+
func oidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) {
169+
switch curve {
170+
case P256():
171+
return oidNamedCurveP256SM2, true
172+
}
173+
174+
return nil, false
175+
}

pubkey/bign/pkcs1.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
package bign
22

33
import (
4+
"fmt"
45
"errors"
6+
"math/big"
7+
"encoding/asn1"
8+
"crypto/elliptic"
59
)
610

11+
const ecPrivKeyVersion = 1
12+
13+
// Per RFC 5915 the NamedCurveOID is marked as ASN.1 OPTIONAL, however in
14+
// most cases it is not.
15+
type ecPrivateKey struct {
16+
Version int
17+
PrivateKey []byte
18+
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
19+
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
20+
}
21+
722
// ParseECPrivateKey parses an EC private key in SEC 1, ASN.1 DER form.
823
//
924
// This kind of key is commonly encoded in PEM blocks of type "EC PRIVATE KEY".
@@ -24,3 +39,78 @@ func MarshalECPrivateKey(key *PrivateKey) ([]byte, error) {
2439

2540
return marshalECPrivateKeyWithOID(key, oid)
2641
}
42+
43+
// marshalECPrivateKeyWithOID marshals an SM2 private key into ASN.1, DER format and
44+
// sets the curve ID to the given OID, or omits it if OID is nil.
45+
func marshalECPrivateKeyWithOID(key *PrivateKey, oid asn1.ObjectIdentifier) ([]byte, error) {
46+
if !key.Curve.IsOnCurve(key.X, key.Y) {
47+
return nil, errors.New("bign: invalid elliptic key public key")
48+
}
49+
50+
privateKey := make([]byte, bitsToBytes(key.D.BitLen()))
51+
52+
return asn1.Marshal(ecPrivateKey{
53+
Version: ecPrivKeyVersion,
54+
PrivateKey: key.D.FillBytes(privateKey),
55+
NamedCurveOID: oid,
56+
PublicKey: asn1.BitString{
57+
Bytes: elliptic.Marshal(key.Curve, key.X, key.Y),
58+
},
59+
})
60+
}
61+
62+
// parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
63+
// The OID for the named curve may be provided from another source (such as
64+
// the PKCS8 container) - if it is provided then use this instead of the OID
65+
// that may exist in the EC private key structure.
66+
func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *PrivateKey, err error) {
67+
var privKey ecPrivateKey
68+
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
69+
return nil, errors.New("bign: failed to parse EC private key: " + err.Error())
70+
}
71+
72+
if privKey.Version != ecPrivKeyVersion {
73+
return nil, fmt.Errorf("bign: unknown EC private key version %d", privKey.Version)
74+
}
75+
76+
var curve elliptic.Curve
77+
if namedCurveOID != nil {
78+
curve = NamedCurveFromOid(*namedCurveOID)
79+
} else {
80+
curve = NamedCurveFromOid(privKey.NamedCurveOID)
81+
}
82+
83+
if curve == nil {
84+
return nil, errors.New("bign: unknown elliptic curve")
85+
}
86+
87+
k := new(big.Int).SetBytes(privKey.PrivateKey)
88+
89+
curveOrder := curve.Params().N
90+
if k.Cmp(curveOrder) >= 0 {
91+
return nil, errors.New("bign: invalid elliptic curve private key value")
92+
}
93+
94+
priv := new(PrivateKey)
95+
priv.Curve = curve
96+
priv.D = k
97+
98+
privateKey := make([]byte, (curveOrder.BitLen()+7)/8)
99+
100+
for len(privKey.PrivateKey) > len(privateKey) {
101+
if privKey.PrivateKey[0] != 0 {
102+
return nil, errors.New("bign: invalid private key length")
103+
}
104+
105+
privKey.PrivateKey = privKey.PrivateKey[1:]
106+
}
107+
108+
copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey)
109+
priv.X, priv.Y = curve.ScalarBaseMult(privateKey)
110+
111+
return priv, nil
112+
}
113+
114+
func bitsToBytes(bits int) int {
115+
return (bits + 7) / 8
116+
}

pubkey/bign/pkcs8.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package bign
22

33
import (
4-
"fmt"
54
"errors"
6-
"math/big"
75
"encoding/asn1"
86
"crypto/elliptic"
97
"crypto/x509/pkix"
@@ -12,8 +10,6 @@ import (
1210
"github.com/deatil/go-cryptobin/elliptic/bign"
1311
)
1412

15-
const ecPrivKeyVersion = 1
16-
1713
var (
1814
oidPublicKeyBign = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 2, 1}
1915

@@ -59,15 +55,6 @@ type publicKeyInfo struct {
5955
PublicKey asn1.BitString
6056
}
6157

62-
// Per RFC 5915 the NamedCurveOID is marked as ASN.1 OPTIONAL, however in
63-
// most cases it is not.
64-
type ecPrivateKey struct {
65-
Version int
66-
PrivateKey []byte
67-
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
68-
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
69-
}
70-
7158
// Marshal PublicKey to der
7259
func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
7360
var publicKeyBytes []byte
@@ -216,77 +203,3 @@ func ParsePrivateKey(derBytes []byte) (*PrivateKey, error) {
216203
return key, nil
217204
}
218205

219-
// marshalECPrivateKeyWithOID marshals an SM2 private key into ASN.1, DER format and
220-
// sets the curve ID to the given OID, or omits it if OID is nil.
221-
func marshalECPrivateKeyWithOID(key *PrivateKey, oid asn1.ObjectIdentifier) ([]byte, error) {
222-
if !key.Curve.IsOnCurve(key.X, key.Y) {
223-
return nil, errors.New("bign: invalid elliptic key public key")
224-
}
225-
226-
privateKey := make([]byte, BitsToBytes(key.D.BitLen()))
227-
228-
return asn1.Marshal(ecPrivateKey{
229-
Version: 1,
230-
PrivateKey: key.D.FillBytes(privateKey),
231-
NamedCurveOID: oid,
232-
PublicKey: asn1.BitString{
233-
Bytes: elliptic.Marshal(key.Curve, key.X, key.Y),
234-
},
235-
})
236-
}
237-
238-
// parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
239-
// The OID for the named curve may be provided from another source (such as
240-
// the PKCS8 container) - if it is provided then use this instead of the OID
241-
// that may exist in the EC private key structure.
242-
func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *PrivateKey, err error) {
243-
var privKey ecPrivateKey
244-
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
245-
return nil, errors.New("bign: failed to parse EC private key: " + err.Error())
246-
}
247-
248-
if privKey.Version != ecPrivKeyVersion {
249-
return nil, fmt.Errorf("bign: unknown EC private key version %d", privKey.Version)
250-
}
251-
252-
var curve elliptic.Curve
253-
if namedCurveOID != nil {
254-
curve = NamedCurveFromOid(*namedCurveOID)
255-
} else {
256-
curve = NamedCurveFromOid(privKey.NamedCurveOID)
257-
}
258-
259-
if curve == nil {
260-
return nil, errors.New("bign: unknown elliptic curve")
261-
}
262-
263-
k := new(big.Int).SetBytes(privKey.PrivateKey)
264-
265-
curveOrder := curve.Params().N
266-
if k.Cmp(curveOrder) >= 0 {
267-
return nil, errors.New("bign: invalid elliptic curve private key value")
268-
}
269-
270-
priv := new(PrivateKey)
271-
priv.Curve = curve
272-
priv.D = k
273-
274-
privateKey := make([]byte, (curveOrder.BitLen()+7)/8)
275-
276-
for len(privKey.PrivateKey) > len(privateKey) {
277-
if privKey.PrivateKey[0] != 0 {
278-
return nil, errors.New("bign: invalid private key length")
279-
}
280-
281-
privKey.PrivateKey = privKey.PrivateKey[1:]
282-
}
283-
284-
copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey)
285-
priv.X, priv.Y = curve.ScalarBaseMult(privateKey)
286-
287-
return priv, nil
288-
}
289-
290-
func BitsToBytes(bits int) int {
291-
return (bits + 7) / 8
292-
}

pubkey/bip0340/pkcs1.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
package bip0340
22

33
import (
4+
"fmt"
45
"errors"
6+
"math/big"
7+
"encoding/asn1"
8+
"crypto/elliptic"
59
)
610

11+
const ecPrivKeyVersion = 1
12+
13+
// Per RFC 5915 the NamedCurveOID is marked as ASN.1 OPTIONAL, however in
14+
// most cases it is not.
15+
type ecPrivateKey struct {
16+
Version int
17+
PrivateKey []byte
18+
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
19+
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
20+
}
21+
722
// ParseECPrivateKey parses an EC private key in SEC 1, ASN.1 DER form.
823
//
924
// This kind of key is commonly encoded in PEM blocks of type "EC PRIVATE KEY".
@@ -24,3 +39,76 @@ func MarshalECPrivateKey(key *PrivateKey) ([]byte, error) {
2439

2540
return marshalECPrivateKeyWithOID(key, oid)
2641
}
42+
43+
// marshalECPrivateKeyWithOID marshals an SM2 private key into ASN.1, DER format and
44+
// sets the curve ID to the given OID, or omits it if OID is nil.
45+
func marshalECPrivateKeyWithOID(key *PrivateKey, oid asn1.ObjectIdentifier) ([]byte, error) {
46+
if !key.Curve.IsOnCurve(key.X, key.Y) {
47+
return nil, errors.New("go-cryptobin/bip0340: invalid elliptic key public key")
48+
}
49+
50+
privateKey := make([]byte, bitsToBytes(key.D.BitLen()))
51+
52+
return asn1.Marshal(ecPrivateKey{
53+
Version: 1,
54+
PrivateKey: key.D.FillBytes(privateKey),
55+
NamedCurveOID: oid,
56+
PublicKey: asn1.BitString{
57+
Bytes: elliptic.Marshal(key.Curve, key.X, key.Y),
58+
},
59+
})
60+
}
61+
62+
// parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
63+
// The OID for the named curve may be provided from another source (such as
64+
// the PKCS8 container) - if it is provided then use this instead of the OID
65+
// that may exist in the EC private key structure.
66+
func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *PrivateKey, err error) {
67+
var privKey ecPrivateKey
68+
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
69+
return nil, errors.New("go-cryptobin/bip0340: failed to parse EC private key: " + err.Error())
70+
}
71+
72+
if privKey.Version != ecPrivKeyVersion {
73+
return nil, fmt.Errorf("go-cryptobin/bip0340: unknown EC private key version %d", privKey.Version)
74+
}
75+
76+
var curve elliptic.Curve
77+
if namedCurveOID != nil {
78+
curve = NamedCurveFromOid(*namedCurveOID)
79+
} else {
80+
curve = NamedCurveFromOid(privKey.NamedCurveOID)
81+
}
82+
83+
if curve == nil {
84+
return nil, errors.New("go-cryptobin/bip0340: unknown elliptic curve")
85+
}
86+
87+
k := new(big.Int).SetBytes(privKey.PrivateKey)
88+
89+
curveOrder := curve.Params().N
90+
if k.Cmp(curveOrder) >= 0 {
91+
return nil, errors.New("go-cryptobin/bip0340: invalid elliptic curve private key value")
92+
}
93+
94+
priv := new(PrivateKey)
95+
priv.Curve = curve
96+
priv.D = k
97+
98+
privateKey := make([]byte, (curveOrder.BitLen()+7)/8)
99+
100+
for len(privKey.PrivateKey) > len(privateKey) {
101+
if privKey.PrivateKey[0] != 0 {
102+
return nil, errors.New("go-cryptobin/bip0340: invalid private key length")
103+
}
104+
105+
privKey.PrivateKey = privKey.PrivateKey[1:]
106+
}
107+
108+
copy(privateKey[len(privateKey)-len(privKey.PrivateKey):], privKey.PrivateKey)
109+
110+
d := new(big.Int).SetBytes(privateKey)
111+
priv.X, priv.Y = curve.ScalarBaseMult(d.Bytes())
112+
113+
return priv, nil
114+
}

0 commit comments

Comments
 (0)