Skip to content

Fix AES-256-CBC content decryption for CLIENT_ENCRYPTION_KEY_SIZE=256#45

Merged
weiherelationalai merged 2 commits into
mainfrom
fix-aes256-cbc-content-decryption
Jun 24, 2026
Merged

Fix AES-256-CBC content decryption for CLIENT_ENCRYPTION_KEY_SIZE=256#45
weiherelationalai merged 2 commits into
mainfrom
fix-aes256-cbc-content-decryption

Conversation

@weiherelationalai

Copy link
Copy Markdown
Contributor

Problem

After fixing the QSMK key unwrap (#43) and matdesc keySize (#44), content decryption still fails with:

failed to decrypt the object contents: EVP_CIPHER_CTX_set_key_length:invalid key length

With `CLIENT_ENCRYPTION_KEY_SIZE=256`, Snowflake writes file content using a 32-byte CEK with AES-CBC. Two bugs caused OpenSSL to receive a key of the wrong length:

S3 path: `"AES/CBC/PKCS5Padding"` was unconditionally mapped to `Aes128Cbc` (16-byte key cipher), regardless of the actual CEK size.

Azure path: `"AES_CBC_256"` was incorrectly mapped to `Aes128Cbc` — clearly a pre-existing bug.

Fix

  • Add `Aes256Cbc` variant to `CryptoScheme` backed by `aes_256_cbc`.
  • S3: select `Aes256Cbc` vs `Aes128Cbc` based on the unwrapped CEK length (32 vs 16 bytes).
  • Azure: map `"AES_CBC_256"` to `Aes256Cbc`.

Related

With CLIENT_ENCRYPTION_KEY_SIZE=256, Snowflake writes file content using
a 32-byte CEK with AES-CBC. Two bugs caused content decryption to fail
with OpenSSL invalid key length:

S3 path: "AES/CBC/PKCS5Padding" was unconditionally mapped to Aes128Cbc
(16-byte key). Now selects Aes256Cbc when the unwrapped CEK is 32 bytes.

Azure path: "AES_CBC_256" was incorrectly mapped to Aes128Cbc. Fixed to
use Aes256Cbc.

Add Aes256Cbc variant to CryptoScheme backed by aes_256_cbc cipher.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@weiherelationalai weiherelationalai force-pushed the fix-aes256-cbc-content-decryption branch from 3e37b6f to ab5b568 Compare June 24, 2026 01:43
- Extract S3/Azure content-scheme selection into pure, unit-testable
  helpers (s3_content_scheme / azure_content_scheme) and document why
  the S3 path infers key size from CEK length while Azure reads it from
  the algorithm name.
- Cover the CLIENT_ENCRYPTION_KEY_SIZE=256 path: CEK-length-based 128 vs
  256 selection, missing/unknown cek-alg handling, and Aes256Cbc
  content encrypt/decrypt + CrypterReader/Writer round-trips.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@weiherelationalai weiherelationalai merged commit 86c31c9 into main Jun 24, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant