Skip to content

Commit

Permalink
Add test-vector suite to tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
J08nY committed Mar 28, 2024
1 parent 6cadd53 commit 59bcf04
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 10 deletions.
3 changes: 3 additions & 0 deletions standalone/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ tasks.named<Test>("test") {
useJUnitPlatform()
// Report is always generated after tests run
finalizedBy(tasks.jacocoTestReport)
jvmArgs(
"--add-exports", "jdk.crypto.ec/sun.security.ec=ALL-UNNAMED"
)
// Add wolfcrypt JNI lib path to LD_LIBRARY_PATH (as our native library loading does not handle it)
environment(
"LD_LIBRARY_PATH", "$rootDir/ext/wolfcrypt-jni/lib/:" + System.getenv("LD_LIBRARY_PATH")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPa
return JNI_TRUE;
}

JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPairGeneratorSpi_00024Botan_paramsSupported(JNIEnv *env, jobject self, jobject params){
jboolean check_params(JNIEnv *env, jobject params) {
if (params == NULL) {
return JNI_FALSE;
}
Expand Down Expand Up @@ -128,6 +128,10 @@ JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPa
return JNI_FALSE;
}

JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPairGeneratorSpi_00024Botan_paramsSupported(JNIEnv *env, jobject self, jobject params){
return check_params(env, params);
}

static jobject biginteger_from_bigint(JNIEnv *env, const Botan::BigInt& bigint) {
std::vector<uint8_t> bigint_data = Botan::BigInt::encode(bigint);
jbyteArray bigint_array = env->NewByteArray(bigint_data.size());
Expand Down Expand Up @@ -305,6 +309,10 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPai
}

JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPairGeneratorSpi_00024Botan_generate__Ljava_security_spec_AlgorithmParameterSpec_2Ljava_security_SecureRandom_2(JNIEnv *env, jobject self, jobject params, jobject random){
if (!check_params(env, params)) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return NULL;
}
Botan::EC_Group curve_group = group_from_params(env, params);
return generate_from_group(env, self, curve_group);
}
Expand Down Expand Up @@ -340,6 +348,10 @@ static std::string get_kdf(const std::string& type_str, size_t *kdf_bits) {
}

jbyteArray generate_secret(JNIEnv *env, jobject self, jbyteArray pubkey, jbyteArray privkey, jobject params, jstring algorithm) {
if (!check_params(env, params)) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return NULL;
}
Botan::EC_Group curve_group = group_from_params(env, params);

jsize privkey_length = env->GetArrayLength(privkey);
Expand Down Expand Up @@ -400,6 +412,10 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyAgr
}

JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSignatureSpi_00024Botan_sign(JNIEnv *env, jobject self, jbyteArray data, jbyteArray privkey, jobject params){
if (!check_params(env, params)) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return NULL;
}
Botan::EC_Group curve_group = group_from_params(env, params);

jclass botan_sig_class = env->FindClass("cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi$Botan");
Expand Down Expand Up @@ -463,6 +479,10 @@ JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSig
}

JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSignatureSpi_00024Botan_verify(JNIEnv *env, jobject self, jbyteArray signature, jbyteArray data, jbyteArray pubkey, jobject params){
if (!check_params(env, params)) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return JNI_FALSE;
}
Botan::EC_Group curve_group = group_from_params(env, params);

