aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cz/crcs/ectester/standalone/ECTesterStandalone.java11
-rw-r--r--src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java22
-rw-r--r--src/cz/crcs/ectester/standalone/libs/TomcryptLib.java2
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/Makefile48
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java25
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/TomCryptProvider.java11
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/native.h54
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c40
-rw-r--r--src/cz/crcs/ectester/standalone/libs/native.h21
9 files changed, 197 insertions, 37 deletions
diff --git a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
index 3ec11ed..de9953a 100644
--- a/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
+++ b/src/cz/crcs/ectester/standalone/ECTesterStandalone.java
@@ -8,10 +8,7 @@ import cz.crcs.ectester.data.EC_Store;
import cz.crcs.ectester.standalone.consts.KeyAgreementIdent;
import cz.crcs.ectester.standalone.consts.KeyPairGeneratorIdent;
import cz.crcs.ectester.standalone.consts.SignatureIdent;
-import cz.crcs.ectester.standalone.libs.BouncyCastleLib;
-import cz.crcs.ectester.standalone.libs.ECLibrary;
-import cz.crcs.ectester.standalone.libs.ProviderECLibrary;
-import cz.crcs.ectester.standalone.libs.SunECLib;
+import cz.crcs.ectester.standalone.libs.*;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
@@ -37,7 +34,7 @@ import java.util.stream.Collectors;
* @version v0.1.0
*/
public class ECTesterStandalone {
- private ECLibrary[] libs = new ECLibrary[]{new SunECLib(), new BouncyCastleLib()};
+ private ECLibrary[] libs = new ECLibrary[]{new SunECLib(), new BouncyCastleLib(), new TomcryptLib()};
private EC_Store dataStore;
private Config cfg;
@@ -297,7 +294,6 @@ public class ECTesterStandalone {
} else {
Signature sig = sigIdent.getInstance(lib.getProvider());
KeyPairGenerator kpg = kpIdent.getInstance(lib.getProvider());
- AlgorithmParameterSpec spec = null;
if (cli.hasOption("ecdsa.bits")) {
int bits = Integer.parseInt(cli.getOptionValue("ecdsa.bits"));
kpg.initialize(bits);
@@ -308,8 +304,7 @@ public class ECTesterStandalone {
System.err.println("Curve not found: " + curveName);
return;
}
- spec = curve.toSpec();
- kpg.initialize(spec);
+ kpg.initialize(curve.toSpec());
}
System.out.println("index;data;signtime;verifytime;pubW;privS;signature;verified");
diff --git a/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java b/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
index 44fb47b..5d1b9d7 100644
--- a/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
+++ b/src/cz/crcs/ectester/standalone/libs/NativeECLibrary.java
@@ -15,11 +15,13 @@ import java.security.Provider;
*/
public abstract class NativeECLibrary extends ProviderECLibrary {
private String resource;
- private String libname;
+ private String[] requriements;
- public NativeECLibrary(String resource, String libname) {
+ public static String LIB_RESOURCE_DIR = "/cz/crcs/ectester/standalone/libs/jni/";
+
+ public NativeECLibrary(String resource, String... requirements) {
this.resource = resource;
- this.libname = libname;
+ this.requriements = requirements;
}
@Override
@@ -33,8 +35,10 @@ public abstract class NativeECLibrary extends ProviderECLibrary {
} else {
suffix = "so";
if (System.getProperty("os.name").startsWith("Linux")) {
- appData = Paths.get(System.getenv("XDG_DATA_HOME"));
- if (appData == null) {
+ String dataHome = System.getenv("XDG_DATA_HOME");
+ if (dataHome != null) {
+ appData = Paths.get(dataHome);
+ } else {
appData = Paths.get(System.getProperty("user.home"), ".local", "share");
}
} else {
@@ -43,10 +47,10 @@ public abstract class NativeECLibrary extends ProviderECLibrary {
}
Path libDir = appData.resolve("ECTesterStandalone");
File libDirFile = libDir.toFile();
- Path libPath = libDir.resolve(libname + "." + suffix);
+ Path libPath = libDir.resolve(resource + "." + suffix);
File libFile = libPath.toFile();
- URL jarURL = NativeECLibrary.class.getResource("/cz/crcs/ectester/standalone/libs/" + resource + "." + suffix);
+ URL jarURL = NativeECLibrary.class.getResource(LIB_RESOURCE_DIR + resource + "." + suffix);
if (jarURL == null) {
return false;
}
@@ -71,6 +75,10 @@ public abstract class NativeECLibrary extends ProviderECLibrary {
}
jarConnection.getInputStream().close();
+ for (String requirement : requriements) {
+ System.loadLibrary(requirement);
+ }
+
System.load(libPath.toString());
provider = createProvider();
diff --git a/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java
index fe4a79d..49e810c 100644
--- a/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java
+++ b/src/cz/crcs/ectester/standalone/libs/TomcryptLib.java
@@ -8,7 +8,7 @@ import java.security.Provider;
public class TomcryptLib extends NativeECLibrary {
public TomcryptLib() {
- super("tomcrypt", "libtomcrypt");
+ super("tomcrypt_provider", "tommath", "tomcrypt");
}
@Override
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/Makefile b/src/cz/crcs/ectester/standalone/libs/jni/Makefile
new file mode 100644
index 0000000..837078c
--- /dev/null
+++ b/src/cz/crcs/ectester/standalone/libs/jni/Makefile
@@ -0,0 +1,48 @@
+ifeq ($(JAVA_HOME),)
+ifeq ($(OS),Windows_NT)
+which = $(shell where $1)
+else
+which = $(shell which $1)
+endif
+JAVAC ?= $(realpath $(call which,javac))
+JAVA_HOME = $(abspath $(dir $(JAVAC))..)
+endif
+
+ifneq ($(JAVA_HOME),)
+JNI_INCLUDEDIR ?= $(JAVA_HOME)/include
+endif
+
+ifeq ($(JNI_INCLUDEDIR),)
+$(error could not determine JNI include dir, try specifying either \
+ JAVA_HOME or JNI_INCLUDEDIR)
+endif
+
+TARGETTRIPLET := $(shell $(CC) -dumpmachine)
+ifeq ($(JNI_PLATFORM),)
+ifeq ($(findstring mingw,$(TARGETTRIPLET)),mingw)
+JNI_PLATFORM:= win32
+else
+ifeq ($(findstring linux,$(TARGETTRIPLET)),linux)
+JNI_PLATFORM:= linux
+# add more checks here
+endif
+endif
+endif
+
+JNI_PLATFORMINCLUDEDIR ?= $(JNI_INCLUDEDIR)/$(JNI_PLATFORM)
+
+
+
+all: tomcrypt_provider.so
+
+tomcrypt_provider.so: tomcrypt.o
+ gcc -DLTM_DESC -DUSE_LTM -fPIC -shared -o $@ $< -ltommath -ltomcrypt
+
+%.o: %.c
+ gcc -fPIC -I"$(JNI_INCLUDEDIR)" -I"$(JNI_PLATFORMINCLUDEDIR)" -I. -c $<
+
+clean:
+ rm -rf *.o
+ rm -rf *.so
+
+.PHONY: all clean \ No newline at end of file
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java b/src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java
new file mode 100644
index 0000000..43b7827
--- /dev/null
+++ b/src/cz/crcs/ectester/standalone/libs/jni/NativeProvider.java
@@ -0,0 +1,25 @@
+package cz.crcs.ectester.standalone.libs.jni;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.Provider;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public abstract class NativeProvider extends Provider {
+
+ public NativeProvider(String name, double version, String info) {
+ super(name, version, info);
+
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ setup();
+ return null;
+ }
+ });
+ }
+
+ abstract void setup();
+
+}
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/TomCryptProvider.java b/src/cz/crcs/ectester/standalone/libs/jni/TomCryptProvider.java
new file mode 100644
index 0000000..01e4c17
--- /dev/null
+++ b/src/cz/crcs/ectester/standalone/libs/jni/TomCryptProvider.java
@@ -0,0 +1,11 @@
+package cz.crcs.ectester.standalone.libs.jni;
+
+public class TomCryptProvider extends NativeProvider {
+
+ public TomCryptProvider(String name, double version, String info) {
+ super(name, version, info);
+ }
+
+ @Override
+ native void setup();
+}
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/native.h b/src/cz/crcs/ectester/standalone/libs/jni/native.h
new file mode 100644
index 0000000..83ef841
--- /dev/null
+++ b/src/cz/crcs/ectester/standalone/libs/jni/native.h
@@ -0,0 +1,54 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class cz_crcs_ectester_standalone_libs_TomcryptLib */
+
+#ifndef _Included_cz_crcs_ectester_standalone_libs_TomcryptLib
+#define _Included_cz_crcs_ectester_standalone_libs_TomcryptLib
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: cz_crcs_ectester_standalone_libs_TomcryptLib
+ * Method: createProvider
+ * Signature: ()Ljava/security/Provider;
+ */
+JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_createProvider
+ (JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+/* Header for class cz_crcs_ectester_standalone_libs_jni_TomCryptProvider */
+
+#ifndef _Included_cz_crcs_ectester_standalone_libs_jni_TomCryptProvider
+#define _Included_cz_crcs_ectester_standalone_libs_jni_TomCryptProvider
+#ifdef __cplusplus
+extern "C" {
+#endif
+#undef cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_serialVersionUID
+#define cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_serialVersionUID 1421746759512286392LL
+#undef cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_MAX_ARRAY_SIZE
+#define cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_MAX_ARRAY_SIZE 2147483639L
+#undef cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_KEYS
+#define cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_KEYS 0L
+#undef cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_VALUES
+#define cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_VALUES 1L
+#undef cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_ENTRIES
+#define cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_ENTRIES 2L
+#undef cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_serialVersionUID
+#define cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_serialVersionUID 4112578634029874840LL
+#undef cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_serialVersionUID
+#define cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_serialVersionUID -4298000515446427739LL
+/*
+ * Class: cz_crcs_ectester_standalone_libs_jni_TomCryptProvider
+ * Method: setup
+ * Signature: ()V
+ */
+JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_setup
+ (JNIEnv *, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c b/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
new file mode 100644
index 0000000..43ae06b
--- /dev/null
+++ b/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
@@ -0,0 +1,40 @@
+#include "native.h"
+#include <stdio.h>
+#define LTM_DESC
+#include <tomcrypt.h>
+
+JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_createProvider(JNIEnv *env, jobject this) {
+ jclass provider_class = (*env)->FindClass(env, "cz/crcs/ectester/standalone/libs/jni/TomCryptProvider");
+
+ jmethodID init = (*env)->GetMethodID(env, provider_class, "<init>", "(Ljava/lang/String;DLjava/lang/String;)V");
+ if (init == NULL) {
+ return NULL;
+ }
+ jstring name = (*env)->NewStringUTF(env, "libtomcrypt " SCRYPT);
+ double version = strtod(SCRYPT, NULL);
+ return (*env)->NewObject(env, provider_class, init, name, version, name);
+}
+
+JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_jni_TomCryptProvider_setup(JNIEnv *env, jobject this) {
+ ltc_mp = ltm_desc;
+ /* Just test ecc key generation at this time. */
+ ecc_key mykey;
+ prng_state prng;
+ int err;
+ /* register yarrow */
+ if (register_prng(&yarrow_desc) == -1) {
+ printf("Error registering Yarrow\n");
+ return;
+ }
+ /* setup the PRNG */
+ if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
+ printf("Error setting up PRNG, %s\n", error_to_string(err));
+ return;
+ }
+ /* make a 192-bit ECC key */
+ if ((err = ecc_make_key(&prng, find_prng("yarrow"), 24, &mykey)) != CRYPT_OK) {
+ printf("Error making key: %s\n", error_to_string(err));
+ return;
+ }
+ return;
+} \ No newline at end of file
diff --git a/src/cz/crcs/ectester/standalone/libs/native.h b/src/cz/crcs/ectester/standalone/libs/native.h
deleted file mode 100644
index 979f04a..0000000
--- a/src/cz/crcs/ectester/standalone/libs/native.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/* DO NOT EDIT THIS FILE - it is machine generated */
-#include <jni.h>
-/* Header for class cz_crcs_ectester_standalone_libs_TomcryptLib */
-
-#ifndef _Included_cz_crcs_ectester_standalone_libs_TomcryptLib
-#define _Included_cz_crcs_ectester_standalone_libs_TomcryptLib
-#ifdef __cplusplus
-extern "C" {
-#endif
-/*
- * Class: cz_crcs_ectester_standalone_libs_TomcryptLib
- * Method: getProvider
- * Signature: ()Ljava/security/Provider;
- */
-JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_getProvider
- (JNIEnv *, jobject);
-
-#ifdef __cplusplus
-}
-#endif
-#endif