Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Hotfix/fix_random_num' into hotf…
Browse files Browse the repository at this point in the history
…ix/fix_random_num
  • Loading branch information
Sean authored and Sean committed Mar 2, 2020
2 parents 2b8bbd7 + 348f0ec commit 0aba1b6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 218 deletions.
183 changes: 1 addition & 182 deletions crypto/src/main/java/org/tron/common/crypto/sm2/SM2.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,56 +533,7 @@ public byte[] Base64toBytes (String signature) {
return ByteUtil.appendByte(temp,first);
}

/**
* Takes the message of data and returns the SM2 signature
*
* @param message -
* @param userID
* @return -
* @throws IllegalStateException if this ECKey does not have the private part.
*/
public SM2Signature signMessage(byte[] message, @Nullable String userID) {
SM2Signature sig = signMsg(message, userID);
// Now we have to work backwards to figure out the recId needed to
// recover the signature.
int recId = -1;
byte[] thisKey = this.pub.getEncoded(/* compressed */ false);

SM2Signer signer = getSigner();
byte[] messageHash = signer.generateSM3Hash(message);
for (int i = 0; i < 4; i++) {
byte[] k = recoverPubBytesFromSignature(i, sig, messageHash);
if (k != null && Arrays.equals(k, thisKey)) {
recId = i;
break;
}
}
if (recId == -1) {
throw new RuntimeException("Could not construct a recoverable key" +
". This should never happen.");
}
sig.v = (byte) (recId + 27);
return sig;
}

/**
* Signs the given hash and returns the R and S components as BigIntegers and putData them in
* SM2Signature
*
* @param msg to sign
* @param userID
* @return SM2Signature signature that contains the R and S components
*/
public SM2.SM2Signature signMsg(byte[] msg,@Nullable String userID) {
if (null == msg) {
throw new IllegalArgumentException("Expected signature message of " +
"SM2 is null");
}
// No decryption of private key required.
SM2Signer signer = getSigner();
BigInteger[] componets = signer.generateSignature(msg);
return new SM2.SM2Signature(componets[0], componets[1]);
}

private SM2Signer getSigner() {
SM2Signer signer = new SM2Signer();
Expand Down Expand Up @@ -694,84 +645,6 @@ private static void check(boolean test, String message) {
}
}

/**
* <p>Verifies the given SM2 signature against the message bytes using the public key bytes.</p>
* <p> <p>When using native SM2 verification, data must be 32 bytes, and no element may be
* larger than 520 bytes.</p>
*
* @param data Hash of the data to verify.
* @param signature signature.
* @param pub The public key bytes to use.
* @return -
*/
public static boolean verify(byte[] data, SM2Signature signature,
byte[] pub) {
SM2Signer signer = new SM2Signer();
ECPublicKeyParameters params = new ECPublicKeyParameters(ecc_param
.getCurve().decodePoint(pub),ecc_param);
signer.init(false, params);
try {
return signer.verifyHashSignature(data, signature.r, signature.s);
} catch (NullPointerException npe) {
// Bouncy Castle contains a bug that can cause NPEs given
// specially crafted signatures.
// Those signatures are inherently invalid/attack sigs so we just
// fail them here rather than crash the thread.
logger.error("Caught NPE inside bouncy castle", npe);
return false;
}
}

/**
* Verifies the given ASN.1 encoded SM2 signature against a hash using the public key.
*
* @param data Hash of the data to verify.
* @param signature signature.
* @param pub The public key bytes to use.
* @return -
*/
public static boolean verify(byte[] data, byte[] signature, byte[] pub) {
return verify(data, SM2Signature.decodeFromDER(signature), pub);
}

/**
* <p>Verifies the given SM2 signature against the message bytes using the public key bytes.
*
* @param msg the message data to verify.
* @param signature signature.
* @param pub The public key bytes to use.
* @return -
*/
public static boolean verifyMessage(byte[] msg, SM2Signature signature,
byte[] pub, @Nullable String userID) {
SM2Signer signer = new SM2Signer();
ECPublicKeyParameters params = new ECPublicKeyParameters(ecc_param
.getCurve().decodePoint(pub),ecc_param);
signer.init(false, params);
try {
return signer.verifySignature(msg, signature.r, signature.s, userID);
} catch (NullPointerException npe) {
// Bouncy Castle contains a bug that can cause NPEs given
// specially crafted signatures.
// Those signatures are inherently invalid/attack sigs so we just
// fail them here rather than crash the thread.
logger.error("Caught NPE inside bouncy castle", npe);
return false;
}
}

/**
* Verifies the given ASN.1 encoded SM2 signature against a hash using the public key.
*
* @param msg the message data to verify.
* @param signature signature.
* @param pub The public key bytes to use.
* @return -
*/
public static boolean verifyMessage(byte[] msg, byte[] signature, byte[] pub, @Nullable String userID) {
return verifyMessage(msg, SM2Signature.decodeFromDER(signature), pub, userID);
}


/**
* Returns true if the given pubkey is canonical, i.e. the correct length taking into account
Expand Down Expand Up @@ -949,27 +822,6 @@ public String toStringWithPrivate() {
}


/**
* Verifies the given ASN.1 encoded SM2 signature against a hash using the public key.
*
* @param data Hash of the data to verify.
* @param signature signature.
* @return -
*/
public boolean verify(byte[] data, byte[] signature) {
return SM2.verify(data, signature, getPubKey());
}

/**
* Verifies the given R/S pair (signature) against a hash using the public key.
*
* @param sigHash -
* @param signature -
* @return -
*/
public boolean verify(byte[] sigHash, SM2Signature signature) {
return SM2.verify(sigHash, signature, getPubKey());
}

/**
* Returns true if this pubkey is canonical, i.e. the correct length taking into account
Expand Down Expand Up @@ -1091,40 +943,7 @@ public static boolean validateComponents(BigInteger r, BigInteger s,
return isLessThan(s, SM2.SM2_N);
}

public static SM2.SM2Signature decodeFromDER(byte[] bytes) {
ASN1InputStream decoder = null;
try {
decoder = new ASN1InputStream(bytes);
DLSequence seq = (DLSequence) decoder.readObject();
if (seq == null) {
throw new RuntimeException("Reached past end of ASN.1 " +
"stream.");
}
ASN1Integer r, s;
try {
r = (ASN1Integer) seq.getObjectAt(0);
s = (ASN1Integer) seq.getObjectAt(1);
} catch (ClassCastException e) {
throw new IllegalArgumentException(e);
}
// OpenSSL deviates from the DER spec by interpreting these
// values as unsigned, though they should not be
// Thus, we always use the positive versions. See:
// http://r6.ca/blog/20111119T211504Z.html
return new SM2.SM2Signature(r.getPositiveValue(), s
.getPositiveValue());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (decoder != null) {
try {
decoder.close();
} catch (IOException x) {

}
}
}
}


public boolean validateComponents() {
return validateComponents(r, s, v);
Expand Down
36 changes: 0 additions & 36 deletions framework/src/test/java/org/tron/common/crypto/SM2KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,42 +115,6 @@ public void testSM3Hash() {
}


@Test
public void testValidHashSignature() {
SM2 key = SM2.fromPrivate(privateKey);
byte[] hash = Hex.decode("B524F552CD82B8B028476E005C377FB19A87E"
+ "6FC682D48BB5D42E3D9B9EFFE76");
SM2.SM2Signature sign = key.sign(hash);
//byte[] signByte = sign.toByteArray();
//System.out.println(Hex.toHexString(signByte));
assertTrue(SM2.verify(hash, sign, pubKey));

}

@Test
public void testValidHashSignature3() {
SM2 key = SM2.fromPrivate(privateKey);
byte[] hash = Hex.decode("B524F552CD82B8B028476E005C377FB19A87E6FC"
+ "682D48BB5D42E3D9B9EFFE76");
SM2.SM2Signature sign = key.sign(hash);
assertTrue(SM2.verify(hash, sign, pubKey));
BigInteger sNeg = sign.s.negate().mod(SM2_N);
SM2.SM2Signature sign2 = new SM2.SM2Signature(sign.r, sNeg);
assertFalse(SM2.verify(hash, sign2, pubKey));
}

@Test
public void testValidHashSignature2() {
SM2 key = SM2.fromPrivate(privateKey);
byte[] hash = Hex.decode("B524F552CD82B8B028476E005C377FB19A87E6FC"
+ "682D48BB5D42E3D9B9EFFE76");
SM2.SM2Signature sign = key.sign(hash);
byte[] signByte = sign.toByteArray();
//System.out.println(Hex.toHexString(signByte));
assertTrue(SM2.verify(hash, sign, pubKey));

}

@Test
public void testSignatureToKeyBytes() throws SignatureException {
SM2 key = SM2.fromPrivate(privateKey);
Expand Down

0 comments on commit 0aba1b6

Please sign in to comment.