F-5225: validate ChaCha IV length before native SetIV read

pull/264/head
Chris Conlon 2026-08-20 16:47:19 -06:00
parent b8392ced50
commit 39974a4f62
2 changed files with 19 additions and 1 deletions

View File

@ -72,6 +72,7 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Chacha_wc_1Chacha_1setIV
int ret = 0;
ChaCha* chacha = NULL;
byte* iv = NULL;
word32 ivSz = 0;
chacha = (ChaCha*)(uintptr_t)getNativeStruct(env, this);
if ((*env)->ExceptionOccurred(env)) {
@ -79,8 +80,9 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Chacha_wc_1Chacha_1setIV
return;
}
iv = getByteArray(env, iv_object);
ivSz = getByteArrayLength(env, iv_object);
if (chacha == NULL || iv == NULL) {
if (chacha == NULL || iv == NULL || ivSz != CHACHA_IV_BYTES) {
ret = BAD_FUNC_ARG;
}

View File

@ -132,6 +132,22 @@ public class ChachaTest {
/* test must throw */
}
/* IV shorter than 12 bytes should be rejected */
try {
chacha.setIV(new byte[4]);
fail("IV shorter than 12 bytes should be rejected.");
} catch (WolfCryptException e) {
/* test must throw */
}
/* An IV longer than 12 bytes is not a valid ChaCha nonce */
try {
chacha.setIV(new byte[16]);
fail("IV longer than 12 bytes should be rejected.");
} catch (WolfCryptException e) {
/* test must throw */
}
chacha.setIV(IV);
chacha.releaseNativeStruct();
}