fix: AES ncleanup and PKCS padding validation #2484 #2480

This commit is contained in:
J-Jamet committed 2026-04-15 15:27:25 +02:00
1 parent 87dadf9a4c
commit b2e4bd3b60
4 files changed
+31 -30

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@ KeePassDX(4.4.0)
* Tags in list view #1539 #2144
* Search by Tags #1338 #2280
* Improved accessibility #2475
* Improved security #2481 #2480
* Improved security #2481 #2484 #2480
KeePassDX(4.3.3)
* Fix 16 KB ELF alignment #2074
+28 -27
View File
@@ -151,7 +151,10 @@ JNIEXPORT jlong JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nInit(
}
JNIEXPORT void JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nCleanup(JNIEnv *env, jclass this, jlong state) {
free((void *)state);
if (state) {
memset((void *)state, 0, sizeof(aes_state));
free((void *)state);
}
}
/*
@@ -187,7 +190,7 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nUpdate
}
// step 1.5: calculate cryptLen and outLen
cryptLen = inputLen + c_state->cache_len;
cryptLen = (uint32_t)inputLen + c_state->cache_len;
if( cryptLen < CACHE_SIZE ) {
(*env)->GetByteArrayRegion(env, input, inputOffset, inputLen, (jbyte *)(c_state->cache + c_state->cache_len));
c_state->cache_len = cryptLen;
@@ -225,16 +228,16 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nUpdate
(*env)->GetByteArrayRegion(env, input, inputOffset, inputLen, (jbyte *)c_input);
}
if( c_state->direction == ENCRYPTION )
aes_ret = aes_cbc_encrypt(c_input, c_output, outLen, c_state->iv, ENC_CTX(c_state));
aes_ret = aes_cbc_encrypt(c_input, c_output, (int)outLen, c_state->iv, ENC_CTX(c_state));
else
aes_ret = aes_cbc_decrypt(c_input, c_output, outLen, c_state->iv, DEC_CTX(c_state));
aes_ret = aes_cbc_decrypt(c_input, c_output, (int)outLen, c_state->iv, DEC_CTX(c_state));
if( aes_ret != EXIT_SUCCESS ) {
free(in);
free(out);
(*env)->ThrowNew(env, bad_arg, "Failed to encrypt input data"); // FIXME: get a better exception class for this...
return -1;
}
(*env)->SetByteArrayRegion(env, output, outputOffset, outLen, (jbyte *)c_output);
(*env)->SetByteArrayRegion(env, output, outputOffset, (jsize)outLen, (jbyte *)c_output);
// step 4: cleanup and return
if( bytes2cache ) {
@@ -251,7 +254,7 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nUpdate
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nUpdate", "exit: outLen=%d", outLen);
#endif
return outLen;
return (jint)outLen;
}
/*
@@ -281,9 +284,9 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nFinal(
// allow fetching of remaining bytes from cache
if( !doPadding ) {
(*env)->SetByteArrayRegion(env, output, outputOffset, c_state->cache_len, (jbyte *)c_state->cache);
(*env)->SetByteArrayRegion(env, output, outputOffset, (jsize)c_state->cache_len, (jbyte *)c_state->cache);
c_state->direction = FINALIZED;
return c_state->cache_len;
return (jint)c_state->cache_len;
}
#if defined(KPD_DEBUG)
@@ -302,17 +305,17 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nFinal(
}
padValue = paddedCacheLen - c_state->cache_len;
if(!padValue) padValue = 16;
memset(c_state->cache + c_state->cache_len, padValue, padValue);
if( aes_cbc_encrypt(c_state->cache, final_output, paddedCacheLen, c_state->iv, ENC_CTX(c_state)) != EXIT_SUCCESS ) {
memset(c_state->cache + c_state->cache_len, (int)padValue, (size_t)padValue);
if( aes_cbc_encrypt(c_state->cache, final_output, (int)paddedCacheLen, c_state->iv, ENC_CTX(c_state)) != EXIT_SUCCESS ) {
(*env)->ThrowNew(env, bad_arg, "Failed to encrypt the final data block(s)"); // FIXME: get a better exception class for this...
return -1;
}
(*env)->SetByteArrayRegion(env, output, outputOffset, paddedCacheLen, (jbyte *)final_output);
(*env)->SetByteArrayRegion(env, output, outputOffset, (jsize)paddedCacheLen, (jbyte *)final_output);
c_state->direction = FINALIZED;
#if defined(KPD_DEBUG)
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nFinal", "encryption operation completed, returning %d bytes", paddedCacheLen);
#endif
return paddedCacheLen;
return (jint)paddedCacheLen;
} else { // DECRYPTION
paddedCacheLen = c_state->cache_len;
@@ -324,18 +327,17 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nFinal(
(*env)->ThrowNew(env, bad_padding, "Incomplete final block in cache for decryption state");
return -1;
}
if( aes_cbc_decrypt(c_state->cache, final_output, paddedCacheLen, c_state->iv, DEC_CTX(c_state)) != EXIT_SUCCESS ) {
if( aes_cbc_decrypt(c_state->cache, final_output, (int)paddedCacheLen, c_state->iv, DEC_CTX(c_state)) != EXIT_SUCCESS ) {
(*env)->ThrowNew(env, bad_arg, "Failed to decrypt the final data block(s)"); // FIXME: get a better exception class for this...
return -1;
}
padValue = final_output[paddedCacheLen-1];
padValue = (uint32_t)final_output[paddedCacheLen-1];
int badPadding;
badPadding = padValue > AES_BLOCK_SIZE;
int badPadding = (padValue == 0 || padValue > AES_BLOCK_SIZE);
if (!badPadding) {
for(i = paddedCacheLen-1; final_output[i] == padValue && i >= 0; i--) {
if (final_output[i] != padValue) {
for(i = 0; i < (int)padValue; i++) {
if (final_output[paddedCacheLen - 1 - i] != (uint8_t)padValue) {
badPadding = 1;
break;
}
@@ -350,14 +352,14 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nFinal(
return -1;
}
int outputSize = AES_BLOCK_SIZE - padValue;
int retOutputSize = (int)AES_BLOCK_SIZE - (int)padValue;
(*env)->SetByteArrayRegion(env, output, outputOffset, outputSize, (jbyte *)final_output);
(*env)->SetByteArrayRegion(env, output, outputOffset, (jsize)retOutputSize, (jbyte *)final_output);
c_state->direction = FINALIZED;
#if defined(KPD_DEBUG)
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nFinal", "decryption operation completed, returning %d bytes", outputSize);
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nFinal", "decryption operation completed, returning %d bytes", retOutputSize);
#endif
return outputSize;
return (jint)retOutputSize;
}
}
@@ -369,7 +371,7 @@ JNIEXPORT jint JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESCipherSpi_nGetCac
(*env)->ThrowNew(env, bad_arg, "Invalid state");
return -1;
}
return c_state->cache_len;
return (jint)c_state->cache_len;
}
#define MASTER_KEY_SIZE 32
@@ -409,7 +411,7 @@ uint32_t generate_key_material(void *arg) {
#endif
aes_encrypt_key256(mk->c_seed, e_ctx);
for (i = 0; i < mk->rounds; i++) {
for (i = 0; i < (uint32_t)mk->rounds; i++) {
if ( flip ) {
aes_encrypt(key2, key1, e_ctx);
flip = 0;
@@ -422,9 +424,9 @@ uint32_t generate_key_material(void *arg) {
#if defined(KPD_PROFILE)
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
if( key1 == mk->key1 )
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nTransformMasterKey", "Thread 1 master key transformation took ~%d seconds", (end.tv_sec-start.tv_sec));
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nTransformMasterKey", "Thread 1 master key transformation took ~%d seconds", (int)(end.tv_sec-start.tv_sec));
else
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nTransformMasterKey", "Thread 2 master key transformation took ~%d seconds", (end.tv_sec-start.tv_sec));
__android_log_print(ANDROID_LOG_INFO, "aes_jni.c/nTransformMasterKey", "Thread 2 master key transformation took ~%d seconds", (int)(end.tv_sec-start.tv_sec));
#endif
if( key1 == mk->key1 ) {
@@ -523,4 +525,3 @@ JNIEXPORT jbyteArray JNICALL Java_com_kunzisoft_encrypt_aes_NativeAESKeyTransfor
return result;
}
#undef MASTER_KEY_SIZE
@@ -2,4 +2,4 @@
* Tags in list view #1539 #2144
* Search by Tags #1338 #2280
* Improved accessibility #2475
* Improved security #2481 #2480
* Improved security #2481 #2484 #2480
@@ -2,4 +2,4 @@
* Tags dans la vue liste #1539 #2144
* Recherche par Tags #1338 #2280
* Accessibilité améliorée #2475
* Sécurité améliorée #2481 #2480
* Sécurité améliorée #2481 #2484 #2480