{"record":{"id":"3ddfef153d6b24d6","repo":"shadowsocks/shadowsocks-rust","slug":"aes-128-init","errorCode":null,"errorMessage":"AES-128 init","messagePattern":"AES-128 init","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/shadowsocks/src/relay/udprelay/aead_2022.rs","lineNumber":227,"sourceCode":"            // AES-*-GCM uses derived key, and part of the packet header as nonce\n\n            let cipher = get_cipher(method, key, session_id);\n\n            // Encrypt the rest of the packet with AEAD cipher (AES-*-GCM)\n            let (packet_header, mut message) = packet.split_at_mut(16);\n            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(","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/shadowsocks/shadowsocks-rust/blob/8eb0f0a65b1d976ab6bed5787327ef86529b0435/crates/shadowsocks/src/relay/udprelay/aead_2022.rs#L209-L245","documentation":"Panic from `Aes128::new_from_slice(ipsk).expect(\"AES-128 init\")` in the UDP AEAD-2022 `encrypt_message`. The `[SessionID + PacketID]` header is ECB-encrypted with the identity PSK; `Aes128::new_from_slice` requires exactly 16 bytes, so the panic means the ipsk supplied is not 16 bytes long for `AEAD2022_BLAKE3_AES_128_GCM`.","triggerScenarios":"Calling `encrypt_message` (directly or via `encrypt_client_payload_aead_2022` / `encrypt_server_payload_aead_2022`) with `method = AEAD2022_BLAKE3_AES_128_GCM` and an `ipsk` that is not exactly 16 bytes — e.g. a 32-byte identity key or a raw password.","commonSituations":"Using the main PSK (32 bytes for AES-256) as the identity PSK while the method is 128-bit; misreading the spec and passing a password instead of the raw 16-byte additional PSK; configs where client and server identity-key lengths differ.","solutions":["Provide an identity PSK of exactly 16 raw bytes when the method is `AEAD2022_BLAKE3_AES_128_GCM` (base64 of 16 bytes = 24 chars).","Match the cipher kind to the key: use the 256-bit branch (`AES-256 init` path) with 32-byte keys instead of forcing a 128-bit method.","Decode base64 keys and assert length before calling the payload encryption API.","Align identity-key length expectations between client and server configs."],"exampleFix":"// before\nlet cipher = Aes128::new_from_slice(ipsk).expect(\"AES-128 init\");\n// after\nassert_eq!(ipsk.len(), 16, \"AEAD2022_AES_128_GCM identity PSK must be 16 bytes, got {}\", ipsk.len());\nlet cipher = Aes128::new_from_slice(ipsk).expect(\"AES-128 init\");","handlingStrategy":"validation","validationCode":"// before UDP payload encryption\nif matches!(method, CipherKind::AEAD2022_BLAKE3_AES_128_GCM) && ipsk.len() != 16 {\n    return Err(format!(\"identity PSK must be 16 bytes for AES-128-GCM, got {}\", ipsk.len()));\n}","typeGuard":"fn is_valid_ipsk(kind: CipherKind, ipsk: &[u8]) -> bool {\n    match kind {\n        CipherKind::AEAD2022_BLAKE3_AES_128_GCM => ipsk.len() == 16,\n        CipherKind::AEAD2022_BLAKE3_AES_256_GCM => ipsk.len() == 32,\n        _ => false,\n    }\n}","tryCatchPattern":null,"preventionTips":["Validate PSK length against the cipher kind at startup.","Use raw decoded keys, not passwords, for the identity PSK.","Keep identity-key length consistent across client and server."],"tags":["rust","panic","aes-128","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"}