diff --git a/Sources/CryptoExtras/OPRFs/VOPRF+API.swift b/Sources/CryptoExtras/OPRFs/VOPRF+API.swift index 44476b0e..6fc03dd3 100644 --- a/Sources/CryptoExtras/OPRFs/VOPRF+API.swift +++ b/Sources/CryptoExtras/OPRFs/VOPRF+API.swift @@ -35,6 +35,9 @@ extension P384 { static var ciphersuite: Ciphersuite { Ciphersuite(H2G.self) } + /// The maximum length, in bytes, of an input, imposed by the two-byte length prefix in the RFC 9497 framing. + static let maximumInputByteCount = 0xFFFF + /// A P-384 public key used to blind inputs and finalize blinded elements. public struct PublicKey { fileprivate typealias BackingPublicKey = P384.Signing.PublicKey @@ -367,6 +370,9 @@ extension P384._VOPRF { @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 2.0, *) extension P384._VOPRF.PublicKey { internal func blind(_ input: D, with fixedBlind: P384._VOPRF.H2G.G.Scalar) throws -> P384._VOPRF.BlindedInput { + guard input.count <= P384._VOPRF.maximumInputByteCount else { + throw CryptoKitError.incorrectParameterSize + } let input = Data(input) let (blind, blindedElement) = Self.client.blindMessage(input, blind: fixedBlind) return P384._VOPRF.BlindedInput( @@ -436,6 +442,9 @@ extension P384._VOPRF.PrivateKey { /// /// - Seealso: [RFC 9497: VOPRF Protocol - Evaluate]( https://cfrg.github.io/draft-irtf-cfrg-voprf/draft-irtf-cfrg-voprf.html#section-3.3.2-7). public func evaluate(_ input: D) throws -> Data { + guard input.count <= P384._VOPRF.maximumInputByteCount else { + throw CryptoKitError.incorrectParameterSize + } let inputElement = P384._VOPRF.H2G.hashToGroup( Data(input), domainSeparationString: Self.hashToGroupDST diff --git a/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift b/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift index 14cdda05..fe3b30be 100644 --- a/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift +++ b/Tests/CryptoExtrasTests/OPRFs/VOPRFPublicAPITests.swift @@ -96,4 +96,21 @@ final class VOPRFPublicAPITests: XCTestCase { XCTAssertThrowsError(try P384._VOPRF.BlindedElement(oprfRepresentation: uncompressed)) } + + func testInputLongerThanRFC9497LimitThrows() throws { + let privateKey = P384._VOPRF.PrivateKey() + let publicKey = privateKey.publicKey + + // RFC 9497 caps inputs at 2^16 - 1 bytes, the most the two-byte length prefix in the framing can encode. + let maximumInput = Data(repeating: 0x41, count: 0xFFFF) + let blindedInput = try publicKey.blind(maximumInput) + let blindEvaluation = try privateKey.evaluate(blindedInput.blindedElement) + XCTAssertNoThrow(try publicKey.finalize(blindedInput, using: blindEvaluation)) + XCTAssertNoThrow(try privateKey.evaluate(maximumInput)) + + // Oversized inputs are rejected where they enter, so finalize can never be given one. + let oversizedInput = Data(repeating: 0x41, count: 0x1_0000) + XCTAssertThrowsError(try publicKey.blind(oversizedInput)) + XCTAssertThrowsError(try privateKey.evaluate(oversizedInput)) + } }