Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: remove padding from decryptedBytes if left in by the cipher #647

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,49 @@ protected Key generateKey(@NonNull final KeyGenParameterSpec spec) throws Genera
return generator.generateKey();
}

private static byte[] maybeRemovePKCS7Padding(byte[] paddedBytes, int maxPaddingLength) {
int paddingLength = paddedBytes[paddedBytes.length - 1];

// Validate the padding
if (paddingLength < 1 || paddingLength > paddedBytes.length || paddingLength > maxPaddingLength) {
return paddedBytes;
}
for (int i = paddedBytes.length - paddingLength; i < paddedBytes.length; i++) {
if (paddedBytes[i] != paddingLength) {
return paddedBytes;
}
}

// Remove the padding
byte[] unpaddedBytes = new byte[paddedBytes.length - paddingLength];
System.arraycopy(paddedBytes, 0, unpaddedBytes, 0, unpaddedBytes.length);

return unpaddedBytes;
}

/** Decrypt provided bytes to a string. */
@NonNull
@Override
protected String decryptBytes(@NonNull final Key key, @NonNull final byte[] bytes,
@Nullable final DecryptBytesHandler handler)
throws GeneralSecurityException, IOException {
@Nullable final DecryptBytesHandler handler)
throws GeneralSecurityException, IOException {
final Cipher cipher = getCachedInstance();

try {
// read the initialization vector from bytes array
final IvParameterSpec iv = IV.readIv(bytes);
cipher.init(Cipher.DECRYPT_MODE, key, iv);

// decrypt the bytes using cipher.doFinal(). Using a CipherInputStream for decryption has historically led to issues
// decrypt the bytes using cipher.doFinal(). Using a CipherInputStream for
// decryption has historically led to issues
// on the Pixel family of devices.
// see https://github.com/oblador/react-native-keychain/issues/383
byte[] decryptedBytes = cipher.doFinal(bytes, IV.IV_LENGTH, bytes.length - IV.IV_LENGTH);
byte[] _decryptedBytes = cipher.doFinal(bytes, IV.IV_LENGTH, bytes.length - IV.IV_LENGTH);

// removing padding is required to work around a decryption padding issue with
// some Pixel devices e.g. for strings of length 256
Copy link
Author

@andrejborstnik andrejborstnik Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my testing up to 2000 bytes the problematic strings lengths are

256 -> 271
528 -> 543
800 -> 815
1280 -> 1295
1552 -> 1567
1824 -> 1839

byte[] decryptedBytes = maybeRemovePKCS7Padding(_decryptedBytes, IV.IV_LENGTH);

return new String(decryptedBytes, UTF8);
} catch (Throwable fail) {
Log.w(LOG_TAG, fail.getMessage(), fail);
Expand Down