@@ -40,6 +40,8 @@ import (
4040 "hash"
4141 "io"
4242 "math/big"
43+
44+ "github.com/ethereum/go-ethereum/crypto"
4345)
4446
4547var (
@@ -95,15 +97,15 @@ func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey {
9597// Generate an elliptic curve public / private keypair. If params is nil,
9698// the recommended default parameters for the key will be chosen.
9799func GenerateKey (rand io.Reader , curve elliptic.Curve , params * ECIESParams ) (prv * PrivateKey , err error ) {
98- pb , x , y , err := elliptic .GenerateKey (curve , rand )
100+ sk , err := ecdsa .GenerateKey (curve , rand )
99101 if err != nil {
100102 return
101103 }
102104 prv = new (PrivateKey )
103- prv .PublicKey .X = x
104- prv .PublicKey .Y = y
105+ prv .PublicKey .X = sk . X
106+ prv .PublicKey .Y = sk . Y
105107 prv .PublicKey .Curve = curve
106- prv .D = new (big.Int ).SetBytes ( pb )
108+ prv .D = new (big.Int ).Set ( sk . D )
107109 if params == nil {
108110 params = ParamsFromCurve (curve )
109111 }
@@ -255,12 +257,15 @@ func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err e
255257
256258 d := messageTag (params .Hash , Km , em , s2 )
257259
258- Rb := elliptic .Marshal (pub .Curve , R .PublicKey .X , R .PublicKey .Y )
259- ct = make ([]byte , len (Rb )+ len (em )+ len (d ))
260- copy (ct , Rb )
261- copy (ct [len (Rb ):], em )
262- copy (ct [len (Rb )+ len (em ):], d )
263- return ct , nil
260+ if curve , ok := pub .Curve .(crypto.EllipticCurve ); ok {
261+ Rb := curve .Marshal (R .PublicKey .X , R .PublicKey .Y )
262+ ct = make ([]byte , len (Rb )+ len (em )+ len (d ))
263+ copy (ct , Rb )
264+ copy (ct [len (Rb ):], em )
265+ copy (ct [len (Rb )+ len (em ):], d )
266+ return ct , nil
267+ }
268+ return nil , ErrInvalidCurve
264269}
265270
266271// Decrypt decrypts an ECIES ciphertext.
@@ -297,21 +302,24 @@ func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error) {
297302
298303 R := new (PublicKey )
299304 R .Curve = prv .PublicKey .Curve
300- R .X , R .Y = elliptic .Unmarshal (R .Curve , c [:rLen ])
301- if R .X == nil {
302- return nil , ErrInvalidPublicKey
303- }
304305
305- z , err := prv . GenerateShared ( R , params . KeyLen , params . KeyLen )
306- if err != nil {
307- return nil , err
308- }
309- Ke , Km := deriveKeys ( hash , z , s1 , params . KeyLen )
306+ if curve , ok := R . Curve .(crypto. EllipticCurve ); ok {
307+ R . X , R . Y = curve . Unmarshal ( c [: rLen ])
308+ if R . X == nil {
309+ return nil , ErrInvalidPublicKey
310+ }
310311
311- d := messageTag (params .Hash , Km , c [mStart :mEnd ], s2 )
312- if subtle .ConstantTimeCompare (c [mEnd :], d ) != 1 {
313- return nil , ErrInvalidMessage
314- }
312+ z , err := prv .GenerateShared (R , params .KeyLen , params .KeyLen )
313+ if err != nil {
314+ return nil , err
315+ }
316+ Ke , Km := deriveKeys (hash , z , s1 , params .KeyLen )
315317
316- return symDecrypt (params , Ke , c [mStart :mEnd ])
318+ d := messageTag (params .Hash , Km , c [mStart :mEnd ], s2 )
319+ if subtle .ConstantTimeCompare (c [mEnd :], d ) != 1 {
320+ return nil , ErrInvalidMessage
321+ }
322+ return symDecrypt (params , Ke , c [mStart :mEnd ])
323+ }
324+ return nil , ErrInvalidCurve
317325}
0 commit comments