aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2018-08-10 11:28:55 +0200
committerJ08nY2018-08-10 11:28:55 +0200
commit3cd9747a4081c5448cfd071ce64433a28dd32d55 (patch)
treec4133ce567896949bc9e51259b240c0425f2153f
parent5c0cfd8398282c940407516f4021882b9ed98571 (diff)
downloadECTester-3cd9747a4081c5448cfd071ce64433a28dd32d55.tar.gz
ECTester-3cd9747a4081c5448cfd071ce64433a28dd32d55.tar.zst
ECTester-3cd9747a4081c5448cfd071ce64433a28dd32d55.zip
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeParser.java10
-rw-r--r--src/cz/crcs/ectester/common/ec/RawECPrivateKey.java45
-rw-r--r--src/cz/crcs/ectester/common/ec/RawECPublicKey.java45
-rw-r--r--src/cz/crcs/ectester/common/util/ECUtil.java57
-rw-r--r--src/cz/crcs/ectester/standalone/ECTesterStandalone.java2
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/NativeKeyAgreementSpi.java4
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java2
7 files changed, 147 insertions, 18 deletions
diff --git a/src/cz/crcs/ectester/common/cli/TreeParser.java b/src/cz/crcs/ectester/common/cli/TreeParser.java
index 23f59b1..657318d 100644
--- a/src/cz/crcs/ectester/common/cli/TreeParser.java
+++ b/src/cz/crcs/ectester/common/cli/TreeParser.java
@@ -100,9 +100,10 @@ public class TreeParser implements CommandLineParser {
if (lastCli.getArgs().length < requiredArgs) {
throw new MissingArgumentException("Not enough arguments: " + reqArgs);
- } else if (lastCli.getArgs().length > maxArgs) {
- throw new MissingArgumentException("Too many arguments.");
}
+ //else if (lastCli.getArgs().length > maxArgs) {
+ // throw new MissingArgumentException("Too many arguments.");
+ //}
subTreeCli.setName(sub);
return new TreeCommandLine(cli, subTreeCli);
@@ -118,9 +119,10 @@ public class TreeParser implements CommandLineParser {
} else {
if (cliArgs.length < requiredArgs) {
throw new MissingArgumentException("Not enough arguments: " + reqArgs);
- } else if (cliArgs.length > maxArgs) {
- throw new MissingArgumentException("Too many arguments.");
}
+ //else if (cliArgs.length > maxArgs) {
+ // throw new MissingArgumentException("Too many arguments.");
+ //}
return new TreeCommandLine(cli, null);
}
diff --git a/src/cz/crcs/ectester/common/ec/RawECPrivateKey.java b/src/cz/crcs/ectester/common/ec/RawECPrivateKey.java
new file mode 100644
index 0000000..f4556a0
--- /dev/null
+++ b/src/cz/crcs/ectester/common/ec/RawECPrivateKey.java
@@ -0,0 +1,45 @@
+package cz.crcs.ectester.common.ec;
+
+import cz.crcs.ectester.common.util.ECUtil;
+
+import java.math.BigInteger;
+import java.security.interfaces.ECPrivateKey;
+import java.security.spec.ECParameterSpec;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class RawECPrivateKey implements ECPrivateKey {
+ private BigInteger scalar;
+ private ECParameterSpec params;
+
+ public RawECPrivateKey(BigInteger scalar, ECParameterSpec params) {
+ this.scalar = scalar;
+ this.params = params;
+ }
+
+ @Override
+ public BigInteger getS() {
+ return scalar;
+ }
+
+ @Override
+ public String getAlgorithm() {
+ return "EC";
+ }
+
+ @Override
+ public String getFormat() {
+ return "Raw";
+ }
+
+ @Override
+ public byte[] getEncoded() {
+ return ECUtil.toByteArray(scalar, params.getOrder().bitLength());
+ }
+
+ @Override
+ public ECParameterSpec getParams() {
+ return params;
+ }
+}
diff --git a/src/cz/crcs/ectester/common/ec/RawECPublicKey.java b/src/cz/crcs/ectester/common/ec/RawECPublicKey.java
new file mode 100644
index 0000000..f09feef
--- /dev/null
+++ b/src/cz/crcs/ectester/common/ec/RawECPublicKey.java
@@ -0,0 +1,45 @@
+package cz.crcs.ectester.common.ec;
+
+import cz.crcs.ectester.common.util.ECUtil;
+
+import java.security.interfaces.ECPublicKey;
+import java.security.spec.ECParameterSpec;
+import java.security.spec.ECPoint;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class RawECPublicKey implements ECPublicKey {
+ private ECPoint point;
+ private ECParameterSpec params;
+
+ public RawECPublicKey(ECPoint point, ECParameterSpec params) {
+ this.point = point;
+ this.params = params;
+ }
+
+ @Override
+ public ECPoint getW() {
+ return point;
+ }
+
+ @Override
+ public String getAlgorithm() {
+ return "EC";
+ }
+
+ @Override
+ public String getFormat() {
+ return "Raw";
+ }
+
+ @Override
+ public byte[] getEncoded() {
+ return ECUtil.toX962Uncompressed(point, params);
+ }
+
+ @Override
+ public ECParameterSpec getParams() {
+ return params;
+ }
+}
diff --git a/src/cz/crcs/ectester/common/util/ECUtil.java b/src/cz/crcs/ectester/common/util/ECUtil.java
index 0979d91..3c1f138 100644
--- a/src/cz/crcs/ectester/common/util/ECUtil.java
+++ b/src/cz/crcs/ectester/common/util/ECUtil.java
@@ -1,6 +1,13 @@
package cz.crcs.ectester.common.util;
+import cz.crcs.ectester.applet.EC_Consts;
+import cz.crcs.ectester.common.ec.*;
+import cz.crcs.ectester.data.EC_Store;
+
import java.math.BigInteger;
+import java.security.KeyPair;
+import java.security.interfaces.ECPrivateKey;
+import java.security.interfaces.ECPublicKey;
import java.security.spec.*;
/**
@@ -33,12 +40,8 @@ public class ECUtil {
return ByteUtil.concatenate(new byte[]{marker}, x);
}
- public static byte[] toX962Compressed(ECPoint point, EllipticCurve curve) {
- return toX962Compressed(point, curve.getField().getFieldSize());
- }
-
public static byte[] toX962Compressed(ECPoint point, ECParameterSpec spec) {
- return toX962Compressed(point, spec.getCurve());
+ return toX962Compressed(point, spec.getOrder().bitLength());
}
public static byte[] toX962Uncompressed(ECPoint point, int bits) {
@@ -50,12 +53,8 @@ public class ECUtil {
return ByteUtil.concatenate(new byte[]{0x04}, x, y);
}
- public static byte[] toX962Uncompressed(ECPoint point, EllipticCurve curve) {
- return toX962Uncompressed(point, curve.getField().getFieldSize());
- }
-
public static byte[] toX962Uncompressed(ECPoint point, ECParameterSpec spec) {
- return toX962Uncompressed(point, spec.getCurve());
+ return toX962Uncompressed(point, spec.getOrder().bitLength());
}
public static byte[] toX962Hybrid(ECPoint point, int bits) {
@@ -146,7 +145,7 @@ public class ECUtil {
alpha = alpha.add(x.multiply(a));
alpha = alpha.add(b);
- if(!isResidue(alpha, p)) {
+ if (!isResidue(alpha, p)) {
throw new IllegalArgumentException();
}
@@ -175,4 +174,40 @@ public class ECUtil {
throw new IllegalArgumentException();
}
}
+
+ private static ECPoint toPoint(EC_Params params) {
+ return new ECPoint(
+ new BigInteger(1, params.getParam(EC_Consts.PARAMETER_W)[0]),
+ new BigInteger(1, params.getParam(EC_Consts.PARAMETER_W)[1]));
+ }
+
+ private static BigInteger toScalar(EC_Params params) {
+ return new BigInteger(1, params.getParam(EC_Consts.PARAMETER_S)[0]);
+ }
+
+ public static ECPublicKey toPublicKey(EC_Key.Public pubkey) {
+ EC_Curve curve = EC_Store.getInstance().getObject(EC_Curve.class, pubkey.getCurve());
+ if (curve == null) {
+ throw new IllegalArgumentException("pubkey curve nor found: " + pubkey.getCurve());
+ }
+ return new RawECPublicKey(toPoint(pubkey), curve.toSpec());
+ }
+
+ public static ECPrivateKey toPrivateKey(EC_Key.Private privkey) {
+ EC_Curve curve = EC_Store.getInstance().getObject(EC_Curve.class, privkey.getCurve());
+ if (curve == null) {
+ throw new IllegalArgumentException("privkey curve nor found: " + privkey.getCurve());
+ }
+ return new RawECPrivateKey(toScalar(privkey), curve.toSpec());
+ }
+
+ public static KeyPair toKeyPair(EC_Keypair kp) {
+ EC_Curve curve = EC_Store.getInstance().getObject(EC_Curve.class, kp.getCurve());
+ if (curve == null) {
+ throw new IllegalArgumentException("keypair curve nor found: " + kp.getCurve());
+ }
+ ECPublicKey pubkey = new RawECPublicKey(toPoint(kp), curve.toSpec());
+ ECPrivateKey privkey = new RawECPrivateKey(toScalar(kp), curve.toSpec());
+ return new KeyPair(pubkey, privkey);
+ }
}
diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
index 639e416..364fde4 100644
--- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
+++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
@@ -524,6 +524,8 @@ public class ECTesterStandalone {
break;
}
+ String suiteName = cli.getArg(0);
+
StandaloneTestSuite suite = new StandaloneDefaultSuite(writer, cfg, cli);
suite.run();
}
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/NativeKeyAgreementSpi.java b/src/cz/crcs/ectester/standalone/libs/jni/NativeKeyAgreementSpi.java
index 12bc354..4858ccd 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/NativeKeyAgreementSpi.java
+++ b/src/cz/crcs/ectester/standalone/libs/jni/NativeKeyAgreementSpi.java
@@ -76,7 +76,7 @@ public abstract class NativeKeyAgreementSpi extends KeyAgreementSpi {
if (publicKey instanceof NativeECPublicKey) {
return ((NativeECPublicKey) publicKey).getData();
} else {
- return ECUtil.toX962Uncompressed(publicKey.getW(), ((ECParameterSpec) params).getCurve());
+ return ECUtil.toX962Uncompressed(publicKey.getW(), ((ECParameterSpec) params));
}
}
@@ -84,7 +84,7 @@ public abstract class NativeKeyAgreementSpi extends KeyAgreementSpi {
if (privateKey instanceof NativeECPrivateKey) {
return ((NativeECPrivateKey) privateKey).getData();
} else {
- return ECUtil.toByteArray(privateKey.getS(), ((ECParameterSpec) params).getCurve().getField().getFieldSize());
+ return ECUtil.toByteArray(privateKey.getS(), ((ECParameterSpec) params).getOrder().bitLength());
}
}
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java b/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java
index b60f2c6..602b1c4 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java
+++ b/src/cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi.java
@@ -71,7 +71,7 @@ public abstract class NativeSignatureSpi extends SignatureSpi {
if (signKey instanceof NativeECPrivateKey) {
privkey = ((NativeECPrivateKey) signKey).getData();
} else {
- privkey = ECUtil.toByteArray(signKey.getS(), params.getCurve().getField().getFieldSize());
+ privkey = ECUtil.toByteArray(signKey.getS(), params.getOrder().bitLength());
}
return sign(buffer.toByteArray(), privkey, params);
}