Skip to content

Validate ciphertext length in AES._CBC.decrypt#438

Open
Nadav0077 wants to merge 1 commit intoapple:mainfrom
Nadav0077:fix/aes-cbc-decrypt-validate-block-alignment
Open

Validate ciphertext length in AES._CBC.decrypt#438
Nadav0077 wants to merge 1 commit intoapple:mainfrom
Nadav0077:fix/aes-cbc-decrypt-validate-block-alignment

Conversation

@Nadav0077
Copy link
Copy Markdown

Validate ciphertext length in AES._CBC.decrypt

Checklist

  • I've run tests to see all new and existing tests pass
  • I've followed the code style of the rest of the project
  • I've read the [Contribution Guidelines](CONTRIBUTING.md)
  • I've updated the documentation if necessary

If you've made changes to gyb files

  • I've run ./scripts/generate_boilerplate_files_with_gyb.sh and included updated generated files in a commit of this pull request

Motivation:

I noticed AES._CBC.decrypt happily accepts ciphertext whose length isn't a multiple of the 16-byte block size, which it really shouldn't, any valid CBC ciphertext is block-aligned by construction.

What happens today is: the decrypt loop slices the ciphertext into 16-byte chunks, but the trailing short chunk gets passed to Block.init(blockBytes:), which silently PKCS#7-pads short inputs to a full block. Those pad bytes then get treated as if they were ciphertext, decrypted, and returned as plaintext. So you don't get an error, you just get garbage bytes appended to the result. The encrypt side already rejects this in noPadding mode (incorrectParameterSize); the decrypt side was inconsistent.

Modifications:

Added a single guard at the top of AES._CBC.decrypt that throws CryptoKitError.incorrectParameterSize if ciphertext.count % blockSize != 0. Same error the encrypt path already uses for the symmetric case.

Also added a regression test (testDecryptRejectsNonBlockAlignedCiphertext) that feeds a 17-byte ciphertext in both noPadding=false and noPadding=true modes and asserts the right error is thrown.

Result:

Mis-aligned ciphertext now throws CryptoKitError.incorrectParameterSize instead of silently returning garbage. Behavior on valid (block-aligned) ciphertext is unchanged, verified by running the existing Wycheproof CBC vectors plus the rest of CBCTests on Swift 6.3.1 (Windows, x86_64-unknown-windows-msvc), all 7 tests pass.

The CBC decrypt path silently accepted ciphertext whose length was not
a multiple of the 16-byte AES block size. The trailing partial block
was passed to Block.init(blockBytes:), which PKCS#7-pads short inputs
internally. The pad bytes were then treated as ciphertext, producing
plaintext bytes that have no relationship to the caller's input.

The encrypt path already rejects mis-aligned input in noPadding mode;
decrypt now performs the same length check unconditionally, since any
valid CBC ciphertext is a multiple of the block size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant