Validate ciphertext length in AES._CBC.decrypt#438
Open
Nadav0077 wants to merge 1 commit intoapple:mainfrom
Open
Validate ciphertext length in AES._CBC.decrypt#438Nadav0077 wants to merge 1 commit intoapple:mainfrom
Nadav0077 wants to merge 1 commit intoapple:mainfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Validate ciphertext length in
AES._CBC.decryptChecklist
If you've made changes to
gybfiles./scripts/generate_boilerplate_files_with_gyb.shand included updated generated files in a commit of this pull requestMotivation:
I noticed
AES._CBC.decrypthappily 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 innoPaddingmode (incorrectParameterSize); the decrypt side was inconsistent.Modifications:
Added a single guard at the top of
AES._CBC.decryptthat throwsCryptoKitError.incorrectParameterSizeifciphertext.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 bothnoPadding=falseandnoPadding=truemodes and asserts the right error is thrown.Result:
Mis-aligned ciphertext now throws
CryptoKitError.incorrectParameterSizeinstead of silently returning garbage. Behavior on valid (block-aligned) ciphertext is unchanged, verified by running the existing Wycheproof CBC vectors plus the rest ofCBCTestson Swift 6.3.1 (Windows, x86_64-unknown-windows-msvc), all 7 tests pass.