Skip to content

fix(mp4): reject CBCS IVs that differ from the init constant IV - #720

Merged
tobbee merged 1 commit into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-77mvkyshvr
Nov 17, 2026
Merged

tobbee merged 1 commit into
Eyevinn:masterfrom
perfloop-oss:perfloop-pr-open-77mvkyshvr

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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.NewFragmentEncryptor covers EncryptFragment, EncryptFragments, the stateful encryptor and mp4ff-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.mp4 and mp4/testdata/1.m4s, using FFmpeg frame hashes (one run per IV case on each revision):

Supplied IV Before After
Different from init Encryption succeeds; 0/60 frames equal Encryption fails with a must-match-IV error
Matches init 60/60 frames equal 60/60 frames equal

The streaming example now chains the IV returned by mp4.EncryptFragment for 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 in examples/stream-encrypt/main_test.go covers 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.

@tobbee tobbee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread examples/stream-encrypt/encryptor.go Resolved
Comment thread mp4/crypto.go Resolved
Comment thread cmd/mp4ff-encrypt/main.go Resolved
Comment thread examples/stream-encrypt/main_test.go Resolved
Comment thread mp4/crypto_test.go Resolved
Comment thread changelog.d/cbcs-constant-iv.md Resolved
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]>
@perfloop
perfloop Bot force-pushed the perfloop-pr-open-77mvkyshvr branch from aedc39b to afaa5f1 Compare November 14, 2026 15:10
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

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.

@tobbee
tobbee merged commit f83fc8c into Eyevinn:master Nov 17, 2026
10 checks passed
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.

CBCS encryption accepts an IV that readers do not receive

2 participants