Skip to content

Commit aa37d2b

Browse files
committed
Add deterministic PRNG support to Nettle.
1 parent c55d804 commit aa37d2b

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

standalone/src/main/java/cz/crcs/ectester/standalone/libs/NettleLib.java

+6
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public static ECGenParameterSpec parametersKnown(AlgorithmParameterSpec params)
4949
}
5050
throw new InvalidAlgorithmParameterException("Unknown curve.");
5151
}
52+
53+
@Override
54+
public native boolean supportsDeterministicPRNG();
55+
56+
@Override
57+
public native boolean setupDeterministicPRNG(byte[] seed);
5258
}

standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/native.h

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

standalone/src/main/resources/cz/crcs/ectester/standalone/libs/jni/nettle.c

+28-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ JNIEXPORT void JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeProvider_
4444
init_classes(env, "Nettle");
4545

4646
yarrow256_init(&yarrow, 0, NULL);
47-
uint8_t file = open("/dev/random", O_RDONLY);
48-
yarrow256_seed(&yarrow, YARROW256_SEED_FILE_SIZE, &file);
49-
close(file);
50-
47+
FILE *urandom = fopen("/dev/urandom", "rb");
48+
uint8_t seed[YARROW256_SEED_FILE_SIZE];
49+
if (urandom) {
50+
size_t read = 0;
51+
while (read < sizeof(seed)) {
52+
read += fread(((uint8_t *)&seed) + read, 1, sizeof(seed) - read, urandom);
53+
}
54+
fclose(urandom);
55+
}
56+
yarrow256_seed(&yarrow, YARROW256_SEED_FILE_SIZE, seed);
5157
}
5258

5359
JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_NettleLib_getCurves(JNIEnv *env, jobject self) {
@@ -66,6 +72,24 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_NettleLib_getCur
6672
return result;
6773
}
6874

75+
JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_NettleLib_supportsDeterministicPRNG(JNIEnv *env, jobject self) {
76+
return JNI_TRUE;
77+
}
78+
79+
JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_NettleLib_setupDeterministicPRNG(JNIEnv *env, jobject self, jbyteArray seed) {
80+
jsize seed_length = (*env)->GetArrayLength(env, seed);
81+
if (seed_length < YARROW256_SEED_FILE_SIZE) {
82+
fprintf(stderr, "Error setting seed, needs to be at least %i bytes.\n", YARROW256_SEED_FILE_SIZE);
83+
return JNI_FALSE;
84+
}
85+
86+
jbyte *seed_data = (*env)->GetByteArrayElements(env, seed, NULL);
87+
yarrow256_init(&yarrow, 0, NULL);
88+
yarrow256_seed(&yarrow, YARROW256_SEED_FILE_SIZE, seed_data);
89+
(*env)->ReleaseByteArrayElements(env, seed, seed_data, JNI_ABORT);
90+
return JNI_TRUE;
91+
}
92+
6993
static const struct ecc_curve* create_curve_from_name(JNIEnv *env, const char* curve_name) {
7094
if (!curve_name) {
7195
return NULL;

0 commit comments

Comments
 (0)