{"record":{"id":"f5a9a181b1573de6","repo":"embassy-rs/embassy","slug":"message-is-too-large-for-given-iv-size","errorCode":null,"errorMessage":"Message is too large for given IV size.","messagePattern":"Message is too large for given IV size\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-stm32/src/cryp/mod.rs","lineNumber":796,"sourceCode":"            aad_header[2] = aad_len_bytes[0];\n            aad_header[3] = aad_len_bytes[1];\n            aad_header[4] = aad_len_bytes[2];\n            aad_header[5] = aad_len_bytes[3];\n            aad_header_len = 6;\n        }\n    }\n    let mut block0: Aligned<A4, [u8; 16]> = Aligned([0; 16]);\n    if aad_len > 0 {\n        block0[0] = 0x40;\n    }\n    block0[0] |= ((((tag_size as u8) - 2) >> 1) & 0x07) << 3;\n    block0[0] |= ((15 - (iv.len() as u8)) - 1) & 0x07;\n    block0[1..1 + iv.len()].copy_from_slice(iv);\n    let payload_len_bytes: [u8; 4] = (payload_len as u32).to_be_bytes();\n    if iv.len() <= 11 {\n        block0[12] = payload_len_bytes[0];\n    } else if payload_len_bytes[0] > 0 {\n        panic!(\"Message is too large for given IV size.\");\n    }\n    if iv.len() <= 12 {\n        block0[13] = payload_len_bytes[1];\n    } else if payload_len_bytes[1] > 0 {\n        panic!(\"Message is too large for given IV size.\");\n    }\n    block0[14] = payload_len_bytes[2];\n    block0[15] = payload_len_bytes[3];\n    let mut ctr: [u8; 16] = [0; 16];\n    ctr[0] = block0[0] & 0x07;\n    ctr[1..1 + iv.len()].copy_from_slice(&block0[1..1 + iv.len()]);\n    ctr[15] = 0x01;\n\n    (aad_header, aad_header_len, block0, ctr)\n}\n\n/// Type-erased AES-CCM operation.\n#[cfg(any(cryp_v2, cryp_v3, cryp_v4))]","sourceCodeStart":778,"sourceCodeEnd":814,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-stm32/src/cryp/mod.rs#L778-L814","documentation":"This panic fires during GCM/GMAC J0 (IV) block construction in the CRYP driver. When iv.len() > 11, byte 12 of the fixed block is occupied by the IV, so a payload length whose most significant byte (payload_len_bytes[0]) is nonzero cannot be encoded — i.e. payload length >= 16,777,216 bytes (~16 MiB) with a long IV.","triggerScenarios":"GCM encrypt/decrypt payload processing with an IV longer than 11 bytes AND payload_len >= 2^24 bytes.","commonSituations":"Streaming very large files (>16 MiB) through GCM with a non-standard long IV; encrypting an entire large buffer in one call instead of chunking.","solutions":["Use the standard 12-byte GCM IV, which encodes the length field without this limitation.","Chunk the message into pieces under 16 MiB, each with its own GCM context.","Shorten the IV to <= 11 bytes if the monolithic payload must be kept."],"exampleFix":"// before\ncipher.encrypt(ctx, &long_iv, aad, &huge_plaintext, &mut out);\n// after\nfor chunk in plaintext.chunks(16 * 1024 * 1024) {\n    cipher.encrypt(ctx, iv, aad, chunk, &mut out_chunk);\n}","handlingStrategy":"validation","validationCode":"const MAX_PAYLOAD_WITH_LONG_IV: usize = (1 << 24) - 1;\nif iv.len() > 11 && payload_len > MAX_PAYLOAD_WITH_LONG_IV {\n    // chunk the message or use a 12-byte IV\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to the RFC 5116 standard 12-byte GCM IV","Cap GCM message sizes even with standard IVs","Chunk large payloads and authenticate per chunk"],"tags":["embedded","crypto","gcm","cryp","panic"],"backgroundTag":"value-out-of-range","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}