Skip to content

Commit 9e4cb53

Browse files
authored
sync 2.1.2 to master (#449)
* Fix gm unusable bugs (#437) * update ChangeLog.md (#448)
1 parent 15bfd9b commit 9e4cb53

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

Changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### v2.1.2
2+
3+
(2019-11-14)
4+
* 修复
5+
1. 修复国密服务无法使用的bug.
6+
7+
18
### v2.1.1
29

310
(2019-10-29)

release_note.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2.1.1
1+
v2.1.2

src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2Algorithm.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import java.io.IOException;
44
import java.math.BigInteger;
55
import java.security.SecureRandom;
6-
import org.bouncycastle.asn1.*;
6+
import org.bouncycastle.asn1.ASN1Encoding;
7+
import org.bouncycastle.asn1.ASN1Integer;
8+
import org.bouncycastle.asn1.ASN1Primitive;
9+
import org.bouncycastle.asn1.ASN1Sequence;
10+
import org.bouncycastle.asn1.DERSequence;
711
import org.bouncycastle.math.ec.ECCurve;
812
import org.bouncycastle.math.ec.ECPoint;
913
import org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest;
@@ -114,8 +118,10 @@ public static byte[] decrypt(String pvk, byte[] data) {
114118
ECPoint s =
115119
calculateS(
116120
new BigInteger(pbX, 16), new BigInteger(pbY, 16), new BigInteger(pvk, 16));
117-
BigInteger x2 = s.getAffineXCoord().toBigInteger();
118-
BigInteger y2 = s.getAffineYCoord().toBigInteger();
121+
122+
ECPoint ecPoint = s.normalize();
123+
BigInteger x2 = ecPoint.getAffineXCoord().toBigInteger();
124+
BigInteger y2 = ecPoint.getAffineYCoord().toBigInteger();
119125

120126
byte[] t = kdf(x2, y2, c2.length);
121127
if (isEmpty(t)) {
@@ -183,11 +189,13 @@ private static ECPoint calculateS(BigInteger x1, BigInteger y1, BigInteger k) {
183189
* 第4步:计算 [k]Pb=(x2,y2)
184190
*/
185191
private static BigInteger calculateX2(ECPoint s) {
186-
return s.getAffineXCoord().toBigInteger();
192+
ECPoint ecPoint = s.normalize();
193+
return ecPoint.getAffineXCoord().toBigInteger();
187194
}
188195

189196
private static BigInteger calculateY2(ECPoint s) {
190-
return s.getAffineYCoord().toBigInteger();
197+
ECPoint ecPoint = s.normalize();
198+
return ecPoint.getAffineYCoord().toBigInteger();
191199
}
192200

193201
/*
@@ -269,8 +277,9 @@ private static byte[] calculateC3(BigInteger x2, byte[] m, BigInteger y2) {
269277
private static byte[] getC(ECPoint c1, byte[] c3, byte[] c2) {
270278
byte[] c = new byte[64 + c3.length + c2.length];
271279

272-
byte[] c1xBuf = padding(c1.getAffineXCoord().toBigInteger().toByteArray());
273-
byte[] c1yBuf = padding(c1.getAffineYCoord().toBigInteger().toByteArray());
280+
ECPoint ecPoint = c1.normalize();
281+
byte[] c1xBuf = padding(ecPoint.getAffineXCoord().toBigInteger().toByteArray());
282+
byte[] c1yBuf = padding(ecPoint.getAffineYCoord().toBigInteger().toByteArray());
274283

275284
System.arraycopy(c1xBuf, 0, c, 0, 32);
276285
System.arraycopy(c1yBuf, 0, c, 32, 32);
@@ -367,7 +376,8 @@ private static BigInteger[] SignSm3(byte[] hash, BigInteger privateKeyS) {
367376
do {
368377
k = createRandom();
369378
kp = g256.multiply(k);
370-
r = e.add(kp.getAffineXCoord().toBigInteger());
379+
ECPoint ecPoint = kp.normalize();
380+
r = e.add(ecPoint.getAffineXCoord().toBigInteger());
371381
r = r.mod(n);
372382
} while (r.equals(BigInteger.ZERO) || r.add(k).equals(n));
373383
BigInteger da_1 = userD.add(BigInteger.ONE).modInverse(n);
@@ -415,8 +425,9 @@ private static boolean verify(byte[] msg, byte[] signData, BigInteger biX, BigIn
415425
BigInteger t = r.add(s).mod(n);
416426
if (t.equals(BigInteger.ZERO)) return false;
417427
ECPoint x1y1 = g256.multiply(s);
428+
ECPoint ecPoint = x1y1.normalize();
418429
x1y1 = x1y1.add(userKey.multiply(t));
419-
BigInteger R = e.add(x1y1.getAffineXCoord().toBigInteger()).mod(n);
430+
BigInteger R = e.add(ecPoint.getAffineXCoord().toBigInteger()).mod(n);
420431

421432
return r.equals(R);
422433
}
@@ -460,8 +471,10 @@ private static byte[] sm2GetZ(byte[] userId, ECPoint publicKey) {
460471
sm3BlockUpdate(sm3, getEncoded(b));
461472
sm3BlockUpdate(sm3, getEncoded(gx));
462473
sm3BlockUpdate(sm3, getEncoded(gy));
463-
sm3BlockUpdate(sm3, getEncoded(publicKey.getAffineXCoord().toBigInteger()));
464-
sm3BlockUpdate(sm3, getEncoded(publicKey.getAffineYCoord().toBigInteger()));
474+
475+
ECPoint ecPoint = publicKey.normalize();
476+
sm3BlockUpdate(sm3, getEncoded(ecPoint.getAffineXCoord().toBigInteger()));
477+
sm3BlockUpdate(sm3, getEncoded(ecPoint.getAffineYCoord().toBigInteger()));
465478

466479
byte[] md = new byte[sm3.getDigestSize()];
467480
sm3.doFinal(md, 0);

src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2PublicKey.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public SM2PublicKey() {}
1818

1919
public SM2PublicKey(ECPoint p) {
2020
this.p = p;
21-
this.x = p.getXCoord().toBigInteger();
22-
this.y = p.getYCoord().toBigInteger();
21+
22+
ECPoint ecPoint = p.normalize();
23+
this.x = ecPoint.getAffineXCoord().toBigInteger();
24+
this.y = ecPoint.getAffineYCoord().toBigInteger();
2325
}
2426

2527
public String getAlgorithm() {

0 commit comments

Comments
 (0)