Skip to content

fix(spanner): reject point-at-infinity and non-canonical EC points in OPAQUE - #15146

Merged
sakthivelmanii merged 3 commits into
mainfrom
fix/spanner-opaque-point-validation
Nov 27, 2026
Merged

sakthivelmanii merged 3 commits into
mainfrom
fix/spanner-opaque-point-validation

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

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.

… 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`.
@sakthivelmanii
sakthivelmanii requested review from a team as code owners November 23, 2026 13:18

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +277 to +288
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;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

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.

Suggested change
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;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sagnghos sagnghos left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall — great security fix. Left one comment on validateNonZeroScalar to return the reduced scalar before merging.

Comment on lines +282 to +287
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — updated validateNonZeroScalar to return the reduced scalar modulo the curve order and added a regression test for oversized scalars (>= 65536 bits).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — split the rogue server and forged OPAQUE response setup into smaller documented helper methods.

@sakthivelmanii
sakthivelmanii merged commit 420f49e into main Nov 27, 2026
199 checks passed
@sakthivelmanii
sakthivelmanii deleted the fix/spanner-opaque-point-validation branch November 27, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants