aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/util/ECUtil.java
diff options
context:
space:
mode:
authorJ08nY2024-03-16 12:43:03 +0100
committerJ08nY2024-03-16 12:43:03 +0100
commit815bf7bfcd10943e7ed60a9900e8a9bacd0c896a (patch)
tree2de0dc784e7cd680d449765be78a03e35657d786 /src/cz/crcs/ectester/common/util/ECUtil.java
parent65d00d2354f4b68919153e35c02e744b3defdb1b (diff)
downloadECTester-815bf7bfcd10943e7ed60a9900e8a9bacd0c896a.tar.gz
ECTester-815bf7bfcd10943e7ed60a9900e8a9bacd0c896a.tar.zst
ECTester-815bf7bfcd10943e7ed60a9900e8a9bacd0c896a.zip
Diffstat (limited to 'src/cz/crcs/ectester/common/util/ECUtil.java')
-rw-r--r--src/cz/crcs/ectester/common/util/ECUtil.java30
1 files changed, 10 insertions, 20 deletions
diff --git a/src/cz/crcs/ectester/common/util/ECUtil.java b/src/cz/crcs/ectester/common/util/ECUtil.java
index db1169e..0703423 100644
--- a/src/cz/crcs/ectester/common/util/ECUtil.java
+++ b/src/cz/crcs/ectester/common/util/ECUtil.java
@@ -5,6 +5,8 @@ import cz.crcs.ectester.common.ec.*;
import cz.crcs.ectester.data.EC_Store;
import org.bouncycastle.asn1.*;
import org.bouncycastle.crypto.digests.SHA1Digest;
+import org.bouncycastle.crypto.signers.PlainDSAEncoding;
+import org.bouncycastle.crypto.signers.StandardDSAEncoding;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
@@ -345,24 +347,7 @@ public class ECUtil {
return new KeyPair(pubkey, privkey);
}
- public static byte[] toDERSignature(byte[] r, byte[] s) throws IOException {
- ASN1Integer rInt = new ASN1Integer(r);
- ASN1Integer sInt = new ASN1Integer(s);
- DERSequence seq = new DERSequence(new ASN1Encodable[]{rInt, sInt});
- return seq.getEncoded();
- }
-
- public static BigInteger[] fromDERSignature(byte[] signature) throws IOException {
- ByteArrayInputStream inputStream = new ByteArrayInputStream(signature);
- ASN1InputStream asn1InputStream = new ASN1InputStream(inputStream);
- ASN1Sequence asn1Sequence = ASN1Sequence.getInstance(asn1InputStream.readObject());
-
- ASN1Integer r = (ASN1Integer) asn1Sequence.getObjectAt(0);
- ASN1Integer s = (ASN1Integer) asn1Sequence.getObjectAt(1);
- return new BigInteger[]{r.getPositiveValue(), s.getPositiveValue()};
- }
-
- public static BigInteger recoverSignatureNonce(byte[] signature, byte[] data, BigInteger privkey, ECParameterSpec params, String hashType) {
+ public static BigInteger recoverSignatureNonce(byte[] signature, byte[] data, BigInteger privkey, ECParameterSpec params, String hashType, String sigType) {
try {
int bitSize = params.getOrder().bitLength();
// Hash the data.
@@ -380,8 +365,13 @@ public class ECUtil {
hashInt = hashInt.shiftRight(hashBits - bitSize);
}
- // Parse DERSignature
- BigInteger[] sigPair = fromDERSignature(signature);
+ // Parse signature
+ BigInteger[] sigPair;
+ if (sigType.contains("CVC") || sigType.contains("PLAIN")) {
+ sigPair = PlainDSAEncoding.INSTANCE.decode(params.getOrder(), signature);
+ } else {
+ sigPair = StandardDSAEncoding.INSTANCE.decode(params.getOrder(), signature);
+ }
BigInteger r = sigPair[0];
BigInteger s = sigPair[1];