{"record":{"id":"17f8b9aeb40645a8","repo":"embassy-rs/embassy","slug":"invalid-key-size","errorCode":null,"errorMessage":"Invalid key size","messagePattern":"Invalid key size","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-stm32/src/crypto.rs","lineNumber":75,"sourceCode":"pub trait Cipher<'c> {\n    /// Processing block size (always 16 bytes for AES).\n    const BLOCK_SIZE: usize = AES_BLOCK_SIZE;\n\n    /// Indicates whether the cipher requires the application to provide padding.\n    const REQUIRES_PADDING: bool = false;\n\n    /// Returns the symmetric key.\n    fn key(&self) -> &[u8];\n\n    /// Returns the initialization vector.\n    fn iv(&self) -> &[u8];\n\n    /// Returns the key size.\n    fn key_size(&self) -> KeySize {\n        match self.key().len() {\n            16 => KeySize::Bits128,\n            32 => KeySize::Bits256,\n            _ => panic!(\"Invalid key size\"),\n        }\n    }\n\n    /// Returns the data type setting for this cipher mode.\n    ///\n    /// The drivers use NO_SWAP (0) consistently with big-endian byte\n    /// conversion (`from_be_bytes`/`to_be_bytes`) for direct NIST test-vector\n    /// compatibility.\n    fn datatype(&self) -> u8 {\n        0\n    }\n\n    /// Returns the raw `CHMOD` field value for this cipher mode.\n    fn chmod_bits(&self) -> u8 {\n        0 // ECB default\n    }\n\n    /// Indicates whether this cipher mode uses GCM/CCM phases (init, header, payload, final).","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-stm32/src/crypto.rs#L57-L93","documentation":"The CRYP driver maps a Rust key buffer length onto the hardware KeySize register setting. Only 16 bytes (AES-128) and 32 bytes (AES-256) are supported here; any other key length panics with 'Invalid key size'. Notably, 24-byte AES-192 keys are not handled by this match.","triggerScenarios":"Constructing a cipher (e.g. Cipher::new with an AES key) whose key() slice is not exactly 16 or 32 bytes when key_size() is called — e.g. a 24-byte AES-192 key, an empty key, or a key with an extra header/IV byte concatenated.","commonSituations":"Using a 24-byte AES-192 key that the driver does not support; accidentally passing a hex-decoded half-length key; concatenating key and IV into one buffer; copying example code with a variable-length key from a KMS/file.","solutions":["Provide a 16-byte key for AES-128 (pad/derive with SHA-256 if necessary) or a 32-byte key for AES-256.","If you have an AES-192 key, derive a 256-bit key (e.g. SHA-256 of the 192-bit key) or use a software AES implementation supporting 192-bit keys.","Assert or log the key length before constructing the cipher to catch encoding mistakes early."],"exampleFix":"// before\nlet key = [0u8; 24]; // AES-192: not supported\ncipher.set_key(&key); // panics\n\n// after\nlet key = [0u8; 32]; // AES-256\ncipher.set_key(&key);","handlingStrategy":"validation","validationCode":"assert!(matches!(key.len(), 16 | 32), \"CRYP key must be 16 (AES-128) or 32 (AES-256) bytes, got {}\", key.len());","typeGuard":"fn is_valid_key(key: &[u8]) -> bool {\n    key.len() == 16 || key.len() == 32\n}","tryCatchPattern":null,"preventionTips":["Store keys as fixed-size arrays ([u8; 16] or [u8; 32]) instead of slices so length is enforced at compile time.","Never use AES-192 (24-byte) keys with this driver.","Verify key decoding (hex/base64) produces the expected byte count before constructing the cipher.","Keep keys and IVs in separate buffers; never concatenate them."],"tags":["embedded","crypto","panic","key-size","aes"],"backgroundTag":"invalid-argument-value","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}