aboutsummaryrefslogtreecommitdiff
path: root/standalone/src
diff options
context:
space:
mode:
authorJ08nY2024-08-07 20:18:23 +0200
committerJ08nY2024-08-07 20:18:23 +0200
commit84858177fe6c2b9bdc6ea9785a92781ba1237e04 (patch)
treeeac1fed49a8605d25a69c5aa9b74bc67280d0c17 /standalone/src
parentae4e8f69b68e51973a20ba59d2fc6fd1e7849972 (diff)
downloadECTester-84858177fe6c2b9bdc6ea9785a92781ba1237e04.tar.gz
ECTester-84858177fe6c2b9bdc6ea9785a92781ba1237e04.tar.zst
ECTester-84858177fe6c2b9bdc6ea9785a92781ba1237e04.zip
Diffstat (limited to 'standalone/src')
-rw-r--r--standalone/src/main/java/cz/crcs/ectester/standalone/libs/MbedTLSLib.java6
-rw-r--r--standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/mbedtls.c46
-rw-r--r--standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/native.h16
-rw-r--r--standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java3
4 files changed, 67 insertions, 4 deletions
diff --git a/standalone/src/main/java/cz/crcs/ectester/standalone/libs/MbedTLSLib.java b/standalone/src/main/java/cz/crcs/ectester/standalone/libs/MbedTLSLib.java
index e44598c..efc8cad 100644
--- a/standalone/src/main/java/cz/crcs/ectester/standalone/libs/MbedTLSLib.java
+++ b/standalone/src/main/java/cz/crcs/ectester/standalone/libs/MbedTLSLib.java
@@ -17,4 +17,10 @@ public class MbedTLSLib extends NativeECLibrary {
@Override
public native Set<String> getCurves();
+
+ @Override
+ public native boolean supportsDeterministicPRNG();
+
+ @Override
+ public native boolean setupDeterministicPRNG(byte[] seed);
}
diff --git a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/mbedtls.c b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/mbedtls.c
index 5fc879b..8f5e0fb 100644
--- a/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/mbedtls.c
+++ b/standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/mbedtls.c
@@ -16,7 +16,8 @@
static mbedtls_ctr_drbg_context ctr_drbg;
-static mbedtls_entropy_context entropy;
+static mbedtls_entropy_context urandom_entropy;
+static mbedtls_entropy_context fixed_entropy;
static jclass provider_class;
@@ -62,6 +63,17 @@ static int dev_urandom(void *data, unsigned char *output, size_t len, size_t *ol
return 0;
}
+static unsigned char seed_store[32] = {0};
+
+static int fixed_random(void *data, unsigned char *output, size_t len, size_t *olen) {
+ for (size_t i = 0; i < len; ++i) {
+ output[i] = seed_store[i % 32];
+ }
+ *olen = len;
+ return 0;
+}
+
+
static int ctr_drbg_wrapper(void *ctx, unsigned char *buf, size_t len) {
native_timing_pause();
int result = mbedtls_ctr_drbg_random(ctx, buf, len);
@@ -77,9 +89,9 @@ JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeProvider_
ADD_SIG(env, this, "NONEwithECDSA", "MbedTLSECDSAwithNONE");
mbedtls_ctr_drbg_init(&ctr_drbg);
- mbedtls_entropy_init(&entropy);
- mbedtls_entropy_add_source(&entropy, dev_urandom, NULL, 32, MBEDTLS_ENTROPY_SOURCE_STRONG);
- mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
+ mbedtls_entropy_init(&urandom_entropy);
+ mbedtls_entropy_add_source(&urandom_entropy, dev_urandom, NULL, 32, MBEDTLS_ENTROPY_SOURCE_STRONG);
+ mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &urandom_entropy, NULL, 0);
init_classes(env, "MbedTLS");
}
@@ -101,6 +113,32 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_MbedTLSLib_getCu
return result;
}
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_MbedTLSLib_supportsDeterministicPRNG(JNIEnv *env, jobject this) {
+ return JNI_TRUE;
+}
+
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_MbedTLSLib_setupDeterministicPRNG(JNIEnv *env, jobject this, jbyteArray seed) {
+ jsize seed_length = (*env)->GetArrayLength(env, seed);
+ if (seed_length > 32) {
+ fprintf(stderr, "Error setting seed, needs to be at most 32 bytes, is %i.\n", seed_length);
+ return JNI_FALSE;
+ }
+ jbyte *seed_data = (*env)->GetByteArrayElements(env, seed, NULL);
+ memcpy(seed_store, seed_data, seed_length);
+ (*env)->ReleaseByteArrayElements(env, seed, seed_data, JNI_ABORT);
+
+ memset(&ctr_drbg, 0, sizeof(ctr_drbg));
+ mbedtls_ctr_drbg_init(&ctr_drbg);
+
+ mbedtls_entropy_init(&fixed_entropy);
+ // This is NASTY! We are accessing something the library does not want us to.
+ fixed_entropy.private_source_count = 0;
+ mbedtls_entropy_add_source(&fixed_entropy, fixed_random, NULL, 32, MBEDTLS_ENTROPY_SOURCE_STRONG);
+ mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &fixed_entropy, NULL, 0);
+
+ return JNI_TRUE;
+}
+
JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPairGeneratorSpi_00024MbedTLS_keysizeSupported(JNIEnv *env, jobject this, jint keysize) {
for (const mbedtls_ecp_curve_info *curve_info = mbedtls_ecp_curve_list();
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
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 627f38f..287b450 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
@@ -1389,6 +1389,22 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_MbedTLSLib_creat
JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_MbedTLSLib_getCurves
(JNIEnv *, jobject);
+/*
+ * Class: cz_crcs_ectester_standalone_libs_MbedTLSLib
+ * Method: supportsDeterministicPRNG
+ * Signature: ()Z
+ */
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_MbedTLSLib_supportsDeterministicPRNG
+ (JNIEnv *, jobject);
+
+/*
+ * Class: cz_crcs_ectester_standalone_libs_MbedTLSLib
+ * Method: setupDeterministicPRNG
+ * Signature: ([B)Z
+ */
+JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_MbedTLSLib_setupDeterministicPRNG
+ (JNIEnv *, jobject, jbyteArray);
+
#ifdef __cplusplus
}
#endif
diff --git a/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java b/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java
index 6072e68..8693b7b 100644
--- a/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java
+++ b/standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java
@@ -106,6 +106,9 @@ public class AppTests {
case "BoringSSL":
args = new String[]{"generate", "-ps", "123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234", "-n", "10", "-cn", "prime256v1", libName};
break;
+ case "mbedTLS":
+ args = new String[]{"generate", "-ps", "12345678", "-n", "10", "-nc", "secg/secp256r1", libName};
+ break;
}
ECTesterStandalone.main(args);
String out1 = out.capturedString();