Skip to content

Commit

Permalink
sync 2.1.2 to master (#449)
Browse files Browse the repository at this point in the history
* Fix gm unusable bugs (#437)

* update ChangeLog.md (#448)
  • Loading branch information
ywy2090 authored Nov 14, 2019
1 parent 15bfd9b commit 9e4cb53
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### v2.1.2

(2019-11-14)
* 修复
1. 修复国密服务无法使用的bug.


### v2.1.1

(2019-10-29)
Expand Down
2 changes: 1 addition & 1 deletion release_note.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.1.1
v2.1.2
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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();
}

/*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 9e4cb53

Please sign in to comment.