Skip to content

Reuse the GCM cipher context between rows in encrypt/decrypt - #131001

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
mkmkme:aes-gcm-key-reuse
Nov 16, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
mkmkme:aes-gcm-key-reuse

Conversation

@mkmkme

@mkmkme mkmkme commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #107339, which made encrypt/decrypt reuse the cipher context and key schedule between rows for every mode except GCM. GCM still set the context up from scratch on every row. In OpenSSL 3.x that frees and re-creates the provider context and updates the reference count of the cipher object that all threads share. With 16 threads that atomic update alone took about 43% of the time, so GCM barely sped up with more threads.

This PR applies the same CachedKeyState approach to the GCM branches. The cipher is passed only on the first row or after a failed decryption. The IV length and IV are set on every row, and the key only when it changes.

A GCM round trip on 100M rows went from 23.7 s to 0.9 s (about 27x faster) with 16 threads, and on 10M rows from 3.9 s to 1.1 s (about 3.6x) on one thread. Output is byte-identical. 04337_encrypt_decrypt_block_modes gains GCM coverage, and encrypt_decrypt.xml gains GCM performance cases.

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Improve performance of the encrypt, decrypt and tryDecrypt functions in AES-GCM modes by up to 27x with many threads.


Workflow [PR]
Sync PR [sync-upstream/pr/131001]

Version info

  • Merged into: 26.11.1.2481-master (included in 26.11 and later)

encrypt and decrypt in GCM mode set up the cipher context from scratch on
every row. Passing the cipher to EVP_EncryptInit_ex makes OpenSSL 3 reset
the context: it frees and re-creates the provider context and updates the
reference count of the EVP_CIPHER that all threads share. With 16 threads
that atomic update took over 40% of the time, and GCM ran about 25x slower
than the other modes. #107339 fixed this for the other modes but left the
GCM branch as it was.

Pass the cipher only on the first row or after a failed decryption, and
the key only when it changes, as the other modes already do. A GCM round
trip over 100M rows with 16 threads goes from 23.7 s to 0.9 s, and from
3.9 s to 1.1 s on one thread. The output is byte-identical.
@clickhouse-gh clickhouse-gh Bot added comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization labels Nov 12, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [14f37e6]

Summary: ✅


AI Review

Summary

Reviewed PR #131001 at 14f37e66898e33e9ef47fb8ccd61969048500bad. The change reuses GCM cipher contexts and key schedules across rows in encrypt, decrypt, and tryDecrypt, with functional coverage and performance cases. No actionable findings. Independent EVP checks matched all 195 added expected ciphertexts and 12,000 cached-versus-fresh encryption/decryption comparisons, including 4,860 authentication failures followed by recovery. The PR provides before/after performance measurements.

Missing context / blind spots
  • ⚠️ No ClickHouse binary is available locally. Independent execution used system OpenSSL 3.0.2; the bundled OpenSSL 3.5.9 implementation was inspected. The modified 04337_encrypt_decrypt_block_modes test is excluded from Fast test, and functional CI is still pending. A passing run of that test on an SSL-enabled build would close this execution gap.
Final Verdict

Status: ✅ Approve. No new inline findings; no prior review threads required action.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 15, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 16, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 16, 2026
Merged via the queue into ClickHouse:master with commit 3101ef2 Nov 16, 2026
210 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 16, 2026
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 18, 2026
Antalya 26.3 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 18, 2026
Antalya 26.6 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 18, 2026
Stable 26.3 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 19, 2026
Stable 26.8 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 20, 2026
Antalya 26.8 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Nov 23, 2026
Reuse the GCM cipher context between rows in encrypt/decrypt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants