fix(pkcs12): validate kdf iteration count >= 1#2285
Merged
baloo merged 1 commit intoRustCrypto:masterfrom Apr 13, 2026
Merged
Conversation
RFC 7292 Appendix C defines iterations as INTEGER (1..MAX).
The loop `for _ in 1..rounds` silently degenerates to a single
hash application for rounds <= 0, producing wrong output without
signalling failure.
Add a rounds < 1 guard to derive_key, derive_key_bmp, and
derive_key_utf8 returning ErrorKind::Value { tag: Tag::Integer }.
Change derive_key and derive_key_bmp return types to der::Result.
Add tests: rounds=1 boundary vectors (verified against OpenSSL
PKCS12KDF), and rounds=0/-1/i32::MIN error cases.
baloo
approved these changes
Apr 13, 2026
baloo
reviewed
Apr 13, 2026
| /// `pass` must be a Unicode (UTF-16BE) byte array in big-endian order without a byte-order mark | ||
| /// and with two terminating zero bytes. | ||
| /// | ||
| /// `rounds` must be ≥ 1 (RFC 7292 Appendix C defines `iterations INTEGER (1..MAX)`). |
Member
There was a problem hiding this comment.
The change makes sense to me (and I already merged it), but I can't actually find the text referred here in the RFC7292. Am I missing something?
This was referenced Apr 16, 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.
Summary
derive_key,derive_key_bmp, andderive_key_utf8all acceptrounds: i32with no validationfor _ in 1..roundssilently degenerates to a single hash application whenrounds <= 0, producing wrong output with no erroriterations INTEGER (1..MAX)— zero and negative values are out-of-specFix
Add a
rounds < 1guard at the top of each function returningErr(ErrorKind::Value { tag: Tag::Integer }).derive_keyandderive_key_bmpreturn types change fromVec<u8>toder::Result<Vec<u8>>to carry the error.Tests
pkcs12_key_derive_rounds_one_boundary— verifiesrounds=1produces the correct output forEncryptionKeyandMackey types (SHA-256, vectors generated withopenssl kdf … -kdfopt iter:1 … PKCS12KDFand hardcoded)pkcs12_key_derive_zero_rounds_errors— asserts thatrounds=0,rounds=-1, androunds=i32::MINall returnErrBreaking change note
derive_keyandderive_key_bmpare public API. Changing their return type fromVec<u8>toder::Result<Vec<u8>>is a breaking change. Given that the previous behaviour was silently incorrect forrounds <= 0, andpkcs12is still pre-release (0.2.0-pre.0), this seems justified. Happy to discuss alternatives (e.g.u32parameter, or a separate validated wrapper).