Back to Security & Vulnerability Analysis

constant-time-analysis

cryptographytiming attacksside-channelconstant-timesecurity auditvulnerability detectioncode reviewsecret leakage
5.7k📄 CC-BY-SA-4.0🕒 2026-06-15Source ↗

Install this skill

npx skills add trailofbits/skills

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

Constant-time analysis is a security-focused diagnostic skill that identifies cryptographic vulnerabilities where program execution time varies based on secret data. Such variations create side-channel leaks, allowing attackers to reconstruct private keys or tokens by observing processing delays. This tool examines source code and compiled binaries to detect data-dependent branching, memory access patterns, or arithmetic operations—like division or modulo—that introduce timing inequalities. By mapping instruction sequences against secret-derived inputs, the tool provides visibility into hidden performance variances that standard unit tests frequently overlook. It ensures that security-critical routines such as decryption, signing, and key derivation operate in fixed durations regardless of the underlying input values, effectively mitigating timing-based observation attacks. The skill supports multiple language environments, adjusting its methodology based on whether it is analyzing machine-native instructions or intermediate language bytecode.

When to Use This Skill

  • Auditing custom implementations of cryptographic primitives like AES or RSA
  • Verifying authentication token validation logic against timing extraction attacks
  • Scanning signature verification functions prior to library deployment
  • Regression testing after applying performance optimizations to crypto-code

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • check if my encryption code has timing leaks
  • is this key derivation function constant-time?
  • run a side-channel analysis on these crypto functions
  • verify resistance against timing attacks for my sign method
  • analyze this code for secret data leakage through execution variations

Pro Tips

  • 💡Run this skill early and often during cryptographic development to catch issues before they compound.
  • 💡Combine with other security analysis skills (e.g., input validation, memory safety) for comprehensive code hardening.
  • 💡Provide specific code snippets or function names (e.g., `sign`, `encrypt`) when prompting for focused analysis.

What this skill does

  • Detection of data-dependent branching paths involving secret variables
  • Identification of non-constant time arithmetic instructions like division and modulo
  • Architecture-aware analysis for native binaries across x86_64 and arm64
  • Optimization-level sensitivity testing to prevent compiler-introduced timing leaks
  • Bytecode inspection for VM-based environments including Java, C#, and Kotlin

When not to use it

  • Code where data-dependent execution time is an intentional design feature
  • Standard business logic or UI components that do not handle secret material

Example workflow

  1. Identify the source file containing cryptographic implementation
  2. Run initial scan using default settings to flag high-level variances
  3. Specify target functions if the source file is large or complex
  4. Execute architecture-specific scans for compiled binaries to ensure hardware-level safety
  5. Review detected timing warnings and refactor branch logic where necessary
  6. Perform final re-scan to confirm the removal of execution time dependencies

Prerequisites

  • Compiler toolchain appropriate for the target language (e.g., clang, gcc, rustc)
  • JDK or relevant runtime environment for bytecode analysis
  • Source code access to the cryptographic implementation

Pitfalls & limitations

  • !Does not account for hardware-specific speculative execution leaks like Spectre
  • !VM-based analysis reviews bytecode and may miss JIT-compiled timing variations
  • !False positives can occur in complex logic if secret dependencies are not correctly flagged

FAQ

Why does this tool ask for optimization levels?
Compilers often transform code during optimization, potentially introducing branching or instruction patterns that invalidate previous constant-time assumptions.
Does this tool work on obfuscated code?
No, it requires readable code or standard bytecode to effectively map execution paths and analyze data dependencies.
Can it prevent all side-channel attacks?
It specifically targets timing-based leaks. It does not protect against power analysis, electromagnetic, or acoustic side-channel attacks.

How it compares

Generic prompts rely on pattern matching and often miss subtle arithmetic leaks, whereas this tool performs rigorous instruction-level flow analysis to mathematically isolate timing variances.

Source & trust

5.7k stars📄 CC-BY-SA-4.0🕒 Updated 2026-06-15
📄 Full skill instructions — original source: trailofbits/skills
# Constant-Time Analysis

Analyze cryptographic code to detect operations that leak secret data through execution timing variations.

## When to Use

User writing crypto code? ──yes──> Use this skill

no

v
User asking about timing attacks? ──yes──> Use this skill

no

v
Code handles secret keys/tokens? ──yes──> Use this skill

no

v
Skip this skill


**Concrete triggers:**

