Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ccc0a17
JNI: fix Ed25519 verify siglen bug, add Ed25519 JUnit tests
cconlon Mar 19, 2026
b1639dd
JNI: fix jlong to word32 pointer cast in RsaFlattenPublicKey, add test
cconlon Mar 19, 2026
78da0e7
JNI: add NULL check before hmac->macType dereference in HmacFinal
cconlon Mar 19, 2026
ee81daa
JNI: fix jlong to word32 pointer cast in RsaExportCrtKey, add test
cconlon Mar 19, 2026
96d94cd
JNI: add missing releaseByteArray calls in ecc_get_curve_id_from_params
cconlon Mar 19, 2026
1cf84b5
JNI: add missing releaseByteArray call in RsaPrivateKeyDecodePKCS8
cconlon Mar 19, 2026
f5a4568
JNI: fix missing releaseByteArray on early return in RsaFlattenPublicKey
cconlon Mar 19, 2026
ed03ba2
JNI: add missing releaseByteArray in AesGcmSetExtIV_fips early return
cconlon Mar 19, 2026
aa55714
JNI: add missing releaseByteArray call in RsaPublicKeyDecode
cconlon Mar 19, 2026
23d46d8
JNI: add bounds validation for offset/length in HmacUpdate, add tests
cconlon Mar 19, 2026
fbf0a75
JNI: add bounds validation for offset/length in HmacUpdate ByteBuffer…
cconlon Mar 19, 2026
0930036
JNI: add NULL checks for array params in PKCS12_PBKDF and PBKDF2
cconlon Mar 19, 2026
234bfa5
JNI: fix wrong error code in HmacFinal hash size check, pass hmacSz n…
cconlon Mar 19, 2026
6bd63e3
JNI: fix unsigned comparison for wc_RsaEncryptSize return in 8 RSA fu…
cconlon Mar 19, 2026
7c9dd08
JNI: add missing releaseByteArray calls in Chacha_process
cconlon Mar 19, 2026
9268cdd
JNI: add NULL checks before ed25519 key dereference in export functions
cconlon Mar 19, 2026
afa379a
JNI: add NULL checks before curve25519 key dereference in export func…
cconlon Mar 19, 2026
85b3662
JCE: return defensive copy of IV in engineGetIV to prevent internal s…
cconlon Mar 19, 2026
280995a
JNI: add missing releaseNativeStruct calls in RSA test cleanup paths
cconlon Mar 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jni/include/com_wolfssl_wolfcrypt_Curve25519.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions jni/include/com_wolfssl_wolfcrypt_Ed25519.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions jni/jni_chacha.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Java_com_wolfssl_wolfcrypt_Chacha_wc_1Chacha_1process(
if (ret == 0) {
output = (byte*)XMALLOC(inputSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (output == NULL) {
releaseByteArray(env, input_obj, input, JNI_ABORT);
throwOutOfMemoryException(env, "Failed to allocate key buffer");
return result;
}
Expand All @@ -188,6 +189,7 @@ Java_com_wolfssl_wolfcrypt_Chacha_wc_1Chacha_1process(
}

LogStr("wc_Chacha_Process(): output = %p, ret = %d\n", output, ret);
releaseByteArray(env, input_obj, input, JNI_ABORT);
XFREE(output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#else
throwNotCompiledInException(env);
Expand Down
29 changes: 19 additions & 10 deletions jni/jni_curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ Java_com_wolfssl_wolfcrypt_Curve25519_wc_1curve25519_1export_1private(
return NULL;
}

if (curve25519 == NULL) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return NULL;
}

outputSz = wc_curve25519_size(curve25519);

output = XMALLOC(outputSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
Expand All @@ -287,9 +292,7 @@ Java_com_wolfssl_wolfcrypt_Curve25519_wc_1curve25519_1export_1private(
}
XMEMSET(output, 0, outputSz);

ret = (!curve25519)
? BAD_FUNC_ARG
: wc_curve25519_export_private_raw(curve25519, output, &outputSz);
ret = wc_curve25519_export_private_raw(curve25519, output, &outputSz);

if (ret == 0) {
result = (*env)->NewByteArray(env, outputSz);
Expand Down Expand Up @@ -334,6 +337,11 @@ Java_com_wolfssl_wolfcrypt_Curve25519_wc_1curve25519_1export_1public (
return NULL;
}

if (curve25519 == NULL) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return NULL;
}

outputSz = wc_curve25519_size(curve25519);

output = XMALLOC(outputSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
Expand All @@ -343,9 +351,7 @@ Java_com_wolfssl_wolfcrypt_Curve25519_wc_1curve25519_1export_1public (
}
XMEMSET(output, 0, outputSz);

ret = (!curve25519)
? BAD_FUNC_ARG
: wc_curve25519_export_public(curve25519, output, &outputSz);
ret = wc_curve25519_export_public(curve25519, output, &outputSz);

if (ret == 0) {
result = (*env)->NewByteArray(env, outputSz);
Expand Down Expand Up @@ -397,18 +403,21 @@ Java_com_wolfssl_wolfcrypt_Curve25519_wc_1curve25519_1make_1shared_1secret(
return NULL;
}

if (curve25519 == NULL || pub == NULL) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return NULL;
}

outputSz = wc_curve25519_size(curve25519);
output = XMALLOC(outputSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (output == NULL) {
throwOutOfMemoryException(env,
"Failed to allocate shared secret buffer");
"Failed to allocate shared secret buffer");
return result;
}
XMEMSET(output, 0, outputSz);

ret = (!curve25519 || !pub)
? BAD_FUNC_ARG
: wc_curve25519_shared_secret(curve25519, pub, output, &outputSz);
ret = wc_curve25519_shared_secret(curve25519, pub, output, &outputSz);

if (ret == 0) {
result = (*env)->NewByteArray(env, outputSz);
Expand Down
7 changes: 7 additions & 0 deletions jni/jni_ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,13 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1get_1curve_1id_1f
}

LogStr("wc_ecc_get_curve_id_from_params() = %d\n", ret);

releaseByteArray(env, prime_object, prime, JNI_ABORT);
releaseByteArray(env, af_object, Af, JNI_ABORT);
releaseByteArray(env, bf_object, Bf, JNI_ABORT);
releaseByteArray(env, order_object, order, JNI_ABORT);
releaseByteArray(env, gx_object, Gx, JNI_ABORT);
releaseByteArray(env, gy_object, Gy, JNI_ABORT);
#else
throwNotCompiledInException(env);
#endif
Expand Down
30 changes: 19 additions & 11 deletions jni/jni_ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1export_1private(
return NULL;
}

if (ed25519 == NULL) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return NULL;
}

outputSz = 2 * wc_ed25519_priv_size(ed25519); /* Export private + public */

output = XMALLOC(outputSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
Expand All @@ -312,9 +317,7 @@ Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1export_1private(
}
XMEMSET(output, 0, outputSz);

ret = (!ed25519)
? BAD_FUNC_ARG
: wc_ed25519_export_private(ed25519, output, &outputSz);
ret = wc_ed25519_export_private(ed25519, output, &outputSz);

if (ret == 0) {
result = (*env)->NewByteArray(env, outputSz);
Expand Down Expand Up @@ -359,6 +362,11 @@ Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1export_1private_1only(
return NULL;
}

if (ed25519 == NULL) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return NULL;
}

outputSz = wc_ed25519_size(ed25519);

output = XMALLOC(outputSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
Expand All @@ -368,9 +376,7 @@ Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1export_1private_1only(
}
XMEMSET(output, 0, outputSz);

ret = (!ed25519)
? BAD_FUNC_ARG
: wc_ed25519_export_private_only(ed25519, output, &outputSz);
ret = wc_ed25519_export_private_only(ed25519, output, &outputSz);

if (ret == 0) {
result = (*env)->NewByteArray(env, outputSz);
Expand Down Expand Up @@ -415,6 +421,11 @@ Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1export_1public(
return NULL;
}

if (ed25519 == NULL) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return NULL;
}

outputSz = wc_ed25519_size(ed25519);

output = XMALLOC(outputSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
Expand All @@ -424,9 +435,7 @@ Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1export_1public(
}
XMEMSET(output, 0, outputSz);

ret = (!ed25519)
? BAD_FUNC_ARG
: wc_ed25519_export_public(ed25519, output, &outputSz);
ret = wc_ed25519_export_public(ed25519, output, &outputSz);

if (ret == 0) {
result = (*env)->NewByteArray(env, outputSz);
Expand Down Expand Up @@ -504,7 +513,6 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1sig
}

LogStr("wc_ed25519_sign_msg(ed25519=%p) = %d\n", ed25519, ret);
printf("wc_ed25519_sign_msg(ed25519=%p) = %d\n", ed25519, ret);
XFREE(output, NULL, DYNAMIC_TYPE_TMP_BUFFER);

releaseByteArray(env, msg_in, msg, JNI_ABORT);
Expand Down Expand Up @@ -535,7 +543,7 @@ JNIEXPORT jboolean JNICALL Java_com_wolfssl_wolfcrypt_Ed25519_wc_1ed25519_1verif
sig = getByteArray(env, sig_in);
msg = getByteArray(env, msg_in);
msglen = getByteArrayLength(env, msg_in);
siglen = getByteArrayLength(env, msg_in);
siglen = getByteArrayLength(env, sig_in);

if (!ed25519) {
ret = BAD_FUNC_ARG;
Expand Down
1 change: 1 addition & 0 deletions jni/jni_fips.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_wc_1AesGcmSetExtIV_1fips_
iv = getByteArray(env, iv_buffer);

if (aes == NULL || iv == NULL || size < 0) {
releaseByteArray(env, iv_buffer, iv, JNI_ABORT);
return BAD_FUNC_ARG;
}

Expand Down
39 changes: 27 additions & 12 deletions jni/jni_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,24 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate___3BII
int ret = 0;
Hmac* hmac = NULL;
byte* data = NULL;
word32 dataSz = 0;

hmac = (Hmac*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
/* getNativeStruct may throw exception, prevent throwing another */
return;
}

data = getByteArray(env, data_object);
data = getByteArray(env, data_object);
dataSz = getByteArrayLength(env, data_object);

ret = (!hmac || !data)
? BAD_FUNC_ARG
: wc_HmacUpdate(hmac, data + offset, length);
if (!hmac || !data || offset < 0 || length < 0 ||
((word32)offset + (word32)length) > dataSz) {
ret = BAD_FUNC_ARG;
}
else {
ret = wc_HmacUpdate(hmac, data + offset, length);
}
Comment thread
cconlon marked this conversation as resolved.

if (ret != 0)
throwWolfCryptExceptionFromError(env, ret);
Expand All @@ -244,6 +250,7 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__Ljava_nio
int ret = 0;
Hmac* hmac = NULL;
byte* data = NULL;
jlong dataSz = 0;

hmac = (Hmac*) getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
Expand All @@ -252,10 +259,15 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacUpdate__Ljava_nio
}

data = getDirectBufferAddress(env, data_object);
dataSz = (*env)->GetDirectBufferCapacity(env, data_object);

ret = (!hmac || !data)
? BAD_FUNC_ARG
: wc_HmacUpdate(hmac, data + offset, length);
if (!hmac || !data || offset < 0 || length < 0 ||
((jlong)offset + (jlong)length) > dataSz) {
ret = BAD_FUNC_ARG;
}
else {
ret = wc_HmacUpdate(hmac, data + offset, length);
}
Comment thread
cconlon marked this conversation as resolved.

if (ret != 0)
throwWolfCryptExceptionFromError(env, ret);
Expand Down Expand Up @@ -284,17 +296,20 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_Hmac_wc_1HmacFinal
/* getNativeStruct may throw exception, prevent throwing another */
return NULL;
}

if (!hmac) {
throwWolfCryptExceptionFromError(env, BAD_FUNC_ARG);
return result;
}

hmacSz = GetHashSizeByType(hmac->macType);

if (hmacSz < 0) {
throwWolfCryptExceptionFromError(env, ret);
throwWolfCryptExceptionFromError(env, hmacSz);
return result;
}

ret = (!hmac)
? BAD_FUNC_ARG
: wc_HmacFinal(hmac, tmp);

ret = wc_HmacFinal(hmac, tmp);
if (ret == 0) {
result = (*env)->NewByteArray(env, hmacSz);

Expand Down
25 changes: 18 additions & 7 deletions jni/jni_pwdbased.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_Pwdbased_wc_1PKCS12_1PBK
}
XMEMSET(outKey, 0, kLen);

pass = (byte*)(*env)->GetByteArrayElements(env, passBuf, NULL);
salt = (byte*)(*env)->GetByteArrayElements(env, saltBuf, NULL);
if (passBuf != NULL) {
pass = (byte*)(*env)->GetByteArrayElements(env, passBuf, NULL);
}
if (saltBuf != NULL) {
salt = (byte*)(*env)->GetByteArrayElements(env, saltBuf, NULL);
}

ret = wc_PKCS12_PBKDF(outKey, pass, passBufLen, salt, sBufLen,
iterations, kLen, typeH, id);
Expand All @@ -80,8 +84,12 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_Pwdbased_wc_1PKCS12_1PBK
XFREE(outKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}

(*env)->ReleaseByteArrayElements(env, passBuf, (jbyte*)pass, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, saltBuf, (jbyte*)salt, JNI_ABORT);
if (pass != NULL) {
(*env)->ReleaseByteArrayElements(env, passBuf, (jbyte*)pass, JNI_ABORT);
}
if (salt != NULL) {
(*env)->ReleaseByteArrayElements(env, saltBuf, (jbyte*)salt, JNI_ABORT);
}

if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
Expand Down Expand Up @@ -133,7 +141,9 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_Pwdbased_wc_1PBKDF2
pass = (byte*)(*env)->GetByteArrayElements(env, passBuf, NULL);
}

salt = (byte*)(*env)->GetByteArrayElements(env, saltBuf, NULL);
if (saltBuf != NULL) {
salt = (byte*)(*env)->GetByteArrayElements(env, saltBuf, NULL);
}

ret = wc_PBKDF2(outKey, pass, passBufLen, salt, sBufLen,
iterations, kLen, hashType);
Expand All @@ -156,8 +166,9 @@ JNIEXPORT jbyteArray JNICALL Java_com_wolfssl_wolfcrypt_Pwdbased_wc_1PBKDF2
if (pass != NULL) {
(*env)->ReleaseByteArrayElements(env, passBuf, (jbyte*)pass, JNI_ABORT);
}

(*env)->ReleaseByteArrayElements(env, saltBuf, (jbyte*)salt, JNI_ABORT);
if (salt != NULL) {
(*env)->ReleaseByteArrayElements(env, saltBuf, (jbyte*)salt, JNI_ABORT);
}

if (ret != 0) {
throwWolfCryptExceptionFromError(env, ret);
Expand Down
Loading
Loading