aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common
diff options
context:
space:
mode:
authordavidhofman2021-09-06 19:51:02 +0200
committerGitHub2021-09-06 19:51:02 +0200
commit69ce36e4537be52c103e8b5a314cca49cc931d8f (patch)
treea911f3c6586a00c151ed054794073bb7cece904f /src/cz/crcs/ectester/common
parentf1f6ca75524d152e347be8926fd43c98aeb504ac (diff)
downloadECTester-69ce36e4537be52c103e8b5a314cca49cc931d8f.tar.gz
ECTester-69ce36e4537be52c103e8b5a314cca49cc931d8f.tar.zst
ECTester-69ce36e4537be52c103e8b5a314cca49cc931d8f.zip
Implement StandaloneWrongSuite. (#12)
* Add StandaloneWrongSuite. * Partially implement StandaloneWrongSuite. * Add setParam method to EC_Params. * Fix new setParam method in EC_Params. * Implement StandaloneWrongSuite * Add custom classes for testing wrong curve parameters. * Update custom classes. * Add more custom classes for testing curves with wrong parameters. * Modify StandaloneWrongSuite to work with the new custom classes. * Various small cosmetic changes to new custom classes. * Add missing author information to various classes. * Fix a small mistake in CustomECFieldF2m * Add randomG test, change some variables to final. * Add option to skip certain tests + various small changes.
Diffstat (limited to 'src/cz/crcs/ectester/common')
-rw-r--r--src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java67
-rw-r--r--src/cz/crcs/ectester/common/ec/CustomECFieldFp.java43
-rw-r--r--src/cz/crcs/ectester/common/ec/CustomECParameterSpec.java47
-rw-r--r--src/cz/crcs/ectester/common/ec/CustomEllipticCurve.java60
-rw-r--r--src/cz/crcs/ectester/common/ec/EC_Params.java39
5 files changed, 256 insertions, 0 deletions
diff --git a/src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java b/src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java
new file mode 100644
index 0000000..24ea5aa
--- /dev/null
+++ b/src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java
@@ -0,0 +1,67 @@
+package cz.crcs.ectester.common.ec;
+
+import java.math.BigInteger;
+import java.security.spec.ECFieldF2m;
+import java.util.Arrays;
+
+/**
+ * @author David Hofman
+ */
+public class CustomECFieldF2m extends ECFieldF2m {
+ private int m;
+ private int[] ks;
+ private BigInteger rp;
+
+ public CustomECFieldF2m(int m, int[] ks) {
+ //feed the constructor of the superclass some default, valid data
+ //getters will return custom parameters instead
+ super(163, new int[] {3, 2, 1});
+ this.m = m;
+ this.ks = ks.clone();
+
+ //causes ArithmeticException if m < 0 or any element of ks < 0
+ this.rp = BigInteger.ONE;
+ this.rp = this.rp.setBit(m);
+ for(int i = 0; i < this.ks.length; ++i) {
+ this.rp = this.rp.setBit(this.ks[i]);
+ }
+ }
+
+ @Override
+ public int getFieldSize() {
+ return m;
+ }
+
+ @Override
+ public int getM() {
+ return m;
+ }
+
+ @Override
+ public int[] getMidTermsOfReductionPolynomial() {
+ return ks.clone();
+ }
+
+ @Override
+ public BigInteger getReductionPolynomial() {
+ return rp;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ } else if (!(o instanceof CustomECFieldF2m)) {
+ return false;
+ } else {
+ return m == ((CustomECFieldF2m) o).m && Arrays.equals(ks, ((CustomECFieldF2m) o).ks);
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = m << 5;
+ hash += rp == null ? 0 : rp.hashCode();
+ return hash;
+ }
+}
diff --git a/src/cz/crcs/ectester/common/ec/CustomECFieldFp.java b/src/cz/crcs/ectester/common/ec/CustomECFieldFp.java
new file mode 100644
index 0000000..eafcb72
--- /dev/null
+++ b/src/cz/crcs/ectester/common/ec/CustomECFieldFp.java
@@ -0,0 +1,43 @@
+package cz.crcs.ectester.common.ec;
+
+import java.math.BigInteger;
+import java.security.spec.ECFieldFp;
+
+/**
+ * @author David Hofman
+ */
+public class CustomECFieldFp extends ECFieldFp {
+ private BigInteger p;
+
+ public CustomECFieldFp(BigInteger p) {
+ //feed the constructor of the superclass some default, valid parameter p
+ //getters will return custom (and possibly invalid) data
+ super(BigInteger.ONE);
+ this.p = p;
+ }
+
+
+ @Override
+ public int getFieldSize() {
+ return p.bitCount();
+ }
+
+ @Override
+ public BigInteger getP() {
+ return p;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ } else {
+ return o instanceof CustomECFieldFp && p.equals(((CustomECFieldFp) o).p);
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return p.hashCode();
+ }
+}
diff --git a/src/cz/crcs/ectester/common/ec/CustomECParameterSpec.java b/src/cz/crcs/ectester/common/ec/CustomECParameterSpec.java
new file mode 100644
index 0000000..cbc15e7
--- /dev/null
+++ b/src/cz/crcs/ectester/common/ec/CustomECParameterSpec.java
@@ -0,0 +1,47 @@
+package cz.crcs.ectester.common.ec;
+
+import java.math.BigInteger;
+import java.security.spec.ECFieldFp;
+import java.security.spec.ECParameterSpec;
+import java.security.spec.ECPoint;
+import java.security.spec.EllipticCurve;
+
+/**
+ * @author David Hofman
+ */
+public class CustomECParameterSpec extends ECParameterSpec {
+ private EllipticCurve curve;
+ private ECPoint g;
+ private BigInteger n;
+ private int h;
+
+ public CustomECParameterSpec(EllipticCurve curve, ECPoint g, BigInteger n, int h) {
+ //feed the constructor of the superclass some default, valid data
+ //getters will return custom (and possibly invalid) parameters instead
+ super(new EllipticCurve(new ECFieldFp(BigInteger.ONE),BigInteger.ZERO,BigInteger.ZERO), new ECPoint(BigInteger.ZERO, BigInteger.ZERO), BigInteger.ONE, 1);
+ this.curve = curve;
+ this.g = g;
+ this.n = n;
+ this.h = h;
+ }
+
+ @Override
+ public EllipticCurve getCurve() {
+ return curve;
+ }
+
+ @Override
+ public ECPoint getGenerator() {
+ return g;
+ }
+
+ @Override
+ public BigInteger getOrder() {
+ return n;
+ }
+
+ @Override
+ public int getCofactor() {
+ return h;
+ }
+}
diff --git a/src/cz/crcs/ectester/common/ec/CustomEllipticCurve.java b/src/cz/crcs/ectester/common/ec/CustomEllipticCurve.java
new file mode 100644
index 0000000..489861c
--- /dev/null
+++ b/src/cz/crcs/ectester/common/ec/CustomEllipticCurve.java
@@ -0,0 +1,60 @@
+package cz.crcs.ectester.common.ec;
+
+import java.math.BigInteger;
+import java.security.spec.ECField;
+import java.security.spec.ECFieldFp;
+import java.security.spec.EllipticCurve;
+
+/**
+ * @author David Hofman
+ */
+public class CustomEllipticCurve extends EllipticCurve {
+ private ECField field;
+ private BigInteger a;
+ private BigInteger b;
+
+ public CustomEllipticCurve(ECField field, BigInteger a, BigInteger b) {
+ //feed the constructor of the superclass some default, valid EC parameters
+ //getters will return custom (and possibly invalid) data instead
+ super(new ECFieldFp(BigInteger.ONE), BigInteger.ZERO, BigInteger.ZERO);
+ this.field = field;
+ this.a = a;
+ this.b = b;
+
+ }
+
+ @Override
+ public BigInteger getA() {
+ return a;
+ }
+
+ @Override
+ public BigInteger getB() {
+ return b;
+ }
+
+ @Override
+ public ECField getField() {
+ return field;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ } else {
+ if (o instanceof CustomEllipticCurve) {
+ CustomEllipticCurve otherCurve = (CustomEllipticCurve) o;
+ if (field.equals(otherCurve.field) && a.equals(otherCurve.a) && b.equals(otherCurve.b)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return field.hashCode() << 6 + (a.hashCode() << 4) + (b.hashCode() << 2);
+ }
+}
diff --git a/src/cz/crcs/ectester/common/ec/EC_Params.java b/src/cz/crcs/ectester/common/ec/EC_Params.java
index b08fdfd..e922feb 100644
--- a/src/cz/crcs/ectester/common/ec/EC_Params.java
+++ b/src/cz/crcs/ectester/common/ec/EC_Params.java
@@ -88,6 +88,45 @@ public class EC_Params extends EC_Data {
return result;
}
+ public boolean setParam(short param, byte[][] value) {
+ if (!hasParam(param)) {
+ return false;
+ }
+ if (Integer.bitCount(param) != 1) {
+ return false;
+ }
+ short paramMask = EC_Consts.PARAMETER_FP;
+ int i = 0;
+ while (paramMask <= EC_Consts.PARAMETER_S) {
+ short masked = (short) (this.params & param & paramMask);
+ short shallow = (short) (this.params & paramMask);
+ if (masked != 0) {
+ if (masked == EC_Consts.PARAMETER_F2M) {
+ data[i] = value[0];
+ data[i + 1] = value[1];
+ data[i + 2] = value[2];
+ data[i + 3] = value[3];
+ break;
+ }
+ if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) {
+ data[i] = value[0];
+ data[i + 1] = value[1];
+ break;
+ }
+ data[i] = value[0];
+ }
+ if (shallow == EC_Consts.PARAMETER_F2M) {
+ i += 4;
+ } else if (shallow == EC_Consts.PARAMETER_G || shallow == EC_Consts.PARAMETER_W) {
+ i += 2;
+ } else if (shallow != 0) {
+ i++;
+ }
+ paramMask = (short) (paramMask << 1);
+ }
+ return true;
+ }
+
public boolean hasParam(short param) {
return (params & param) != 0;
}