Skip to content

Use constant-time comparison for TOTP codes and plaintext passwords - #125785

Merged
vdimir merged 1 commit into
masterfrom
vdimir/totp-const-time-comparison
Oct 16, 2026
Merged

vdimir merged 1 commit into
masterfrom
vdimir/totp-const-time-comparison

Conversation

@vdimir

@vdimir vdimir commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Compare TOTP codes and plaintext_password passwords in constant time, so that the response time does not depend on how many leading characters are correct or on which time window matched.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.91-master (included in 26.11 and later)
  • Backported to: 26.10.2.31, 26.9.21.5, 26.8.30.4, 26.7.35.5, 26.3.57.12

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5d44c34]

Summary: ✅


AI Review

Summary

This PR replaces prefix-sensitive comparisons for TOTP codes and basic plaintext_password authentication with constantTimeEquals, and generates all three accepted TOTP windows. At head 5d44c34c92f131586d83e926372c7e79abdd6520, matched-window selection still depends on comparison results, leaving the promised independence from the matching window unproven. ⚠️ Request changes.

Findings

⚠️ Majors

  • src/Access/Common/OneTimePassword.cpp:164–165: Matched-window selection still has secret-dependent control flow. With the same secret and server time, codes matching the current and next windows execute the conditional assignment in different iterations, and subsequent checks of result.has_value differ. Generating every candidate does not make this selection constant-time. The existing unresolved finding reports retained match-dependent branches in an isolated optimized AArch64 build; the current source still contains the same condition. This path runs through checkOneTimePassword before the ordinary password check, so failed password attempts also reach it. Compute candidate time steps unconditionally, select the first match using audited constant-time operations while preserving the 0, -1, 1 priority, and construct the optional result afterward. This retains the existing finding; no duplicate inline comment was posted.
Missing context / blind spots
  • ⚠️ Optimized release binaries and the release compiler are unavailable locally. The prior thread's AArch64 disassembly was not independently reproduced in this run. Reproducible inspection of the optimized callers on supported x86_64 and AArch64 release configurations would close this gap.
Tests
  • ⚠️ Existing authentication tests verify login outcomes and replay rejection, but would also pass before this timing fix. Provide focused regression evidence that every candidate is checked regardless of the matching window, plus optimized-code evidence that comparisons and selection do not branch on secret-derived values. The CI report also reports new_tests_check failing with No new tests have been added.
Final Verdict

⚠️ Request changes. Remove match-dependent window selection and provide reproducible evidence for the claimed constant-time behavior in supported optimized builds.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 14, 2026
@vdimir vdimir added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 14, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-authentication Identity verification: users, credentials, auth methods, external auth providers. label Oct 14, 2026
Comment on lines +164 to +165
if (matched && !result.has_value())
result = static_cast<Int64>(current_time / secret.params.period) + delta;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Matched-window selection still has data-dependent control flow

constantTimeEquals removes prefix-dependent exits, but this condition still branches on each comparison result and computes the time step only for the first match. With the same secret and server time, codes matching delta = 0 and delta = 1 take different paths despite both being accepted. This also runs through checkOneTimePassword before the ordinary password check, so the distinction is observable even when the ordinary password is wrong.

In an isolated build of the unchanged helper and its callers with Clang 21, -O3, and AArch64, the match results retain cbz/cbnz/tbnz branches and conditionally executed udiv. Computing all three candidates therefore does not establish the changelog's promised independence from the matching window.

Please compute candidate steps unconditionally and use audited constant-time selection of the first match, preserving the 0, -1, 1 priority. Construct the optional result after selection, and verify the optimized code in supported release configurations; the existing authentication tests do not prove this property.

@vdimir vdimir added pr-improvement Pull request with some product improvements and removed pr-bugfix Pull request with bugfix, not backported by default labels Oct 16, 2026
@vdimir
vdimir added this pull request to the merge queue Oct 16, 2026
Merged via the queue into master with commit cdd777c Oct 16, 2026
692 of 695 checks passed
@vdimir
vdimir deleted the vdimir/totp-const-time-comparison branch October 16, 2026 16:07
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 16, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 16, 2026
Backport #125785 to 26.9: Use constant-time comparison for TOTP codes and plaintext passwords
clickhouse-gh Bot added a commit that referenced this pull request Oct 16, 2026
Backport #125785 to 26.8: Use constant-time comparison for TOTP codes and plaintext passwords
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #125785 onto 26.3: add only the two constantTimeEquals overloads (and the
<span> include) to StringUtils.h; the ASCII case helpers next to them in the conflict are
separate master changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PedroTadim added a commit that referenced this pull request Oct 19, 2026
Cherry-pick of #125785 onto 26.7: add only the two constantTimeEquals overloads (and the
<span> include) to StringUtils.h; the ASCII case helpers next to them in the conflict are
separate master changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Oct 19, 2026
robot-clickhouse added a commit that referenced this pull request Oct 19, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 19, 2026
Backport #125785 to 26.7: Use constant-time comparison for TOTP codes and plaintext passwords
PedroTadim added a commit that referenced this pull request Oct 20, 2026
Backport #125785 to 26.3: Use constant-time comparison for TOTP codes and plaintext passwords
PedroTadim added a commit that referenced this pull request Oct 20, 2026
Backport #125785 to 26.10: Use constant-time comparison for TOTP codes and plaintext passwords
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-authentication Identity verification: users, credentials, auth methods, external auth providers. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants