Skip to content

Commit

Permalink
Merge pull request #33 from crocs-muni/fix/card-runtest
Browse files Browse the repository at this point in the history
Fix test selection
  • Loading branch information
J08nY authored Aug 2, 2024
2 parents e8cdbe9 + 6ed466c commit 7647d4b
Show file tree
Hide file tree
Showing 47 changed files with 1,073 additions and 1,027 deletions.
27 changes: 27 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
comment:
layout: "condensed_header, condensed_files, diff, components, condensed_footer" # show component info in the PR comment

component_management:
default_rules: # default rules that will be inherited by all components
statuses:
- type: project # in this case every component that doesn't have a status defined will have a project type one
target: auto
branches:
- "!master"
individual_components:
- component_id: applet_package
name: Applet
paths:
- applet/**
- component_id: common_package
name: Common
paths:
- common/**
- component_id: reader_package
name: Reader
paths:
- reader/**
- component_id: standalone_package
name: Standalone
paths:
- standalone/**
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ jobs:
path: |
reader/build/libs/ECTesterReader.jar
- name: Upload code coverage
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: crocs-muni/ECTester

standalone:
runs-on: ubuntu-latest
permissions:
Expand Down
62 changes: 49 additions & 13 deletions common/src/main/java/cz/crcs/ectester/common/ec/EC_Curve.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.math.BigInteger;
import java.security.spec.*;
import java.util.Arrays;

/**
* An Elliptic curve, contains parameters Fp/F2M, A, B, G, R, (K)?.
Expand Down Expand Up @@ -53,22 +54,35 @@ public String toString() {
return "<" + getId() + "> " + (field == EC_Consts.ALG_EC_FP ? "Prime" : "Binary") + " field Elliptic curve (" + String.valueOf(bits) + "b)" + (desc == null ? "" : ": " + desc) + System.lineSeparator() + super.toString();
}

private int[] getPowers() {
if (this.field == EC_Consts.ALG_EC_F2M) {
byte[][] fieldData = getParam(EC_Consts.PARAMETER_F2M);
int e1 = ByteUtil.getShort(fieldData[1], 0);
int e2 = ByteUtil.getShort(fieldData[2], 0);
int e3 = ByteUtil.getShort(fieldData[3], 0);
int[] powers = Arrays.stream(new int[]{e1, e2, e3}).sorted().toArray();
e1 = powers[0];
e2 = powers[1];
e3 = powers[2];
if (e1 == 0 && e2 == 0) {
powers = new int[]{e3};
} else {
powers = new int[]{e3, e2, e1};
}
return powers;
} else {
return null;
}
}

public EllipticCurve toCurve() {
ECField field;
if (this.field == EC_Consts.ALG_EC_FP) {
field = new ECFieldFp(new BigInteger(1, getData(0)));
} else {
byte[][] fieldData = getParam(EC_Consts.PARAMETER_F2M);
int m = ByteUtil.getShort(fieldData[0], 0);
int e1 = ByteUtil.getShort(fieldData[1], 0);
int e2 = ByteUtil.getShort(fieldData[2], 0);
int e3 = ByteUtil.getShort(fieldData[3], 0);
int[] powers;
if (e2 == 0 && e3 == 0) {
powers = new int[]{e1};
} else {
powers = new int[]{e1, e2, e3};
}
int[] powers = getPowers();
field = new ECFieldF2m(m, powers);
}

Expand All @@ -78,6 +92,26 @@ public EllipticCurve toCurve() {
return new EllipticCurve(field, a, b);
}

/**
* Constructs EllipticCurve from EC_Curve even if the parameters of the curve are wrong.
*/
public EllipticCurve toCustomCurve() {
ECField field;
if (this.field == EC_Consts.ALG_EC_FP) {
field = new CustomECFieldFp(new BigInteger(1, this.getData(0)));
} else {
byte[][] fieldData = this.getParam(EC_Consts.PARAMETER_F2M);
int m = ByteUtil.getShort(fieldData[0], 0);
int[] powers = getPowers();
field = new CustomECFieldF2m(m, powers);
}

BigInteger a = new BigInteger(1, this.getParam(EC_Consts.PARAMETER_A)[0]);
BigInteger b = new BigInteger(1, this.getParam(EC_Consts.PARAMETER_B)[0]);

return new CustomEllipticCurve(field, a, b);
}

