Skip to content
Crypto Audit finding Advanced

validateSignature(...) in EllipticCurve mixes up Jacobian and projective coordinates

Holmgren Follow with auditor0517

Originally disclosed on Code4rena

Submitted by Holmgren, also found by auditor0517

Currently not exploitable because this bug is cancelled out by another issue (see my Gas report). If the other issue is fixed validateSignature will return completely incorrect values.

Details

In https://github.com/code-423n4/2023-04-ens/blob/45ea10bacb2a398e14d711fe28d1738271cd7640/contracts/dnssec-oracle/algorithms/EllipticCurve.sol#L415 validateSignature converts to affine coordinates from Jacobian coordinates, i.e. $X_a = X_j \cdot (Z_j^{-1})^2$.

However, the inputs from the previous computation https://github.com/code-423n4/2023-04-ens/blob/45ea10bacb2a398e14d711fe28d1738271cd7640/contracts/dnssec-oracle/algorithms/EllipticCurve.sol#L408 are actually projective coordinates and the correct conversion formula is $X_a = X_p \cdot Z_p^{-1}$.

This has been working so far only because the EllipticCurve performs a redundant chain of immediate conversions projective->affine->projective->affine and so during that last conversion $Z = 1$. Should the chain of redundant conversions be fixed, validateSignature will no longer work correctly.

Recommended Mitigation Steps

To just fix this bug:

diff --git a/contracts/dnssec-oracle/algorithms/EllipticCurve.sol b/contracts/dnssec-oracle/algorithms/EllipticCurve.sol
index 6861264..ea7e865 100644
--- a/contracts/dnssec-oracle/algorithms/EllipticCurve.sol
+++ b/contracts/dnssec-oracle/algorithms/EllipticCurve.sol
@@ -412,7 +412,7 @@ contract EllipticCurve {
         }

         uint256 Px = inverseMod(P[2], p);
-        Px = mulmod(P[0], mulmod(Px, Px, p), p);
+        Px = mulmod(P[0], Px, p);

         return Px % n == rs[0];
     }

Or to fix this bug and optimize out the redundant conversions chain:

diff --git a/contracts/dnssec-oracle/algorithms/EllipticCurve.sol b/contracts/dnssec-oracle/algorithms/EllipticCurve.sol
index 6861264..8568be2 100644
--- a/contracts/dnssec-oracle/algorithms/EllipticCurve.sol
+++ b/contracts/dnssec-oracle/algorithms/EllipticCurve.sol
@@ -405,14 +405,13 @@ contract EllipticCurve {
         uint256 sInv = inverseMod(rs[1], n);
         (x1, y1) = multiplyScalar(gx, gy, mulmod(uint256(message), sInv, n));
         (x2, y2) = multiplyScalar(Q[0], Q[1], mulmod(rs[0], sInv, n));
-        uint256[3] memory P = addAndReturnProjectivePoint(x1, y1, x2, y2);
+        (uint256 Px,, uint256 Pz) = addProj(x1, y1, 1, x2, y2, 1);

-        if (P[2] == 0) {
+        if (Pz == 0) {
             return false;
         }

-        uint256 Px = inverseMod(P[2], p);
-        Px = mulmod(P[0], mulmod(Px, Px, p), p);
+        Px = mulmod(Px, inverseMod(Pz, p), p);

         return Px % n == rs[0];
     }

Arachnid (ENS) confirmed

d3e4 (warden) commented:

I agree that EllipticCurve.sol is somewhat of a bodge. It’s correct what the warden says in that ”EllipticCurve mixes up” something. But actually it adds affine points and then trivially converts them to jacobian/projective coordinates. Since they are trivial they are the same in jacobian as in projective, so one could say that it’s as much a misnamed function as a confused computation.

But it’s also correct what the warden himself said that it is “currently not exploitable”. In fact, I’m quite sure it’s not exploitable even if “the other issue is fixed”. Then it will just invalidate every signature.

Both recommendations here are therefore just gas savings and refactoring. mulmod(Px, Px, p) is indeed a redundant computation, because Px == 1 always here. But so is inverseMod(P[2], p) redundant. The first recommendation can be simplified even further to just

- uint256 Px = inverseMod(P[2], p);
- Px = mulmod(P[0], mulmod(Px, Px, p), p);

- return Px % n == rs[0];
+ return P[0] % n == rs[0];

instead, since P[2] == 1.

The second recommendation is a better optimisation but still does redundant conversions in multiplyScalar, which also should be corrected then. The whole point of using projective coordinates is to do the modular inverse (i.e. convert to affine) only at the end.

In any case, there is nothing exploitable here, no funds or functionality are at risk. The code does what it’s supposed to do as it is, just not in the prettiest way. And the way in which the hypothetical issue is proposed to arise is by fixing the very same thing that the hypothetical issue is itself based on, i.e. it is said that fixing the needless conversion between coordinate representations causes an error due to coordinate conversions. One should assume that reworking the coordinate handling would fix all coordinate issues. And if the bug described here were to arise in the suggested manner, it would be immediately noticed in testing.

This is a bug that is not yet a bug but could be a bug that is impossible to miss if someone were to create this bug so it’s never going to actually be a bug.

I think this is a good catch, but the severity is only QA/Gas.

LSDan (judge) commented:

I agree with @d3e4 that the bug is highly unlikely to make it to production without being caught. In this case, however, if it were to make it into production, the “function of the protocol or availability could be impacted”. That makes this a valid medium in my view. I agree with the warden and sponsor.


The original report as published on Code4rena, by Holmgren. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

What is known

A medium-severity crypto bug in ENS. Code4rena made it public on 31 August 2023. Found in the 2023-04-ens audit contest (finding M-06), also found by 1 other warden.

About Crypto

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

More by Holmgren

View all

1 more disclosed report, best first.

More from ENS

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.