- User implements signature, encryption, or key derivation
- Code contains / or % operators on secret-derived values
- User mentions "constant-time", "timing attack", "side-channel", "KyberSlash"
- Reviewing functions named sign, verify, encrypt, decrypt, derive_key

## When NOT to Use

- Non-cryptographic code (business logic, UI, etc.)
- Public data processing where timing leaks don't matter
- Code that doesn't handle secrets, keys, or authentication tokens
- High-level API usage where timing is handled by the library

## Language Selection

Based on the file extension or language context, refer to the appropriate guide:

| Language | File Extensions | Guide |
| ---------- | --------------------------------- | -------------------------------------------------------- |
| C, C++ | .c, .h, .cpp, .cc, .hpp | [references/compiled.md](references/compiled.md) |
| Go | .go | [references/compiled.md](references/compiled.md) |
| Rust | .rs | [references/compiled.md](references/compiled.md) |
| Swift | .swift | [references/swift.md](references/swift.md) |
| Java | .java | [references/vm-compiled.md](references/vm-compiled.md) |
| Kotlin | .kt, .kts | [references/kotlin.md](references/kotlin.md) |
| C# | .cs | [references/vm-compiled.md](references/vm-compiled.md) |
| PHP | .php | [references/php.md](references/php.md) |
| JavaScript | .js, .mjs, .cjs | [references/javascript.md](references/javascript.md) |
| TypeScript | .ts, .tsx | [references/javascript.md](references/javascript.md) |
| Python | .py | [references/python.md](references/python.md) |
| Ruby | .rb | [references/ruby.md](references/ruby.md) |

## Quick Start

# Analyze any supported file type
uv run {baseDir}/ct_analyzer/analyzer.py <source_file>

# Include conditional branch warnings
uv run {baseDir}/ct_analyzer/analyzer.py --warnings <source_file>

# Filter to specific functions
uv run {baseDir}/ct_analyzer/analyzer.py --func 'sign|verify' <source_file>

# JSON output for CI
uv run {baseDir}/ct_analyzer/analyzer.py --json <source_file>


### Native Compiled Languages Only (C, C++, Go, Rust)

# Cross-architecture testing (RECOMMENDED)
uv run {baseDir}/ct_analyzer/analyzer.py --arch x86_64 crypto.c
uv run {baseDir}/ct_analyzer/analyzer.py --arch arm64 crypto.c

# Multiple optimization levels
uv run {baseDir}/ct_analyzer/analyzer.py --opt-level O0 crypto.c
uv run {baseDir}/ct_analyzer/analyzer.py --opt-level O3 crypto.c


### VM-Compiled Languages (Java, Kotlin, C#)

# Analyze Java bytecode
uv run {baseDir}/ct_analyzer/analyzer.py CryptoUtils.java

# Analyze Kotlin bytecode (Android/JVM)
uv run {baseDir}/ct_analyzer/analyzer.py CryptoUtils.kt

# Analyze C# IL
uv run {baseDir}/ct_analyzer/analyzer.py CryptoUtils.cs


Note: Java, Kotlin, and C# compile to bytecode (JVM/CIL) that runs on a virtual machine with JIT compilation. The analyzer examines the bytecode directly, not the JIT-compiled native code. The --arch and --opt-level flags do not apply to these languages.

### Swift (iOS/macOS)

# Analyze Swift for native architecture
uv run {baseDir}/ct_analyzer/analyzer.py crypto.swift

# Analyze for specific architecture (iOS devices)
uv run {baseDir}/ct_analyzer/analyzer.py --arch arm64 crypto.swift

# Analyze with different optimization levels
uv run {baseDir}/ct_analyzer/analyzer.py --opt-level O0 crypto.swift


Note: Swift compiles to native code like C/C++/Go/Rust, so it uses assembly-level analysis and supports --arch and --opt-level flags.

### Prerequisites

| Language | Requirements |
| ---------------------- | --------------------------------------------------------- |
| C, C++, Go, Rust | Compiler in PATH (gcc/clang, go, rustc) |
| Swift | Xcode or Swift toolchain (swiftc in PATH) |
| Java | JDK with javac and javap in PATH |
| Kotlin | Kotlin compiler (kotlinc) + JDK (javap) in PATH |
| C# | .NET SDK + ilspycmd (dotnet tool install -g ilspycmd) |
| PHP | PHP with VLD extension or OPcache |
| JavaScript/TypeScript | Node.js in PATH |
| Python | Python 3.x in PATH |
| Ruby | Ruby with --dump=insns support |

