From 9e4cb535f157678ef2b81abf363caa334a2c7d90 Mon Sep 17 00:00:00 2001 From: ywy2090 <912554887@qq.com> Date: Thu, 14 Nov 2019 21:37:36 +0800 Subject: [PATCH] sync 2.1.2 to master (#449) * Fix gm unusable bugs (#437) * update ChangeLog.md (#448) --- Changelog.md | 7 ++++ release_note.txt | 2 +- .../sm2/crypto/asymmetric/SM2Algorithm.java | 35 +++++++++++++------ .../sm2/crypto/asymmetric/SM2PublicKey.java | 6 ++-- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Changelog.md b/Changelog.md index ebe3d7d67..d7b6ec162 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,10 @@ +### v2.1.2 + +(2019-11-14) +* 修复 +1. 修复国密服务无法使用的bug. + + ### v2.1.1 (2019-10-29) diff --git a/release_note.txt b/release_note.txt index 826e14246..59696826f 100644 --- a/release_note.txt +++ b/release_note.txt @@ -1 +1 @@ -v2.1.1 +v2.1.2 diff --git a/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2Algorithm.java b/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2Algorithm.java index 30abecb4a..b38ffaeb6 100644 --- a/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2Algorithm.java +++ b/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2Algorithm.java @@ -3,7 +3,11 @@ import java.io.IOException; import java.math.BigInteger; import java.security.SecureRandom; -import org.bouncycastle.asn1.*; +import org.bouncycastle.asn1.ASN1Encoding; +import org.bouncycastle.asn1.ASN1Integer; +import org.bouncycastle.asn1.ASN1Primitive; +import org.bouncycastle.asn1.ASN1Sequence; +import org.bouncycastle.asn1.DERSequence; import org.bouncycastle.math.ec.ECCurve; import org.bouncycastle.math.ec.ECPoint; import org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest; @@ -114,8 +118,10 @@ public static byte[] decrypt(String pvk, byte[] data) { ECPoint s = calculateS( new BigInteger(pbX, 16), new BigInteger(pbY, 16), new BigInteger(pvk, 16)); - BigInteger x2 = s.getAffineXCoord().toBigInteger(); - BigInteger y2 = s.getAffineYCoord().toBigInteger(); + + ECPoint ecPoint = s.normalize(); + BigInteger x2 = ecPoint.getAffineXCoord().toBigInteger(); + BigInteger y2 = ecPoint.getAffineYCoord().toBigInteger(); byte[] t = kdf(x2, y2, c2.length); if (isEmpty(t)) { @@ -183,11 +189,13 @@ private static ECPoint calculateS(BigInteger x1, BigInteger y1, BigInteger k) { * 第4步:计算 [k]Pb=(x2,y2) */ private static BigInteger calculateX2(ECPoint s) { - return s.getAffineXCoord().toBigInteger(); + ECPoint ecPoint = s.normalize(); + return ecPoint.getAffineXCoord().toBigInteger(); } private static BigInteger calculateY2(ECPoint s) { - return s.getAffineYCoord().toBigInteger(); + ECPoint ecPoint = s.normalize(); + return ecPoint.getAffineYCoord().toBigInteger(); } /* @@ -269,8 +277,9 @@ private static byte[] calculateC3(BigInteger x2, byte[] m, BigInteger y2) { private static byte[] getC(ECPoint c1, byte[] c3, byte[] c2) { byte[] c = new byte[64 + c3.length + c2.length]; - byte[] c1xBuf = padding(c1.getAffineXCoord().toBigInteger().toByteArray()); - byte[] c1yBuf = padding(c1.getAffineYCoord().toBigInteger().toByteArray()); + ECPoint ecPoint = c1.normalize(); + byte[] c1xBuf = padding(ecPoint.getAffineXCoord().toBigInteger().toByteArray()); + byte[] c1yBuf = padding(ecPoint.getAffineYCoord().toBigInteger().toByteArray()); System.arraycopy(c1xBuf, 0, c, 0, 32); System.arraycopy(c1yBuf, 0, c, 32, 32); @@ -367,7 +376,8 @@ private static BigInteger[] SignSm3(byte[] hash, BigInteger privateKeyS) { do { k = createRandom(); kp = g256.multiply(k); - r = e.add(kp.getAffineXCoord().toBigInteger()); + ECPoint ecPoint = kp.normalize(); + r = e.add(ecPoint.getAffineXCoord().toBigInteger()); r = r.mod(n); } while (r.equals(BigInteger.ZERO) || r.add(k).equals(n)); BigInteger da_1 = userD.add(BigInteger.ONE).modInverse(n); @@ -415,8 +425,9 @@ private static boolean verify(byte[] msg, byte[] signData, BigInteger biX, BigIn BigInteger t = r.add(s).mod(n); if (t.equals(BigInteger.ZERO)) return false; ECPoint x1y1 = g256.multiply(s); + ECPoint ecPoint = x1y1.normalize(); x1y1 = x1y1.add(userKey.multiply(t)); - BigInteger R = e.add(x1y1.getAffineXCoord().toBigInteger()).mod(n); + BigInteger R = e.add(ecPoint.getAffineXCoord().toBigInteger()).mod(n); return r.equals(R); } @@ -460,8 +471,10 @@ private static byte[] sm2GetZ(byte[] userId, ECPoint publicKey) { sm3BlockUpdate(sm3, getEncoded(b)); sm3BlockUpdate(sm3, getEncoded(gx)); sm3BlockUpdate(sm3, getEncoded(gy)); - sm3BlockUpdate(sm3, getEncoded(publicKey.getAffineXCoord().toBigInteger())); - sm3BlockUpdate(sm3, getEncoded(publicKey.getAffineYCoord().toBigInteger())); + + ECPoint ecPoint = publicKey.normalize(); + sm3BlockUpdate(sm3, getEncoded(ecPoint.getAffineXCoord().toBigInteger())); + sm3BlockUpdate(sm3, getEncoded(ecPoint.getAffineYCoord().toBigInteger())); byte[] md = new byte[sm3.getDigestSize()]; sm3.doFinal(md, 0); diff --git a/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2PublicKey.java b/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2PublicKey.java index 3e1314809..f7395fac4 100644 --- a/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2PublicKey.java +++ b/src/main/java/org/fisco/bcos/web3j/crypto/gm/sm2/crypto/asymmetric/SM2PublicKey.java @@ -18,8 +18,10 @@ public SM2PublicKey() {} public SM2PublicKey(ECPoint p) { this.p = p; - this.x = p.getXCoord().toBigInteger(); - this.y = p.getYCoord().toBigInteger(); + + ECPoint ecPoint = p.normalize(); + this.x = ecPoint.getAffineXCoord().toBigInteger(); + this.y = ecPoint.getAffineYCoord().toBigInteger(); } public String getAlgorithm() {