HLS AES-128 implicit IV corruption at media sequence ≥ 2³¹ — source-level investigation

GitHub source

Reproduced. This page is generated from GitHub Issue #54; GitHub remains the canonical authoring and discussion source.

Local reproduction and solution — 18 November 2026#

Status: Actual AES-CBC source function independently reproduced and fixed.

Source and evidence#

Upstream case · local report and evidence · portable reproduction instructions

Observed results are Reproduced only within the stated seam. Implementation explanations remain Inferred, not Apple Confirmed.

Reconstruct the scene#

Encrypt synthetic plaintext with an independent BigInt-derived 128-bit IV, then call the pinned actual SegmentUtils.aesDecrypt using firstMediaSequenceNumber + position.

The old implicit IV corrupts plaintext at 2^31, 3,000,000,000, 2^32, 2^32+1 and Number.MAX_SAFE_INTEGER. Fixed code passes 14/14 implicit/explicit cases; all 14 explicit-IV controls across both revisions pass.

Solution / guideline#

Use/backport 64d477d9a733cd068fc2fb699ee2a9d245a6fb3f: derive each byte with sequence % 256 and advance with Math.floor(sequence / 256), replacing signed bitwise shifts.

All pinned patches and results

Limits / remaining work#

Node 24 WebCrypto plus actual pinned source and minimal Closure stubs, not full HLS/Safari/FairPlay playback. Number-based sequence arithmetic remains limited to safe integers.

This update supersedes earlier no-local-run statements below only within the stated scope. Historical source reports are retained; no unresolved issue is closed by this publication.


Earlier investigation and source history

Verification update — 17 November 2026#

Status: independently reproduced at the real Shaka AES-CBC function; fix merged and local test passes. PR #10937 merged on 2026-11-09 as 64d477d9a733cd068fc2fb699ee2a9d245a6fb3f.

Local verification on 2026-11-17 used Node v24.5.0 WebCrypto and the actual SegmentUtils source from the fix's parent b2f2111c01614c49505962b4e9bf303ee25d8c36 versus the fixed commit, with minimal Closure/BufferUtils stubs. An independent BigInt implementation generated the reference 128-bit IV. Synthetic plaintext was encrypted using that IV and decrypted through each revision's aesDecrypt(), exercising firstMediaSequenceNumber + position.

Media sequence Before fix After fix Explicit-IV control on old code
0; 2^31−1 Plaintext matches Plaintext matches Plaintext matches
2^31; 3,000,000,000; 2^32; 2^32+1; Number.MAX_SAFE_INTEGER Plaintext mismatch Plaintext matches Plaintext matches
  • Solution: derive bytes with sequence % 256 and advance with Math.floor(sequence / 256), replacing signed bitwise shifts. Apply the pinned patch or use a build containing it; the Number-based calculation is limited to safe integer inputs.
  • Evidence boundary: 7/7 fixed-code and 7/7 explicit-IV controls passed; the old code failed the five high-sequence cases. This is real-function cryptographic verification with synthetic data, not a full HLS playback, Safari, FairPlay or native AVPlayer test. Release inclusion remains unverified.

This update supersedes older statements below about missing fix verification; the historical investigation and prompt are retained.


Source: shaka-project/shaka-player#10936

Problem and observed behavior#

Shaka Player v5.3.4 was reported to derive an incorrect initialization vector (IV) for HLS METHOD=AES-128 when EXT-X-KEY omits the IV attribute and the media sequence reaches 2³¹. The reporter supplied a deterministic Node reproduction and captured the IV passed to WebCrypto. This is a JavaScript AES-128 HLS issue, not a demonstrated FairPlay or native AVPlayer failure.

Evidence and root-cause boundary#

RFC 8216 §5.2 specifies using the media sequence number as a big-endian 128-bit IV when an explicit IV is absent: https://www.rfc-editor.org/rfc/rfc8216#section-5.2

The reported implementation iterated over 16 IV bytes using sequence >>= 8. JavaScript bitwise shifts coerce the number to a signed 32-bit integer. At 2³¹, sign extension fills the upper IV bytes with ff; at 2³², the high bits are discarded.

The reporter supplied the following expected/observed examples (not independently executed here):

Media sequence Expected IV (hex) Reported old IV (hex)
2147483647 0000000000000000000000007fffffff Same
2147483648 00000000000000000000000080000000 ffffffffffffffffffffffff80000000
3000000000 000000000000000000000000b2d05e00 ffffffffffffffffffffffffb2d05e00
4294967296 00000000000000000000000100000000 00000000000000000000000000000000

A direct GitHub source read of lib/media/segment_utils.js found the current default-branch file blob SHA 8267bad5d6f879c36dc9f39328cff18a90c51d0c. Around lines 919–927, aesDecrypt() now uses iv[i] = sequence % 256 and sequence = Math.floor(sequence / 256), with an inline comment explaining the signed-shift problem.

Source: https://github.com/shaka-project/shaka-player/blob/main/lib/media/segment_utils.js

This supports that the inspected repository snapshot contains a correction. The exact introducing commit, fixing commit, release integration, and device-level outcomes remain unverified.

First causal divergence#