**macOS users**: Homebrew installs Java and .NET as "keg-only". You must add them to your PATH:

# For Java (add to ~/.zshrc)
export PATH="/opt/homebrew/opt/openjdk@21/bin:$PATH"

# For .NET tools (add to ~/.zshrc)
export PATH="$HOME/.dotnet/tools:$PATH"


See [references/vm-compiled.md](references/vm-compiled.md) for detailed setup instructions and troubleshooting.

## Quick Reference

| Problem | Detection | Fix |
| ---------------------- | ------------------------------- | -------------------------------------------- |
| Division on secrets | DIV, IDIV, SDIV, UDIV | Barrett reduction or multiply-by-inverse |
| Branch on secrets | JE, JNE, BEQ, BNE | Constant-time selection (cmov, bit masking) |
| Secret comparison | Early-exit memcmp | Use crypto/subtle or constant-time compare |
| Weak RNG | rand(), mt_rand, Math.random | Use crypto-secure RNG |
| Table lookup by secret | Array subscript on secret index | Bit-sliced lookups |

## Interpreting Results

**PASSED** - No variable-time operations detected.

**FAILED** - Dangerous instructions found. Example:

[ERROR] SDIV
Function: decompose_vulnerable
Reason: SDIV has early termination optimization; execution time depends on operand values


## Verifying Results (Avoiding False Positives)

**CRITICAL**: Not every flagged operation is a vulnerability. The tool has no data flow analysis - it flags ALL potentially dangerous operations regardless of whether they involve secrets.

For each flagged violation, ask: **Does this operation's input depend on secret data?**

1. **Identify the secret inputs** to the function (private keys, plaintext, signatures, tokens)

2. **Trace data flow** from the flagged instruction back to inputs

3. **Common false positive patterns**:

// FALSE POSITIVE: Division uses public constant, not secret
int num_blocks = data_len / 16; // data_len is length, not content

// TRUE POSITIVE: Division involves secret-derived value
int32_t q = secret_coef / GAMMA2; // secret_coef from private key


4. **Document your analysis** for each flagged item

### Quick Triage Questions

| Question | If Yes | If No |
| ------------------------------------------------- | --------------------- | --------------------- |
| Is the operand a compile-time constant? | Likely false positive | Continue |
| Is the operand a public parameter (length, count)?| Likely false positive | Continue |
| Is the operand derived from key/plaintext/secret? | **TRUE POSITIVE** | Likely false positive |
| Can an attacker influence the operand value? | **TRUE POSITIVE** | Likely false positive |

## Limitations

1. **Static Analysis Only**: Analyzes assembly/bytecode, not runtime behavior. Cannot detect cache timing or microarchitectural side-channels.

2. **No Data Flow Analysis**: Flags all dangerous operations regardless of whether they process secrets. Manual review required.

3. **Compiler/Runtime Variations**: Different compilers, optimization levels, and runtime versions may produce different output.

## Real-World Impact

- **KyberSlash (2023)**: Division instructions in post-quantum ML-KEM implementations allowed key recovery
- **Lucky Thirteen (2013)**: Timing differences in CBC padding validation enabled plaintext recovery
- **RSA Timing Attacks**: Early implementations leaked private key bits through division timing

## References

- [Cryptocoding Guidelines](https://github.com/veorq/cryptocoding) - Defensive coding for crypto
- [KyberSlash](https://kyberslash.cr.yp.to/) - Division timing in post-quantum crypto
- [BearSSL Constant-Time](https://www.bearssl.org/constanttime.html) - Practical constant-time techniques

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/constant-time-analysis/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/trailofbits/skills/constant-time-analysis/SKILL.md
  • Cursor: ~/.cursor/skills/trailofbits/skills/constant-time-analysis/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/trailofbits/skills/constant-time-analysis/SKILL.md

🚀 Install with CLI:
npx skills add trailofbits/skills

Read the Master Guide: Mastering Agent Skills

Recommended Rules

View more rules

Recommended Workflows

View more workflows

Recommended MCP Servers

View more MCP servers

Take It Further

Maximize your productivity with these powerful resources

📋

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
📖

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid security & vulnerability analysis issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under Security & Vulnerability Analysis and is published by Trail of Bits, maintained in trailofbits/skills.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.