Skip to content

Commit eef7787

Browse files
committed
fixed
1 parent 31f7a3d commit eef7787

File tree

7 files changed

+567
-17
lines changed

7 files changed

+567
-17
lines changed

pubkey/ecgdsa/pkcs1.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func ParseECPrivateKey(der []byte) (*PrivateKey, error) {
3434
func MarshalECPrivateKey(key *PrivateKey) ([]byte, error) {
3535
oid, ok := OidFromNamedCurve(key.Curve)
3636
if !ok {
37-
return nil, errors.New("ecgdsa: unknown elliptic curve")
37+
return nil, errors.New("go-cryptobin/ecgdsa: unknown elliptic curve")
3838
}
3939

4040
return marshalECPrivateKeyWithOID(key, oid)
@@ -44,7 +44,7 @@ func MarshalECPrivateKey(key *PrivateKey) ([]byte, error) {
4444
// sets the curve ID to the given OID, or omits it if OID is nil.
4545
func marshalECPrivateKeyWithOID(key *PrivateKey, oid asn1.ObjectIdentifier) ([]byte, error) {
4646
if !key.Curve.IsOnCurve(key.X, key.Y) {
47-
return nil, errors.New("ecgdsa: invalid elliptic key public key")
47+
return nil, errors.New("go-cryptobin/ecgdsa: invalid elliptic key public key")
4848
}
4949

5050
privateKey := make([]byte, bitsToBytes(key.D.BitLen()))
@@ -66,11 +66,11 @@ func marshalECPrivateKeyWithOID(key *PrivateKey, oid asn1.ObjectIdentifier) ([]b
6666
func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *PrivateKey, err error) {
6767
var privKey ecPrivateKey
6868
if _, err := asn1.Unmarshal(der, &privKey); err != nil {
69-
return nil, errors.New("ecgdsa: failed to parse EC private key: " + err.Error())
69+
return nil, errors.New("go-cryptobin/ecgdsa: failed to parse EC private key: " + err.Error())
7070
}
7171

7272
if privKey.Version != ecPrivKeyVersion {
73-
return nil, fmt.Errorf("ecgdsa: unknown EC private key version %d", privKey.Version)
73+
return nil, fmt.Errorf("go-cryptobin/ecgdsa: unknown EC private key version %d", privKey.Version)
7474
}
7575

7676
var curve elliptic.Curve
@@ -81,14 +81,14 @@ func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *P
8181
}
8282

8383
if curve == nil {
84-
return nil, errors.New("ecgdsa: unknown elliptic curve")
84+
return nil, errors.New("go-cryptobin/ecgdsa: unknown elliptic curve")
8585
}
8686

8787
k := new(big.Int).SetBytes(privKey.PrivateKey)
8888

8989
curveOrder := curve.Params().N
9090
if k.Cmp(curveOrder) >= 0 {
91-
return nil, errors.New("ecgdsa: invalid elliptic curve private key value")
91+
return nil, errors.New("go-cryptobin/ecgdsa: invalid elliptic curve private key value")
9292
}
9393

9494
priv := new(PrivateKey)
@@ -99,7 +99,7 @@ func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *P
9999

100100
for len(privKey.PrivateKey) > len(privateKey) {
101101
if privKey.PrivateKey[0] != 0 {
102-
return nil, errors.New("ecgdsa: invalid private key length")
102+
return nil, errors.New("go-cryptobin/ecgdsa: invalid private key length")
103103
}
104104

105105
privKey.PrivateKey = privKey.PrivateKey[1:]

pubkey/ecgdsa/pkcs8.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
6565

6666
oid, ok := OidFromNamedCurve(pub.Curve)
6767
if !ok {
68-
return nil, errors.New("ecgdsa: unsupported ecgdsa curve")
68+
return nil, errors.New("go-cryptobin/ecgdsa: unsupported ecgdsa curve")
6969
}
7070

7171
var paramBytes []byte
@@ -78,7 +78,7 @@ func MarshalPublicKey(pub *PublicKey) ([]byte, error) {
7878
publicKeyAlgorithm.Parameters.FullBytes = paramBytes
7979

8080
if !pub.Curve.IsOnCurve(pub.X, pub.Y) {
81-
return nil, errors.New("ecgdsa: invalid elliptic curve public key")
81+
return nil, errors.New("go-cryptobin/ecgdsa: invalid elliptic curve public key")
8282
}
8383

8484
publicKeyBytes = elliptic.Marshal(pub.Curve, pub.X, pub.Y)
@@ -101,7 +101,7 @@ func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
101101
if err != nil {
102102
return
103103
} else if len(rest) != 0 {
104-
err = errors.New("ecgdsa: trailing data after ASN.1 of public-key")
104+
err = errors.New("go-cryptobin/ecgdsa: trailing data after ASN.1 of public-key")
105105
return
106106
}
107107

@@ -115,25 +115,25 @@ func ParsePublicKey(derBytes []byte) (pub *PublicKey, err error) {
115115
der := cryptobyte.String(pki.PublicKey.RightAlign())
116116

117117
if !oid.Equal(oidPublicKeyECGDSA) {
118-
err = errors.New("ecgdsa: unknown public key algorithm")
118+
err = errors.New("go-cryptobin/ecgdsa: unknown public key algorithm")
119119
return
120120
}
121121

122122
paramsDer := cryptobyte.String(params.FullBytes)
123123
namedCurveOID := new(asn1.ObjectIdentifier)
124124
if !paramsDer.ReadASN1ObjectIdentifier(namedCurveOID) {
125-
return nil, errors.New("ecgdsa: invalid parameters")
125+
return nil, errors.New("go-cryptobin/ecgdsa: invalid parameters")
126126
}
127127

128128
namedCurve := NamedCurveFromOid(*namedCurveOID)
129129
if namedCurve == nil {
130-
err = errors.New("ecgdsa: unsupported ecgdsa curve")
130+
err = errors.New("go-cryptobin/ecgdsa: unsupported ecgdsa curve")
131131
return
132132
}
133133

134134
x, y := elliptic.Unmarshal(namedCurve, der)
135135
if x == nil {
136-
err = errors.New("ecgdsa: failed to unmarshal elliptic curve point")
136+
err = errors.New("go-cryptobin/ecgdsa: failed to unmarshal elliptic curve point")
137137
return
138138
}
139139

@@ -152,13 +152,13 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
152152

153153
oid, ok := OidFromNamedCurve(key.Curve)
154154
if !ok {
155-
return nil, errors.New("ecgdsa: unsupported ecgdsa curve")
155+
return nil, errors.New("go-cryptobin/ecgdsa: unsupported ecgdsa curve")
156156
}
157157

158158
// 创建数据
159159
oidBytes, err := asn1.Marshal(oid)
160160
if err != nil {
161-
return nil, errors.New("ecgdsa: failed to marshal algo param: " + err.Error())
161+
return nil, errors.New("go-cryptobin/ecgdsa: failed to marshal algo param: " + err.Error())
162162
}
163163

164164
privKey.Algo = pkix.AlgorithmIdentifier{
@@ -170,7 +170,7 @@ func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
170170

171171
privKey.PrivateKey, err = marshalECPrivateKeyWithOID(key, nil)
172172
if err != nil {
173-
return nil, errors.New("ecgdsa: failed to marshal EC private key while building PKCS#8: " + err.Error())
173+
return nil, errors.New("go-cryptobin/ecgdsa: failed to marshal EC private key while building PKCS#8: " + err.Error())
174174
}
175175

176176
return asn1.Marshal(privKey)

pubkey/elgamalecc/curves.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package elgamalecc
2+
3+
import (
4+
"encoding/asn1"
5+
"crypto/elliptic"
6+
)
7+
8+
type namedCurveInfo struct {
9+
namedCurve elliptic.Curve
10+
oid asn1.ObjectIdentifier
11+
}
12+
13+
var namedCurves = make([]namedCurveInfo, 0)
14+
15+
func AddNamedCurve(curve elliptic.Curve, oid asn1.ObjectIdentifier) {
16+
namedCurves = append(namedCurves, namedCurveInfo{
17+
namedCurve: curve,
18+
oid: oid,
19+
})
20+
}
21+
22+
func NamedCurveFromOid(oid asn1.ObjectIdentifier) elliptic.Curve {
23+
for i := range namedCurves {
24+
cur := &namedCurves[i]
25+
if cur.oid.Equal(oid) {
26+
return cur.namedCurve
27+
}
28+
}
29+
30+
return nil
31+
}
32+
33+
func OidFromNamedCurve(curve elliptic.Curve) (asn1.ObjectIdentifier, bool) {
34+
for i := range namedCurves {
35+
cur := &namedCurves[i]
36+
if cur.namedCurve == curve {
37+
return cur.oid, true
38+
}
39+
}
40+
41+
return asn1.ObjectIdentifier{}, false
42+
}

pubkey/elgamalecc/elgamalecc.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"crypto/rand"
99
"crypto/subtle"
1010
"crypto/elliptic"
11+
"encoding/asn1"
12+
)
13+
14+
var (
15+
ErrPrivateKey = errors.New("go-cryptobin/elgamalecc: incorrect private key")
16+
ErrPublicKey = errors.New("go-cryptobin/elgamalecc: incorrect public key")
1117
)
1218

1319
var one = big.NewInt(1)
@@ -160,6 +166,52 @@ func Decrypt(priv *PrivateKey, C1x, C1y *big.Int, C2 *big.Int) (plain []byte, er
160166
return
161167
}
162168

169+
type encryptedData struct {
170+
C1 []byte
171+
C2 *big.Int
172+
}
173+
174+
// Encrypted and return asn.1 data
175+
func EncryptASN1(random io.Reader, pub *PublicKey, data []byte) ([]byte, error) {
176+
if pub == nil {
177+
return nil, ErrPublicKey
178+
}
179+
180+
C1x, C1y, C2, err := Encrypt(random, pub, data)
181+
if err != nil {
182+
return nil, err
183+
}
184+
185+
C1 := elliptic.Marshal(pub.Curve, C1x, C1y)
186+
187+
enc, err := asn1.Marshal(encryptedData{
188+
C1: C1,
189+
C2: C2,
190+
})
191+
if err != nil {
192+
return nil, err
193+
}
194+
195+
return enc, nil
196+
}
197+
198+
// Decrypt asn.1 marshal data
199+
func DecryptASN1(priv *PrivateKey, data []byte) ([]byte, error) {
200+
if priv == nil {
201+
return nil, ErrPrivateKey
202+
}
203+
204+
var enc encryptedData
205+
_, err := asn1.Unmarshal(data, &enc)
206+
if err != nil {
207+
return nil, err
208+
}
209+
210+
C1x, C1y := elliptic.Unmarshal(priv.Curve, enc.C1)
211+
212+
return Decrypt(priv, C1x, C1y, enc.C2)
213+
}
214+
163215
// randFieldElement returns a random element of the order of the given
164216
// curve using the procedure given in FIPS 186-4, Appendix B.5.2.
165217
func randFieldElement(rand io.Reader, c elliptic.Curve) (k *big.Int, err error) {

pubkey/elgamalecc/elgamalecc_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"crypto/rand"
88
"crypto/elliptic"
99
"encoding/hex"
10+
"encoding/asn1"
11+
12+
"github.com/deatil/go-cryptobin/elliptic/secp256k1"
1013
)
1114

1215
func fromHex(s string) []byte {
@@ -79,3 +82,139 @@ func Test_Encrypt_Check(t *testing.T) {
7982
t.Errorf("Test_Encrypt_Check fail, got %x, want %x", p2, data)
8083
}
8184
}
85+
86+
func Test_EncryptASN1(t *testing.T) {
87+
c := elliptic.P256()
88+
89+
priv, err := GenerateKey(rand.Reader, c)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
pub := &priv.PublicKey
95+
96+
data := []byte("test-data test-data test-data test-data test-data")
97+
98+
ct, err := EncryptASN1(rand.Reader, pub, data)
99+
if err != nil {
100+
t.Fatal(err)
101+
}
102+
103+
p, _ := DecryptASN1(priv, ct)
104+
if !bytes.Equal(data, p) {
105+
t.Errorf("Test_Encrypt fail, got %x, want %x", p, data)
106+
}
107+
108+
}
109+
110+
func Test_EncryptASN1_Check(t *testing.T) {
111+
c := elliptic.P256()
112+
113+
x := fromHex("67e87b98e8a4383098fedb448c1f3d278bebba50525dec57c0576c605bfffdda")
114+
Y := fromHex("045ec79d828a77af85aa9c8ef3ba5afd7674376d6b134d14b10bb4afb7fd0952b0e894f696b38096ff547bbd0a0f1d14a195f2fc9ce951901fe2925560f29d98c0")
115+
116+
priv, err := NewPrivateKey(c, x)
117+
if err != nil {
118+
t.Fatal(err)
119+
}
120+
121+
pub, err := NewPublicKey(c, Y)
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
126+
data := []byte("Hello")
127+
128+
ct, err := EncryptASN1(rand.Reader, pub, data)
129+
if err != nil {
130+
t.Fatal(err)
131+
}
132+
133+
p, _ := DecryptASN1(priv, ct)
134+
if !bytes.Equal(data, p) {
135+
t.Errorf("ElgamalDecrypt fail, got %x, want %x", p, data)
136+
}
137+
138+
C1 := fromHex("04af7035184190ce72b1ee000ec8f18927a664c23358ce4d41ff757283a5846bb58c19c551753ea0af151c31c1a3698606af565c122a387dbe67d7fa5deba2393f")
139+
C2, _ := new(big.Int).SetString("64998866770800537035816591092081487793369751526287129670052291837083837454710935744289325621649282383337514803150244272041445838052262073601284819093853352", 10)
140+
141+
enc, err := asn1.Marshal(encryptedData{
142+
C1: C1,
143+
C2: C2,
144+
})
145+
146+
p2, _ := DecryptASN1(priv, enc)
147+
if !bytes.Equal(data, p2) {
148+
t.Errorf("Test_Encrypt_Check fail, got %x, want %x", p2, data)
149+
}
150+
}
151+
152+
func Test_key(t *testing.T) {
153+
test_key(t, elliptic.P224())
154+
test_key(t, elliptic.P256())
155+
test_key(t, elliptic.P384())
156+
test_key(t, elliptic.P521())
157+
158+
test_key(t, secp256k1.S256())
159+
}
160+
161+
func test_key(t *testing.T, c elliptic.Curve) {
162+
priv, err := GenerateKey(rand.Reader, c)
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
167+
pub := &priv.PublicKey
168+
169+
{
170+
priKey, err := MarshalECPrivateKey(priv)
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
175+
parsekey, err := ParseECPrivateKey(priKey)
176+
if err != nil {
177+
t.Fatal(err)
178+
}
179+
180+
if !parsekey.Equal(priv) {
181+
t.Error("MarshalECPrivateKey fail")
182+
}
183+
184+
}
185+
186+
{
187+
priKey, err := MarshalPrivateKey(priv)
188+
if err != nil {
189+
t.Fatal(err)
190+
}
191+
192+
parsekey, err := ParsePrivateKey(priKey)
193+
if err != nil {
194+
t.Fatal(err)
195+
}
196+
197+
if !parsekey.Equal(priv) {
198+
t.Error("MarshalPrivateKey fail")
199+
}
200+
201+
}
202+
203+
{
204+
pubKey, err := MarshalPublicKey(pub)
205+
if err != nil {
206+
t.Fatal(err)
207+
}
208+
209+
parsekey, err := ParsePublicKey(pubKey)
210+
if err != nil {
211+
t.Fatal(err)
212+
}
213+
214+
if !parsekey.Equal(pub) {
215+
t.Error("MarshalPublicKey fail")
216+
}
217+
218+
}
219+
220+
}

0 commit comments

Comments
 (0)