constant-time-analysis by trailofbits/skills
npx skills add https://github.com/trailofbits/skills --skill constant-time-analysis分析密码学代码,检测通过执行时间差异泄露秘密数据的操作。
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
具体触发条件:
/ 或 % 运算符sign、verify、encrypt、decrypt、 的函数广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
derive_key根据文件扩展名或语言上下文,参考相应的指南:
| 语言 | 文件扩展名 | 指南 |
|---|---|---|
| C, C++ | .c, .h, .cpp, .cc, .hpp | references/compiled.md |
| Go | .go | references/compiled.md |
| Rust | .rs | references/compiled.md |
| Swift | .swift | references/swift.md |
| Java | .java | references/vm-compiled.md |
| Kotlin | .kt, .kts | references/kotlin.md |
| C# | .cs | references/vm-compiled.md |
| PHP | .php | references/php.md |
| JavaScript | .js, .mjs, .cjs | references/javascript.md |
| TypeScript | .ts, .tsx | references/javascript.md |
| Python | .py | references/python.md |
| Ruby | .rb | references/ruby.md |
# 分析任何支持的文件类型
uv run {baseDir}/ct_analyzer/analyzer.py <source_file>
# 包含条件分支警告
uv run {baseDir}/ct_analyzer/analyzer.py --warnings <source_file>
# 筛选特定函数
uv run {baseDir}/ct_analyzer/analyzer.py --func 'sign|verify' <source_file>
# 用于持续集成的 JSON 输出
uv run {baseDir}/ct_analyzer/analyzer.py --json <source_file>
# 跨架构测试(推荐)
uv run {baseDir}/ct_analyzer/analyzer.py --arch x86_64 crypto.c
uv run {baseDir}/ct_analyzer/analyzer.py --arch arm64 crypto.c
# 多个优化级别
uv run {baseDir}/ct_analyzer/analyzer.py --opt-level O0 crypto.c
uv run {baseDir}/ct_analyzer/analyzer.py --opt-level O3 crypto.c
# 分析 Java 字节码
uv run {baseDir}/ct_analyzer/analyzer.py CryptoUtils.java
# 分析 Kotlin 字节码(Android/JVM)
uv run {baseDir}/ct_analyzer/analyzer.py CryptoUtils.kt
# 分析 C# 中间语言
uv run {baseDir}/ct_analyzer/analyzer.py CryptoUtils.cs
注意:Java、Kotlin 和 C# 编译为在虚拟机(JVM/CIL)上运行的字节码,并通过即时编译执行。分析器直接检查字节码,而非即时编译后的原生代码。--arch 和 --opt-level 标志不适用于这些语言。
# 分析原生架构的 Swift 代码
uv run {baseDir}/ct_analyzer/analyzer.py crypto.swift
# 分析特定架构(iOS 设备)
uv run {baseDir}/ct_analyzer/analyzer.py --arch arm64 crypto.swift
# 使用不同优化级别进行分析
uv run {baseDir}/ct_analyzer/analyzer.py --opt-level O0 crypto.swift
注意:Swift 像 C/C++/Go/Rust 一样编译为原生代码,因此使用汇编级分析并支持 --arch 和 --opt-level 标志。
| 语言 | 要求 |
|---|---|
| C, C++, Go, Rust | PATH 中的编译器(gcc/clang、go、rustc) |
| Swift | Xcode 或 Swift 工具链(PATH 中的 swiftc) |
| Java | PATH 中包含 javac 和 javap 的 JDK |
| Kotlin | PATH 中包含 Kotlin 编译器(kotlinc)+ JDK(javap) |
| C# | .NET SDK + ilspycmd(dotnet tool install -g ilspycmd) |
| PHP | 带有 VLD 扩展或 OPcache 的 PHP |
| JavaScript/TypeScript | PATH 中的 Node.js |
| Python | PATH 中的 Python 3.x |
| Ruby | 支持 --dump=insns 的 Ruby |
macOS 用户:Homebrew 将 Java 和 .NET 安装为"仅桶装"。您必须将它们添加到 PATH 中:
# 对于 Java(添加到 ~/.zshrc)
export PATH="/opt/homebrew/opt/openjdk@21/bin:$PATH"
# 对于 .NET 工具(添加到 ~/.zshrc)
export PATH="$HOME/.dotnet/tools:$PATH"
有关详细设置说明和故障排除,请参阅 references/vm-compiled.md。
| 问题 | 检测 | 修复方法 |
|---|---|---|
| 对秘密进行除法运算 | DIV, IDIV, SDIV, UDIV | Barrett 约简或乘以逆元 |
| 基于秘密的分支 | JE, JNE, BEQ, BNE | 恒定时间选择(cmov、位掩码) |
| 秘密比较 | 提前退出的 memcmp | 使用 crypto/subtle 或恒定时间比较 |
| 弱随机数生成器 | rand(), mt_rand, Math.random | 使用密码学安全的随机数生成器 |
| 基于秘密的查表 | 使用秘密索引的数组下标 | 位切片查表 |
通过 - 未检测到可变时间操作。
失败 - 发现危险指令。示例:
[ERROR] SDIV
Function: decompose_vulnerable
Reason: SDIV has early termination optimization; execution time depends on operand values
关键提示:并非每个标记的操作都是漏洞。该工具没有数据流分析功能 - 它会标记所有潜在危险操作,无论它们是否涉及秘密。
对于每个标记的违规项,请询问:此操作的输入是否依赖于秘密数据?
识别函数的秘密输入(私钥、明文、签名、令牌)
从标记的指令回溯到输入的数据流
常见的误报模式:
// 误报:除法使用公开常量,而非秘密
int num_blocks = data_len / 16; // data_len 是长度,而非内容
// 真阳性:除法涉及秘密派生值
int32_t q = secret_coef / GAMMA2; // secret_coef 来自私钥
记录每个标记项的分析
| 问题 | 如果是 | 如果否 |
|---|---|---|
| 操作数是编译时常量吗? | 可能是误报 | 继续 |
| 操作数是公共参数(长度、计数)吗? | 可能是误报 | 继续 |
| 操作数派生自密钥/明文/秘密吗? | 真阳性 | 可能是误报 |
| 攻击者能影响操作数值吗? | 真阳性 | 可能是误报 |
仅静态分析:分析汇编/字节码,而非运行时行为。无法检测缓存时序或微架构侧信道。
无数据流分析:标记所有危险操作,无论它们是否处理秘密。需要人工审查。
编译器/运行时差异:不同的编译器、优化级别和运行时版本可能产生不同的输出。
每周安装量
1.1K
代码仓库
GitHub 星标数
3.9K
首次出现
2026年1月19日
安全审计
安装于
claude-code974
opencode932
gemini-cli910
codex906
cursor880
github-copilot850
Analyze cryptographic code to detect operations that leak secret data through execution timing variations.
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:
/ or % operators on secret-derived valuessign, verify, encrypt, decrypt, derive_keyBased 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 |
| Go | .go | references/compiled.md |
| Rust |
# 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>
# 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
# 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.
# 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.
| 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 () in PATH |
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 for detailed setup instructions and troubleshooting.
| 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 |
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
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?
Identify the secret inputs to the function (private keys, plaintext, signatures, tokens)
Trace data flow from the flagged instruction back to inputs
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
Document your analysis for each flagged item
| 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 |
Static Analysis Only : Analyzes assembly/bytecode, not runtime behavior. Cannot detect cache timing or microarchitectural side-channels.
No Data Flow Analysis : Flags all dangerous operations regardless of whether they process secrets. Manual review required.
Compiler/Runtime Variations : Different compilers, optimization levels, and runtime versions may produce different output.
Weekly Installs
1.1K
Repository
GitHub Stars
3.9K
First Seen
Jan 19, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
claude-code974
opencode932
gemini-cli910
codex906
cursor880
github-copilot850
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
NestJS专家服务 | 企业级TypeScript后端开发与架构设计
1,000 周安装
安全代码卫士:AI驱动的安全编码指南与最佳实践,防止SQL注入、XSS攻击
1,000 周安装
ESLint迁移到Oxlint完整指南:JavaScript/TypeScript项目性能优化工具
1,000 周安装
Chrome CDP 命令行工具:轻量级浏览器自动化,支持截图、执行JS、无障碍快照
1,000 周安装
Sanity内容建模最佳实践:结构化内容设计原则与无头CMS指南
1,000 周安装
AI Sprint规划器 - 敏捷团队Scrum迭代计划工具,自动估算故事点与容量管理
1,000 周安装
.rs| references/compiled.md |
| Swift | .swift | references/swift.md |
| Java | .java | references/vm-compiled.md |
| Kotlin | .kt, .kts | references/kotlin.md |
| C# | .cs | references/vm-compiled.md |
| PHP | .php | references/php.md |
| JavaScript | .js, .mjs, .cjs | references/javascript.md |
| TypeScript | .ts, .tsx | references/javascript.md |
| Python | .py | references/python.md |
| Ruby | .rb | references/ruby.md |
javap| 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 |