XOR-Instead-of-Exponentiation Notation Bug (Diffie-Hellman)
- •
What it is
- •
A specific, memorable implementation bug where a "Diffie-Hellman" implementation accidentally uses the bitwise XOR operator (
^) instead of exponentiation (**/pow) — sometimes from genuine confusion (many languages use^for exponentiation, but Python and C-family languages use it for XOR), sometimes compounded by an operator precedence mistake on top. In the specific case here, the codeg ^ a % plooks like it should mean , but Python evaluates%before^, so it actually computes — a completely different, trivially breakable operation (XOR is its own inverse and commutative/associative, giving zero cryptographic hardness — the exact same underlying weakness as Additive Group Diffie-Hellman Break (Trivial Discrete Log in Additive Groups), just via XOR instead of addition).
- •
- •
When to apply
- •
Source code labeled as Diffie-Hellman uses
^anywhere near where exponentiation should appear — always check the actual operator precedence and semantics rather than trusting variable names likegenerate_public_int.
- •
- •
Math
- •
With (so trivially), the "public value" ; since similarly, and the "shared secret" is computed as : . An eavesdropper who intercepts , , and can recover the same value directly: — matching the legitimate shared secret exactly, using nothing but public values.
- •
- •
Worked example
- •
Given intercepted , , and from a real captured exchange, computing directly reproduced the exact AES key both parties derived — no private values, no search, just three XORs.
- •
- •
Python
- •
shared_secret = A ^ g ^ B # recovers the "shared secret" from public values alone
- •
- •
- •
Cards
- •
What's the specific Python gotcha that turns "exponentiation" into XOR here?
- •
%binds tighter than^in Python, sog ^ a % pevaluates asg ^ (a % p), not(g ^ a) % p— silently replacing modular exponentiation with plain XOR.
- •
- •
Why can an eavesdropper recover the shared secret from just the three public values A, g, B?
- •
Because XOR is commutative, associative, and self-inverse, so A ^ g ^ B algebraically cancels down to exactly the same value both legitimate parties compute.
- •
- •