{"record":{"id":"16f677dafba0cffc","repo":"embassy-rs/embassy","slug":"output-buffer-length-must-match-input-length","errorCode":null,"errorMessage":"Output buffer length must match input length.","messagePattern":"Output buffer length must match input length\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-stm32/src/cryp/mod.rs","lineNumber":1419,"sourceCode":"\n        // Perform checks for correctness.\n        if !ctx.aad_complete && ctx.header_len > 0 {\n            panic!(\"Additional associated data must be processed first!\");\n        } else if !ctx.aad_complete {\n            #[cfg(any(cryp_v2, cryp_v3, cryp_v4))]\n            {\n                ctx.aad_complete = true;\n                T::regs().cr().modify(|w| w.set_crypen(false));\n                T::regs().cr().modify(|w| w.set_gcm_ccmph(2));\n                T::regs().cr().modify(|w| w.set_fflush(true));\n                T::regs().cr().modify(|w| w.set_crypen(true));\n            }\n        }\n        if ctx.last_block_processed {\n            panic!(\"The last block has already been processed!\");\n        }\n        if input.len() > output.len() {\n            panic!(\"Output buffer length must match input length.\");\n        }\n        if !last_block {\n            if last_block_remainder != 0 {\n                panic!(\"Input length must be a multiple of {} bytes.\", C::BLOCK_SIZE);\n            }\n        }\n        if C::REQUIRES_PADDING {\n            if last_block_remainder != 0 {\n                panic!(\n                    \"Input must be a multiple of {} bytes in ECB and CBC modes. Consider padding or ciphertext stealing.\",\n                    C::BLOCK_SIZE\n                );\n            }\n        }\n        if last_block {\n            ctx.last_block_processed = true;\n        }\n","sourceCodeStart":1401,"sourceCodeEnd":1437,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-stm32/src/cryp/mod.rs#L1401-L1437","documentation":"The CRYP payload call requires the output buffer to be at least as long as the input; hardware writes one block per input block, so a shorter output would be overrun. It panics when input.len() > output.len().","triggerScenarios":"Calling payload()/encrypt/decrypt with an output slice shorter than the input — e.g. decrypting 16 bytes of ciphertext into an 8-byte buffer, or a mis-sized scratch buffer.","commonSituations":"Hardcoded buffer sizes not matching input length; in-place buffers reused with wrong slice bounds; porting from APIs that return an owned buffer to this caller-buffer API.","solutions":["Size the output buffer to be at least input.len() (same length for streaming modes).","For padded block modes, allocate output covering the padded length as the driver requires.","If the destination is smaller, process the input in chunks that fit it."],"exampleFix":"// before\nlet mut out = [0u8; 8];\nctx.payload(&ciphertext16, &mut out, true); // panics\n// after\nlet mut out = [0u8; 16];\nctx.payload(&ciphertext16, &mut out, true);","handlingStrategy":"validation","validationCode":"assert!(output.len() >= input.len(), \"output buffer too small for CRYP payload\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive output buffer size from input length, never hardcode","Double-check padded-mode buffer requirements","Unit-test buffer sizing at edge lengths (0, block size +/- 1)"],"tags":["embedded","crypto","cryp","buffer-size","panic"],"backgroundTag":"buffer-size-mismatch","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"}