diff options
| author | J08nY | 2017-01-31 20:15:24 +0100 |
|---|---|---|
| committer | J08nY | 2017-01-31 20:15:24 +0100 |
| commit | abe9f00e15993d55a71b8b328f430421f2f2f151 (patch) | |
| tree | 237e1be2bf0209ab422c1b6ce02671bcf33f5d2d /src/cz/crcs/ectester/reader | |
| parent | c3e48df92858bad5e74e9cec69c16397b6b12481 (diff) | |
| download | ECTester-abe9f00e15993d55a71b8b328f430421f2f2f151.tar.gz ECTester-abe9f00e15993d55a71b8b328f430421f2f2f151.tar.zst ECTester-abe9f00e15993d55a71b8b328f430421f2f2f151.zip | |
Added export instruction, and action to reader
One can now export the default domain parameters of the card/simulation
with:
`ectester.jar -e -fp -b 192 -o params.txt`
- Renamed ParamReader to ECParams
- Added Command.Export and Response.Export
- Moved ECKeyGenerator.KEY_* to EC_Consts.KEY_*
Diffstat (limited to 'src/cz/crcs/ectester/reader')
| -rw-r--r-- | src/cz/crcs/ectester/reader/Command.java | 66 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/reader/ECParams.java (renamed from src/cz/crcs/ectester/reader/ParamReader.java) | 81 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/reader/ECTester.java | 196 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/reader/Response.java | 217 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/reader/Util.java | 2 |
5 files changed, 339 insertions, 223 deletions
diff --git a/src/cz/crcs/ectester/reader/Command.java b/src/cz/crcs/ectester/reader/Command.java index 31cde4d..c6ce2b5 100644 --- a/src/cz/crcs/ectester/reader/Command.java +++ b/src/cz/crcs/ectester/reader/Command.java @@ -96,7 +96,6 @@ public abstract class Command { */ public static class Set extends Command { private byte keyPair; - private byte export; private byte curve; private short params; private short corrupted; @@ -108,34 +107,31 @@ public abstract class Command { * * @param cardManager * @param keyPair which keyPair to set params on, local/remote (KEYPAIR_* || ...) - * @param export whether to export set params from keyPair * @param curve curve to set (EC_Consts.CURVE_*) * @param params parameters to set (EC_Consts.PARAMETER_* | ...) * @param corrupted parameters to corrupt (EC_Consts.PARAMETER_* | ...) * @param corruption corruption type (EC_Consts.CORRUPTION_*) * @param external external curve data, can be null */ - public Set(CardMngr cardManager, byte keyPair, byte export, byte curve, short params, short corrupted, byte corruption, byte[] external) { + public Set(CardMngr cardManager, byte keyPair, byte curve, short params, short corrupted, byte corruption, byte[] external) { super(cardManager); this.keyPair = keyPair; - this.export = export; this.curve = curve; this.params = params; this.corrupted = corrupted; this.corruption = corruption; this.external = external; - int len = external != null ? 6 + 2 + external.length : 6; + int len = external != null ? 5 + 2 + external.length : 5; byte[] data = new byte[len]; - data[0] = curve; - Util.setShort(data, 1, params); - Util.setShort(data, 3, corrupted); - data[5] = corruption; + Util.setShort(data, 0, params); + Util.setShort(data, 2, corrupted); + data[4] = corruption; if (external != null) { - System.arraycopy(external, 0, data, 6, external.length); + System.arraycopy(external, 0, data, 5, external.length); } - this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_SET, keyPair, export, data); + this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_SET, keyPair, curve, data); } @Override @@ -143,7 +139,7 @@ public abstract class Command { long elapsed = -System.nanoTime(); ResponseAPDU response = cardManager.send(cmd); elapsed += System.nanoTime(); - return new Response.Set(response, elapsed, keyPair, export, curve, params, corrupted); + return new Response.Set(response, elapsed, keyPair, curve, params, corrupted); } } @@ -152,21 +148,18 @@ public abstract class Command { */ public static class Generate extends Command { private byte keyPair; - private byte export; /** * Creates the INS_GENERATE instruction. * * @param cardManager * @param keyPair which keyPair to generate, local/remote (KEYPAIR_* || ...) - * @param export whether to export generated keys from keyPair */ - public Generate(CardMngr cardManager, byte keyPair, byte export) { + public Generate(CardMngr cardManager, byte keyPair) { super(cardManager); this.keyPair = keyPair; - this.export = export; - this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_GENERATE, keyPair, export); + this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_GENERATE, keyPair, 0); } @Override @@ -174,7 +167,44 @@ public abstract class Command { long elapsed = -System.nanoTime(); ResponseAPDU response = cardManager.send(cmd); elapsed += System.nanoTime(); - return new Response.Generate(response, elapsed, keyPair, export); + return new Response.Generate(response, elapsed, keyPair); + } + } + + /** + * + */ + public static class Export extends Command { + private byte keyPair; + private byte key; + private short params; + + /** + * Creates the INS_EXPORT instruction. + * + * @param cardManager + * @param keyPair keyPair to export from (KEYPAIR_* | ...) + * @param key key to export from (EC_Consts.KEY_* | ...) + * @param params params to export (EC_Consts.PARAMETER_* | ...) + */ + public Export(CardMngr cardManager, byte keyPair, byte key, short params) { + super(cardManager); + this.keyPair = keyPair; + this.key = key; + this.params = params; + + byte[] data = new byte[2]; + Util.setShort(data, 0, params); + + this.cmd = new CommandAPDU(ECTesterApplet.CLA_ECTESTERAPPLET, ECTesterApplet.INS_EXPORT, keyPair, key, data); + } + + @Override + public Response.Export send() throws CardException { + long elapsed = -System.nanoTime(); + ResponseAPDU response = cardManager.send(cmd); + elapsed += System.nanoTime(); + return new Response.Export(response, elapsed, keyPair, key, params); } } diff --git a/src/cz/crcs/ectester/reader/ParamReader.java b/src/cz/crcs/ectester/reader/ECParams.java index ca14d2d..c19640e 100644 --- a/src/cz/crcs/ectester/reader/ParamReader.java +++ b/src/cz/crcs/ectester/reader/ECParams.java @@ -3,22 +3,23 @@ package cz.crcs.ectester.reader; import cz.crcs.ectester.applet.EC_Consts; import java.io.*; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Scanner; import java.util.regex.Pattern; /** - * * @author Jan Jancar johny@neuromancer.sk */ -public class ParamReader { +public class ECParams { private static final Pattern hex = Pattern.compile("[a-fA-F\\d]+"); /** * Flattens params read from String[] data into a byte[] with their lengths prepended as short entries. + * * @param params (EC_Consts.PARAMETER_* | ...) - * @param data data read by readString, readFile, readResource + * @param data data read by readString, readFile, readResource * @return byte[] with params flattened, or null */ public static byte[] flatten(short params, String[] data) { @@ -38,16 +39,16 @@ public class ParamReader { param = Util.concatenate(param, parse(data[i + 1]), parse(data[i + 2])); i += 2; if (param.length != 6) - return null; + throw new RuntimeException("PARAMETER_F2M length is not 6.(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 = parse(data[i + 1]); - param = Util.concatenate(new byte[]{4}, param, y);//<- ugly but works! + param = Util.concatenate(new byte[]{4}, param, y); //<- ugly but works! i++; } if (param.length == 0) - return null; + throw new RuntimeException("Empty parameter read?"); //write length byte[] length = new byte[2]; @@ -64,7 +65,60 @@ public class ParamReader { } /** + * @param data + * @param params + * @return + */ + public static String[] expand(byte[][] data, short params) { + 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) { + //split into three shorts + if (param.length != 6) { + throw new RuntimeException("PARAMETER_F2M length is not 6.(should be)"); + } + for (int i = 0; i < 3; ++i) { + out.add(String.format("%04x", Util.getShort(param, i*2))); + } + + } else if (masked == EC_Consts.PARAMETER_G || masked == EC_Consts.PARAMETER_W) { + //split from X962 format into X and Y + //disregard the first 04 and then split into half(uncompress) + int half = (param.length - 1) / 2; + out.add(Util.bytesToHex(param, 1, half, false)); + out.add(Util.bytesToHex(param, half + 1, half, false)); + } else { + //read raw + out.add(Util.bytesToHex(data[index], false)); + } + index++; + } + paramMask = (short) (paramMask << 1); + } + return out.toArray(new String[out.size()]); + } + + /** + * @param filePath + * @param data + * @throws IOException + */ + public static void writeFile(String filePath, String[] data) throws IOException { + FileOutputStream out = new FileOutputStream(filePath); + write(out, data); + out.close(); + } + + /** * Reads hex params from a CSV String data. + * * @param data String containing CSV data(hex) * @return String array containing the CSV entries */ @@ -74,15 +128,17 @@ public class ParamReader { /** * Reads hex params from a CSV Resource (inside jar). + * * @param resourcePath path to the resourse * @return String array containing the CSV entries */ public static String[] readResource(String resourcePath) { - return read(ParamReader.class.getResourceAsStream(resourcePath)); + return read(ECParams.class.getResourceAsStream(resourcePath)); } /** * Reads hex params from a CSV file. + * * @param filePath path to the file * @return String array containing the CSV entries * @throws FileNotFoundException if the file cannot be opened @@ -132,4 +188,15 @@ public class ParamReader { } return null; } + + private static void write(OutputStream out, String[] data) throws IOException { + Writer w = new OutputStreamWriter(out); + for (int i = 0; i < data.length; ++i) { + w.write(data[i]); + if (i < data.length - 1) { + w.write(","); + } + } + w.flush(); + } } diff --git a/src/cz/crcs/ectester/reader/ECTester.java b/src/cz/crcs/ectester/reader/ECTester.java index 4cb63f7..b359e16 100644 --- a/src/cz/crcs/ectester/reader/ECTester.java +++ b/src/cz/crcs/ectester/reader/ECTester.java @@ -62,73 +62,19 @@ public class ECTester { private boolean optFresh = false; private boolean optSimulate = false; + //Action-related options private int optGenerateAmount; private String optECDSASign; private Options opts = new Options(); - private static final String CLI_HEADER = ""; - private static final String CLI_FOOTER = ""; - + private static final String CLI_HEADER = "\nECTester, a javacard Elliptic Curve Cryptograhy support tester/utility.\n\n"; + private static final String CLI_FOOTER = "\nMIT Licensed\nCopyright (c) 2016-2017 Petr Svenda <petr@svenda.com>"; private static final byte[] SELECT_ECTESTERAPPLET = {(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x0a, (byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x31}; - private static final byte[] AID = {(byte) 0x4C, (byte) 0x61, (byte) 0x62, (byte) 0x61, (byte) 0x6B, (byte) 0x41, (byte) 0x70, (byte) 0x70, (byte) 0x6C, (byte) 0x65, (byte) 0x74}; + private static final byte[] AID = {(byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x31}; private static final byte[] INSTALL_DATA = new byte[10]; - /* - private static final byte[] ALLOCATE = { - (byte) 0xB0, - (byte) 0x5a, //INS ALLOCATE - (byte) 0x00, //P1 *byte keyPair - (byte) 0x00, //P2 - (byte) 0x03, //LC - (byte) 0x00, //DATA *short keyLength - (byte) 0x00, - (byte) 0x00 // *byte keyClass - }; - - private static final byte[] SET = { - (byte) 0xB0, - (byte) 0x5B, //INS SET - (byte) 0x00, //P1 *byte keyPair - (byte) 0x00, //P2 *byte export - (byte) 0x06, //LC - (byte) 0x00, //DATA *byte curve - (byte) 0x00, // *short params - (byte) 0x00, // - (byte) 0x00, // *short corruptedParams - (byte) 0x00, // - (byte) 0x00 // *byte corruptionType - // [short paramLength, byte[] param] for all params in params - }; - - private static final byte[] GENERATE = { - (byte) 0xB0, - (byte) 0x5C, //INS GENERATE - (byte) 0x00, //P1 *byte keyPair - (byte) 0x00, //P2 *byte export - (byte) 0x00 //LC - }; - - private static final byte[] ECDH = { - (byte) 0xB0, - (byte) 0x5D, //INS ECDH - (byte) 0x00, //P1 *byte keyPair - (byte) 0x00, //P2 *byte export - (byte) 0x01, //LC - (byte) 0x00 //DATA *byte valid - }; - - private static final byte[] ECDSA = { - (byte) 0xB0, - (byte) 0x5E, //INS ECDSA - (byte) 0x00, //P1 *byte keyPair - (byte) 0x00, //P2 *byte export - (byte) 0x00, //LC - //DATA [*short dataLength, byte[] data] - }; - */ - private void run(String[] args) { try { CommandLine cli = parseArgs(args); @@ -144,6 +90,7 @@ public class ECTester { } cardManager = new CardMngr(optSimulate); + //connect or simulate connection if (optSimulate) { if (!cardManager.prepareLocalSimulatorApplet(AID, INSTALL_DATA, ECTesterApplet.class)) { System.err.println("Failed to establish a simulator."); @@ -160,7 +107,9 @@ public class ECTester { systemOutLogger = new DirtyLogger(optLog, true); //do action - if (cli.hasOption("generate")) { + if (cli.hasOption("export")) { + export(); + } else if (cli.hasOption("generate")) { generate(); } else if (cli.hasOption("test")) { test(); @@ -170,6 +119,7 @@ public class ECTester { ecdsa(); } + //disconnect cardManager.disconnectFromCard(); systemOutLogger.close(); @@ -186,15 +136,13 @@ public class ECTester { } } catch (MissingArgumentException maex) { System.err.println("Option, " + maex.getOption().getOpt() + " requires an argument: " + maex.getOption().getArgName()); - } catch (ParseException | CardException pex) { - System.err.println(pex.getMessage()); } catch (NumberFormatException nfex) { System.err.println("Not a number. " + nfex.getMessage()); nfex.printStackTrace(System.err); } catch (FileNotFoundException fnfe) { System.err.println("File " + fnfe.getMessage() + " not found."); - } catch (IOException e) { - e.printStackTrace(); + } catch (ParseException | IOException | CardException ex) { + System.err.println(ex.getMessage()); } } @@ -209,6 +157,7 @@ public class ECTester { /* * Actions: * -h / --help + * -e / --export * -g / --generate [amount] * -t / --test * -dh / --ecdh @@ -228,6 +177,7 @@ public class ECTester { OptionGroup actions = new OptionGroup(); actions.setRequired(true); actions.addOption(Option.builder("h").longOpt("help").desc("Print help.").build()); + actions.addOption(Option.builder("e").longOpt("export").desc("Export the defaut curve parameters of the card(if any).").build()); actions.addOption(Option.builder("g").longOpt("generate").desc("Generate [amount] of EC keys.").hasArg().argName("amount").optionalArg(true).build()); actions.addOption(Option.builder("t").longOpt("test").desc("Test ECC support.").build()); actions.addOption(Option.builder("dh").longOpt("ecdh").desc("Do ECDH.").build()); @@ -297,7 +247,26 @@ public class ECTester { return false; } - if (cli.hasOption("generate")) { + + if (cli.hasOption("export")) { + if (optPrimeField == optBinaryField) { + System.err.print("Need to specify field with -fp or -f2m. (not both)"); + return false; + } + if (optKey != null || optPublic != null || optPrivate != null) { + System.err.println("Keys should not be specified when generating keys."); + return false; + } + if (optOutput == null) { + System.err.println("You have to specify an output file for curve parameter export."); + return false; + } + if (optAll) { + System.err.println("You have to specify curve bit-size with -b"); + return false; + } + + } else if (cli.hasOption("generate")) { if (optPrimeField == optBinaryField) { System.err.print("Need to specify field with -fp or -f2m. (not both)"); return false; @@ -361,10 +330,34 @@ public class ECTester { */ private void help() { HelpFormatter help = new HelpFormatter(); + help.setOptionComparator(null); help.printHelp("ECTester.jar", CLI_HEADER, opts, CLI_FOOTER, true); } /** + * Exports default card/simulation EC domain parameters to output file. + * + * @throws CardException if APDU transmission fails + * @throws IOException if an IO error occurs when writing to key file. + */ + private void export() throws CardException, IOException { + byte keyClass = optPrimeField ? KeyPair.ALG_EC_FP : KeyPair.ALG_EC_F2M; + //skip cofactor in domain export, since it doesnt need to be initialized for the key to be initialized. + //and generally isn't initialized on cards with default domain params(TODO, check, is it assumed to be ==1?) + short domain = (short) ((optPrimeField ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M) ^ EC_Consts.PARAMETER_K); + + List<Response> sent = Command.sendAll(prepareKeyPair(ECTesterApplet.KEYPAIR_LOCAL, (short) optBits, keyClass)); + sent.add(new Command.Clear(cardManager, ECTesterApplet.KEYPAIR_LOCAL).send()); + sent.add(new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_LOCAL).send()); + Response.Export export = new Command.Export(cardManager, ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.KEY_PUBLIC, domain).send(); + sent.add(export); + + systemOutLogger.println(Response.toString(sent)); + + ECParams.writeFile(optOutput, ECParams.expand(export.getParams(), domain)); + } + + /** * Generates EC keyPairs and outputs them to output file. * * @throws CardException if APDU transmission fails @@ -372,8 +365,9 @@ public class ECTester { */ private void generate() throws CardException, IOException { byte keyClass = optPrimeField ? KeyPair.ALG_EC_FP : KeyPair.ALG_EC_F2M; - List<Response> prepare = Command.sendAll(prepareKeyPair(ECTesterApplet.KEYPAIR_LOCAL, (short) optBits, keyClass)); - prepare.addAll(Command.sendAll(prepareCurve(ECTesterApplet.KEYPAIR_LOCAL, (short) optBits, keyClass))); + + Command.sendAll(prepareKeyPair(ECTesterApplet.KEYPAIR_LOCAL, (short) optBits, keyClass)); + List<Command> curve = prepareCurve(ECTesterApplet.KEYPAIR_LOCAL, (short) optBits, keyClass); FileWriter keysFile = new FileWriter(optOutput); keysFile.write("index;time;pubW;privS\n"); @@ -381,15 +375,17 @@ public class ECTester { int generated = 0; int retry = 0; while (generated < optGenerateAmount || optGenerateAmount == 0) { - if (optFresh) { - Command.sendAll(prepareCurve(ECTesterApplet.KEYPAIR_LOCAL, (short) optBits, keyClass)); + if (optFresh || generated == 0) { + Command.sendAll(curve); } - Command.Generate generate = new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_LOCAL, (byte) (ECTesterApplet.EXPORT_BOTH | ECTesterApplet.KEYPAIR_LOCAL)); + Command.Generate generate = new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_LOCAL); Response.Generate response = generate.send(); long elapsed = response.getDuration(); - if (!response.successful()) { + Response.Export export = new Command.Export(cardManager, ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.KEY_BOTH, EC_Consts.PARAMETERS_KEYPAIR).send(); + + if (!response.successful() || !export.successful()) { if (retry < 10) { retry++; continue; @@ -400,8 +396,8 @@ public class ECTester { } systemOutLogger.println(response.toString()); - String pub = Util.bytesToHex(response.getPublic(ECTesterApplet.KEYPAIR_LOCAL), false); - String priv = Util.bytesToHex(response.getPrivate(ECTesterApplet.KEYPAIR_LOCAL), false); + String pub = Util.bytesToHex(export.getParameter(ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.PARAMETER_W), false); + String priv = Util.bytesToHex(export.getParameter(ECTesterApplet.KEYPAIR_LOCAL, EC_Consts.PARAMETER_S), false); String line = String.format("%d;%d;%s;%s\n", generated, elapsed / 1000000, pub, priv); keysFile.write(line); keysFile.flush(); @@ -456,16 +452,13 @@ public class ECTester { ecdh.addAll(Command.sendAll(prepareCurve(ECTesterApplet.KEYPAIR_BOTH, (short) optBits, keyClass))); if (optPublic != null || optPrivate != null || optKey != null) { - Response local = new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_NONE).send(); - Response remote = prepareKey(ECTesterApplet.KEYPAIR_REMOTE).send(); - ecdh.add(local); - ecdh.add(remote); + ecdh.add(new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_LOCAL).send()); + ecdh.add(prepareKey(ECTesterApplet.KEYPAIR_REMOTE).send()); } else { - Response both = new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_BOTH, ECTesterApplet.EXPORT_NONE).send(); - ecdh.add(both); + ecdh.add(new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_BOTH).send()); } - Response.ECDH perform = new Command.ECDH(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_ECDH, (byte) 0).send(); + Response.ECDH perform = new Command.ECDH(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_TRUE, (byte) 0).send(); ecdh.add(perform); systemOutLogger.println(Response.toString(ecdh)); @@ -495,7 +488,7 @@ public class ECTester { if (optKey != null || (optPublic != null && optPrivate != null)) { keys = prepareKey(ECTesterApplet.KEYPAIR_LOCAL).send(); } else { - keys = new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_NONE).send(); + keys = new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_LOCAL).send(); } ecdsa.add(keys); @@ -510,7 +503,7 @@ public class ECTester { data = Files.readAllBytes(in.toPath()); } - Response.ECDSA perform = new Command.ECDSA(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_SIG, data).send(); + Response.ECDSA perform = new Command.ECDSA(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_TRUE, data).send(); ecdsa.add(perform); systemOutLogger.println(Response.toString(ecdsa)); @@ -550,14 +543,14 @@ public class ECTester { short domainParams = keyClass == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M; if (optNamed) { // Set named curve (one of the SECG curves embedded applet-side) - commands.add(new Command.Set(cardManager, keyPair, ECTesterApplet.EXPORT_NONE, EC_Consts.getCurve(keyLength, keyClass), domainParams, EC_Consts.PARAMETERS_NONE, EC_Consts.CORRUPTION_NONE, null)); + commands.add(new Command.Set(cardManager, keyPair, EC_Consts.getCurve(keyLength, keyClass), domainParams, EC_Consts.PARAMETERS_NONE, EC_Consts.CORRUPTION_NONE, null)); } else if (optCurve != null) { // Set curve loaded from a file - byte[] external = ParamReader.flatten(domainParams, ParamReader.readFile(optCurve)); + byte[] external = ECParams.flatten(domainParams, ECParams.readFile(optCurve)); if (external == null) { throw new IOException("Couldn't read the curve file correctly."); } - commands.add(new Command.Set(cardManager, keyPair, ECTesterApplet.EXPORT_NONE, EC_Consts.CURVE_external, domainParams, EC_Consts.PARAMETERS_NONE, EC_Consts.CORRUPTION_NONE, external)); + commands.add(new Command.Set(cardManager, keyPair, EC_Consts.CURVE_external, domainParams, EC_Consts.PARAMETERS_NONE, EC_Consts.CORRUPTION_NONE, external)); } else { // Set default curve commands.add(new Command.Clear(cardManager, keyPair)); @@ -576,26 +569,29 @@ public class ECTester { byte[] data = null; if (optKey != null) { params |= EC_Consts.PARAMETERS_KEYPAIR; - data = ParamReader.flatten(EC_Consts.PARAMETERS_KEYPAIR, ParamReader.readFile(optKey)); + data = ECParams.flatten(EC_Consts.PARAMETERS_KEYPAIR, ECParams.readFile(optKey)); + if (data == null) { + throw new IOException("Couldn't read the key file correctly."); + } } if (optPublic != null) { params |= EC_Consts.PARAMETER_W; - data = ParamReader.flatten(EC_Consts.PARAMETER_W, ParamReader.readFile(optPublic)); + byte[] pubkey = ECParams.flatten(EC_Consts.PARAMETER_W, ECParams.readFile(optPublic)); + if (pubkey == null) { + throw new IOException("Couldn't read the key file correctly."); + } + data = pubkey; } if (optPrivate != null) { params |= EC_Consts.PARAMETER_S; - data = Util.concatenate(data, ParamReader.flatten(EC_Consts.PARAMETER_S, ParamReader.readFile(optPrivate))); - } - - if (data == null && params != EC_Consts.PARAMETERS_NONE) { - /* - TODO: this is not correct, in case (optPublic != null) and (optPrivate != null), - only one can actually load(return not null from ParamReader.flatten) and an exception will not be thrown - */ - throw new IOException("Couldn't read the key file correctly."); + byte[] privkey = ECParams.flatten(EC_Consts.PARAMETER_S, ECParams.readFile(optPrivate)); + if (privkey == null) { + throw new IOException("Couldn't read the key file correctly."); + } + data = Util.concatenate(data, privkey); } - return new Command.Set(cardManager, keyPair, ECTesterApplet.EXPORT_NONE, EC_Consts.CURVE_external, params, EC_Consts.PARAMETERS_NONE, EC_Consts.CORRUPTION_NONE, data); + return new Command.Set(cardManager, keyPair, EC_Consts.CURVE_external, params, EC_Consts.PARAMETERS_NONE, EC_Consts.CORRUPTION_NONE, data); } /** @@ -608,10 +604,10 @@ public class ECTester { List<Command> commands = new LinkedList<>(); commands.addAll(prepareKeyPair(ECTesterApplet.KEYPAIR_BOTH, keyLength, keyClass)); commands.addAll(prepareCurve(ECTesterApplet.KEYPAIR_BOTH, keyLength, keyClass)); - commands.add(new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_BOTH, ECTesterApplet.EXPORT_NONE)); - commands.add(new Command.ECDH(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_NONE, (byte) 0)); - commands.add(new Command.ECDH(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_NONE, (byte) 1)); - commands.add(new Command.ECDSA(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_NONE, null)); + commands.add(new Command.Generate(cardManager, ECTesterApplet.KEYPAIR_BOTH)); + commands.add(new Command.ECDH(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, (byte) 0)); + commands.add(new Command.ECDH(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, (byte) 1)); + commands.add(new Command.ECDSA(cardManager, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_FALSE, null)); return commands; } diff --git a/src/cz/crcs/ectester/reader/Response.java b/src/cz/crcs/ectester/reader/Response.java index 05cd92a..368a8ef 100644 --- a/src/cz/crcs/ectester/reader/Response.java +++ b/src/cz/crcs/ectester/reader/Response.java @@ -102,6 +102,10 @@ public abstract class Response { return params[index]; } + public byte[][] getParams() { + return params; + } + public int getLength() { return resp.getNr(); } @@ -192,15 +196,13 @@ public abstract class Response { */ public static class Set extends Response { private byte keyPair; - private byte export; private byte curve; private short parameters; private short corrupted; - protected Set(ResponseAPDU response, long time, byte keyPair, byte export, byte curve, short parameters, short corrupted) { + protected Set(ResponseAPDU response, long time, byte keyPair, byte curve, short parameters, short corrupted) { super(response, time); this.keyPair = keyPair; - this.export = export; this.curve = curve; this.parameters = parameters; this.corrupted = corrupted; @@ -208,65 +210,8 @@ public abstract class Response { int pairs = 0; if ((keyPair & ECTesterApplet.KEYPAIR_LOCAL) != 0) pairs++; if ((keyPair & ECTesterApplet.KEYPAIR_REMOTE) != 0) pairs++; - int exported = 0; - if ((export & ECTesterApplet.KEYPAIR_LOCAL) != 0) exported++; - if ((export & ECTesterApplet.KEYPAIR_REMOTE) != 0) exported++; - int keys = 0; - if ((export & ECTesterApplet.EXPORT_PUBLIC) != 0) keys++; - if ((export & ECTesterApplet.EXPORT_PRIVATE) != 0) keys++; - int paramCount = 0; - short mask = EC_Consts.PARAMETER_FP; - while (mask <= EC_Consts.PARAMETER_K) { - if ((mask & parameters) != 0) { - paramCount++; - } - mask = (short) (mask << 1); - } - int other = 0; - if ((export & ECTesterApplet.EXPORT_PUBLIC) != 0 && (parameters & EC_Consts.PARAMETER_W) != 0) other++; - if ((export & ECTesterApplet.EXPORT_PRIVATE) != 0 && (parameters & EC_Consts.PARAMETER_S) != 0) other++; - - parse(pairs, exported * keys * paramCount + exported * other); - } - - private int getIndex(byte keyPair, short param) { - byte key = ECTesterApplet.KEYPAIR_LOCAL; - int index = 0; - while (key <= ECTesterApplet.KEYPAIR_REMOTE) { - short mask = EC_Consts.PARAMETER_FP; - while (mask <= EC_Consts.PARAMETER_S) { - if (key == keyPair && param == mask) { - return index; - } - if ((parameters & mask) != 0 && (key & export) != 0) { - if (mask == EC_Consts.PARAMETER_W) { - if ((export & ECTesterApplet.EXPORT_PUBLIC) != 0) - index++; - } else if (mask == EC_Consts.PARAMETER_S) { - if ((export & ECTesterApplet.EXPORT_PRIVATE) != 0) - index++; - } else { - index++; - } - } - mask = (short) (mask << 1); - } - - key = (byte) (key << 1); - } - return -1; - } - public boolean hasParameter(byte keyPair, short param) { - if ((export & keyPair) == 0 || (parameters & param) == 0) { - return false; - } - int index = getIndex(keyPair, param); - return index != -1 && hasParam(index); - } - - public byte[] getParameter(byte keyPair, short param) { - return getParam(getIndex(keyPair, param)); + parse(pairs, 0); } @Override @@ -299,49 +244,18 @@ public abstract class Response { */ public static class Generate extends Response { private byte keyPair; - private byte export; - private short[] contents; - protected Generate(ResponseAPDU response, long time, byte keyPair, byte export) { + protected Generate(ResponseAPDU response, long time, byte keyPair) { super(response, time); this.keyPair = keyPair; - this.export = export; - int keys = 0; - if ((export & ECTesterApplet.EXPORT_PUBLIC) != 0) keys++; - if ((export & ECTesterApplet.EXPORT_PRIVATE) != 0) keys++; - int pairs = 0; - if ((export & ECTesterApplet.KEYPAIR_LOCAL) != 0) pairs++; - if ((export & ECTesterApplet.KEYPAIR_REMOTE) != 0) pairs++; int generated = 0; if ((keyPair & ECTesterApplet.KEYPAIR_LOCAL) != 0) generated++; if ((keyPair & ECTesterApplet.KEYPAIR_REMOTE) != 0) generated++; - parse(generated, keys * pairs); - - this.contents = new short[4]; - int offset = 0; - if ((export & ECTesterApplet.KEYPAIR_LOCAL) != 0) { - if ((export & ECTesterApplet.EXPORT_PUBLIC) != 0) { - this.contents[offset] = ECTesterApplet.KEYPAIR_LOCAL | ECTesterApplet.EXPORT_PUBLIC; - offset++; - } - if ((export & ECTesterApplet.EXPORT_PRIVATE) != 0) { - this.contents[offset] = ECTesterApplet.KEYPAIR_LOCAL | ECTesterApplet.EXPORT_PRIVATE; - offset++; - } - } - if ((export & ECTesterApplet.KEYPAIR_REMOTE) != 0) { - if ((export & ECTesterApplet.EXPORT_PUBLIC) != 0) { - this.contents[offset] = ECTesterApplet.KEYPAIR_REMOTE | ECTesterApplet.EXPORT_PUBLIC; - offset++; - } - if ((export & ECTesterApplet.EXPORT_PRIVATE) != 0) { - this.contents[offset] = ECTesterApplet.KEYPAIR_REMOTE | ECTesterApplet.EXPORT_PRIVATE; - offset++; - } - } + parse(generated, 0); } + /* private int getIndex(byte key) { for (int i = 0; i < contents.length; i++) { if (key == contents[i]) @@ -375,6 +289,7 @@ public abstract class Response { int index = getIndex((byte) (keyPair | ECTesterApplet.EXPORT_PRIVATE)); return getParam(index); } + */ @Override public String toString() { @@ -392,6 +307,114 @@ public abstract class Response { /** * */ + public static class Export extends Response { + private byte keyPair; + private byte key; + private short parameters; + + public Export(ResponseAPDU response, long time, byte keyPair, byte key, short parameters) { + super(response, time); + this.keyPair = keyPair; + this.key = key; + this.parameters = parameters; + + int exported = 0; + if ((keyPair & ECTesterApplet.KEYPAIR_LOCAL) != 0) exported++; + if ((keyPair & ECTesterApplet.KEYPAIR_REMOTE) != 0) exported++; + int keys = 0; + if ((key & EC_Consts.KEY_PUBLIC) != 0) keys++; + if ((key & EC_Consts.KEY_PRIVATE) != 0) keys++; + int paramCount = 0; + short mask = EC_Consts.PARAMETER_FP; + while (mask <= EC_Consts.PARAMETER_K) { + if ((mask & parameters) != 0) { + paramCount++; + } + mask = (short) (mask << 1); + } + int other = 0; + if ((key & EC_Consts.KEY_PUBLIC) != 0 && (parameters & EC_Consts.PARAMETER_W) != 0) other++; + if ((key & EC_Consts.KEY_PRIVATE) != 0 && (parameters & EC_Consts.PARAMETER_S) != 0) other++; + + parse(exported, exported * keys * paramCount + exported * other); + } + + private int getIndex(byte keyPair, short param) { + byte pair = ECTesterApplet.KEYPAIR_LOCAL; + int index = 0; + while (pair <= ECTesterApplet.KEYPAIR_REMOTE) { + short mask = EC_Consts.PARAMETER_FP; + while (mask <= EC_Consts.PARAMETER_S) { + if (pair == keyPair && param == mask) { + return index; + } + if ((parameters & mask) != 0 && (pair & keyPair) != 0) { + if (mask == EC_Consts.PARAMETER_W) { + if ((key & EC_Consts.KEY_PUBLIC) != 0) + index++; + } else if (mask == EC_Consts.PARAMETER_S) { + if ((key & EC_Consts.KEY_PRIVATE) != 0) + index++; + } else { + index++; + } + } + mask = (short) (mask << 1); + } + + pair = (byte) (pair << 1); + } + return -1; + } + + public boolean hasParameters(byte keyPair, short params) { + if ((keyPair & this.keyPair) == 0 || (params ^ parameters) != 0) { + return false; + } + short param = EC_Consts.PARAMETER_FP; + while (param <= EC_Consts.PARAMETER_S) { + short masked = (short) (param & params); + if (masked != 0 && !hasParameter(keyPair, masked)) { + return false; + } + param = (short) (param << 1); + } + return true; + } + + public boolean hasParameter(byte keyPair, short param) { + if ((keyPair & this.keyPair) == 0 || (parameters & param) == 0) { + return false; + } + int index = getIndex(keyPair, param); + return index != -1 && hasParam(index); + } + + public byte[] getParameter(byte keyPair, short param) { + return getParam(getIndex(keyPair, param)); + } + + @Override + public String toString() { + String source; + if (key == EC_Consts.KEY_BOTH) { + source = "both keys"; + } else { + source = ((key == EC_Consts.KEY_PUBLIC) ? "public" : "private") + " key"; + } + String pair; + if (keyPair == ECTesterApplet.KEYPAIR_BOTH) { + pair = "both keypairs"; + } else { + pair = ((keyPair == ECTesterApplet.KEYPAIR_LOCAL) ? "local" : "remote") + " keypair"; + } + return String.format("Exported params from %s of %s", source, pair); + } + } + + /** + * + */ public static class ECDH extends Response { private byte pubkey; private byte privkey; @@ -405,7 +428,7 @@ public abstract class Response { this.export = export; this.invalid = invalid; - parse(1, (export & ECTesterApplet.EXPORT_ECDH) != 0 ? 1 : 0); + parse(1, (export == ECTesterApplet.EXPORT_TRUE) ? 1 : 0); } public boolean hasSecret() { @@ -439,7 +462,7 @@ public abstract class Response { this.export = export; this.raw = raw; - parse(1, (export & ECTesterApplet.EXPORT_SIG) != 0 ? 1 : 0); + parse(1, (export == ECTesterApplet.EXPORT_TRUE) ? 1 : 0); } public boolean hasSignature() { diff --git a/src/cz/crcs/ectester/reader/Util.java b/src/cz/crcs/ectester/reader/Util.java index 38db3bf..f876fe4 100644 --- a/src/cz/crcs/ectester/reader/Util.java +++ b/src/cz/crcs/ectester/reader/Util.java @@ -115,7 +115,7 @@ public class Util { codeStr = "SIG_VERIFY_FAIL"; break; } - return String.format("fail\t(%s,\t0x%4x)", codeStr, code); + return String.format("fail\t(%s,\t0x%04x)", codeStr, code); } } } |
