pkcs5: return an error instead of panicking on a short DES/3DES IV#2383
Merged
tarcieri merged 1 commit intoJul 20, 2026
Merged
Conversation
The DES-CBC and 3DES-EDE3-CBC arms of the EncryptionScheme parser slice iv[0..DES_BLOCK_SIZE] before the try_into length check, so an AlgorithmIdentifier carrying one of those OIDs with an IV octet string shorter than 8 bytes panics with a slice range error instead of returning an error. The AES arms already run try_into on the whole slice, which length-checks gracefully; mirror that in the DES arms. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
tarcieri
approved these changes
Jul 20, 2026
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.
While reading the PBES2
EncryptionSchemeparser I noticed the DES-CBC and 3DES-EDE3-CBC arms can panic on attacker-controlled input.Both DES arms do
iv[0..DES_BLOCK_SIZE].try_into(), so the fixed-size slice happens before thetry_intolength check. If anAlgorithmIdentifiercarries a DES-CBC or 3DES OID with an IV octet string shorter than 8 bytes, that range slice panics (range end index 8 out of range for slice of length N) instead of returning an error. The AES arms already calltry_intoon the whole slice, which reports a wrong length as an error, so this is just an inconsistency between the branches.The IV comes straight from the DER parameters, so any code path that decodes an untrusted
EncryptionSchemeis affected, includingpkcs8::EncryptedPrivateKeyInfo::decrypt, where the params are decoded before the password is ever used. A crafted 3DES-encrypted key blob panics regardless of the password.The fix drops the manual slice and lets
try_intoon the full slice do the length check, matching the AES arms.DesCbc/DesEde3Cbcboth hold[u8; 8], soTryInto<[u8; 8]>enforces exactly 8 bytes.Added regression tests in
pkcs5/tests/pbes2.rs: a short (3-byte) IV for both DES-CBC and 3DES now decodes toErr, and a valid 8-byte 3DES IV still decodes. The 3DES test is behind the3desfeature and the DES-CBC one behinddes-insecure; ran withcargo test -p pkcs5 --features 3des,des-insecure. Before the change the short-IV cases panicked at pbes2.rs:546 and :552; after it they pass.