aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader
diff options
context:
space:
mode:
Diffstat (limited to 'src/cz/crcs/ectester/reader')
-rw-r--r--src/cz/crcs/ectester/reader/command/Command.java63
-rw-r--r--src/cz/crcs/ectester/reader/response/Response.java8
-rw-r--r--src/cz/crcs/ectester/reader/test/CardTestVectorSuite.java15
3 files changed, 34 insertions, 52 deletions
diff --git a/src/cz/crcs/ectester/reader/command/Command.java b/src/cz/crcs/ectester/reader/command/Command.java
index b5b9393..1789451 100644
--- a/src/cz/crcs/ectester/reader/command/Command.java
+++ b/src/cz/crcs/ectester/reader/command/Command.java
@@ -3,11 +3,10 @@ package cz.crcs.ectester.reader.command;
import cz.crcs.ectester.applet.ECTesterApplet;
import cz.crcs.ectester.applet.EC_Consts;
import cz.crcs.ectester.common.ec.EC_Curve;
-import cz.crcs.ectester.common.ec.EC_Key;
-import cz.crcs.ectester.common.ec.EC_Keypair;
import cz.crcs.ectester.common.ec.EC_Params;
import cz.crcs.ectester.common.util.ByteUtil;
import cz.crcs.ectester.common.util.CardUtil;
+import cz.crcs.ectester.common.util.ECUtil;
import cz.crcs.ectester.data.EC_Store;
import cz.crcs.ectester.reader.CardMngr;
import cz.crcs.ectester.reader.ECTesterReader;
@@ -60,12 +59,12 @@ public abstract class Command implements Cloneable {
return (Command) super.clone();
}
- public static EC_Curve findCurve(EC_Store dataStore, ECTesterReader.Config cfg, short keyLength, byte keyClass) throws IOException {
+ public static EC_Curve findCurve(ECTesterReader.Config cfg, short keyLength, byte keyClass) throws IOException {
if (cfg.customCurve) {
byte curveId = EC_Consts.getCurve(keyLength, keyClass);
- return dataStore.getObject(EC_Curve.class, "secg", CardUtil.getCurveName(curveId));
+ return EC_Store.getInstance().getObject(EC_Curve.class, "secg", CardUtil.getCurveName(curveId));
} else if (cfg.namedCurve != null) {
- EC_Curve curve = dataStore.getObject(EC_Curve.class, cfg.namedCurve);
+ EC_Curve curve = EC_Store.getInstance().getObject(EC_Curve.class, cfg.namedCurve);
if (curve == null) {
throw new IOException("Curve could no be found.");
}
@@ -96,14 +95,14 @@ public abstract class Command implements Cloneable {
* @return a Command to send in order to prepare the curve on the keypairs.
* @throws IOException if curve file cannot be found/opened
*/
- public static Command prepareCurve(CardMngr cardManager, EC_Store dataStore, ECTesterReader.Config cfg, byte keyPair, short keyLength, byte keyClass) throws IOException {
+ public static Command prepareCurve(CardMngr cardManager, ECTesterReader.Config cfg, byte keyPair, short keyLength, byte keyClass) throws IOException {
if (cfg.customCurve) {
// Set custom curve (one of the SECG curves embedded applet-side)
short domainParams = keyClass == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M;
return new Command.Set(cardManager, keyPair, EC_Consts.getCurve(keyLength, keyClass), domainParams, null);
}
- EC_Curve curve = findCurve(dataStore, cfg, keyLength, keyClass);
+ EC_Curve curve = findCurve(cfg, keyLength, keyClass);
if ((curve == null || curve.flatten() == null) && (cfg.namedCurve != null || cfg.curveFile != null)) {
if (cfg.namedCurve != null) {
throw new IOException("Couldn't read named curve data.");
@@ -117,7 +116,11 @@ public abstract class Command implements Cloneable {
/**
- * @param keyPair which keyPair/s to set the key params on
+ * @param cardManager
+ * @param dataStore
+ * @param cfg
+ * @param keyPair which keyPair/s to set the key params on
+ * @param allowedParams
* @return a CommandAPDU setting params loaded on the keyPair/s
* @throws IOException if any of the key files cannot be found/opened
*/
@@ -127,16 +130,7 @@ public abstract class Command implements Cloneable {
if (cfg.key != null || cfg.namedKey != null) {
params |= EC_Consts.PARAMETERS_KEYPAIR;
- EC_Params keypair;
- if (cfg.key != null) {
- keypair = new EC_Params(EC_Consts.PARAMETERS_KEYPAIR);
-
- FileInputStream in = new FileInputStream(cfg.key);
- keypair.readCSV(in);
- in.close();
- } else {
- keypair = dataStore.getObject(EC_Keypair.class, cfg.namedKey);
- }
+ EC_Params keypair = ECUtil.loadParams(EC_Consts.PARAMETERS_KEYPAIR, cfg.namedKey, cfg.key);
if (keypair == null) {
throw new IOException("KeyPair not found.");
}
@@ -147,21 +141,9 @@ public abstract class Command implements Cloneable {
}
}
- if ((cfg.publicKey != null || cfg.namedPublicKey != null) && ((allowedParams & EC_Consts.PARAMETER_W )!= 0)) {
+ if ((cfg.publicKey != null || cfg.namedPublicKey != null) && ((allowedParams & EC_Consts.PARAMETER_W) != 0)) {
params |= EC_Consts.PARAMETER_W;
- EC_Params pub;
- if (cfg.publicKey != null) {
- pub = new EC_Params(EC_Consts.PARAMETER_W);
-
- FileInputStream in = new FileInputStream(cfg.publicKey);
- pub.readCSV(in);
- in.close();
- } else {
- pub = dataStore.getObject(EC_Key.Public.class, cfg.namedPublicKey);
- if (pub == null) {
- pub = dataStore.getObject(EC_Keypair.class, cfg.namedPublicKey);
- }
- }
+ EC_Params pub = ECUtil.loadParams(EC_Consts.PARAMETER_W, cfg.namedPublicKey, cfg.publicKey);
if (pub == null) {
throw new IOException("Public key not found.");
}
@@ -172,21 +154,10 @@ public abstract class Command implements Cloneable {
}
data = pubkey;
}
- if ((cfg.privateKey != null || cfg.namedPrivateKey != null) && ((allowedParams & EC_Consts.PARAMETER_S )!= 0)) {
- params |= EC_Consts.PARAMETER_S;
- EC_Params priv;
- if (cfg.privateKey != null) {
- priv = new EC_Params(EC_Consts.PARAMETER_S);
- FileInputStream in = new FileInputStream(cfg.privateKey);
- priv.readCSV(in);
- in.close();
- } else {
- priv = dataStore.getObject(EC_Key.Private.class, cfg.namedPrivateKey);
- if (priv == null) {
- priv = dataStore.getObject(EC_Keypair.class, cfg.namedPrivateKey);
- }
- }
+ if ((cfg.privateKey != null || cfg.namedPrivateKey != null) && ((allowedParams & EC_Consts.PARAMETER_S) != 0)) {
+ params |= EC_Consts.PARAMETER_S;
+ EC_Params priv = ECUtil.loadParams(EC_Consts.PARAMETER_S, cfg.namedPrivateKey, cfg.privateKey);
if (priv == null) {
throw new IOException("Private key not found.");
}
diff --git a/src/cz/crcs/ectester/reader/response/Response.java b/src/cz/crcs/ectester/reader/response/Response.java
index 6232423..53a757b 100644
--- a/src/cz/crcs/ectester/reader/response/Response.java
+++ b/src/cz/crcs/ectester/reader/response/Response.java
@@ -376,6 +376,14 @@ public abstract class Response {
parse(1, (export == ECTesterApplet.EXPORT_TRUE) ? 1 : 0);
}
+ public short getTransformation() {
+ return transformation;
+ }
+
+ public byte getType() {
+ return type;
+ }
+
public boolean hasSecret() {
return hasParam(0);
}
diff --git a/src/cz/crcs/ectester/reader/test/CardTestVectorSuite.java b/src/cz/crcs/ectester/reader/test/CardTestVectorSuite.java
index 690425d..3c4378a 100644
--- a/src/cz/crcs/ectester/reader/test/CardTestVectorSuite.java
+++ b/src/cz/crcs/ectester/reader/test/CardTestVectorSuite.java
@@ -163,14 +163,16 @@ public class CardTestVectorSuite extends CardTestSuite {
try {
ka.init(privKey);
ka.doPhase(pubKey, true);
- byte[] rawDerived = ka.generateSecret();
+ byte[] derived = ka.generateSecret();
int fieldSize = (curve.getBits() + 7) / 8;
- if (rawDerived.length < fieldSize) {
+ if (derived.length < fieldSize) {
byte[] padded = new byte[fieldSize];
- System.arraycopy(rawDerived, 0, padded, fieldSize - rawDerived.length, rawDerived.length);
- rawDerived = padded;
+ System.arraycopy(derived, 0, padded, fieldSize - derived.length, derived.length);
+ derived = padded;
+ }
+ if (ecdhData.getType() == EC_Consts.KeyAgreement_ALG_EC_SVDP_DH) {
+ derived = md.digest(derived);
}
- byte[] derived = md.digest(rawDerived);
if (secret.length != derived.length) {
if (secret.length < derived.length) {
return new Result(Value.FAILURE, String.format("Derived secret was shorter than expected: %d vs %d (expected).", secret.length, derived.length));
@@ -190,6 +192,7 @@ public class CardTestVectorSuite extends CardTestSuite {
}
};
Test ecdhTest = CommandTest.function(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_TRUE, EC_Consts.TRANSFORMATION_NONE, EC_Consts.KeyAgreement_ALG_EC_SVDP_DH), kaCallback);
+ Test ecdhRawTest = CommandTest.function(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_TRUE, EC_Consts.TRANSFORMATION_NONE, EC_Consts.KeyAgreement_ALG_EC_SVDP_DH_PLAIN), kaCallback);
byte[] data = new byte[32];
TestCallback<CommandTestable> sigCallback = new TestCallback<CommandTestable>() {
@Override
@@ -222,7 +225,7 @@ public class CardTestVectorSuite extends CardTestSuite {
}
};
Test ecdsaTest = CommandTest.function(new Command.ECDSA_sign(this.card, ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.Signature_ALG_ECDSA_SHA, ECTesterApplet.EXPORT_TRUE, data), sigCallback);
- testVector.add(CompoundTest.all(ExpectedValue.SUCCESS, "", ecdhTest, ecdsaTest));
+ testVector.add(CompoundTest.all(ExpectedValue.SUCCESS, "Test.", ecdhTest, ecdhRawTest, ecdsaTest));
if (cfg.cleanup) {
testVector.add(CommandTest.expect(new Command.Cleanup(this.card), ExpectedValue.ANY));
}