From 1c80d3aff53fedc445b60692980cb4f19a618657 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Fri, 9 Oct 2026 12:38:27 +0100 Subject: [PATCH] Require compressed encoding when deserializing OPRF elements (#479) ### Motivation: `P384._VOPRF.BlindedElement(oprfRepresentation:)` accepts uncompressed x9.63 encoded inputs, but RFC 9497 states only inputs using compressed encoding should be accepted: ``` SerializeElement(A): Implemented using the compressed Elliptic-Curve-Point-to-Octet-String method... DeserializeElement(buf): Implemented by attempting to deserialize a 49-byte array to a public key using the compressed Octet-String-to-Elliptic-Curve-Point method... ``` ### Modifications: - Add input guard to only accept points using compressed representation. - Extend the tests. ### Result: - OPRF element deserialization accepts only the compressed encoding. - Uncompressed encodings now throw `CryptoKitError.incorrectParameterSize`. --- .../ECToolbox/BoringSSL/ECToolbox_boring.swift | 3 +++ .../OPRFs/VOPRFPublicAPITests.swift | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Sources/CryptoExtras/ECToolbox/BoringSSL/ECToolbox_boring.swift b/Sources/CryptoExtras/ECToolbox/BoringSSL/ECToolbox_boring.swift index 4bbd37ef..6f9aa347 100644 --- a/Sources/CryptoExtras/ECToolbox/BoringSSL/ECToolbox_boring.swift +++ b/Sources/CryptoExtras/ECToolbox/BoringSSL/ECToolbox_boring.swift @@ -276,6 +276,9 @@ extension OpenSSLCurvePoint { @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) extension OpenSSLCurvePoint: OPRFGroupElement { init(oprfRepresentation data: Data) throws { + guard data.count == C.compressedx962PointByteCount else { + throw CryptoKitError.incorrectParameterSize + } let point = try EllipticCurvePoint(x962Representation: data, on: C.group, context: C.__ffac) self.init(ecPoint: point) } diff --git a/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift b/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift index 2a446541..14cdda05 100644 --- a/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift +++ b/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift @@ -79,4 +79,21 @@ final class VOPRFPublicAPITests: XCTestCase { // [Server] Compute the PRF for the input, without blinding or proof generation. let _: Data = try privateKey.evaluate(input) } + + func testBlindedElementRejectsUncompressedOPRFRepresentation() throws { + /// Check we only accept compressed encoded elements, as per RFC 9497: + /// + /// > SerializeElement(A): Implemented using the compressed Elliptic-Curve-Point-to-Octet-String method... + /// > + /// > DeserializeElement(buf): Implemented by attempting to deserialize a 49-byte array to a public key + /// using the compressed Octet-String-to-Elliptic-Curve-Point method... + /// + /// - See: https://www.rfc-editor.org/rfc/rfc9497.html#name-oprfp-384-sha-384 + let publicKey = P384._VOPRF.PrivateKey().publicKey + let blindedElement = try publicKey.blind(Data("This is some input data".utf8)).blindedElement + let compressed = blindedElement.oprfRepresentation + let uncompressed = try P384.Signing.PublicKey(compressedRepresentation: compressed).x963Representation + + XCTAssertThrowsError(try P384._VOPRF.BlindedElement(oprfRepresentation: uncompressed)) + } }