aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2018-01-05 15:56:34 +0100
committerJ08nY2018-01-05 15:56:34 +0100
commit3a654a1b6919c8d4c5982dac645c227a8ff2c5f1 (patch)
tree6e48f02794932b50aacb10896358e44a4f074910
parente9fcef97c71d01e92658af522b5ecb99a751b540 (diff)
downloadECTester-3a654a1b6919c8d4c5982dac645c227a8ff2c5f1.tar.gz
ECTester-3a654a1b6919c8d4c5982dac645c227a8ff2c5f1.tar.zst
ECTester-3a654a1b6919c8d4c5982dac645c227a8ff2c5f1.zip
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/botan.cpp53
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/c_utils.c7
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/c_utils.h15
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.cpp5
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.hpp15
-rw-r--r--src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c7
-rwxr-xr-xutil/plot_dh.py3
-rwxr-xr-xutil/plot_gen.py3
8 files changed, 86 insertions, 22 deletions
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/botan.cpp b/src/cz/crcs/ectester/standalone/libs/jni/botan.cpp
index f87d68b..8e666d6 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/botan.cpp
+++ b/src/cz/crcs/ectester/standalone/libs/jni/botan.cpp
@@ -347,14 +347,19 @@ static jobject generate_from_group(JNIEnv* env, jobject self, Botan::EC_Group gr
env->ReleaseStringUTFChars(type, type_data);
std::unique_ptr<Botan::EC_PrivateKey> skey;
- if (type_str == "ECDH") {
- skey = std::make_unique<Botan::ECDH_PrivateKey>(rng, group);
- } else if (type_str == "ECDSA") {
- skey = std::make_unique<Botan::ECDSA_PrivateKey>(rng, group);
- } else if (type_str == "ECKCDSA") {
- skey = std::make_unique<Botan::ECKCDSA_PrivateKey>(rng, group);
- } else if (type_str == "ECGDSA") {
- skey = std::make_unique<Botan::ECGDSA_PrivateKey>(rng, group);
+ try {
+ if (type_str == "ECDH") {
+ skey = std::make_unique<Botan::ECDH_PrivateKey>(rng, group);
+ } else if (type_str == "ECDSA") {
+ skey = std::make_unique<Botan::ECDSA_PrivateKey>(rng, group);
+ } else if (type_str == "ECKCDSA") {
+ skey = std::make_unique<Botan::ECKCDSA_PrivateKey>(rng, group);
+ } else if (type_str == "ECGDSA") {
+ skey = std::make_unique<Botan::ECGDSA_PrivateKey>(rng, group);
+ }
+ } catch (Botan::Exception & ex) {
+ throw_new(env, "java/security/GeneralSecurityException", ex.what());
+ return NULL;
}
jobject ec_param_spec = params_from_group(env, group);
@@ -403,7 +408,8 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPai
return generate_from_group(env, self, curve_group);
}
}
- //TODO throw an exception here? InvalidAlgorithmParameters one?
+
+ throw_new(env, "java/security/InvalidAlgorithmParameterException", "Curve not found.");
return NULL;
}
@@ -469,13 +475,17 @@ JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKey
} else if (type_str == "ECDHwithSHA512KDF") {
kdf = "KDF1(SHA-512)";
key_len = 64;
- } else {
- //TODO what?
}
Botan::PK_Key_Agreement ka(skey, rng, kdf);
- std::vector<uint8_t> derived = Botan::unlock(ka.derive_key(key_len, pkey.public_value()).bits_of());
+ std::vector<uint8_t> derived;
+ try {
+ derived = Botan::unlock(ka.derive_key(key_len, pkey.public_value()).bits_of());
+ } catch (Botan::Exception & ex) {
+ throw_new(env, "java/security/GeneralSecurityException", ex.what());
+ return NULL;
+ }
jbyteArray result = env->NewByteArray(derived.size());
jbyte *result_data = env->GetByteArrayElements(result, NULL);
std::copy(derived.begin(), derived.end(), result_data);
@@ -534,7 +544,14 @@ JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSig
jsize data_length = env->GetArrayLength(data);
jbyte *data_bytes = env->GetByteArrayElements(data, NULL);
- std::vector<uint8_t> sig = signer.sign_message((uint8_t*) data_bytes, data_length, rng);
+ std::vector<uint8_t> sig;
+ try {
+ sig = signer.sign_message((uint8_t*) data_bytes, data_length, rng);
+ } catch (Botan::Exception & ex) {
+ throw_new(env, "java/security/GeneralSecurityException", ex.what());
+ env->ReleaseByteArrayElements(data, data_bytes, JNI_ABORT);
+ return NULL;
+ }
env->ReleaseByteArrayElements(data, data_bytes, JNI_ABORT);
jbyteArray result = env->NewByteArray(sig.size());
@@ -596,7 +613,15 @@ JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSigna
jbyte *data_bytes = env->GetByteArrayElements(data, NULL);
jbyte *sig_bytes = env->GetByteArrayElements(signature, NULL);
- bool result = verifier.verify_message((uint8_t*)data_bytes, data_length, (uint8_t*)sig_bytes, sig_length);
+ bool result;
+ try {
+ result = verifier.verify_message((uint8_t*)data_bytes, data_length, (uint8_t*)sig_bytes, sig_length);
+ } catch (Botan::Exception & ex) {
+ throw_new(env, "java/security/GeneralSecurityException", ex.what());
+ env->ReleaseByteArrayElements(data, data_bytes, JNI_ABORT);
+ env->ReleaseByteArrayElements(signature, sig_bytes, JNI_ABORT);
+ return JNI_FALSE;
+ }
env->ReleaseByteArrayElements(data, data_bytes, JNI_ABORT);
env->ReleaseByteArrayElements(signature, sig_bytes, JNI_ABORT);
if (result) {
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/c_utils.c b/src/cz/crcs/ectester/standalone/libs/jni/c_utils.c
index 230f516..336f4a1 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/c_utils.c
+++ b/src/cz/crcs/ectester/standalone/libs/jni/c_utils.c
@@ -58,4 +58,9 @@ void init_classes(JNIEnv *env, const char* lib_name) {
jclass local_illegal_state_exception_class = (*env)->FindClass(env, "java/lang/IllegalStateException");
illegal_state_exception_class = (*env)->NewGlobalRef(env, local_illegal_state_exception_class);
-} \ No newline at end of file
+}
+
+void throw_new(JNIEnv *env, const char *class, const char *message) {
+ jclass clazz = (*env)->FindClass(env, class);
+ (*env)->ThrowNew(env, clazz, message);
+}
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/c_utils.h b/src/cz/crcs/ectester/standalone/libs/jni/c_utils.h
index edd0bda..d925dfe 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/c_utils.h
+++ b/src/cz/crcs/ectester/standalone/libs/jni/c_utils.h
@@ -1,5 +1,10 @@
+#pragma once
+
#include "native.h"
+/**
+ * Classes that are accessed alot are cached here, manually.
+ */
extern jclass ec_parameter_spec_class;
extern jclass ecgen_parameter_spec_class;
extern jclass pubkey_class;
@@ -12,4 +17,12 @@ extern jclass point_class;
extern jclass biginteger_class;
extern jclass illegal_state_exception_class;
-void init_classes(JNIEnv *env, const char* lib_name); \ No newline at end of file
+/**
+ * Initialize the classes.
+ */
+void init_classes(JNIEnv *env, const char* lib_name);
+
+/**
+ * Throw a new exception of class with message.
+ */
+void throw_new(JNIEnv *env, const char *class, const char *message); \ No newline at end of file
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.cpp b/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.cpp
index ed59d51..cef4bfe 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.cpp
+++ b/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.cpp
@@ -51,4 +51,9 @@ void init_classes(JNIEnv *env, std::string lib_name) {
jclass local_illegal_state_exception_class = env->FindClass("java/lang/IllegalStateException");
illegal_state_exception_class = (jclass) env->NewGlobalRef(local_illegal_state_exception_class);
+}
+
+void throw_new(JNIEnv *env, const std::string& klass, const std::string& message) {
+ jclass clazz = env->FindClass(klass.c_str());
+ env->ThrowNew(clazz, message.c_str());
} \ No newline at end of file
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.hpp b/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.hpp
index d0bf8f2..bbca521 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.hpp
+++ b/src/cz/crcs/ectester/standalone/libs/jni/cpp_utils.hpp
@@ -1,6 +1,11 @@
+#pragma once
+
#include "native.h"
#include <string>
+/**
+ * Classes that are accessed alot are cached here, manually.
+ */
extern jclass ec_parameter_spec_class;
extern jclass ecgen_parameter_spec_class;
extern jclass pubkey_class;
@@ -13,4 +18,12 @@ extern jclass point_class;
extern jclass biginteger_class;
extern jclass illegal_state_exception_class;
-void init_classes(JNIEnv *env, std::string lib_name); \ No newline at end of file
+/**
+ * Initialize the classes.
+ */
+void init_classes(JNIEnv *env, std::string lib_name);
+
+/**
+ * Throw a new exception of class with message.
+ */
+void throw_new(JNIEnv *env, const std::string& klass, const std::string& message); \ No newline at end of file
diff --git a/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c b/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
index 0fb69a3..29ee707 100644
--- a/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
+++ b/src/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
@@ -229,11 +229,6 @@ static ltc_ecc_set_type* create_curve(JNIEnv *env, jobject params) {
return curve;
}
-static void throw_new(JNIEnv *env, const char *class, const char *message) {
- jclass clazz = (*env)->FindClass(env, class);
- (*env)->ThrowNew(env, clazz, message);
-}
-
static jobject generate_from_curve(JNIEnv *env, const ltc_ecc_set_type *curve) {
ecc_key key;
int err;
@@ -280,6 +275,7 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPai
}
if (curve->size == 0) {
+ throw_new(env, "java/security/InvalidAlgorithmParameterException", "Curve for given bitsize not found.");
return NULL;
}
@@ -307,6 +303,7 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPai
return generate_from_curve(env, curve);
} else {
+ throw_new(env, "java/security/InvalidAlgorithmParameterException", "Curve not found.");
return NULL;
}
}
diff --git a/util/plot_dh.py b/util/plot_dh.py
index 8c1dfff..2354688 100755
--- a/util/plot_dh.py
+++ b/util/plot_dh.py
@@ -20,6 +20,7 @@ from operator import itemgetter
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Plot ECTester ECDH timing.")
parser.add_argument("-o", "--output", dest="output", type=argparse.FileType("wb"), help="Write image to [file], do not display.", metavar="file")
+ parser.add_argument("--skip-first", dest="skip_first", action="store_true", help="Skip first entry, as it's usually a large outlier.")
parser.add_argument("file", type=str, help="The file to plot(csv).")
opts = parser.parse_args()
@@ -30,6 +31,8 @@ if __name__ == "__main__":
hx = lambda x: int(x, 16)
data = np.genfromtxt(opts.file, delimiter=";", skip_header=1, converters={2: hx, 3: hx, 4: hx}, dtype=np.dtype([("index","u4"), ("time","u4"), ("pub", "O"), ("priv", "O"), ("secret","O")]))
+ if opts.skip_first:
+ data = data[1:]
if "nano" in header_names[1]:
unit = r"$\mu s$"
diff --git a/util/plot_gen.py b/util/plot_gen.py
index 016dd15..12f7089 100755
--- a/util/plot_gen.py
+++ b/util/plot_gen.py
@@ -23,6 +23,7 @@ if __name__ == "__main__":
parser.add_argument("--pub", dest="pub", action="store_true", help="Show public key scatter plot.")
parser.add_argument("--priv", dest="priv", action="store_true", help="Show private key scatter plot.")
parser.add_argument("--hist", dest="hist", action="store_true", help="Show histogram.")
+ parser.add_argument("--skip-first", dest="skip_first", action="store_true", help="Skip first entry, as it's usually a large outlier.")
parser.add_argument("file", type=str, help="The file to plot(csv).")
opts = parser.parse_args()
@@ -39,6 +40,8 @@ if __name__ == "__main__":
hx = lambda x: int(x, 16)
data = np.genfromtxt(opts.file, delimiter=";", skip_header=1, converters={2: hx, 3: hx}, dtype=np.dtype([("index","u4"), ("time","u4"), ("pub", "O"), ("priv", "O")]))
+ if opts.skip_first:
+ data = data[1:]
if "nano" in header_names[1]:
unit = r"$\mu s$"