aboutsummaryrefslogtreecommitdiff
path: root/standalone/src/main/resources
diff options
context:
space:
mode:
authorJ08nY2024-08-05 17:50:11 +0200
committerJ08nY2024-08-05 17:50:11 +0200
commitabb89ea702d046e27ea457df31a7d69bab3b0802 (patch)
tree09a9bc75ec019f11f92566d9a25bc91a2440fc2b /standalone/src/main/resources
parent33a859073f34fab3843b80754de1a558d1b125f9 (diff)
downloadECTester-abb89ea702d046e27ea457df31a7d69bab3b0802.tar.gz
ECTester-abb89ea702d046e27ea457df31a7d69bab3b0802.tar.zst
ECTester-abb89ea702d046e27ea457df31a7d69bab3b0802.zip
Add deterministic PRNG to Botan.
Diffstat (limited to 'standalone/src/main/resources')
-rw-r--r--standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/Makefile4
-rw-r--r--standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/botan.cpp47
-rw-r--r--standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/native.h24
-rw-r--r--standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/openssl.c3
-rw-r--r--standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c3
5 files changed, 60 insertions, 21 deletions
diff --git a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/Makefile b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/Makefile
index 6282574..aec0c03 100644
--- a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/Makefile
+++ b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/Makefile
@@ -12,9 +12,9 @@ DEBUG ?= 0
PROJECT_ROOT_PATH ?= ../../../../../../../../../..
ifeq ($(DEBUG), 1)
- CFLAGS+=-g -Wall
+ CFLAGS+=-g -O0 -Wall
LFLAGS+=-g
- CXXFLAGS+=-g -Wall
+ CXXFLAGS+=-g -O0 -Wall
else
CFLAGS+=-O2
LFLAGS+=-O2
diff --git a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/botan.cpp b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/botan.cpp
index 52c8dbb..b3977e1 100644
--- a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/botan.cpp
+++ b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/botan.cpp
@@ -4,7 +4,9 @@
#include <botan/version.h>
#include <botan/rng.h>
#include <botan/secmem.h>
+#include <botan/system_rng.h>
#include <botan/auto_rng.h>
+#include <botan/chacha_rng.h>
#include <botan/ec_group.h>
#include <botan/ecc_key.h>
@@ -23,7 +25,7 @@
*/
static jclass provider_class;
-static Botan::AutoSeeded_RNG rng;
+std::unique_ptr<Botan::RandomNumberGenerator> rng = std::make_unique<Botan::AutoSeeded_RNG>();
JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_createProvider(JNIEnv *env, jobject self) {
/* Create the custom provider. */
@@ -82,7 +84,7 @@ JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeProvider_
init_classes(env, "Botan");
}
-JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_getCurves(JNIEnv *env, jobject self){
+JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_getCurves(JNIEnv *env, jobject self) {
jclass set_class = env->FindClass("java/util/TreeSet");
jmethodID set_ctr = env->GetMethodID(set_class, "<init>", "()V");
@@ -99,6 +101,25 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_getCurv
return result;
}
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_supportsDeterministicPRNG(JNIEnv *env, jobject self) {
+ return JNI_TRUE;
+}
+
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_setupDeterministicPRNG(JNIEnv *env, jobject self, jbyteArray seed) {
+ jsize seed_length = env->GetArrayLength(seed);
+ if (seed_length < 32) {
+ fprintf(stderr, "Error setting seed, needs to be at least 32 bytes.\n");
+ return JNI_FALSE;
+ }
+ jbyte *seed_data = env->GetByteArrayElements(seed, nullptr);
+ Botan::secure_vector<uint8_t> vec((uint8_t *)seed_data, (uint8_t *)seed_data + seed_length);
+ Botan::ChaCha_RNG *cha = new Botan::ChaCha_RNG(vec);
+ rng.reset(cha);
+ env->ReleaseByteArrayElements(seed, seed_data, JNI_ABORT);
+ return JNI_TRUE;
+}
+
+
JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPairGeneratorSpi_00024Botan_keysizeSupported(JNIEnv *env, jobject self, jint keysize){
return JNI_TRUE;
}
@@ -252,13 +273,13 @@ static jobject generate_from_group(JNIEnv* env, jobject self, Botan::EC_Group gr
try {
native_timing_start();
if (type_str == "ECDH") {
- skey = std::make_unique<Botan::ECDH_PrivateKey>(rng, group);
+ skey = std::make_unique<Botan::ECDH_PrivateKey>(*rng, group);
} else if (type_str == "ECDSA") {
- skey = std::make_unique<Botan::ECDSA_PrivateKey>(rng, group);
+ skey = std::make_unique<Botan::ECDSA_PrivateKey>(*rng, group);
} else if (type_str == "ECKCDSA") {
- skey = std::make_unique<Botan::ECKCDSA_PrivateKey>(rng, group);
+ skey = std::make_unique<Botan::ECKCDSA_PrivateKey>(*rng, group);
} else if (type_str == "ECGDSA") {
- skey = std::make_unique<Botan::ECGDSA_PrivateKey>(rng, group);
+ skey = std::make_unique<Botan::ECGDSA_PrivateKey>(*rng, group);
}
native_timing_stop();
} catch (Botan::Exception & ex) {
@@ -375,7 +396,7 @@ jbyteArray generate_secret(JNIEnv *env, jobject self, jbyteArray pubkey, jbyteAr
Botan::BigInt privkey_scalar((unsigned char *) privkey_data, privkey_length);
env->ReleaseByteArrayElements(privkey, privkey_data, JNI_ABORT);
- Botan::ECDH_PrivateKey skey(rng, curve_group, privkey_scalar);
+ Botan::ECDH_PrivateKey skey(*rng, curve_group, privkey_scalar);
jsize pubkey_length = env->GetArrayLength(pubkey);
jbyte *pubkey_data = env->GetByteArrayElements(pubkey, nullptr);
@@ -402,7 +423,7 @@ jbyteArray generate_secret(JNIEnv *env, jobject self, jbyteArray pubkey, jbyteAr
size_t key_len = (get_kdf_bits(env, algorithm) + 7) / 8;
std::string kdf = get_kdf(type_str, &key_len);
- Botan::PK_Key_Agreement ka(skey, rng, kdf);
+ Botan::PK_Key_Agreement ka(skey, *rng, kdf);
std::vector<uint8_t> derived;
try {
@@ -469,11 +490,11 @@ JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSig
std::unique_ptr<Botan::EC_PrivateKey> skey;
try {
if (type_str.find("ECDSA") != std::string::npos) {
- skey = std::make_unique<Botan::ECDSA_PrivateKey>(rng, curve_group, privkey_scalar);
+ skey = std::make_unique<Botan::ECDSA_PrivateKey>(*rng, curve_group, privkey_scalar);
} else if (type_str.find("ECKCDSA") != std::string::npos) {
- skey = std::make_unique<Botan::ECKCDSA_PrivateKey>(rng, curve_group, privkey_scalar);
+ skey = std::make_unique<Botan::ECKCDSA_PrivateKey>(*rng, curve_group, privkey_scalar);
} else if (type_str.find("ECGDSA") != std::string::npos) {
- skey = std::make_unique<Botan::ECGDSA_PrivateKey>(rng, curve_group, privkey_scalar);
+ skey = std::make_unique<Botan::ECGDSA_PrivateKey>(*rng, curve_group, privkey_scalar);
}
} catch (Botan::Exception & ex) {
throw_new(env, "java/security/GeneralSecurityException", ex.what());
@@ -504,10 +525,10 @@ JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSig
jbyte *data_bytes = env->GetByteArrayElements(data, nullptr);
std::vector<uint8_t> sig;
try {
- Botan::PK_Signer signer(*skey, rng, emsa, sigformat);
+ Botan::PK_Signer signer(*skey, *rng, emsa, sigformat);
native_timing_start();
- sig = signer.sign_message((uint8_t*) data_bytes, data_length, rng);
+ sig = signer.sign_message((uint8_t*) data_bytes, data_length, *rng);
native_timing_stop();
} catch (Botan::Exception & ex) {
throw_new(env, "java/security/GeneralSecurityException", ex.what());
diff --git a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/native.h b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/native.h
index b1296cc..49be801 100644
--- a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/native.h
+++ b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/native.h
@@ -85,9 +85,9 @@ JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_sup
/*
* Class: cz_crcs_ectester_standalone_libs_TomcryptLib
* Method: setupDeterministicPRNG
- * Signature: ([B)V
+ * Signature: ([B)Z
*/
-JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_setupDeterministicPRNG
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_setupDeterministicPRNG
(JNIEnv *, jobject, jbyteArray);
#ifdef __cplusplus
@@ -271,6 +271,22 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_createP
JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_getCurves
(JNIEnv *, jobject);
+/*
+ * Class: cz_crcs_ectester_standalone_libs_BotanLib
+ * Method: supportsDeterministicPRNG
+ * Signature: ()Z
+ */
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_supportsDeterministicPRNG
+ (JNIEnv *, jobject);
+
+/*
+ * Class: cz_crcs_ectester_standalone_libs_BotanLib
+ * Method: setupDeterministicPRNG
+ * Signature: ([B)Z
+ */
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_BotanLib_setupDeterministicPRNG
+ (JNIEnv *, jobject, jbyteArray);
+
#ifdef __cplusplus
}
#endif
@@ -644,9 +660,9 @@ JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_OpensslLib_supp
/*
* Class: cz_crcs_ectester_standalone_libs_OpensslLib
* Method: setupDeterministicPRNG
- * Signature: ([B)V
+ * Signature: ([B)Z
*/
-JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_OpensslLib_setupDeterministicPRNG
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_OpensslLib_setupDeterministicPRNG
(JNIEnv *, jobject, jbyteArray);
#ifdef __cplusplus
diff --git a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/openssl.c b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/openssl.c
index 0b07821..5110c8b 100644
--- a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/openssl.c
+++ b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/openssl.c
@@ -113,12 +113,13 @@ RAND_METHOD stdlib_rand_meth = { stdlib_rand_seed,
stdlib_rand_status
};
-JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_OpensslLib_setupDeterministicPRNG(JNIEnv *env, jobject self, jbyteArray seed) {
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_OpensslLib_setupDeterministicPRNG(JNIEnv *env, jobject self, jbyteArray seed) {
RAND_set_rand_method(&stdlib_rand_meth);
jbyte *seed_data = (*env)->GetByteArrayElements(env, seed, NULL);
jsize seed_length = (*env)->GetArrayLength(env, seed);
RAND_seed(seed_data, seed_length);
(*env)->ReleaseByteArrayElements(env, seed, seed_data, JNI_ABORT);
+ return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPairGeneratorSpi_00024Openssl_keysizeSupported(JNIEnv *env, jobject self, jint keysize) {
diff --git a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
index 6e9dfbf..12c1936 100644
--- a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
+++ b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/tomcrypt.c
@@ -81,13 +81,14 @@ JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_sup
return JNI_TRUE;
}
-JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_setupDeterministicPRNG(JNIEnv *env, jobject self, jbyteArray seed) {
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_TomcryptLib_setupDeterministicPRNG(JNIEnv *env, jobject self, jbyteArray seed) {
yarrow_start(&ltc_prng);
jbyte *seed_data = (*env)->GetByteArrayElements(env, seed, NULL);
jsize seed_length = (*env)->GetArrayLength(env, seed);
yarrow_add_entropy(seed_data, seed_length, &ltc_prng);
yarrow_ready(&ltc_prng);
(*env)->ReleaseByteArrayElements(env, seed, seed_data, JNI_ABORT);
+ return JNI_TRUE;
}