mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
### Motivation: Our AES-CBC padding check is a padding-oracle timing side channel. Validation short-circuits on the first mismatching byte, so the time taken leaks padding validity; an attacker with a decryption oracle could recover plaintext without the key. The AES core is already BoringSSL-backed, so the remaining fix is to move the CBC mode onto BoringSSL and update padding removal to be constant-time. Aside: padding removal cannot be delegated to BoringSSL, whose generic unpad is itself a documented padding oracle absent authentication. ### Modifications: - Replace the Swift CBC block loop in `AES._CBC` with BoringSSL's `AES_cbc_encrypt`. - Add padding on encrypt in Swift, since `AES_cbc_encrypt` does not pad. - Replace `trimPadding` with `removingPKCS7PaddingConstantTime` - Add regression tests rejecting over-long pad lengths. - Add unpad tests for every valid length. - Add unpad tests for every corruption position. ### Result: AES-CBC is BoringSSL-backed and padding validation runs in constant time, removing a timing side channel.