diff options
| author | J08nY | 2024-03-22 23:58:55 +0100 |
|---|---|---|
| committer | J08nY | 2024-03-25 14:52:43 +0100 |
| commit | 73af477a8774e1ede5dd8de6491eb353dc0b12bd (patch) | |
| tree | 2d4e3b19bc5fb55308b886032312be76341736d4 /src/cz/crcs/ectester/common/ec | |
| parent | 64b95fa059295e1dc23371c849f2302c1c18f5b4 (diff) | |
| download | ECTester-73af477a8774e1ede5dd8de6491eb353dc0b12bd.tar.gz ECTester-73af477a8774e1ede5dd8de6491eb353dc0b12bd.tar.zst ECTester-73af477a8774e1ede5dd8de6491eb353dc0b12bd.zip | |
Basic Gradle setup.
Diffstat (limited to 'src/cz/crcs/ectester/common/ec')
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java | 67 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/CustomECFieldFp.java | 43 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/CustomECParameterSpec.java | 47 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/CustomEllipticCurve.java | 60 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_Category.java | 110 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_Curve.java | 164 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_Data.java | 265 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_KAResult.java | 65 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_Key.java | 83 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_Keypair.java | 41 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_Params.java | 234 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/EC_SigResult.java | 75 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/RawECPrivateKey.java | 46 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/ec/RawECPublicKey.java | 46 |
14 files changed, 0 insertions, 1346 deletions
diff --git a/src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java b/src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java deleted file mode 100644 index 24ea5aa..0000000 --- a/src/cz/crcs/ectester/common/ec/CustomECFieldF2m.java +++ /dev/null @@ -1,67 +0,0 @@ -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 deleted file mode 100644 index eafcb72..0000000 --- a/src/cz/crcs/ectester/common/ec/CustomECFieldFp.java +++ /dev/null @@ -1,43 +0,0 @@ -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 deleted file mode 100644 index cbc15e7..0000000 --- a/src/cz/crcs/ectester/common/ec/CustomECParameterSpec.java +++ /dev/null @@ -1,47 +0,0 @@ -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 deleted file mode 100644 index 489861c..0000000 --- a/src/cz/crcs/ectester/common/ec/CustomEllipticCurve.java +++ /dev/null @@ -1,60 +0,0 @@ -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_Category.java b/src/cz/crcs/ectester/common/ec/EC_Category.java deleted file mode 100644 index 154403e..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_Category.java +++ /dev/null @@ -1,110 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.common.cli.Colors; - -import java.util.*; -import java.util.stream.Collectors; - -/** - * A category of EC_Data objects, has a name, description and represents a directory in - * the cz.crcs.ectester.data package. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public class EC_Category { - - private String name; - private String directory; - private String desc; - - private Map<String, EC_Data> objects; - - - public EC_Category(String name, String directory) { - this.name = name; - this.directory = directory; - } - - public EC_Category(String name, String directory, String desc) { - this(name, directory); - this.desc = desc; - } - - public EC_Category(String name, String directory, String desc, Map<String, EC_Data> objects) { - this(name, directory, desc); - this.objects = objects; - } - - public String getName() { - return name; - } - - public String getDirectory() { - return directory; - } - - public String getDesc() { - return desc; - } - - public Map<String, EC_Data> getObjects() { - return Collections.unmodifiableMap(objects); - } - - public <T extends EC_Data> Map<String, T> getObjects(Class<T> cls) { - Map<String, T> objs = new TreeMap<>(); - for (Map.Entry<String, EC_Data> entry : objects.entrySet()) { - if (cls.isInstance(entry.getValue())) { - objs.put(entry.getKey(), cls.cast(entry.getValue())); - } - } - return Collections.unmodifiableMap(objs); - } - - public <T extends EC_Data> T getObject(Class<T> cls, String id) { - EC_Data obj = objects.get(id); - if (cls.isInstance(obj)) { - return cls.cast(obj); - } else { - return null; - } - } - - @Override - public String toString() { - StringBuilder out = new StringBuilder(); - out.append("\t- ").append(Colors.bold(name)).append((desc == null || desc.equals("")) ? "" : ": " + desc); - out.append(System.lineSeparator()); - - String[] headers = new String[]{"Curves", "Public keys", "Private keys", "KeyPairs", "Results(KA)", "Results(SIG)"}; - Class<EC_Data>[] classes = new Class[]{EC_Curve.class, EC_Key.Public.class, EC_Key.Private.class, EC_Keypair.class, EC_KAResult.class, EC_SigResult.class}; - for (int i = 0; i < headers.length; ++i) { - Map<String, EC_Data> data = getObjects(classes[i]); - int size = data.size(); - if (size > 0) { - out.append(Colors.bold(String.format("\t\t%s: ", headers[i]))); - List<EC_Data> sorted = new ArrayList<>(data.values()); - Collections.sort(sorted); - for (EC_Data element : sorted) { - out.append(element.getId()); - size--; - if (size > 0) - out.append(", "); - } - out.append(System.lineSeparator()); - } - } - return out.toString(); - } - - @Override - public boolean equals(Object obj) { - return obj instanceof EC_Category && Objects.equals(this.name, ((EC_Category) obj).name); - } - - @Override - public int hashCode() { - return this.name.hashCode() ^ this.directory.hashCode(); - } - -} diff --git a/src/cz/crcs/ectester/common/ec/EC_Curve.java b/src/cz/crcs/ectester/common/ec/EC_Curve.java deleted file mode 100644 index d188551..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_Curve.java +++ /dev/null @@ -1,164 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.applet.EC_Consts; -import cz.crcs.ectester.common.util.ByteUtil; -import javacard.security.KeyPair; -import org.bouncycastle.math.ec.ECCurve; - -import java.math.BigInteger; -import java.security.spec.*; - -/** - * An Elliptic curve, contains parameters Fp/F2M, A, B, G, R, (K)?. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public class EC_Curve extends EC_Params { - private short bits; - private byte field; - private String desc; - - /** - * @param bits - * @param field KeyPair.ALG_EC_FP or KeyPair.ALG_EC_F2M - */ - public EC_Curve(short bits, byte field) { - super(field == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M); - this.bits = bits; - this.field = field; - } - - public EC_Curve(String id, short bits, byte field) { - this(bits, field); - this.id = id; - } - - public EC_Curve(String id, short bits, byte field, String desc) { - this(id, bits, field); - this.desc = desc; - } - - public short getBits() { - return bits; - } - - public byte getField() { - return field; - } - - public String getDesc() { - return desc; - } - - @Override - public String toString() { - return "<" + getId() + "> " + (field == KeyPair.ALG_EC_FP ? "Prime" : "Binary") + " field Elliptic curve (" + String.valueOf(bits) + "b)" + (desc == null ? "" : ": " + desc) + System.lineSeparator() + super.toString(); - } - - public EllipticCurve toCurve() { - ECField field; - if (this.field == KeyPair.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}; - } - field = new ECFieldF2m(m, powers); - } - - BigInteger a = new BigInteger(1, getParam(EC_Consts.PARAMETER_A)[0]); - BigInteger b = new BigInteger(1, getParam(EC_Consts.PARAMETER_B)[0]); - - return new EllipticCurve(field, a, b); - } - - public ECCurve toBCCurve() { - if (this.field == KeyPair.ALG_EC_FP) { - BigInteger p = new BigInteger(1, getParam(EC_Consts.PARAMETER_FP)[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.Fp(p, a, b, r, k); - } 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); - } - } - - public ECParameterSpec toSpec() { - EllipticCurve curve = toCurve(); - - byte[][] G = getParam(EC_Consts.PARAMETER_G); - BigInteger gx = new BigInteger(1, G[0]); - BigInteger gy = new BigInteger(1, G[1]); - ECPoint generator = new ECPoint(gx, gy); - - BigInteger n = new BigInteger(1, getParam(EC_Consts.PARAMETER_R)[0]); - - int h = ByteUtil.getShortInt(getParam(EC_Consts.PARAMETER_K)[0], 0); - return new ECParameterSpec(curve, generator, n, h); - } - - public static EC_Curve fromSpec(ECParameterSpec spec) { - EllipticCurve curve = spec.getCurve(); - ECField field = curve.getField(); - - short bits = (short) field.getFieldSize(); - byte[][] params; - int paramIndex = 0; - byte fieldType; - if (field instanceof ECFieldFp) { - ECFieldFp primeField = (ECFieldFp) field; - params = new byte[7][]; - params[paramIndex++] = primeField.getP().toByteArray(); - fieldType = KeyPair.ALG_EC_FP; - } else if (field instanceof ECFieldF2m) { - ECFieldF2m binaryField = (ECFieldF2m) field; - params = new byte[10][]; - params[paramIndex] = new byte[2]; - ByteUtil.setShort(params[paramIndex++], 0, (short) binaryField.getM()); - int[] powers = binaryField.getMidTermsOfReductionPolynomial(); - for (int i = 0; i < 3; ++i) { - params[paramIndex] = new byte[2]; - short power = (i < powers.length) ? (short) powers[i] : 0; - ByteUtil.setShort(params[paramIndex++], 0, power); - } - fieldType = KeyPair.ALG_EC_F2M; - } else { - throw new IllegalArgumentException("ECParameterSpec with an unknown field."); - } - - ECPoint generator = spec.getGenerator(); - - params[paramIndex++] = curve.getA().toByteArray(); - params[paramIndex++] = curve.getB().toByteArray(); - - params[paramIndex++] = generator.getAffineX().toByteArray(); - params[paramIndex++] = generator.getAffineY().toByteArray(); - - params[paramIndex++] = spec.getOrder().toByteArray(); - params[paramIndex] = new byte[2]; - ByteUtil.setShort(params[paramIndex], 0, (short) spec.getCofactor()); - - EC_Curve result = new EC_Curve(bits, fieldType); - result.readByteArray(params); - return result; - } -} diff --git a/src/cz/crcs/ectester/common/ec/EC_Data.java b/src/cz/crcs/ectester/common/ec/EC_Data.java deleted file mode 100644 index 14ae1c5..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_Data.java +++ /dev/null @@ -1,265 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.common.util.ByteUtil; - -import java.io.*; -import java.util.*; -import java.util.regex.Pattern; - -/** - * A list of byte arrays for holding EC data. - * <p> - * The data can be read from a byte array via <code>readBytes()</code>, from a CSV via <code>readCSV()</code>. - * The data can be exported to a byte array via <code>flatten()</code> or to a string array via <code>expand()</code>. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public abstract class EC_Data implements Comparable<EC_Data> { - String id; - int count; - byte[][] data; - - private static final Pattern HEX = Pattern.compile("(0x|0X)?[a-fA-F\\d]+"); - - EC_Data() { - } - - EC_Data(int count) { - this.count = count; - this.data = new byte[count][]; - } - - EC_Data(byte[][] data) { - this.count = data.length; - this.data = data; - } - - EC_Data(String id, int count) { - this(count); - this.id = id; - } - - EC_Data(String id, byte[][] data) { - this(data); - this.id = id; - } - - public String getId() { - return id; - } - - public int getCount() { - return count; - } - - public byte[][] getData() { - return data; - } - - public byte[] getData(int index) { - return data[index]; - } - - public boolean hasData() { - return data != null; - } - - public byte[] flatten() { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - for (byte[] param : data) { - byte[] length = new byte[2]; - ByteUtil.setShort(length, 0, (short) param.length); - - out.write(length, 0, 2); - out.write(param, 0, param.length); - } - - return out.toByteArray(); - } - - public String[] expand() { - List<String> out = new ArrayList<>(count); - for (byte[] param : data) { - out.add(ByteUtil.bytesToHex(param, false)); - } - - return out.toArray(new String[out.size()]); - } - - private static byte[] pad(byte[] data) { - if (data.length == 1) { - return new byte[]{(byte) 0, data[0]}; - } else if (data.length == 0 || data.length > 2) { - return data; - } - return null; - } - - protected static byte[] parse(String param) { - byte[] data; - if (param.startsWith("0x") || param.startsWith("0X")) { - data = ByteUtil.hexToBytes(param.substring(2)); - } else { - data = ByteUtil.hexToBytes(param); - } - if (data == null) - return new byte[0]; - if (data.length < 2) - return pad(data); - return data; - } - - private boolean readHex(String[] hex) { - if (hex.length != count) { - return false; - } - - for (int i = 0; i < count; ++i) { - this.data[i] = parse(hex[i]); - } - return true; - } - - public boolean readCSV(InputStream in) { - Scanner s = new Scanner(in); - - s.useDelimiter("[,;]"); - List<String> data = new LinkedList<>(); - while (s.hasNext()) { - String field = s.next(); - data.add(field.replaceAll("\\s+", "")); - } - - if (data.isEmpty()) { - return false; - } - for (String param : data) { - if (!HEX.matcher(param).matches()) { - return false; - } - } - return readHex(data.toArray(new String[data.size()])); - } - - public boolean readBytes(byte[] bytes) { - if (bytes == null) { - return false; - } - - int offset = 0; - for (int i = 0; i < count; i++) { - if (bytes.length - offset < 2) { - return false; - } - short paramLength = ByteUtil.getShort(bytes, offset); - offset += 2; - if (bytes.length < offset + paramLength) { - return false; - } - data[i] = new byte[paramLength]; - System.arraycopy(bytes, offset, data[i], 0, paramLength); - offset += paramLength; - } - return true; - } - - public boolean readByteArray(byte[][] bytes) { - if (bytes == null || count != bytes.length) { - return false; - } - - for (int i = 0; i < count; ++i) { - data[i] = new byte[bytes[i].length]; - System.arraycopy(bytes[i], 0, data[i], 0, bytes[i].length); - } - return true; - } - - public void writeCSV(OutputStream out) throws IOException { - Writer w = new OutputStreamWriter(out); - w.write(String.join(",", expand())); - w.flush(); - } - - @Override - public String toString() { - return String.join(",", expand()); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof EC_Data) { - EC_Data other = (EC_Data) obj; - if (this.id != null || other.id != null) { - return Objects.equals(this.id, other.id); - } - - if (this.count != other.count) - return false; - for (int i = 0; i < this.count; ++i) { - if (!Arrays.equals(this.data[i], other.data[i])) { - return false; - } - } - return true; - } else { - return false; - } - } - - @Override - public int hashCode() { - if (this.id != null) { - return this.id.hashCode(); - } - return Arrays.deepHashCode(this.data); - } - - @Override - public int compareTo(EC_Data o) { - if (o == this) return 0; - if (this.id != null && o.id != null) { - - int minLength = Math.min(this.id.length(), o.id.length()); - for (int i = 0; i < minLength; i++) { - if (this.id.charAt(i) != o.id.charAt(i)) { - String thisEnd = this.id.substring(i); - String oEnd = o.id.substring(i); - try { - int thisIndex = Integer.parseInt(thisEnd); - int oIndex = Integer.parseInt(oEnd); - return Integer.compare(thisIndex, oIndex); - } catch (NumberFormatException ignored) { - break; - } - } - } - return this.id.compareTo(o.id); - } else if (this.id == null && o.id == null) { - if (Arrays.equals(this.data, o.data)) { - return 0; - } else { - int minCount = (this.count < o.count) ? this.count : o.count; - for (int i = 0; i < minCount; ++i) { - byte[] thisData = this.data[i]; - byte[] oData = o.data[i]; - int innerMinCount = (thisData.length < oData.length) ? thisData.length : oData.length; - for (int j = 0; j < innerMinCount; ++j) { - if (thisData[j] < oData[j]) { - return -1; - } else if (thisData[j] > oData[j]) { - return 1; - } - } - } - } - } else { - if (this.id == null) { - return -1; - } else { - return 1; - } - } - return 0; - } -} diff --git a/src/cz/crcs/ectester/common/ec/EC_KAResult.java b/src/cz/crcs/ectester/common/ec/EC_KAResult.java deleted file mode 100644 index 4e97950..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_KAResult.java +++ /dev/null @@ -1,65 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.common.util.CardUtil; - -/** - * A result of EC based Key agreement operation. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public class EC_KAResult extends EC_Data { - private String ka; - private String curve; - private String oneKey; - private String otherKey; - - private String desc; - - public EC_KAResult(String ka, String curve, String oneKey, String otherKey) { - super(1); - this.ka = ka; - this.curve = curve; - this.oneKey = oneKey; - this.otherKey = otherKey; - } - - public EC_KAResult(String id, String ka, String curve, String oneKey, String otherKey) { - this(ka, curve, oneKey, otherKey); - this.id = id; - } - - public EC_KAResult(String id, String ka, String curve, String oneKey, String otherKey, String desc) { - this(id, ka, curve, oneKey, otherKey); - this.desc = desc; - } - - public String getKA() { - return ka; - } - - public byte getJavaCardKA() { - return CardUtil.getKA(ka); - } - - public String getCurve() { - return curve; - } - - public String getOneKey() { - return oneKey; - } - - public String getOtherKey() { - return otherKey; - } - - public String getDesc() { - return desc; - } - - @Override - public String toString() { - return "<" + getId() + "> " + ka + " result over " + curve + ", " + oneKey + " + " + otherKey + (desc == null ? "" : ": " + desc) + System.lineSeparator() + super.toString(); - } - -} diff --git a/src/cz/crcs/ectester/common/ec/EC_Key.java b/src/cz/crcs/ectester/common/ec/EC_Key.java deleted file mode 100644 index 754775d..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_Key.java +++ /dev/null @@ -1,83 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.applet.EC_Consts; - -/** - * An abstract-like EC key. Concrete implementations create a public and private keys. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public class EC_Key extends EC_Params { - - private String curve; - private String desc; - - private EC_Key(short mask, String curve) { - super(mask); - this.curve = curve; - } - - private EC_Key(short mask, String curve, String desc) { - this(mask, curve); - this.desc = desc; - } - - private EC_Key(String id, short mask, String curve, String desc) { - this(mask, curve, desc); - this.id = id; - } - - public String getCurve() { - return curve; - } - - public String getDesc() { - return desc; - } - - /** - * An EC public key, contains the W parameter. - */ - public static class Public extends EC_Key { - - public Public(String curve) { - super(EC_Consts.PARAMETER_W, curve); - } - - public Public(String curve, String desc) { - super(EC_Consts.PARAMETER_W, curve, desc); - } - - public Public(String id, String curve, String desc) { - super(id, EC_Consts.PARAMETER_W, curve, desc); - } - - @Override - public String toString() { - return "<" + getId() + "> EC Public key, over " + getCurve() + (getDesc() == null ? "" : ": " + getDesc()) + System.lineSeparator() + super.toString(); - } - } - - /** - * An EC private key, contains the S parameter. - */ - public static class Private extends EC_Key { - - public Private(String curve) { - super(EC_Consts.PARAMETER_S, curve); - } - - public Private(String curve, String desc) { - super(EC_Consts.PARAMETER_S, curve, desc); - } - - public Private(String id, String curve, String desc) { - super(id, EC_Consts.PARAMETER_S, curve, desc); - } - - @Override - public String toString() { - return "<" + getId() + "> EC Private key, over " + getCurve() + (getDesc() == null ? "" : ": " + getDesc()) + System.lineSeparator() + super.toString(); - } - } -} diff --git a/src/cz/crcs/ectester/common/ec/EC_Keypair.java b/src/cz/crcs/ectester/common/ec/EC_Keypair.java deleted file mode 100644 index 24ddba7..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_Keypair.java +++ /dev/null @@ -1,41 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.applet.EC_Consts; - -/** - * An EC keypair, contains both the W and S parameters. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public class EC_Keypair extends EC_Params { - private String curve; - private String desc; - - public EC_Keypair(String curve) { - super(EC_Consts.PARAMETERS_KEYPAIR); - this.curve = curve; - } - - public EC_Keypair(String curve, String desc) { - this(curve); - this.desc = desc; - } - - public EC_Keypair(String id, String curve, String desc) { - this(curve, desc); - this.id = id; - } - - public String getCurve() { - return curve; - } - - public String getDesc() { - return desc; - } - - @Override - public String toString() { - return "<" + getId() + "> EC Keypair, over " + curve + (desc == null ? "" : ": " + desc) + System.lineSeparator() + super.toString(); - } -} diff --git a/src/cz/crcs/ectester/common/ec/EC_Params.java b/src/cz/crcs/ectester/common/ec/EC_Params.java deleted file mode 100644 index e922feb..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_Params.java +++ /dev/null @@ -1,234 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.applet.EC_Consts; -import cz.crcs.ectester.common.util.ByteUtil; - -import java.io.ByteArrayOutputStream; -import java.util.ArrayList; -import java.util.List; - -/** - * A list of EC parameters, can contain a subset of the Fp/F2M, A, B, G, R, K, W, S parameters. - * <p> - * The set of parameters is uniquely identified by a short bit string. - * The parameters can be exported to a byte array via <code>flatten()</code> or to a comma delimited - * string via <code>expand()</code>. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public class EC_Params extends EC_Data { - private short params; - - public EC_Params(short params) { - this.params = params; - this.count = numParams(); - this.data = new byte[this.count][]; - } - - public EC_Params(short params, byte[][] data) { - this.params = params; - this.count = data.length; - this.data = data; - } - - public EC_Params(String id, short params) { - this(params); - this.id = id; - } - - public EC_Params(String id, short params, byte[][] data) { - this(params, data); - this.id = id; - } - - public short getParams() { - return params; - } - - public byte[][] getParam(short param) { - if (!hasParam(param)) { - return null; - } - if (Integer.bitCount(param) != 1) { - return null; - } - short paramMask = EC_Consts.PARAMETER_FP; - byte[][] result = null; - 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) { - result = new byte[4][]; - result[0] = data[i].clone(); - result[1] = data[i + 1].clone(); - result[2] = data[i + 2].clone(); - result[3] = data[i + 3].clone(); - break; - } - if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) { - result = new byte[2][]; - result[0] = data[i].clone(); - result[1] = data[i + 1].clone(); - break; - } - result = new byte[1][]; - result[0] = data[i].clone(); - } - 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 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; - } - - public int numParams() { - short paramMask = EC_Consts.PARAMETER_FP; - int num = 0; - while (paramMask <= EC_Consts.PARAMETER_S) { - if ((paramMask & params) != 0) { - if (paramMask == EC_Consts.PARAMETER_F2M) { - num += 3; - } - if (paramMask == EC_Consts.PARAMETER_W || paramMask == EC_Consts.PARAMETER_G) { - num += 1; - } - ++num; - } - paramMask = (short) (paramMask << 1); - } - return num; - } - - @Override - public byte[] flatten() { - return flatten(params); - } - - public byte[] flatten(short params) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - short paramMask = EC_Consts.PARAMETER_FP; - int i = 0; - while (paramMask <= EC_Consts.PARAMETER_S) { - short masked = (short) (this.params & params & paramMask); - short shallow = (short) (this.params & paramMask); - if (masked != 0) { - byte[] param = data[i]; - if (masked == EC_Consts.PARAMETER_F2M) { - //add m, e_1, e_2, e_3 - param = ByteUtil.concatenate(param, data[i + 1]); - if (!ByteUtil.allValue(data[i + 2], (byte) 0)) { - param = ByteUtil.concatenate(param, data[i + 2]); - } - if (!ByteUtil.allValue(data[i + 3], (byte) 0)) { - param = ByteUtil.concatenate(param, data[i + 3]); - } - if (!(param.length == 4 || param.length == 8)) - throw new RuntimeException("PARAMETER_F2M length is not 8.(should be)"); - } - if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) { - //read another param (the y coord) and put into X962 format. - byte[] y = data[i + 1]; - param = ByteUtil.concatenate(new byte[]{4}, param, y); //<- ugly but works! - } - if (param.length == 0) - throw new RuntimeException("Empty parameter read?"); - - //write length - byte[] length = new byte[2]; - ByteUtil.setShort(length, 0, (short) param.length); - out.write(length, 0, 2); - //write data - out.write(param, 0, param.length); - } - 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 (out.size() == 0) ? null : out.toByteArray(); - } - - @Override - public String[] expand() { - List<String> out = new ArrayList<>(); - - short paramMask = EC_Consts.PARAMETER_FP; - int index = 0; - while (paramMask <= EC_Consts.PARAMETER_S) { - short masked = (short) (params & paramMask); - if (masked != 0) { - byte[] param = data[index]; - if (masked == EC_Consts.PARAMETER_F2M) { - for (int i = 0; i < 4; ++i) { - out.add(ByteUtil.bytesToHex(data[index + i], false)); - } - index += 4; - } else if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) { - out.add(ByteUtil.bytesToHex(param, false)); - out.add(ByteUtil.bytesToHex(data[index + 1], false)); - index += 2; - } else { - out.add(ByteUtil.bytesToHex(param, false)); - index++; - } - } - paramMask = (short) (paramMask << 1); - } - return out.toArray(new String[0]); - } -} diff --git a/src/cz/crcs/ectester/common/ec/EC_SigResult.java b/src/cz/crcs/ectester/common/ec/EC_SigResult.java deleted file mode 100644 index d97ced1..0000000 --- a/src/cz/crcs/ectester/common/ec/EC_SigResult.java +++ /dev/null @@ -1,75 +0,0 @@ -package cz.crcs.ectester.common.ec; - -import cz.crcs.ectester.common.util.CardUtil; - -/** - * A result of EC based Signature operation. - * - * @author Jan Jancar johny@neuromancer.sk - */ -public class EC_SigResult extends EC_Data { - private String sig; - private String curve; - private String signKey; - private String verifyKey; - - private String data; - private String desc; - - public EC_SigResult(String sig, String curve, String signKey, String verifyKey, String raw) { - super(1); - this.sig = sig; - this.curve = curve; - this.signKey = signKey; - this.verifyKey = verifyKey; - this.data = raw; - } - - public EC_SigResult(String id, String sig, String curve, String signKey, String verifyKey, String data) { - this(sig, curve, signKey, verifyKey, data); - this.id = id; - } - - public EC_SigResult(String id, String sig, String curve, String signKey, String verifyKey, String data, String desc) { - this(id, sig, curve, signKey, verifyKey, data); - this.desc = desc; - } - - public String getSig() { - return sig; - } - - public byte getJavaCardSig() { - return CardUtil.getSig(sig); - } - - public String getCurve() { - return curve; - } - - public String getSignKey() { - return signKey; - } - - public String getVerifyKey() { - return verifyKey; - } - - public byte[] getSigData() { - if (data == null) { - return null; - } else { - return parse(data); - } - } - - public String getDesc() { - return desc; - } - - @Override - public String toString() { - return "<" + getId() + "> " + sig + " result over " + curve + ", " + signKey + " + " + verifyKey + (data == null ? "" : " of data \"" + data + "\"") + (desc == null ? "" : ": " + desc) + System.lineSeparator() + super.toString(); - } - -} diff --git a/src/cz/crcs/ectester/common/ec/RawECPrivateKey.java b/src/cz/crcs/ectester/common/ec/RawECPrivateKey.java deleted file mode 100644 index 479118f..0000000 --- a/src/cz/crcs/ectester/common/ec/RawECPrivateKey.java +++ /dev/null @@ -1,46 +0,0 @@ -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 - */ -@SuppressWarnings("serial") -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 deleted file mode 100644 index 7888854..0000000 --- a/src/cz/crcs/ectester/common/ec/RawECPublicKey.java +++ /dev/null @@ -1,46 +0,0 @@ -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 - */ -@SuppressWarnings("serial") -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; - } -} |
