Skip to content

Commit dd2b418

Browse files
committed
更新
1 parent b54a596 commit dd2b418

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

gm/sm2/marshal.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func unmarshalCipherASN1(curve elliptic.Curve, data []byte, mode Mode) (encrypte
142142
type cipherASN1New struct {
143143
XCoordinate *big.Int
144144
YCoordinate *big.Int
145-
HASH []byte
145+
Hash []byte
146146
CipherText []byte
147147
}
148148

@@ -152,7 +152,7 @@ func marshalCipherASN1New(data encryptedData) ([]byte, error) {
152152
return asn1.Marshal(cipherASN1New{
153153
XCoordinate: bytesToBigInt(data.XCoordinate),
154154
YCoordinate: bytesToBigInt(data.YCoordinate),
155-
HASH: data.Hash,
155+
Hash: data.Hash,
156156
CipherText: data.CipherText,
157157
})
158158
}
@@ -171,7 +171,7 @@ func unmarshalCipherASN1New(curve elliptic.Curve, b []byte) (encryptedData, erro
171171
return encryptedData{
172172
XCoordinate: x, // x分量
173173
YCoordinate: y, // y分量
174-
Hash: data.HASH, // hash
174+
Hash: data.Hash, // hash
175175
CipherText: data.CipherText, // cipherText
176176
}, nil
177177
}
@@ -181,7 +181,7 @@ type cipherASN1Old struct {
181181
XCoordinate *big.Int
182182
YCoordinate *big.Int
183183
CipherText []byte
184-
HASH []byte
184+
Hash []byte
185185
}
186186

187187
// sm2 密文转 asn.1 编码格式
@@ -191,7 +191,7 @@ func marshalCipherASN1Old(data encryptedData) ([]byte, error) {
191191
XCoordinate: bytesToBigInt(data.XCoordinate),
192192
YCoordinate: bytesToBigInt(data.YCoordinate),
193193
CipherText: data.CipherText,
194-
HASH: data.Hash,
194+
Hash: data.Hash,
195195
})
196196
}
197197

@@ -210,7 +210,7 @@ func unmarshalCipherASN1Old(curve elliptic.Curve, b []byte) (encryptedData, erro
210210
XCoordinate: x, // x分量
211211
YCoordinate: y, // y分量
212212
CipherText: data.CipherText, // cipherText
213-
Hash: data.HASH, // hash
213+
Hash: data.Hash, // hash
214214
}, nil
215215
}
216216

pubkey/bip0340/bip0340.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
var (
2323
ErrPrivateKey = errors.New("go-cryptobin/bip0340: invalid PrivateKey")
2424
ErrParametersNotSetUp = errors.New("go-cryptobin/bip0340: parameters not set up before generating key")
25+
ErrInvalidK = errors.New("go-cryptobin/bip0340: use another K")
2526
ErrInvalidASN1 = errors.New("go-cryptobin/bip0340: invalid ASN.1")
2627
ErrInvalidSignerOpts = errors.New("go-cryptobin/bip0340: opts must be *SignerOpts")
2728
)
@@ -270,12 +271,18 @@ func SignToRS(random io.Reader, priv *PrivateKey, hashFunc Hasher, msg []byte) (
270271
e := new(big.Int).Set(one)
271272
e.Lsh(e, 8 * uint(qlen))
272273

274+
Retry:
273275
k, err := rand.Int(random, e)
274276
if err != nil {
275277
return
276278
}
277279

278-
return SignUsingKToRS(k, priv, hashFunc, msg)
280+
r, s, err = SignUsingKToRS(k, priv, hashFunc, msg)
281+
if err == ErrInvalidK {
282+
goto Retry
283+
}
284+
285+
return
279286
}
280287

281288
// sign with k
@@ -309,7 +316,6 @@ func SignUsingKToRS(k *big.Int, priv *PrivateKey, hashFunc Hasher, msg []byte) (
309316
/* Adjust d depending on public key y */
310317
bip0340SetScalar(d, n, py)
311318

312-
Retry:
313319
sig := make([]byte, qlen)
314320
k.FillBytes(sig)
315321

@@ -344,7 +350,7 @@ Retry:
344350
k.Mod(k, n)
345351

346352
if k.Cmp(zero) == 0 {
347-
goto Retry
353+
return nil, nil, ErrInvalidK
348354
}
349355

350356
kGx, kGy := curve.ScalarBaseMult(k.Bytes())

pubkey/ecies/ecies.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ import (
1919
)
2020

2121
var (
22-
ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve")
23-
ErrInvalidParams = fmt.Errorf("ecies: invalid ECIES parameters")
24-
ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key")
25-
ErrInvalidPrivateKey = fmt.Errorf("ecies: invalid private key")
26-
ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity")
27-
ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key params are too big")
28-
ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")
29-
30-
ErrKeyDataTooLong = fmt.Errorf("ecies: can't supply requested key data")
31-
ErrSharedTooLong = fmt.Errorf("ecies: shared secret is too long")
32-
ErrInvalidMessage = fmt.Errorf("ecies: invalid message")
22+
ErrInvalidCurve = fmt.Errorf("go-cryptobin/ecies: invalid elliptic curve")
23+
ErrInvalidParams = fmt.Errorf("go-cryptobin/ecies: invalid ECIES parameters")
24+
ErrInvalidPublicKey = fmt.Errorf("go-cryptobin/ecies: invalid public key")
25+
ErrInvalidPrivateKey = fmt.Errorf("go-cryptobin/ecies: invalid private key")
26+
ErrSharedKeyIsPointAtInfinity = fmt.Errorf("go-cryptobin/ecies: shared key is point at infinity")
27+
ErrSharedKeyTooBig = fmt.Errorf("go-cryptobin/ecies: shared key params are too big")
28+
ErrUnsupportedECIESParameters = fmt.Errorf("go-cryptobin/ecies: unsupported ECIES parameters")
29+
30+
ErrKeyDataTooLong = fmt.Errorf("go-cryptobin/ecies: can't supply requested key data")
31+
ErrSharedTooLong = fmt.Errorf("go-cryptobin/ecies: shared secret is too long")
32+
ErrInvalidMessage = fmt.Errorf("go-cryptobin/ecies: invalid message")
3333
)
3434

3535
type ECIESParams struct {
@@ -90,9 +90,9 @@ func AddParamsFromCurve(curve elliptic.Curve, ecie *ECIESParams) {
9090

9191
// PublicKey is a representation of an elliptic curve public key.
9292
type PublicKey struct {
93-
X *big.Int
94-
Y *big.Int
9593
elliptic.Curve
94+
95+
X, Y *big.Int
9696
Params *ECIESParams
9797
}
9898

@@ -118,6 +118,7 @@ func ImportECDSAPublicKey(pub *ecdsa.PublicKey) *PublicKey {
118118
// PrivateKey is a representation of an elliptic curve private key.
119119
type PrivateKey struct {
120120
PublicKey
121+
121122
D *big.Int
122123
}
123124

@@ -298,8 +299,6 @@ func (priv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error) {
298299
return
299300
}
300301

301-
// =================================
302-
303302
// Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1.
304303
//
305304
// s1 and s2 contain shared information that is not part of the resulting

x509/csr_enveloped.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type SM2EnvelopedKey struct {
6969
type SM2EncryptedKey struct {
7070
XCoordinate *big.Int
7171
YCoordinate *big.Int
72-
HASH []byte
72+
Hash []byte
7373
CipherText []byte
7474
}
7575

0 commit comments

Comments
 (0)