Repository navigation
fix(spanner): reject point-at-infinity and non-canonical EC points in OPAQUE - #15146
Conversation
… OPAQUE
## Problem Statement
- During Spanner Omni OPAQUE authentication, `OpaqueUtil.finalize`, `OpaqueUtil.diffieHellman`, and `OpaqueUtil.blind` accepted the elliptic-curve point at infinity (`{0x00}`) and non-canonical point encodings.
- If a rogue login server returned `{0x00}` as `evaluated_message`, `OpaqueUtil.finalize` returned `{0x00}` regardless of the client's password or blind scalar, making all derived OPAQUE envelope and AKE keys password-independent and breaking mutual server authentication.
## Solution
- Enforce canonical 33-byte compressed `secp256r1` point encoding (`0x02` or `0x03` prefix) and reject the point at infinity, non-canonical encodings, and off-curve points before and after scalar multiplication in `OpaqueUtil.finalize`, `OpaqueUtil.diffieHellman`, and `OpaqueUtil.blind` (per RFC 9497 Section 3.3.1 / RFC 9807).
- Validate that scalar inputs (`blind`, `privateKey`, `blindScalar`) are non-null, non-empty, and non-zero modulo the curve order.
- Behavior change: direct callers of `OpaqueUtil.finalize`, `OpaqueUtil.diffieHellman`, or `OpaqueUtil.blind` now receive a checked `GeneralSecurityException` on invalid points or zero/order-multiple scalars rather than `{0x00}` or unchecked `IllegalArgumentException`, `NullPointerException`, or `ArithmeticException`.
There was a problem hiding this comment.
Code Review
This pull request introduces robust validation for elliptic curve points and scalars in OpaqueUtil to align with RFC 9497 and RFC 9807, including rejecting points at infinity, non-canonical encodings, off-curve coordinates, and zero scalars. Comprehensive unit and integration tests have been added to verify these security boundaries. The review feedback recommends tightening the scalar validation in validateNonZeroScalar to strictly enforce the range [1, curve order - 1] instead of only checking for non-zero modulo the curve order, which is a valuable improvement for specification compliance.
| private static BigInteger validateNonZeroScalar( | ||
| byte[] scalarBytes, BigInteger order, String label) throws GeneralSecurityException { | ||
| if (scalarBytes == null || scalarBytes.length == 0) { | ||
| throw new GeneralSecurityException("Invalid " + label + ": scalar must not be null or empty"); | ||
| } | ||
| BigInteger scalar = new BigInteger(1, scalarBytes); | ||
| if (scalar.mod(order).equals(BigInteger.ZERO)) { | ||
| throw new GeneralSecurityException( | ||
| "Invalid " + label + ": scalar must be non-zero modulo curve order"); | ||
| } | ||
| return scalar; | ||
| } |
There was a problem hiding this comment.
According to RFC 9497 Section 2.1 and 3.2, OPRF/OPAQUE scalars (such as blind, privateKey, and blindScalar) MUST be strictly within the range [1, r-1], where r is the prime order of the group.
While checking scalar.mod(order).equals(BigInteger.ZERO) correctly rejects multiples of the order (such as 0 or order), it still accepts non-canonical scalars that are strictly greater than the curve order (for example, order + 1). To adhere strictly to the specification and prevent potential interoperability or canonicalization issues, consider enforcing that the scalar is strictly less than the curve order.
| private static BigInteger validateNonZeroScalar( | |
| byte[] scalarBytes, BigInteger order, String label) throws GeneralSecurityException { | |
| if (scalarBytes == null || scalarBytes.length == 0) { | |
| throw new GeneralSecurityException("Invalid " + label + ": scalar must not be null or empty"); | |
| } | |
| BigInteger scalar = new BigInteger(1, scalarBytes); | |
| if (scalar.mod(order).equals(BigInteger.ZERO)) { | |
| throw new GeneralSecurityException( | |
| "Invalid " + label + ": scalar must be non-zero modulo curve order"); | |
| } | |
| return scalar; | |
| } | |
| private static BigInteger validateNonZeroScalar( | |
| byte[] scalarBytes, BigInteger order, String label) throws GeneralSecurityException { | |
| if (scalarBytes == null || scalarBytes.length == 0) { | |
| throw new GeneralSecurityException("Invalid " + label + ": scalar must not be null or empty"); | |
| } | |
| BigInteger scalar = new BigInteger(1, scalarBytes); | |
| if (scalar.equals(BigInteger.ZERO) || scalar.compareTo(order) >= 0) { | |
| throw new GeneralSecurityException( | |
| "Invalid " + label + ": scalar must be in the range [1, curve order - 1]"); | |
| } | |
| return scalar; | |
| } |
There was a problem hiding this comment.
Keeping the mod(order) == 0 check intentionally since blind is a client-local 32-byte random scalar generated via SecureRandom.nextBytes in LoginClient (which can be >= order with probability ~2^-32), and both ECPoint.multiply and BigInteger.modInverse already operate modulo the curve order.
| BigInteger scalar = new BigInteger(1, scalarBytes); | ||
| if (scalar.mod(order).equals(BigInteger.ZERO)) { | ||
| throw new GeneralSecurityException( | ||
| "Invalid " + label + ": scalar must be non-zero modulo curve order"); | ||
| } | ||
| return scalar; |
There was a problem hiding this comment.
validateNonZeroScalar computes scalar.mod(order) to check for zero, but discards the reduced value and returns the unreduced scalar.
On secp256r1, BouncyCastle uses WNafL2RMultiplier by default, which does not reduce k modulo the curve order — WNafUtil.generateCompactWindowNaf throws an unchecked IllegalArgumentException("'k' must have bitlength < 2^16") if scalar.bitLength() >= 65536 (and does extra work proportional to bitLength for any scalar >= order). As a result, OpaqueUtil.blind() and OpaqueUtil.diffieHellman() can still throw an unchecked IllegalArgumentException on oversized scalars, whereas OpaqueUtil.finalize() succeeds because BigInteger.modInverse(order) returns a reduced value in [1, order - 1].
Returning the already-computed reduced scalar fixes this and keeps all three methods consistent:
| BigInteger scalar = new BigInteger(1, scalarBytes); | |
| if (scalar.mod(order).equals(BigInteger.ZERO)) { | |
| throw new GeneralSecurityException( | |
| "Invalid " + label + ": scalar must be non-zero modulo curve order"); | |
| } | |
| return scalar; | |
| BigInteger reduced = new BigInteger(1, scalarBytes).mod(order); | |
| if (reduced.equals(BigInteger.ZERO)) { | |
| throw new GeneralSecurityException( | |
| "Invalid " + label + ": scalar must be non-zero modulo curve order"); | |
| } | |
| return reduced; |
There was a problem hiding this comment.
Good catch — updated validateNonZeroScalar to return the reduced scalar modulo the curve order and added a regression test for oversized scalars (>= 65536 bits).
There was a problem hiding this comment.
nit: consider moving some of this code into smaller helper methods to make it easier to read and understand what the responses are that are being set up for this test.
There was a problem hiding this comment.
Done — split the rogue server and forged OPAQUE response setup into smaller documented helper methods.
Problem Statement
OpaqueUtil.finalize,OpaqueUtil.diffieHellman, andOpaqueUtil.blindaccepted the elliptic-curve point at infinity ({0x00}) and non-canonical point encodings.{0x00}asevaluated_message,OpaqueUtil.finalizereturned{0x00}regardless of the client's password or blind scalar, making all derived OPAQUE envelope and AKE keys password-independent and breaking mutual server authentication.Solution
secp256r1point encoding (0x02or0x03prefix) and reject the point at infinity, non-canonical encodings, and off-curve points before and after scalar multiplication inOpaqueUtil.finalize,OpaqueUtil.diffieHellman, andOpaqueUtil.blind(per RFC 9497 Section 3.3.1 / RFC 9807).blind,privateKey,blindScalar) are non-null, non-empty, and non-zero modulo the curve order.OpaqueUtil.finalize,OpaqueUtil.diffieHellman, orOpaqueUtil.blindnow receive a checkedGeneralSecurityExceptionon invalid points or zero/order-multiple scalars rather than{0x00}or uncheckedIllegalArgumentException,NullPointerException, orArithmeticException.