jclass botan_sig_class = env->FindClass("cz/crcs/ectester/standalone/libs/jni/NativeSignatureSpi$Botan");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,18 @@ char *biginteger_to_hex(JNIEnv *env, jobject big, jint bytes) {
jstring big_string = (*env)->CallObjectMethod(env, big, to_string, (jint) 16);

jsize len = (*env)->GetStringUTFLength(env, big_string);
#if defined(__WIN32__) || defined(_MSC_VER)
char *raw_string = _alloca(len);
#else
char raw_string[len];
#endif
(*env)->GetStringUTFRegion(env, big_string, 0, len, raw_string);
const char *raw_string = (*env)->GetStringUTFChars(env, big_string, 0);

char *result = calloc(bytes, 2);
char *result = calloc(bytes, sizeof(char) * 2);
if (len >= bytes) {
return strncpy(result, raw_string, 2*bytes);
strncpy(result, raw_string, 2*bytes);
} else {
jsize diff = bytes - len;
for (jint i = 0; i < diff*2; ++i) {
result[i] = '0';
}
return strncpy(result + diff*2, raw_string, 2*bytes);
strncpy(result + diff*2, raw_string, 2*bytes);
}
(*env)->ReleaseStringUTFChars(env, big_string, raw_string);
return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ static int create_curve(JNIEnv *env, jobject params, mbedtls_ecp_group *group) {
jmethodID get_field = (*env)->GetMethodID(env, elliptic_curve_class, "getField", "()Ljava/security/spec/ECField;");
jobject field = (*env)->CallObjectMethod(env, curve, get_field);

if (!(*env)->IsInstanceOf(env, field, fp_field_class)) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return 1;
}

jmethodID get_p = (*env)->GetMethodID(env, fp_field_class, "getP", "()Ljava/math/BigInteger;");
jobject p = (*env)->CallObjectMethod(env, field, get_p);
mpi_from_biginteger(env, p, &group->P);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ static ltc_ecc_set_type* create_curve(JNIEnv *env, jobject params) {
jmethodID get_field = (*env)->GetMethodID(env, elliptic_curve_class, "getField", "()Ljava/security/spec/ECField;");
jobject field = (*env)->CallObjectMethod(env, elliptic_curve, get_field);

if (!(*env)->IsInstanceOf(env, field, fp_field_class)) {
return NULL;
}

jmethodID get_bits = (*env)->GetMethodID(env, fp_field_class, "getFieldSize", "()I");
jint bits = (*env)->CallIntMethod(env, field, get_bits);
jint bytes = (bits + 7) / 8;
Expand Down Expand Up @@ -284,6 +288,10 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPai
JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyPairGeneratorSpi_00024TomCrypt_generate__Ljava_security_spec_AlgorithmParameterSpec_2Ljava_security_SecureRandom_2(JNIEnv *env, jobject this, jobject params, jobject random){
if ((*env)->IsInstanceOf(env, params, ec_parameter_spec_class)) {
ltc_ecc_set_type *curve = create_curve(env, params);
if (!curve) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return NULL;
}
jobject result = generate_from_curve(env, curve);
free_curve(curve);
return result;
Expand Down Expand Up @@ -352,6 +360,10 @@ static jboolean pubkey_from_bytes(JNIEnv *env, jbyteArray pubkey, const ltc_ecc_

JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyAgreementSpi_00024TomCrypt_generateSecret___3B_3BLjava_security_spec_ECParameterSpec_2(JNIEnv *env, jobject this, jbyteArray pubkey, jbyteArray privkey, jobject params){
ltc_ecc_set_type *curve = create_curve(env, params);
if (!curve) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return NULL;
}

ecc_key pub;
if (!pubkey_from_bytes(env, pubkey, curve, &pub)) {
Expand Down Expand Up @@ -395,6 +407,10 @@ JNIEXPORT jobject JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeKeyAgr

JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSignatureSpi_00024TomCryptRaw_sign(JNIEnv *env, jobject this, jbyteArray data, jbyteArray privkey, jobject params) {
ltc_ecc_set_type *curve = create_curve(env, params);
if (!curve) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return NULL;
}

ecc_key priv;
if (!privkey_from_bytes(env, privkey, curve, &priv)) {
Expand Down Expand Up @@ -432,6 +448,10 @@ JNIEXPORT jbyteArray JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSig

JNIEXPORT jboolean JNICALL Java_cz_crcs_ectester_standalone_libs_jni_NativeSignatureSpi_00024TomCryptRaw_verify(JNIEnv *env, jobject this, jbyteArray signature, jbyteArray data, jbyteArray pubkey, jobject params) {
ltc_ecc_set_type *curve = create_curve(env, params);
if (!curve) {
throw_new(env, "java/lang/UnsupportedOperationException", "Not supported.");
return JNI_FALSE;
}

ecc_key pub;
if (!pubkey_from_bytes(env, pubkey, curve, &pub)) {
Expand Down
19 changes: 19 additions & 0 deletions standalone/src/test/java/cz/crcs/ectester/standalone/AppTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ public void defaultSuite(String libName, StdOut out) {
}
}

@SuppressWarnings("JUnitMalformedDeclaration")
@ParameterizedTest
@ValueSource(strings = {"Bouncy", "Sun", "libtomcrypt", "Botan", "Crypto++", "OpenSSL 3", "BoringSSL", "libgcrypt", "mbed TLS", "2021" /* IPPCP */, "Nettle", "LibreSSL", "wolfCrypt"})
@StdIo()
public void testVectorSuite(String libName, StdOut out) {
// TODO: Fix libgcrypt and IPPCP in handling binary field curves (reject them).
assumeFalse(libName.equals("libgcrypt") || libName.equals("2021"));

String[] args = new String[]{"test", "test-vectors", libName};
if (libName.equals("Botan") || libName.equals("Crypto++")) {
args = new String[]{"test", "--kpg-type", "ECDH", "test-vectors", libName};
}
ECTesterStandalone.main(args);
String sout = out.capturedString();
if (sout.contains("Exception")) {
System.err.printf("%s: Test vector suite has exceptions.%n", libName);
}
}

@ParameterizedTest
@ValueSource(strings = {"Bouncy", "Sun", "libtomcrypt", "Botan", "Crypto++", "OpenSSL 3", "BoringSSL", "libgcrypt", "mbed TLS", "2021" /* IPPCP */, "Nettle", "LibreSSL", "wolfCrypt"})
public void performanceSuite(String libName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cz.crcs.ectester.standalone;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junitpioneer.jupiter.StdIo;
import org.junitpioneer.jupiter.StdOut;

import static org.junit.jupiter.api.Assertions.assertFalse;

public class OutputTests {

@SuppressWarnings("JUnitMalformedDeclaration")
@ParameterizedTest
@ValueSource(strings = {"text", "xml", "yml"})
@StdIo()
public void formats(String format, StdOut out) {
ECTesterStandalone.main(new String[]{"test", "-f", format, "default", "Sun"});
String s = out.capturedString();
assertFalse(s.isEmpty());
}
}

0 comments on commit 59bcf04

Please sign in to comment.