EXT-X-MEDIA-SEQUENCE + segment position → implicit IV derivation → incorrect upper IV bytes → AES-CBC decryption with wrong IV → corrupted plaintext → potential demux/playback failure.

For CBC, a wrong IV changes the first decrypted block; whether subsequent bytes fail demux or produce visible corruption depends on the media and padding. Do not claim every affected segment necessarily fails identically.

Verification plan#

  1. Pin the old release and corrected source revision, identify the exact commit diff and affected releases.
  2. Compute IVs for 2³¹−1, 2³¹, 3×10⁹, 2³² and 2³²+1 with an independent big-endian reference implementation.
  3. Encrypt a known MPEG-TS plaintext with an authorized AES-128 test key, then compare old and corrected decryption. Add explicit-IV and low-sequence controls.
  4. Capture the selected playlist, EXT-X-KEY, sequence/position and the derived IV. Never log production keys.
  5. Independently test native AVPlayer only to assess applicability; do not infer AVFoundation uses Shaka's JavaScript arithmetic.

Engineering guideline#

For Shaka-based clients, prefer a verified version containing the corrected IV derivation, or backport the exact arithmetic change with boundary tests. Validate the version before rollout. CDN/packager changes are separate proposals, not assumed requirements.

Reusable insight: HLS implicit IVs are 128-bit big-endian values; JavaScript signed 32-bit bitwise operations are unsafe for high media-sequence numbers.

Status and evidence gaps#

Source-level arithmetic defect is supported by the reporter's reproduction and the inspected corrected source. No local encrypted-media playback test, verified fixing commit, release mapping, Safari test, or native AVPlayer test was performed. FairPlay SPC/CKC analysis is not applicable absent separate DRM evidence.

Extended investigation prompt#

Copy the full senior reverse-engineering and DRM investigation prompt
Act as a senior iOS/tvOS reverse engineer, AVFoundation/CoreMedia investigator, HLS cryptography forensic engineer and FairPlay DRM specialist.

CASE: https://github.com/shaka-project/shaka-player/issues/10936
Source under review: https://github.com/shaka-project/shaka-player/blob/main/lib/media/segment_utils.js
Question: Shaka's AES-128 HLS implicit IV calculation reportedly sign-extends at media sequence 2^31 and loses high bits at 2^32. A newer repository snapshot uses Math.floor(sequence / 256). Determine exact root cause, affected commits/releases, and whether the fix is merged/released. Do not assume native AVPlayer or FairPlay is affected.

EVIDENCE HIERARCHY (mandatory):
T1: RFC 8216 §5.2 and relevant IETF updates; official Apple HLS/AVFoundation/FairPlay documentation; ECMAScript bitwise/number semantics; W3C WebCrypto contracts.
T2: Commit-pinned Shaka source and git diffs, verified WebKit code if directly relevant, Apple open-source components, symbolicated AVFoundation/CoreMedia traces, peer-reviewed or independently verified security research. Cite repository, full commit SHA, file, function, exact line numbers, and control flow. WebKit is not native AVPlayer source.
T3: Byte-preserved manifest/segment captures, deterministic JavaScript reproduction, encrypted known-plaintext test vectors, request/decrypt timelines, and physical-device tests where applicable.
T4: Clearly labeled, falsifiable hypotheses. Do not promote assumptions to facts.

METHOD:
1. Audit EXT-X-KEY METHOD=AES-128, explicit vs omitted IV, EXT-X-MEDIA-SEQUENCE, segment positions, discontinuities, key rotation and sequence arithmetic.
2. Trace sequence -> implicit 128-bit big-endian IV -> WebCrypto AES-CBC -> decrypted segment -> demux -> playback. Establish first wrong byte and boundary.
3. Inspect old signed shift (>>=8) vs Math.floor(/256), plus %256 vs &255. Verify 2^31-1, 2^31, 3e9, 2^32, 2^32+1 and near Number.MAX_SAFE_INTEGER. State JS precision limits.
4. Pin affected and corrected Shaka revisions, verify upstream PR/commit/release notes and unit tests; never claim a fix is released merely because main changed.
5. Reproduce with independently computed 128-bit IV and known-plaintext ciphertext; compare old/new code and explicit-IV control. No production secrets.
6. Test Safari/WebKit and native AVPlayer only as separate controls. AES-128 identity-key HLS is not FairPlay SAMPLE-AES; do not trace SPC/CKC without a FairPlay-specific failure.
7. Explain applicability to live streams, very high media sequence numbers, server resets, and risk of misdiagnosing decrypt errors as CDN/network failures.

For every conclusion: CLAIM | TIER | SOURCE URL | OBSERVATION | COUNTEREVIDENCE | CONFIDENCE | FALSIFICATION TEST.
ROOT CAUSE only if source behavior and controlled reproduction prove the first divergence; distinguish upstream-confirmed fix from experiments you ran.
OUTPUT: executive finding, RFC contract, source audit, IV comparison table, first causal divergence, verified fix status, test matrix, client-only Shaka mitigation, separately labeled packager proposals, regression tests, unknowns, exact citations and publication-ready article.
Never invent experiments, source line numbers, version fixes or private AVFoundation internals.