Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Dec 27, 2024
1 parent 2fa63bf commit ef61b54
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gm/sm2/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Decompress(data []byte) (*PublicKey, error) {

x, y := sm2curve.UnmarshalCompressed(c, data)
if x == nil || y == nil {
return nil, errors.New("cryptobin/sm2: compress publicKey is incorrect.")
return nil, errors.New("go-cryptobin/sm2: compress publicKey is incorrect.")
}

pub := &PublicKey{
Expand Down
2 changes: 1 addition & 1 deletion gm/sm2/key_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func keyExchange(klen int, ida, idb []byte, pri *PrivateKey, pub *PublicKey, rpr
k = smkdf.Key(sm3.New, kk, klen)

if alias.ConstantTimeAllZero(k) {
err = errors.New("cryptobin/sm2: zero key")
err = errors.New("go-cryptobin/sm2: zero key")
return
}

Expand Down
6 changes: 3 additions & 3 deletions gm/sm2/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func MarshalSignatureBytes(curve elliptic.Curve, r, s *big.Int) ([]byte, error)
func UnmarshalSignatureBytes(curve elliptic.Curve, sign []byte) (r, s *big.Int, err error) {
byteLen := (curve.Params().BitSize + 7) / 8
if len(sign) != 2*byteLen {
err = errors.New("cryptobin/sm2: incorrect signature")
err = errors.New("go-cryptobin/sm2: incorrect signature")
return
}

Expand Down Expand Up @@ -81,14 +81,14 @@ func marshalCipherBytes(c encryptedData, mode Mode) []byte {
func unmarshalCipherBytes(curve elliptic.Curve, data []byte, mode Mode, h hashFunc) (encryptedData, error) {
typ := data[0]
if typ != byte(0x04) {
return encryptedData{}, errors.New("cryptobin/sm2: encrypted data is error and miss prefix '4'.")
return encryptedData{}, errors.New("go-cryptobin/sm2: encrypted data is error and miss prefix '4'.")
}

hashSize := h().Size()

byteLen := (curve.Params().BitSize + 7) / 8
if len(data) < 2*byteLen + hashSize {
return encryptedData{}, errors.New("cryptobin/sm2: encrypt data is too short.")
return encryptedData{}, errors.New("go-cryptobin/sm2: encrypt data is too short.")
}

data = data[1:]
Expand Down
4 changes: 2 additions & 2 deletions gm/sm2/sm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool {
bigIntEqual(priv.D, xx.D)
}

// sign data and return asn.1 or bytes marshal data
// sign data and return asn.1 or bytes marshal data, default asn.1
func (priv *PrivateKey) Sign(random io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
opt := DefaultSignerOpts
if o, ok := opts.(SignerOpts); ok {
Expand All @@ -353,7 +353,7 @@ func (priv *PrivateKey) Sign(random io.Reader, msg []byte, opts crypto.SignerOpt
return nil, errors.New("go-cryptobin/sm2: Sign fail")
}

// sign data use k and return asn.1 or bytes marshal data
// sign data use k and return asn.1 or bytes marshal data, default asn.1
func (priv *PrivateKey) SignUsingK(k *big.Int, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
opt := DefaultSignerOpts
if o, ok := opts.(SignerOpts); ok {
Expand Down
4 changes: 2 additions & 2 deletions gm/sm2/sm2curve/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (e *Element) bytes(out *[p256ElementLen]byte) []byte {
// SetBytes returns nil and an error, and e is unchanged.
func (e *Element) SetBytes(v []byte) (*Element, error) {
if len(v) != p256ElementLen {
return nil, errors.New("cryptobin/sm2: invalid Element encoding")
return nil, errors.New("go-cryptobin/sm2: invalid Element encoding")
}

// Check for non-canonical encodings (p + k, 2p + k, etc.) by comparing to
Expand All @@ -77,7 +77,7 @@ func (e *Element) SetBytes(v []byte) (*Element, error) {
break
}
if v[i] > minusOneEncoding[i] {
return nil, errors.New("cryptobin/sm2: invalid Element encoding")
return nil, errors.New("go-cryptobin/sm2: invalid Element encoding")
}
}

Expand Down
2 changes: 1 addition & 1 deletion gm/sm2/sm2curve/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,6 @@ func panicIfNotOnCurve(curve elliptic.Curve, x, y *big.Int) {
}

if !curve.IsOnCurve(x, y) {
panic("cryptobin/sm2: attempted operation on invalid point")
panic("go-cryptobin/sm2: attempted operation on invalid point")
}
}
26 changes: 13 additions & 13 deletions gm/sm2/sm2curve/sm2curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func (curve *sm2Curve) pointFromAffine(x, y *big.Int) (p *Point, err error) {

// Reject values that would not get correctly encoded.
if x.Sign() < 0 || y.Sign() < 0 {
return p, errors.New("cryptobin/sm2: negative coordinate")
return p, errors.New("go-cryptobin/sm2: negative coordinate")
}

if x.BitLen() > curve.params.BitSize || y.BitLen() > curve.params.BitSize {
return p, errors.New("cryptobin/sm2: overflowing coordinate")
return p, errors.New("go-cryptobin/sm2: overflowing coordinate")
}

// Encode the coordinates and let SetBytes reject invalid points.
Expand Down Expand Up @@ -66,12 +66,12 @@ func (curve *sm2Curve) pointToAffine(p *Point) (x, y *big.Int) {
func (curve *sm2Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
p1, err := curve.pointFromAffine(x1, y1)
if err != nil {
panic("cryptobin/sm2: Add was called on an invalid point")
panic("go-cryptobin/sm2: Add was called on an invalid point")
}

p2, err := curve.pointFromAffine(x2, y2)
if err != nil {
panic("cryptobin/sm2: Add was called on an invalid point")
panic("go-cryptobin/sm2: Add was called on an invalid point")
}

return curve.pointToAffine(p1.Add(p1, p2))
Expand All @@ -80,7 +80,7 @@ func (curve *sm2Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
func (curve *sm2Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
p, err := curve.pointFromAffine(x1, y1)
if err != nil {
panic("cryptobin/sm2: Double was called on an invalid point")
panic("go-cryptobin/sm2: Double was called on an invalid point")
}

return curve.pointToAffine(p.Double(p))
Expand All @@ -106,13 +106,13 @@ func (curve *sm2Curve) normalizeScalar(scalar []byte) []byte {
func (curve *sm2Curve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
p, err := curve.pointFromAffine(Bx, By)
if err != nil {
panic("cryptobin/sm2: ScalarMult was called on an invalid point")
panic("go-cryptobin/sm2: ScalarMult was called on an invalid point")
}

scalar = curve.normalizeScalar(scalar)
p, err = p.ScalarMult(p, scalar)
if err != nil {
panic("cryptobin/sm2: sm2 rejected normalized scalar")
panic("go-cryptobin/sm2: sm2 rejected normalized scalar")
}

return curve.pointToAffine(p)
Expand All @@ -123,7 +123,7 @@ func (curve *sm2Curve) ScalarBaseMult(scalar []byte) (*big.Int, *big.Int) {

p, err := curve.newPoint().ScalarBaseMult(scalar)
if err != nil {
panic("cryptobin/sm2: sm2 rejected normalized scalar")
panic("go-cryptobin/sm2: sm2 rejected normalized scalar")
}

return curve.pointToAffine(p)
Expand All @@ -135,18 +135,18 @@ func (curve *sm2Curve) CombinedMult(Px, Py *big.Int, s1, s2 []byte) (x, y *big.I
s1 = curve.normalizeScalar(s1)
q, err := curve.newPoint().ScalarBaseMult(s1)
if err != nil {
panic("cryptobin/sm2: sm2 rejected normalized scalar")
panic("go-cryptobin/sm2: sm2 rejected normalized scalar")
}

p, err := curve.pointFromAffine(Px, Py)
if err != nil {
panic("cryptobin/sm2: CombinedMult was called on an invalid point")
panic("go-cryptobin/sm2: CombinedMult was called on an invalid point")
}

s2 = curve.normalizeScalar(s2)
p, err = p.ScalarMult(p, s2)
if err != nil {
panic("cryptobin/sm2: sm2 rejected normalized scalar")
panic("go-cryptobin/sm2: sm2 rejected normalized scalar")
}

return curve.pointToAffine(p.Add(p, q))
Expand Down Expand Up @@ -201,7 +201,7 @@ func (curve *sm2Curve) Inverse(k *big.Int) *big.Int {
scalar := k.FillBytes(make([]byte, 32))
inverse, err := P256OrdInverse(scalar)
if err != nil {
panic("cryptobin/sm2: sm2 rejected normalized scalar")
panic("go-cryptobin/sm2: sm2 rejected normalized scalar")
}

return new(big.Int).SetBytes(inverse)
Expand All @@ -210,7 +210,7 @@ func (curve *sm2Curve) Inverse(k *big.Int) *big.Int {
func bigFromHex(s string) *big.Int {
b, ok := new(big.Int).SetString(s, 16)
if !ok {
panic("cryptobin/sm2: internal error: invalid encoding")
panic("go-cryptobin/sm2: internal error: invalid encoding")
}

return b
Expand Down
12 changes: 6 additions & 6 deletions gm/sm2/sm2curve/sm2ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (p *Point) SetBytes(b []byte) (*Point, error) {
// y² = x³ - 3x + b
y := p256Polynomial(new(field.Element), x)
if !p256Sqrt(y, y) {
return nil, errors.New("cryptobin/sm2: invalid compressed point encoding")
return nil, errors.New("go-cryptobin/sm2: invalid compressed point encoding")
}
// Select the positive or negative root, as indicated by the least
// significant bit, based on the encoding type byte.
Expand All @@ -95,7 +95,7 @@ func (p *Point) SetBytes(b []byte) (*Point, error) {
p.z.One()
return p, nil
default:
return nil, errors.New("cryptobin/sm2: invalid point encoding")
return nil, errors.New("go-cryptobin/sm2: invalid point encoding")
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func p256CheckOnCurve(x, y *field.Element) error {
rhs := p256Polynomial(new(field.Element), x)
lhs := new(field.Element).Square(y)
if rhs.Equal(lhs) != 1 {
return errors.New("cryptobin/sm2: point not on curve")
return errors.New("go-cryptobin/sm2: point not on curve")
}
return nil
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func (p *Point) BytesX() ([]byte, error) {

func (p *Point) bytesX(out *[p256ElementLength]byte) ([]byte, error) {
if p.z.IsZero() == 1 {
return nil, errors.New("cryptobin/sm2: point is the point at infinity")
return nil, errors.New("go-cryptobin/sm2: point is the point at infinity")
}
zinv := new(field.Element).Invert(p.z)
x := new(field.Element).Mul(p.x, zinv)
Expand Down Expand Up @@ -314,7 +314,7 @@ type lookupTable [15]*Point
// constant time by iterating over every entry of the table. n must be in [0, 15].
func (table *lookupTable) Select(p *Point, n uint8) {
if n >= 16 {
panic("cryptobin/sm2: lookupTable called with out-of-bounds value")
panic("go-cryptobin/sm2: lookupTable called with out-of-bounds value")
}
p.Set(NewPoint())
for i, f := range table {
Expand Down Expand Up @@ -398,7 +398,7 @@ func (p *Point) generatorTable() *[p256ElementLength * 2]lookupTable {
// returns p.
func (p *Point) ScalarBaseMult(scalar []byte) (*Point, error) {
if len(scalar) != p256ElementLength {
return nil, errors.New("cryptobin/sm2: invalid scalar length")
return nil, errors.New("go-cryptobin/sm2: invalid scalar length")
}
tables := p.generatorTable()

Expand Down
2 changes: 1 addition & 1 deletion gm/sm2/sm2curve/sm2ec_ord.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
//
func P256OrdInverse(k []byte) ([]byte, error) {
if len(k) != 32 {
return nil, errors.New("cryptobin/sm2: invalid scalar length")
return nil, errors.New("go-cryptobin/sm2: invalid scalar length")
}
x := new(field.OrderElement)
_1 := new(field.OrderElement)
Expand Down

0 comments on commit ef61b54

Please sign in to comment.