Skip to content

Commit e354ab4

Browse files
committed
Removes use of elliptic Marshal and Unmarshal functions.
1 parent 1987ada commit e354ab4

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

.golangci.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ linters-settings:
6161
enable-all: true
6262
disable:
6363
- fieldalignment
64-
staticcheck:
65-
# TODO: replace deprecated elliptic.Marshal, elliptic.GenerateKey,
66-
# elliptic.Unmarshal, params.ScalarBaseMult before re-enabling SA1019.
67-
checks: ["*", "-SA1019"]
6864
gosec:
6965
excludes:
7066
- G115

group/short.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package group
22

33
import (
44
"crypto"
5+
"crypto/ecdh"
6+
"crypto/ecdsa"
57
"crypto/elliptic"
68
_ "crypto/sha256"
79
_ "crypto/sha512"
@@ -10,21 +12,23 @@ import (
1012
"io"
1113
"math/big"
1214

13-
"github.com/cloudflare/circl/ecc/p384"
15+
optP384 "github.com/cloudflare/circl/ecc/p384"
1416
"github.com/cloudflare/circl/expander"
1517
)
1618

1719
var (
1820
// P256 is the group generated by P-256 elliptic curve.
19-
P256 Group = wG{elliptic.P256()}
21+
P256 Group = wG{ellC: elliptic.P256, ecdhC: ecdh.P256, c: elliptic.P256()}
2022
// P384 is the group generated by P-384 elliptic curve.
21-
P384 Group = wG{p384.P384()}
23+
P384 Group = wG{ellC: elliptic.P384, ecdhC: ecdh.P384, c: optP384.P384()}
2224
// P521 is the group generated by P-521 elliptic curve.
23-
P521 Group = wG{elliptic.P521()}
25+
P521 Group = wG{ellC: elliptic.P521, ecdhC: ecdh.P521, c: elliptic.P521()}
2426
)
2527

2628
type wG struct {
27-
c elliptic.Curve
29+
c elliptic.Curve
30+
ellC func() elliptic.Curve
31+
ecdhC func() ecdh.Curve
2832
}
2933

3034
func (g wG) String() string { return g.c.Params().Name }
@@ -226,9 +230,15 @@ func (e *wElt) MarshalBinary() ([]byte, error) {
226230
if e.IsIdentity() {
227231
return []byte{0x0}, nil
228232
}
233+
229234
e.x.Mod(e.x, e.c.Params().P)
230235
e.y.Mod(e.y, e.c.Params().P)
231-
return elliptic.Marshal(e.wG.c, e.x, e.y), nil
236+
pk, err := (&ecdsa.PublicKey{Curve: e.wG.ellC(), X: e.x, Y: e.y}).ECDH()
237+
if err != nil {
238+
return nil, err
239+
}
240+
241+
return pk.Bytes(), nil
232242
}
233243

234244
func (e *wElt) MarshalBinaryCompress() ([]byte, error) {
@@ -254,11 +264,13 @@ func (e *wElt) UnmarshalBinary(b []byte) error {
254264
}
255265
e.x, e.y = x, y
256266
case l == 1+2*byteLen && b[0] == 0x04: // uncompressed
257-
x, y := elliptic.Unmarshal(e.wG.c, b)
258-
if x == nil {
267+
_, err := e.wG.ecdhC().NewPublicKey(b)
268+
if err != nil {
259269
return ErrUnmarshal
260270
}
261-
e.x, e.y = x, y
271+
272+
e.x.SetBytes(b[1 : 1+byteLen])
273+
e.y.SetBytes(b[1+byteLen : 1+2*byteLen])
262274
default:
263275
return ErrUnmarshal
264276
}

0 commit comments

Comments
 (0)