Repository navigation
fix(mp4): reject CBCS IVs that differ from the init constant IV - #720
Conversation
tobbee
left a comment
There was a problem hiding this comment.
Thanks for the PR, and for the detailed reproduction in #710!
The fix is correct and in the right place. EncryptFragment, EncryptFragments, FragmentEncryptor and mp4ff-encrypt all go through NewFragmentEncryptor, so one check covers them all. Padding both IVs with zeros to 16 bytes matches how decryptSamplesInPlace builds the IV from the constant IV. Rejecting cbcs init data without a constant IV is a conformance check too: ISO/IEC 23001-7 clause 10.4.1 requires cbcs to use constant IVs (per-sample IV size 0) of 16 bytes.
I checked that the new tests catch the bug: with only the example change reverted, and with the whole fix reverted, the cbcs case of TestStep3_Encryption fails. Tests, golangci-lint, the changelog check and codespell all pass locally.
The main suggestion is in the streaming example, where the cenc IV derivation has two problems that predate this PR (inline below). The other inline comments are smaller points.
An optional idea for later, not for this PR: for cbcs with -init, mp4ff-encrypt could take the IV from the init's tenc, so that -iv isn't needed at all, and the API could accept a nil IV for cbcs in the same way.
Reject cbcs IVs that differ from the tenc constant IV and init data without one, before constructing the sample protector and AES cipher. Keep the existing zero-padding behavior for eight-byte IVs. Continue the stream-encrypt example's IV with EncryptFragment's return value for both schemes. This keeps cbcs on its constant IV, avoids cenc counter overlap between fragments and supports eight-byte cenc IVs. Cover the binding errors, matching round trips and streaming IV handling with the existing media fixtures, and document the IV requirements. Assisted-by: Perfloop Signed-off-by: Tomás Senart <[email protected]>
aedc39b to
afaa5f1
Compare
|
The streaming example now uses the returned IV for both schemes, with both IV sizes covered by its HTTP test. The optional init-derived or nil-IV interface change is left for later. |
CBCS encryption can silently succeed with an initialization vector (IV) different from the protected init segment's constant IV. Readers use the init's value and recover different media bytes; reject the mismatch before encrypting a fragment instead.
The check in
InitProtectData.NewFragmentEncryptorcoversEncryptFragment,EncryptFragments, the stateful encryptor andmp4ff-encrypt -init. CBCS init data without a constant IV is also rejected, since this encryptor does not write per-sample CBCS IVs. Validation runs before sample-protector and AES construction; API documentation and CLI help describe the requirement.Local reproduction of #710 with committed
mp4/testdata/init.mp4andmp4/testdata/1.m4s, using FFmpeg frame hashes (one run per IV case on each revision):The streaming example now chains the IV returned by
mp4.EncryptFragmentfor both schemes instead of deriving it from the fragment number. This fixes its cbcs output, overlapping cenc fragment counters and eight-byte cenc IV failures. Its HTTP test inexamples/stream-encrypt/main_test.gocovers both schemes and IV sizes, round trips and backwards cenc counters.Eight-byte cbcs IVs retain zero-padding. A supplied IV remains required.
Go encryption, IV, CLI and HTTP streaming tests pass. Scoped static analysis and changed-file validators pass.
Closes #710.
Case and evidence
Made by Perfloop. Human sponsor: Tomás Senart.