From 05091926cb63105959017c8bafd9dd7f2c751acb Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 27 Apr 2023 10:46:15 -0600 Subject: [PATCH] fix build errors and warnings for Visual Studio on Windows --- jni/include/wolfcrypt_jni_error.h | 4 +- jni/jni_aes.c | 18 ++- jni/jni_asn.c | 21 ++- jni/jni_chacha.c | 4 +- jni/jni_curve25519.c | 4 +- jni/jni_des3.c | 16 ++- jni/jni_dh.c | 4 +- jni/jni_ecc.c | 18 ++- jni/jni_ed25519.c | 4 +- jni/jni_error.c | 4 +- jni/jni_feature_detect.c | 4 +- jni/jni_fips.c | 122 ++++++++++-------- jni/jni_hmac.c | 4 +- jni/jni_logging.c | 4 +- jni/jni_md5.c | 7 +- jni/jni_native_struct.c | 10 +- jni/jni_rng.c | 4 +- jni/jni_rsa.c | 31 +++-- jni/jni_sha.c | 53 +++++--- jni/jni_wolfobject.c | 4 +- .../wolfssl/provider/jce/WolfCryptCipher.java | 5 + .../com/wolfssl/wolfcrypt/FeatureDetect.java | 30 +++++ .../com/wolfssl/wolfcrypt/WolfObject.java | 30 +++++ 23 files changed, 285 insertions(+), 120 deletions(-) diff --git a/jni/include/wolfcrypt_jni_error.h b/jni/include/wolfcrypt_jni_error.h index 88eb57a..db6774e 100644 --- a/jni/include/wolfcrypt_jni_error.h +++ b/jni/include/wolfcrypt_jni_error.h @@ -22,7 +22,9 @@ #ifndef _Included_wolfcrypt_jni_error #define _Included_wolfcrypt_jni_error -#pragma GCC diagnostic ignored "-Wpointer-to-int-cast" +#ifndef USE_WINDOWS_API + #pragma GCC diagnostic ignored "-Wpointer-to-int-cast" +#endif #include diff --git a/jni/jni_aes.c b/jni/jni_aes.c index 6f2d7d2..0752c26 100644 --- a/jni/jni_aes.c +++ b/jni/jni_aes.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -116,10 +118,14 @@ Java_com_wolfssl_wolfcrypt_Aes_native_1update__I_3BII_3BI( else if (offset < 0 || length < 0 || outputOffset < 0) { ret = BAD_FUNC_ARG; /* signed sanizizers */ } - else if (offset + length > getByteArrayLength(env, input_object)) { + else if (length == 0) { + ret = 0; + } else if ((word32)(offset + length) > + getByteArrayLength(env, input_object)) { ret = BUFFER_E; /* buffer overflow check */ } - else if (outputOffset + length > getByteArrayLength(env, output_object)) { + else if ((word32)(outputOffset + length) > + getByteArrayLength(env, output_object)) { ret = BUFFER_E; /* buffer overflow check */ } else if (opmode == AES_ENCRYPTION) { @@ -181,10 +187,12 @@ Java_com_wolfssl_wolfcrypt_Aes_native_1update__ILjava_nio_ByteBuffer_2IILjava_ni else if (offset < 0 || length < 0) { ret = BAD_FUNC_ARG; /* signed sanizizers */ } - else if (offset + length > getDirectBufferLimit(env, input_object)) { + else if ((word32)(offset + length) > + getDirectBufferLimit(env, input_object)) { ret = BUFFER_E; /* buffer overflow check */ } - else if (outputOffset + length > getDirectBufferLimit(env, output_object)) { + else if ((word32)(outputOffset + length) > + getDirectBufferLimit(env, output_object)) { ret = BUFFER_E; /* buffer overflow check */ } else if (opmode == AES_ENCRYPTION) { diff --git a/jni/jni_asn.c b/jni/jni_asn.c index 05fc793..edfc9d8 100644 --- a/jni/jni_asn.c +++ b/jni/jni_asn.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -39,11 +41,13 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Asn_encodeSignature__Ljava_nio byte* encoded = getDirectBufferAddress(env, encoded_object); byte* hash = getDirectBufferAddress(env, hash_object); - if (!encoded || !hash) + if (encoded == NULL || hash == NULL) { throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG); - else + } + else { setDirectBufferLimit(env, encoded_object, - wc_EncodeSignature(encoded, hash, hashSize, hashOID)); + wc_EncodeSignature(encoded, hash, (word32)hashSize, hashOID)); + } } JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_Asn_encodeSignature___3B_3BJI( @@ -54,9 +58,12 @@ JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_Asn_encodeSignature___3B_3BJI byte* hash = getByteArray(env, hash_object); jlong ret = 0; - ret = (!encoded || !hash) - ? BAD_FUNC_ARG - : wc_EncodeSignature(encoded, hash, hashSize, hashOID); + if (encoded == NULL || hash == NULL) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_EncodeSignature(encoded, hash, (word32)hashSize, hashOID); + } releaseByteArray(env, encoded_object, encoded, ret < 0); releaseByteArray(env, hash_object, hash, ret < 0); diff --git a/jni/jni_chacha.c b/jni/jni_chacha.c index 0d963e3..100ff1c 100644 --- a/jni/jni_chacha.c +++ b/jni/jni_chacha.c @@ -20,7 +20,9 @@ */ #include -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_curve25519.c b/jni/jni_curve25519.c index 7794167..a4439bb 100644 --- a/jni/jni_curve25519.c +++ b/jni/jni_curve25519.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_des3.c b/jni/jni_des3.c index 528ecd4..cf9a99c 100644 --- a/jni/jni_des3.c +++ b/jni/jni_des3.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -114,10 +116,12 @@ Java_com_wolfssl_wolfcrypt_Des3_native_1update__I_3BII_3BI( else if (offset < 0 || length < 0 || outputOffset < 0) { ret = BAD_FUNC_ARG; /* signed sanizizers */ } - else if (offset + length > getByteArrayLength(env, input_object)) { + else if ((word32)(offset + length) > + getByteArrayLength(env, input_object)) { ret = BUFFER_E; /* buffer overflow check */ } - else if (outputOffset + length > getByteArrayLength(env, output_object)) { + else if ((word32)(outputOffset + length) > + getByteArrayLength(env, output_object)) { ret = BUFFER_E; /* buffer overflow check */ } else if (opmode == DES_ENCRYPTION) { @@ -179,10 +183,12 @@ Java_com_wolfssl_wolfcrypt_Des3_native_1update__ILjava_nio_ByteBuffer_2IILjava_n else if (offset < 0 || length < 0) { ret = BAD_FUNC_ARG; /* signed sanizizers */ } - else if (offset + length > getDirectBufferLimit(env, input_object)) { + else if ((word32)(offset + length) > + getDirectBufferLimit(env, input_object)) { ret = BUFFER_E; /* buffer overflow check */ } - else if (outputOffset + length > getDirectBufferLimit(env, output_object)) { + else if ((word32)(outputOffset + length) > + getDirectBufferLimit(env, output_object)) { ret = BUFFER_E; /* buffer overflow check */ } else if (opmode == DES_ENCRYPTION) { diff --git a/jni/jni_dh.c b/jni/jni_dh.c index 0a76534..d301a55 100644 --- a/jni/jni_dh.c +++ b/jni/jni_dh.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_ecc.c b/jni/jni_ecc.c index 1dea4d0..c8f77b6 100644 --- a/jni/jni_ecc.c +++ b/jni/jni_ecc.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -755,7 +757,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1verify_1hash( JNIEnv* env, jobject this, jbyteArray hash_object, jbyteArray signature_object) { - jlong ret = 0; + int ret = 0; #ifdef HAVE_ECC_VERIFY int status = 0; @@ -776,9 +778,13 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1verify_1hash( signature = getByteArray(env, signature_object); signatureSz = getByteArrayLength(env, signature_object); - ret = (!ecc || !hash || !signature) - ? BAD_FUNC_ARG - : wc_ecc_verify_hash(signature, signatureSz, hash,hashSz, &status, ecc); + if (ecc == NULL || hash == NULL || signature == NULL) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_ecc_verify_hash(signature, signatureSz, hash, + hashSz, &status, ecc); + } if (ret == 0) { ret = status; @@ -788,7 +794,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1verify_1hash( LogStr( "wc_ecc_verify_hash(sig, sigSz, hash, hashSz, &status, ecc); = %d\n", - (int)ret); + ret); releaseByteArray(env, hash_object, hash, JNI_ABORT); releaseByteArray(env, signature_object, signature, JNI_ABORT); diff --git a/jni/jni_ed25519.c b/jni/jni_ed25519.c index e30e9fb..256e366 100644 --- a/jni/jni_ed25519.c +++ b/jni/jni_ed25519.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_error.c b/jni/jni_error.c index 35c7015..7464623 100644 --- a/jni/jni_error.c +++ b/jni/jni_error.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif diff --git a/jni/jni_feature_detect.c b/jni/jni_feature_detect.c index 08342b2..a8deae8 100644 --- a/jni/jni_feature_detect.c +++ b/jni/jni_feature_detect.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_fips.c b/jni/jni_fips.c index 4cfd38f..1a2f32e 100644 --- a/jni/jni_fips.c +++ b/jni/jni_fips.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif @@ -216,7 +218,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_AesSetKey_1fips__Lcom_wol if (!aes || !key) return BAD_FUNC_ARG; - ret = AesSetKey_fips(aes, key, size, iv, dir); + ret = AesSetKey_fips(aes, key, (word32)size, iv, dir); LogStr("AesSetKey_fips(aes=%p, key, iv, %s) = %d\n", aes, dir ? "dec" : "enc", ret); @@ -252,7 +254,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_AesSetKey_1fips__Lcom_wol iv = getByteArray(env, iv_buffer); ret = (!aes || !key) ? BAD_FUNC_ARG - : AesSetKey_fips(aes, key, size, iv, dir); + : AesSetKey_fips(aes, key, (word32)size, iv, dir); LogStr("AesSetKey_fips(aes=%p, key, iv, %s) = %d\n", aes, dir ? "dec" : "enc", ret); @@ -577,7 +579,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_AesGcmSetKey_1fips__Lcom_ if (!aes || !key) return BAD_FUNC_ARG; - ret = AesGcmSetKey_fips(aes, key, size); + ret = AesGcmSetKey_fips(aes, key, (word32)size); LogStr("AesGcmSetKey_fips(aes=%p, key) = %d\n", aes, ret); LogStr("key[%u]: [%p]\n", (word32)size, key); @@ -608,7 +610,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_AesGcmSetKey_1fips__Lcom_ key = getByteArray(env, key_buffer); ret = (!aes || !key) ? BAD_FUNC_ARG - : AesGcmSetKey_fips(aes, key, size); + : AesGcmSetKey_fips(aes, key, (word32)size); LogStr("AesGcmSetKey_fips(aes=%p, key) = %d\n", aes, ret); LogStr("key[%u]: [%p]\n", (word32)size, key); @@ -1178,7 +1180,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_HmacSetKey_1fips__Lcom_wo if (!hmac || !key) return BAD_FUNC_ARG; - ret = HmacSetKey_fips(hmac, type, key, keySz); + ret = HmacSetKey_fips(hmac, type, key, (word32)keySz); LogStr("HmacSetKey_fips(hmac=%p, type=%d, key, keySz) = %d\n", hmac, type, ret); @@ -1210,7 +1212,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_HmacSetKey_1fips__Lcom_wo key = getByteArray(env, key_buffer); ret = (!hmac || !key) ? BAD_FUNC_ARG - : HmacSetKey_fips(hmac, type, key, keySz); + : HmacSetKey_fips(hmac, type, key, (word32)keySz); LogStr("HmacSetKey_fips(hmac=%p, type=%d, key, keySz) = %d\n", hmac, type, ret); @@ -1246,7 +1248,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_HmacUpdate_1fips__Lcom_wo if (!hmac || !data) return BAD_FUNC_ARG; - ret = HmacUpdate_fips(hmac, data, len); + ret = HmacUpdate_fips(hmac, data, (word32)len); LogStr("HmacUpdate_fips(hmac=%p, data, len) = %d\n", hmac, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -1277,7 +1279,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_HmacUpdate_1fips__Lcom_wo data = getByteArray(env, data_buffer); ret = (!hmac || !data) ? BAD_FUNC_ARG - : HmacUpdate_fips(hmac, data, len); + : HmacUpdate_fips(hmac, data, (word32)len); LogStr("HmacUpdate_fips(hmac=%p, data, len) = %d\n", hmac, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -1422,7 +1424,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RNG_1GenerateBlock_1fips_ if (!rng || !buf) return BAD_FUNC_ARG; - ret = RNG_GenerateBlock_fips(rng, buf, bufSz); + ret = RNG_GenerateBlock_fips(rng, buf, (word32)bufSz); LogStr("RNG_GenerateBlock_fips(rng=%p, buf, bufSz) = %d\n", rng, ret); LogStr("output[%u]: [%p]\n", (word32)bufSz, buf); @@ -1453,7 +1455,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RNG_1GenerateBlock_1fips_ buf = getByteArray(env, buf_buffer); ret = (!rng || !buf) ? BAD_FUNC_ARG - : RNG_GenerateBlock_fips(rng, buf, bufSz); + : RNG_GenerateBlock_fips(rng, buf, (word32)bufSz); LogStr("RNG_GenerateBlock_fips(rng=%p, buf, bufSz) = %d\n", rng, ret); LogStr("output[%u]: [%p]\n", (word32)bufSz, buf); @@ -1482,8 +1484,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RNG_1HealthTest_1fips__IL if (!entropyA || (reseed && !entropyB) || !output) return BAD_FUNC_ARG; - ret = RNG_HealthTest_fips(reseed, entropyA, entropyASz, entropyB, - entropyBSz, output, outputSz); + ret = RNG_HealthTest_fips(reseed, entropyA, (word32)entropyASz, entropyB, + (word32)entropyBSz, output, (word32)outputSz); LogStr("RNG_HealthTest_fips(reseed=%d, entropyA, entropyASz, " "entropyB, entropyBSz, output, outputSz) = %d\n", reseed, ret); @@ -1514,8 +1516,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RNG_1HealthTest_1fips__I_ ret = (!entropyA || (reseed && !entropyB) || !output) ? BAD_FUNC_ARG - : RNG_HealthTest_fips(reseed, entropyA, entropyASz, entropyB, - entropyBSz, output, outputSz); + : RNG_HealthTest_fips(reseed, entropyA, (word32)entropyASz, entropyB, + (word32)entropyBSz, output, (word32)outputSz); LogStr("RNG_HealthTest_fips(reseed=%d, entropyA, entropyASz, " "entropyB, entropyBSz, output, outputSz) = %d\n", reseed, ret); @@ -1625,7 +1627,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaSSL_1Sign_1fips__Ljava if (!in || !out) return BAD_FUNC_ARG; - ret = RsaSSL_Sign_fips(in, inLen, out, outLen, key, rng); + ret = RsaSSL_Sign_fips(in, (word32)inLen, out, (word32)outLen, key, rng); LogStr("RsaSSL_Sign_fips(in, inLen, out, outLen, key=%p, rng=%p) = %d\n", key, rng, ret); @@ -1681,7 +1683,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaSSL_1Sign_1fips___3BJ_ * Providing an rng is optional. RNG_GenerateBlock will return * BAD_FUNC_ARG on a NULL rng if an RNG is needed by RsaPad. */ - ret = RsaSSL_Sign_fips(in, inLen, out, outLen, key, rng); + ret = RsaSSL_Sign_fips(in, (word32)inLen, out, (word32)outLen, + key, rng); } LogStr("RsaSSL_Sign_fips(in, inLen, out, outLen, key=%p, rng=%p) = %d\n", @@ -1729,7 +1732,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaSSL_1Verify_1fips__Lja if (!in || !out) return BAD_FUNC_ARG; - ret = RsaSSL_Verify_fips(in, inLen, out, outLen, key); + ret = RsaSSL_Verify_fips(in, (word32)inLen, out, (word32)outLen, key); LogStr("RsaSSL_Verify_fips(in, inLen, out, outLen, key=%p) = %d\n", key, ret); @@ -1774,7 +1777,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaSSL_1Verify_1fips___3B ret = BAD_FUNC_ARG; } else { - ret = RsaSSL_Verify_fips(in, inLen, out, outLen, key); + ret = RsaSSL_Verify_fips(in, (word32)inLen, out, (word32)outLen, key); LogStr("RsaSSL_Verify_fips(in, inLen, out, outLen, key=%p) = %d\n", key, ret); @@ -1842,9 +1845,11 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPrivateKeyDecode_1fips } #if (HAVE_FIPS_VERSION >= 2) - ret = 0; RsaPrivateKeyDecode(input, (word32*) &tmpIdx, key, inSz); + ret = 0; RsaPrivateKeyDecode(input, (word32*) &tmpIdx, key, + (word32)inSz); #else - ret = 0; RsaPrivateKeyDecode_fips(input, (word32*) &tmpIdx, key, inSz); + ret = 0; RsaPrivateKeyDecode_fips(input, (word32*) &tmpIdx, key, + (word32)inSz); #endif (*env)->SetLongArrayRegion(env, inOutIdx, 0, 1, &tmpIdx); @@ -1888,11 +1893,12 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPrivateKeyDecode_1fips #if (HAVE_FIPS_VERSION >= 2) ret = (!input || !key) ? BAD_FUNC_ARG - : RsaPrivateKeyDecode(input, (word32*) &tmpIdx, key, inSz); + : RsaPrivateKeyDecode(input, (word32*) &tmpIdx, key, (word32)inSz); #else ret = (!input || !key) ? BAD_FUNC_ARG - : RsaPrivateKeyDecode_fips(input, (word32*) &tmpIdx, key, inSz); + : RsaPrivateKeyDecode_fips(input, (word32*) &tmpIdx, key, + (word32)inSz); #endif (*env)->SetLongArrayRegion(env, inOutIdx, 0, 1, &tmpIdx); @@ -1936,9 +1942,10 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPublicKeyDecode_1fips_ } #if (HAVE_FIPS_VERSION >= 2) - ret = RsaPublicKeyDecode(input, (word32*) &tmpIdx, key, inSz); + ret = RsaPublicKeyDecode(input, (word32*) &tmpIdx, key, (word32)inSz); #else - ret = RsaPublicKeyDecode_fips(input, (word32*) &tmpIdx, key, inSz); + ret = RsaPublicKeyDecode_fips(input, (word32*) &tmpIdx, key, + (word32)inSz); #endif (*env)->SetLongArrayRegion(env, inOutIdx, 0, 1, &tmpIdx); @@ -1980,11 +1987,12 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPublicKeyDecode_1fips_ #if (HAVE_FIPS_VERSION >= 2) ret = (!input) ? BAD_FUNC_ARG - : RsaPublicKeyDecode(input, (word32*) &tmpIdx, key, inSz); + : RsaPublicKeyDecode(input, (word32*) &tmpIdx, key, (word32)inSz); #else ret = (!input) ? BAD_FUNC_ARG - : RsaPublicKeyDecode_fips(input, (word32*) &tmpIdx, key, inSz); + : RsaPublicKeyDecode_fips(input, (word32*) &tmpIdx, key, + (word32)inSz); #endif (*env)->SetLongArrayRegion(env, inOutIdx, 0, 1, &tmpIdx); @@ -2046,7 +2054,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_ShaUpdate_1fips__Lcom_wol if (!data) return BAD_FUNC_ARG; - ret = ShaUpdate_fips(sha, data, len); + ret = ShaUpdate_fips(sha, data, (word32)len); LogStr("ShaUpdate_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2076,7 +2084,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_ShaUpdate_1fips__Lcom_wol data = getByteArray(env, data_buffer); ret = (!data) ? BAD_FUNC_ARG - : ShaUpdate_fips(sha, data, len); + : ShaUpdate_fips(sha, data, (word32)len); LogStr("ShaUpdate_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2191,7 +2199,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Sha256Update_1fips__Lcom_ if (!data) return BAD_FUNC_ARG; - ret = Sha256Update_fips(sha, data, len); + ret = Sha256Update_fips(sha, data, (word32)len); LogStr("Sha256Update_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2221,7 +2229,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Sha256Update_1fips__Lcom_ data = getByteArray(env, data_buffer); ret = (!data) ? BAD_FUNC_ARG - : Sha256Update_fips(sha, data, len); + : Sha256Update_fips(sha, data, (word32)len); LogStr("Sha256Update_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2335,7 +2343,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Sha384Update_1fips__Lcom_ if (!data) return BAD_FUNC_ARG; - ret = Sha384Update_fips(sha, data, len); + ret = Sha384Update_fips(sha, data, (word32)len); LogStr("Sha384Update_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2364,7 +2372,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Sha384Update_1fips__Lcom_ data = getByteArray(env, data_buffer); ret = (!data) ? BAD_FUNC_ARG - : Sha384Update_fips(sha, data, len); + : Sha384Update_fips(sha, data, (word32)len); LogStr("Sha384Update_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2478,7 +2486,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Sha512Update_1fips__Lcom_ if (!data) return BAD_FUNC_ARG; - ret = Sha512Update_fips(sha, data, len); + ret = Sha512Update_fips(sha, data, (word32)len); LogStr("Sha512Update_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2507,7 +2515,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Sha512Update_1fips__Lcom_ data = getByteArray(env, data_buffer); ret = (!data) ? BAD_FUNC_ARG - : Sha512Update_fips(sha, data, len); + : Sha512Update_fips(sha, data, (word32)len); LogStr("Sha512Update_fips(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -2645,7 +2653,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPublicEncrypt_1fips__L if (!in || !out) return BAD_FUNC_ARG; - ret = RsaPublicEncrypt_fips(in, inLen, out, outLen, key, rng); + ret = RsaPublicEncrypt_fips(in, (word32)inLen, out, (word32)outLen, + key, rng); LogStr( "RsaPublicEncrypt_fips(in, inLen, out, outLen, key=%p, rng=%p) = %d\n", @@ -2692,7 +2701,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPublicEncrypt_1fips___ */ ret = (!in || !out) ? BAD_FUNC_ARG - : RsaPublicEncrypt_fips(in, inLen, out, outLen, key, rng); + : RsaPublicEncrypt_fips(in, (word32)inLen, out, (word32)outLen, + key, rng); LogStr( "RsaPublicEncrypt_fips(in, inLen, out, outLen, key=%p, rng=%p) = %d\n", @@ -2733,7 +2743,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPrivateDecrypt_1fips__ if (!in || !out) return BAD_FUNC_ARG; - ret = RsaPrivateDecrypt_fips(in, inLen, out, outLen, key); + ret = RsaPrivateDecrypt_fips(in, (word32)inLen, out, (word32)outLen, key); LogStr("RsaPrivateDecrypt_fips(in, inLen, out, outLen, key=%p) = %d\n", key, ret); @@ -2769,7 +2779,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPrivateDecrypt_1fips__ ret = (!in || !out) ? BAD_FUNC_ARG - : RsaPrivateDecrypt_fips(in, inLen, out, outLen, key); + : RsaPrivateDecrypt_fips(in, (word32)inLen, out, (word32)outLen, key); LogStr("RsaPrivateDecrypt_fips(in, inLen, out, outLen, key=%p) = %d\n", key, ret); @@ -2830,7 +2840,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Md5Update__Lcom_wolfssl_w if (!data) return BAD_FUNC_ARG; - Md5Update(md5, data, len); + Md5Update(md5, data, (word32)len); ret = com_wolfssl_wolfcrypt_WolfCrypt_SUCCESS; LogStr("Md5Update_fips(md5=%p, data, len) = %d\n", md5, ret); @@ -2862,7 +2872,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Md5Update__Lcom_wolfssl_w if (!data) ret = BAD_FUNC_ARG; else { - Md5Update(md5, data, len); + Md5Update(md5, data, (word32)len); ret = com_wolfssl_wolfcrypt_WolfCrypt_SUCCESS; } @@ -3139,7 +3149,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhAgree__Lcom_wolfssl_wol return BAD_FUNC_ARG; } - ret = DhAgree(key, agree, (word32*) &tmpAgreeSz, priv, privSz, pub, pubSz); + ret = DhAgree(key, agree, (word32*) &tmpAgreeSz, priv, (word32)privSz, + pub, (word32)pubSz); (*env)->SetLongArrayRegion(env, agreeSz, 0, 1, &tmpAgreeSz); @@ -3188,7 +3199,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhAgree__Lcom_wolfssl_wol ret = (!key || !agree || !priv || !pub) ? BAD_FUNC_ARG - : DhAgree(key, agree, (word32*) &tmpAgreeSz, priv, privSz, pub, pubSz); + : DhAgree(key, agree, (word32*) &tmpAgreeSz, priv, (word32)privSz, + pub, (word32)pubSz); (*env)->SetLongArrayRegion(env, agreeSz, 0, 1, &tmpAgreeSz); @@ -3236,7 +3248,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhKeyDecode__Ljava_nio_By return BAD_FUNC_ARG; } - ret = DhKeyDecode(input, (word32*) &tmpInOutIdx, key, inSz); + ret = DhKeyDecode(input, (word32*) &tmpInOutIdx, key, (word32)inSz); (*env)->SetLongArrayRegion(env, inOutIdx, 0, 1, &tmpInOutIdx); @@ -3274,7 +3286,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhKeyDecode___3B_3JLcom_w input = getByteArray(env, input_buffer); ret = (!input) ? BAD_FUNC_ARG - : DhKeyDecode(input, (word32*) &tmpInOutIdx, key, inSz); + : DhKeyDecode(input, (word32*) &tmpInOutIdx, key, (word32)inSz); (*env)->SetLongArrayRegion(env, inOutIdx, 0, 1, &tmpInOutIdx); @@ -3312,7 +3324,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhSetKey__Lcom_wolfssl_wo if (!p || !g) return BAD_FUNC_ARG; - ret = DhSetKey(key, p, pSz, g, gSz); + ret = DhSetKey(key, p, (word32)pSz, g, (word32)gSz); LogStr("DhSetKey(key=%p, p, pSz, g, gSz) = %d\n", key, ret); LogStr("p[%u]: [%p]\n", (word32)pSz, p); @@ -3347,7 +3359,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhSetKey__Lcom_wolfssl_wo ret = (!p || !g) ? BAD_FUNC_ARG - : DhSetKey(key, p, pSz, g, gSz); + : DhSetKey(key, p, (word32)pSz, g, (word32)gSz); LogStr("DhSetKey(key=%p, p, pSz, g, gSz) = %d\n", key, ret); LogStr("p[%u]: [%p]\n", (word32)pSz, p); @@ -3390,8 +3402,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhParamsLoad__Ljava_nio_B return BAD_FUNC_ARG; } - ret = DhParamsLoad(input, inSz, p, (word32*) &tmpPInOutSz, - g, (word32*) &tmpGInOutSz); + ret = DhParamsLoad(input, (word32)inSz, p, (word32*) &tmpPInOutSz, + g, (word32*) &tmpGInOutSz); (*env)->SetLongArrayRegion(env, pInOutSz, 0, 1, &tmpPInOutSz); if ((*env)->ExceptionOccurred(env)) { @@ -3443,8 +3455,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhParamsLoad___3BJ_3B_3J_ ret = (!input || !p || !g) ? BAD_FUNC_ARG - : DhParamsLoad(input, inSz, p, (word32*) &tmpPInOutSz, - g, (word32*) &tmpGInOutSz); + : DhParamsLoad(input, (word32)inSz, p, (word32*) &tmpPInOutSz, + g, (word32*) &tmpGInOutSz); (*env)->SetLongArrayRegion(env, pInOutSz, 0, 1, &tmpPInOutSz); if ((*env)->ExceptionOccurred(env)) { @@ -3473,8 +3485,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhParamsLoad___3BJ_3B_3J_ return ret; } -JNIEXPORT int JNICALL Java_com_wolfssl_wolfcrypt_Fips_ecc_1init( - JNIEnv *env, jclass class, jobject key_object) +JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_ecc_1init + (JNIEnv* env, jclass class, jobject key_object) { jint ret = NOT_COMPILED_IN; @@ -3660,7 +3672,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_ecc_1import_1x963__Ljava_ if (!in) return BAD_FUNC_ARG; - ret = ecc_import_x963(in, inLen, key); + ret = ecc_import_x963(in, (word32)inLen, key); LogStr("ecc_import_x963(in, inLen, key=%p) = %d\n", key, ret); LogStr("in[%u]: [%p]\n", (word32)inLen, in); @@ -3690,7 +3702,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_ecc_1import_1x963___3BJLc in = getByteArray(env, in_buffer); ret = (!in) ? BAD_FUNC_ARG - : ecc_import_x963(in, inLen, key); + : ecc_import_x963(in, (word32)inLen, key); LogStr("ecc_import_x963(in, inLen, key=%p) = %d\n", key, ret); LogStr("in[%u]: [%p]\n", (word32)inLen, in); diff --git a/jni/jni_hmac.c b/jni/jni_hmac.c index 2ebbeb4..d49913a 100644 --- a/jni/jni_hmac.c +++ b/jni/jni_hmac.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_logging.c b/jni/jni_logging.c index b4b9d8d..1f95fe8 100644 --- a/jni/jni_logging.c +++ b/jni/jni_logging.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_md5.c b/jni/jni_md5.c index 4a765bb..72d791a 100644 --- a/jni/jni_md5.c +++ b/jni/jni_md5.c @@ -20,7 +20,9 @@ */ #include -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -175,7 +177,8 @@ Java_com_wolfssl_wolfcrypt_Md5_native_1update___3BII( data = getByteArray(env, data_buffer); dataSz = getByteArrayLength(env, data_buffer); - if (!md5 || !data || (offset + len > dataSz)) { + if (md5 == NULL || data == NULL || + ((word32)(offset + len) > dataSz)) { throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG); } else { ret = wc_Md5Update(md5, data + offset, len); diff --git a/jni/jni_native_struct.c b/jni/jni_native_struct.c index 25de609..233ca96 100644 --- a/jni/jni_native_struct.c +++ b/jni/jni_native_struct.c @@ -20,7 +20,9 @@ */ #include -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -32,12 +34,14 @@ /* #define WOLFCRYPT_JNI_DEBUG_ON */ #include -#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" +#ifndef USE_WINDOWS_API + #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" +#endif JavaVM* g_vm = NULL; /* called when native library is loaded */ -jint JNI_OnLoad(JavaVM* vm, void* reserved) +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) { /* store JavaVM */ g_vm = vm; diff --git a/jni/jni_rng.c b/jni/jni_rng.c index 98b004b..fbbd903 100644 --- a/jni/jni_rng.c +++ b/jni/jni_rng.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/jni/jni_rsa.c b/jni/jni_rsa.c index eec1390..af7968a 100644 --- a/jni/jni_rsa.c +++ b/jni/jni_rsa.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -78,9 +80,12 @@ Java_com_wolfssl_wolfcrypt_Rsa_MakeRsaKey( return; } - ret = (!key || !rng) - ? BAD_FUNC_ARG - : wc_MakeRsaKey(key, size, e, rng); + if (key == NULL || rng == NULL) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_MakeRsaKey(key, size, (long)e, rng); + } if (ret != 0) throwWolfCryptExceptionFromError(env, ret); @@ -111,9 +116,12 @@ Java_com_wolfssl_wolfcrypt_Rsa_wc_1RsaPublicKeyDecodeRaw__Ljava_nio_ByteBuffer_2 n = getDirectBufferAddress(env, n_object); e = getDirectBufferAddress(env, e_object); - ret = (!key || !n || !e) - ? BAD_FUNC_ARG - : wc_RsaPublicKeyDecodeRaw(n, nSize, e, eSize, key); + if (key == NULL || n == NULL || e == NULL) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_RsaPublicKeyDecodeRaw(n, (long)nSize, e, (long)eSize, key); + } if (ret != 0) throwWolfCryptExceptionFromError(env, ret); @@ -148,9 +156,12 @@ Java_com_wolfssl_wolfcrypt_Rsa_wc_1RsaPublicKeyDecodeRaw___3BJ_3BJ( n = getByteArray(env, n_object); e = getByteArray(env, e_object); - ret = (!key || !n || !e) - ? BAD_FUNC_ARG - : wc_RsaPublicKeyDecodeRaw(n, nSize, e, eSize, key); + if (key == NULL || n == NULL || e == NULL) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_RsaPublicKeyDecodeRaw(n, (long)nSize, e, (long)eSize, key); + } if (ret != 0) throwWolfCryptExceptionFromError(env, ret); diff --git a/jni/jni_sha.c b/jni/jni_sha.c index f666c27..7119a02 100644 --- a/jni/jni_sha.c +++ b/jni/jni_sha.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include @@ -248,9 +250,13 @@ Java_com_wolfssl_wolfcrypt_Sha_native_1update___3BII( data = getByteArray(env, data_buffer); dataSz = getByteArrayLength(env, data_buffer); - ret = (!sha || !data || ((offset + len) > dataSz)) - ? BAD_FUNC_ARG - : wc_ShaUpdate(sha, data + offset, len); + if (sha == NULL || data == NULL || + (word32)(offset + len) > dataSz) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_ShaUpdate(sha, data + offset, len); + } if (ret != 0) throwWolfCryptExceptionFromError(env, ret); @@ -439,12 +445,17 @@ Java_com_wolfssl_wolfcrypt_Sha256_native_1update___3BII( data = getByteArray(env, data_buffer); dataSz = getByteArrayLength(env, data_buffer); - ret = (!sha || !data || ((offset + len) > dataSz)) - ? BAD_FUNC_ARG - : wc_Sha256Update(sha, data + offset, len); + if (sha == NULL || data == NULL || + (word32)(offset + len) > dataSz) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_Sha256Update(sha, data + offset, len); + } - if (ret != 0) + if (ret != 0) { throwWolfCryptExceptionFromError(env, ret); + } LogStr("wc_Sha256Update(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data); @@ -630,12 +641,17 @@ Java_com_wolfssl_wolfcrypt_Sha384_native_1update___3BII( data = getByteArray(env, data_buffer); dataSz = getByteArrayLength(env, data_buffer); - ret = (!sha || !data || ((offset + len) > dataSz)) - ? BAD_FUNC_ARG - : wc_Sha384Update(sha, data + offset, len); + if (sha == NULL || data == NULL || + (word32)(offset + len) > dataSz) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_Sha384Update(sha, data + offset, len); + } - if (ret != 0) + if (ret != 0) { throwWolfCryptExceptionFromError(env, ret); + } LogStr("wc_Sha384Update(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data + offset); @@ -821,12 +837,17 @@ Java_com_wolfssl_wolfcrypt_Sha512_native_1update___3BII( data = getByteArray(env, data_buffer); dataSz = getByteArrayLength(env, data_buffer); - ret = (!sha || !data || ((offset + len) > dataSz)) - ? BAD_FUNC_ARG - : wc_Sha512Update(sha, data + offset, len); + if (sha == NULL || data == NULL || + (word32)(offset + len) > dataSz) { + ret = BAD_FUNC_ARG; + } + else { + ret = wc_Sha512Update(sha, data + offset, len); + } - if (ret != 0) + if (ret != 0) { throwWolfCryptExceptionFromError(env, ret); + } LogStr("wc_Sha512Update(sha=%p, data, len) = %d\n", sha, ret); LogStr("data[%u]: [%p]\n", (word32)len, data + offset); diff --git a/jni/jni_wolfobject.c b/jni/jni_wolfobject.c index 1687d58..3948194 100644 --- a/jni/jni_wolfobject.c +++ b/jni/jni_wolfobject.c @@ -19,7 +19,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __ANDROID__ +#ifdef WOLFSSL_USER_SETTINGS + #include +#elif !defined(__ANDROID__) #include #endif #include diff --git a/src/main/java/com/wolfssl/provider/jce/WolfCryptCipher.java b/src/main/java/com/wolfssl/provider/jce/WolfCryptCipher.java index 74e3ea3..74dcd62 100644 --- a/src/main/java/com/wolfssl/provider/jce/WolfCryptCipher.java +++ b/src/main/java/com/wolfssl/provider/jce/WolfCryptCipher.java @@ -480,6 +480,11 @@ public class WolfCryptCipher extends CipherSpi { if (input == null || len < 0) throw new IllegalArgumentException("Null input buffer or len < 0"); + if ((buffered.length + len) == 0) { + /* no data to process */ + return null; + } + if ((cipherType == CipherType.WC_RSA) || ((buffered.length + len) < blockSize)) { /* buffer for short inputs, or RSA */ diff --git a/src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java b/src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java index 73c41d7..628637b 100644 --- a/src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java +++ b/src/main/java/com/wolfssl/wolfcrypt/FeatureDetect.java @@ -62,7 +62,37 @@ public class FeatureDetect { */ public static native boolean Sha512Enabled(); + /** + * Loads JNI library. + * + * The native library is expected to be called "wolfcryptjni", and must be + * on the system library search path. + * + * "wolfcryptjni" links against the wolfSSL native C library ("wolfssl"), + * and for Windows compatibility "wolfssl" needs to be explicitly loaded + * first here. + */ static { + int fipsLoaded = 0; + + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.contains("win")) { + try { + /* Default wolfCrypt FIPS library on Windows is compiled + * as "wolfssl-fips" by Visual Studio solution */ + System.loadLibrary("wolfssl-fips"); + fipsLoaded = 1; + } catch (UnsatisfiedLinkError e) { + /* wolfCrypt FIPS not available */ + } + + if (fipsLoaded == 0) { + /* FIPS library not loaded, try normal libwolfssl */ + System.loadLibrary("wolfssl"); + } + } + + /* Load wolfcryptjni library */ System.loadLibrary("wolfcryptjni"); } diff --git a/src/main/java/com/wolfssl/wolfcrypt/WolfObject.java b/src/main/java/com/wolfssl/wolfcrypt/WolfObject.java index d0e9aef..4d5918b 100644 --- a/src/main/java/com/wolfssl/wolfcrypt/WolfObject.java +++ b/src/main/java/com/wolfssl/wolfcrypt/WolfObject.java @@ -29,7 +29,37 @@ public class WolfObject { private static native int init(); + /** + * Loads JNI library. + * + * The native library is expected to be called "wolfcryptjni", and must be + * on the system library search path. + * + * "wolfcryptjni" links against the wolfSSL native C library ("wolfssl"), + * and for Windows compatibility "wolfssl" needs to be explicitly loaded + * first here. + */ static { + int fipsLoaded = 0; + + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.contains("win")) { + try { + /* Default wolfCrypt FIPS library on Windows is compiled + * as "wolfssl-fips" by Visual Studio solution */ + System.loadLibrary("wolfssl-fips"); + fipsLoaded = 1; + } catch (UnsatisfiedLinkError e) { + /* wolfCrypt FIPS not available */ + } + + if (fipsLoaded == 0) { + /* FIPS library not loaded, try normal libwolfssl */ + System.loadLibrary("wolfssl"); + } + } + + /* Load wolfcryptjni library */ System.loadLibrary("wolfcryptjni"); /* initialize native wolfCrypt library */