public ECCurve toBCCurve() {
if (this.field == EC_Consts.ALG_EC_FP) {
BigInteger p = new BigInteger(1, getParam(EC_Consts.PARAMETER_FP)[0]);
Expand All @@ -89,14 +123,16 @@ public ECCurve toBCCurve() {
} else {
byte[][] fieldData = getParam(EC_Consts.PARAMETER_F2M);
int m = ByteUtil.getShort(fieldData[0], 0);
int e1 = ByteUtil.getShort(fieldData[1], 0);
int e2 = ByteUtil.getShort(fieldData[2], 0);
int e3 = ByteUtil.getShort(fieldData[3], 0);
BigInteger a = new BigInteger(1, getParam(EC_Consts.PARAMETER_A)[0]);
BigInteger b = new BigInteger(1, getParam(EC_Consts.PARAMETER_B)[0]);
BigInteger r = new BigInteger(1, getParam(EC_Consts.PARAMETER_R)[0]);
BigInteger k = new BigInteger(1, getParam(EC_Consts.PARAMETER_K)[0]);
return new ECCurve.F2m(m, e1, e2, e3, a, b, r, k);
int[] powers = getPowers();
if (powers.length == 1) {
return new ECCurve.F2m(m, powers[0], 0, 0, a, b, r, k);
} else {
return new ECCurve.F2m(m, powers[2], powers[1], powers[0], a, b, r, k);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ private String errorString(Throwable error) {
for (Throwable t = error; t != null; t = t.getCause()) {
sb.append("═══ ").append(t.toString()).append(" ═══");
sb.append(System.lineSeparator());
sb.append("═══ Stack trace: ═══").append(System.lineSeparator());
for (StackTraceElement s : t.getStackTrace()) {
sb.append("═══ ").append(s.toString()).append(" ═══");
sb.append(System.lineSeparator());
}
if (t.getCause() != null) {
sb.append("═══ Caused by: ═══").append(System.lineSeparator());
}
}
sb.append("═══ Stack trace: ═══").append(System.lineSeparator());
for (StackTraceElement s : error.getStackTrace()) {
sb.append("═══ ").append(s.toString()).append(" ═══");
sb.append(System.lineSeparator());
}

return sb.toString();
}

Expand Down
38 changes: 27 additions & 11 deletions common/src/main/java/cz/crcs/ectester/common/test/CompoundTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

Expand All @@ -16,13 +17,35 @@ public class CompoundTest extends Test implements Cloneable {
private Test[] tests;
private String description = "";

private final static Consumer<Test[]> RUN_ALL = tests -> {
public final static BiFunction<Result.ExpectedValue, Test[], Result> EXPECT_ALL = (what, tests) -> {
for (Test test : tests) {
if (!Result.Value.fromExpected(what, test.ok()).ok()) {
return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
}
}
return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
};

public final static Function<Test[], Result> EXPECT_ALL_SUCCESS = tests -> EXPECT_ALL.apply(Result.ExpectedValue.SUCCESS, tests);
public final static Function<Test[], Result> EXPECT_ALL_FAILURE = tests -> EXPECT_ALL.apply(Result.ExpectedValue.FAILURE, tests);
public final static Function<Test[], Result> EXPECT_ALL_ANY = tests -> EXPECT_ALL.apply(Result.ExpectedValue.ANY, tests);

public final static Consumer<Test[]> RUN_ALL = tests -> {
for (Test t : tests) {
t.run();
}
};

private final static Consumer<Test[]> RUN_GREEDY_ALL = tests -> {
public final static Consumer<Test[]> RUN_ALL_IF_FIRST = tests -> {
tests[0].run();
if (tests[0].getResult().getValue().equals(Result.Value.SUCCESS) || tests[0].getResult().getValue().equals(Result.Value.UXSUCCESS)) {
for (int i = 1; i < tests.length; i++) {
tests[i].run();
}
}
};

public final static Consumer<Test[]> RUN_GREEDY_ALL = tests -> {
for (Test t : tests) {
t.run();
if (!t.ok()) {
Expand All @@ -31,7 +54,7 @@ public class CompoundTest extends Test implements Cloneable {
}
};

private final static Consumer<Test[]> RUN_GREEDY_ANY = tests -> {
public final static Consumer<Test[]> RUN_GREEDY_ANY = tests -> {
for (Test t : tests) {
t.run();
if (t.ok()) {
Expand Down Expand Up @@ -68,14 +91,7 @@ public static CompoundTest function(Function<Test[], Result> callback, Consumer<
}

private static CompoundTest expectAll(Result.ExpectedValue what, Consumer<Test[]> runCallback, Test[] all) {
return new CompoundTest((tests) -> {
for (Test test : tests) {
if (!Result.Value.fromExpected(what, test.ok()).ok()) {
return new Result(Result.Value.FAILURE, "Some sub-tests did not have the expected result.");
}
}
return new Result(Result.Value.SUCCESS, "All sub-tests had the expected result.");
}, runCallback, all);
return new CompoundTest((tests) -> EXPECT_ALL.apply(what, tests), runCallback, all);
}

public static CompoundTest all(Result.ExpectedValue what, Test... all) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void run(int from, int to) {
* @return The test that was run.
* @throws TestException
*/
protected <T extends Test> T runTest(T t) {
private <T extends Test> T runTest(T t) {
running = t;
writer.beginTest(t);
t.run();
Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/cz/crcs/ectester/common/util/CardUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,23 @@ public static String getSigTypeString(byte sigType) {
}
}

public static String getSigTypeAlgoString(byte sigType) {
switch (sigType) {
case EC_Consts.Signature_ALG_ECDSA_SHA:
return "SHA1withECDSA";
case EC_Consts.Signature_ALG_ECDSA_SHA_224:
return "SHA224withECDSA";
case EC_Consts.Signature_ALG_ECDSA_SHA_256:
return "SHA256withECDSA";
case EC_Consts.Signature_ALG_ECDSA_SHA_384:
return "SHA384withECDSA";
case EC_Consts.Signature_ALG_ECDSA_SHA_512:
return "SHA512withECDSA";
default:
return "unknown";
}
}

public static byte getSigType(String sigTypeString) {
switch (sigTypeString) {
case "ECDSA_SHA":
Expand Down
Loading

0 comments on commit 7647d4b

Please sign in to comment.