aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/Util.java
diff options
context:
space:
mode:
authorJ08nY2017-01-17 02:55:31 +0100
committerJ08nY2017-01-17 03:17:08 +0100
commit4debe5adb4bb486f488878e348ee7bcf386c43f2 (patch)
tree2cacbee1b1fac0c6afb686f5c2ce6f64bc4e1499 /src/cz/crcs/ectester/reader/Util.java
parentbffdcc6925d806d74179a76b2dc57a619e9c1886 (diff)
downloadECTester-4debe5adb4bb486f488878e348ee7bcf386c43f2.tar.gz
ECTester-4debe5adb4bb486f488878e348ee7bcf386c43f2.tar.zst
ECTester-4debe5adb4bb486f488878e348ee7bcf386c43f2.zip
major changes, ECTester rewrite, moved to valid package
reader: ECTester, mostly rewritten SimpleAPDU - communication with applet now done through simpler instructions: allocate, set, generate, ecdh, ecdsa - moved to a valid Java package dir cz.crcs.ectester - SimpleAPDU: renamed to ECTester - CardMngr: seamlessly supports simulation vs real card - DirtyLogger: takes a nullable String and creates file - ECTester: currently only supports key generation, curve testing under way - supports external curve setting, example files in data package - tests can be done through files, to achieve a more modular approach - Util: static utility class - ParamReader: reads curve domain parameters and keys from simple csv-like human-readable files with hex strings applet: ECTesterApplet, rewrite of SimpleECCApplet - more granularity in instructions - moved complexity over to the reader side - ECKeyGenerator: now a class that takes KeyPair as param - ECKeyTester: now a class that takes KeyPair as param - EC_Consts: removed ecsp curves(now done externally), removed unused methods - ECTesterApplet: currently only tested instructions are: allocate, set, generate data: contains several curve and pubkey files in format supported by ParamReader - Prime field curves: p,a,b,gx,gy,r,k - Binary field curves: e1,a,b,gx,gy,r,k or e1,e2,e3,a,b,gx,gy,r,k - Public key: wx,wy - Private key: s - Key: wx,wy,s - all values are hex strings
Diffstat (limited to 'src/cz/crcs/ectester/reader/Util.java')
-rw-r--r--src/cz/crcs/ectester/reader/Util.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/cz/crcs/ectester/reader/Util.java b/src/cz/crcs/ectester/reader/Util.java
new file mode 100644
index 0000000..7af6e9c
--- /dev/null
+++ b/src/cz/crcs/ectester/reader/Util.java
@@ -0,0 +1,82 @@
+package cz.crcs.ectester.reader;
+
+/**
+ * @author Petr Svenda petr@svenda.com
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class Util {
+
+ public static short getShort(byte[] array, int offset) {
+ return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF));
+ }
+
+ public static void setShort(byte[] array, int offset, short value) {
+ array[offset + 1] = (byte) (value & 0xFF);
+ array[offset] = (byte) ((value >> 8) & 0xFF);
+ }
+
+ public static byte[] hexToBytes(String hex) {
+ return hexToBytes(hex, true);
+ }
+
+ public static byte[] hexToBytes(String hex, boolean bigEndian) {
+ StringBuilder sb = new StringBuilder(hex.replace(" ", ""));
+ if (!bigEndian) {
+ sb.reverse();
+ }
+ int len = sb.length();
+ if (len % 2 == 1) {
+ sb.insert(0, "0");
+ ++len;
+ }
+
+ String data = sb.toString();
+ byte[] result = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ result[i / 2] = (byte) ((Character.digit(data.charAt(i), 16) << 4)
+ + (Character.digit(data.charAt(i + 1), 16)));
+ }
+ return result;
+ }
+
+ public static String byteToHex(byte data) {
+ return String.format("%02x", data);
+ }
+
+ public static String bytesToHex(byte[] data) {
+ return bytesToHex(data, 0, data.length, true);
+ }
+
+ public static String bytesToHex(byte[] data, int offset, int len) {
+ return bytesToHex(data, offset, len, true);
+ }
+
+ public static String bytesToHex(byte[] data, int offset, int len, boolean bAddSpace) {
+ StringBuilder buf = new StringBuilder();
+ for (int i = offset; i < (offset + len); i++) {
+ buf.append(byteToHex(data[i]));
+ if (bAddSpace && i != (offset + len - 1)) {
+ buf.append(" ");
+ }
+ }
+ return (buf.toString());
+ }
+
+ public static byte[] concatenate(byte[]... arrays) {
+ int len = 0;
+ for (byte[] array : arrays) {
+ if (array == null)
+ continue;
+ len += array.length;
+ }
+ byte[] out = new byte[len];
+ int offset = 0;
+ for (byte[] array : arrays) {
+ if (array == null)
+ continue;
+ System.arraycopy(array, 0, out, offset, array.length);
+ offset += array.length;
+ }
+ return out;
+ }
+}