{"record":{"id":"8ab94ebe4d612503","repo":"shadowsocks/shadowsocks-rust","slug":"aes-256-init","errorCode":null,"errorMessage":"AES-256 init","messagePattern":"AES-256 init","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/shadowsocks/src/relay/udprelay/aead_2022.rs","lineNumber":233,"sourceCode":"            let nonce = &packet_header[4..16];\n\n            if eih_len > 0 {\n                message = &mut message[eih_len..];\n            }\n\n            cipher.encrypt_packet(nonce, message);\n\n            // [SessionID + PacketID] is encrypted with AES-ECB with PSK\n            // No padding is required because these 2 fields are 128-bits, which is exactly the same as AES's block size\n            match method {\n                CipherKind::AEAD2022_BLAKE3_AES_128_GCM => {\n                    let cipher = Aes128::new_from_slice(ipsk).expect(\"AES-128 init\");\n                    let block = <&mut Block as TryFrom<&mut [u8]>>::try_from(packet_header)\n                        .expect(\"Packet header length mismatch\");\n                    cipher.encrypt_block(block);\n                }\n                CipherKind::AEAD2022_BLAKE3_AES_256_GCM => {\n                    let cipher = Aes256::new_from_slice(ipsk).expect(\"AES-256 init\");\n                    let block = <&mut Block as TryFrom<&mut [u8]>>::try_from(packet_header)\n                        .expect(\"Packet header length mismatch\");\n                    cipher.encrypt_block(block);\n                }\n                _ => unreachable!(\"{} is not an AES-*-GCM cipher\", method),\n            }\n        }\n        _ => unreachable!(\"{} is not an AEAD 2022 cipher\", method),\n    }\n}\n\nfn decrypt_message(\n    _context: &Context,\n    method: CipherKind,\n    key: &[u8],\n    packet: &mut [u8],\n    user_manager: Option<&ServerUserManager>,\n) -> ProtocolResult<Option<Arc<ServerUser>>> {","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/shadowsocks/shadowsocks-rust/blob/8eb0f0a65b1d976ab6bed5787327ef86529b0435/crates/shadowsocks/src/relay/udprelay/aead_2022.rs#L215-L251","documentation":"Panic from `Aes256::new_from_slice(ipsk).expect(\"AES-256 init\")` in UDP AEAD-2022 `encrypt_message` (AES-256 branch). The identity PSK must be exactly 32 bytes for `Aes256::new_from_slice`; the panic means a wrong-length ipsk was supplied with method `AEAD2022_BLAKE3_AES_256_GCM`.","triggerScenarios":"Calling `encrypt_message` (or the client/server payload wrappers) with `method = AEAD2022_BLAKE3_AES_256_GCM` and an `ipsk` shorter/longer than 32 bytes — e.g. a 16-byte identity key from a 128-bit setup or a raw password string.","commonSituations":"Mixing 128-bit identity keys with a 256-bit method in config; passing the wrong array element when multiple PSKs are configured; pasting a truncated base64 key into the identity-PSK field.","solutions":["Supply a 32-byte raw identity/PSK for AES-256-GCM (44-character base64 of 32 bytes).","Match method and key size consistently: 16-byte ipsk with AES-128 branch, 32-byte with AES-256 branch.","Validate all configured PSK lengths at startup, before any packet is sent.","Replace the expect with an explicit length assertion including the actual length for faster diagnosis."],"exampleFix":"// before\nlet cipher = Aes256::new_from_slice(ipsk).expect(\"AES-256 init\");\n// after\nassert_eq!(ipsk.len(), 32, \"AEAD2022_AES_256_GCM identity PSK must be 32 bytes, got {}\", ipsk.len());\nlet cipher = Aes256::new_from_slice(ipsk).expect(\"AES-256 init\");","handlingStrategy":"validation","validationCode":"if matches!(method, CipherKind::AEAD2022_BLAKE3_AES_256_GCM) && ipsk.len() != 32 {\n    return Err(format!(\"identity PSK must be 32 bytes for AES-256-GCM, got {}\", ipsk.len()));\n}","typeGuard":"fn is_valid_ipsk256(ipsk: &[u8]) -> bool { ipsk.len() == 32 }","tryCatchPattern":null,"preventionTips":["Check all configured PSK lengths at config load time.","Use 44-char base64 (32 raw bytes) keys for AES-256-GCM.","Guard against mixing 128-bit keys into 256-bit methods in config management code."],"tags":["rust","panic","aes-256","udp","key-length","aead-2022"],"backgroundTag":"invalid-argument-value","analyzedSha":"8eb0f0a65b1d976ab6bed5787327ef86529b0435","analyzedAt":"2026-09-09T12:20:43.168Z","contentChangedAt":"2026-09-09T12:20:43.168Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}