Files
swift-crypto/Sources/_CryptoExtras/OPRFs/VOPRFServer.swift
T
Si Beaumont ff0f781cf7 extras: Add EC toolbox abstractions and OPRF(P-384, SHA-384) VOPRF API (#292)
## Motivation

We would like to provide support for the P384-SHA384 Verifiable Oblivious Pseudorandom Function (VOPRF) as defined in [RFC 9497: VOPRF Protocol](https://www.rfc-editor.org/rfc/rfc9497.html#name-voprf-protocol).

## Modifications

- Add protocols for some "ECToolbox" abstractions: `Group`, `GroupElement`, `GroupScalar`, `HashToGroup`.
- Add implementations backed by BoringSSL.
- Duplicate some utilities from Crypto into _CryptoExtras: hexadecimal bytes and I2OSP.
- Add VOPRF API, rooted at `enum P384._VOPRF`.

## Results

- New internal abstractions that make it easier to bring up new implementations.
- New API for `OPRF(P-384, SHA-384)` in VOPRF mode.

## Tests

- Test vectors for internal `HashToField` implementation.
- Test vectors for internal VOPRF implementation.
- Tests of the VOPRF public API.
2024-11-22 09:30:35 +00:00

56 lines
2.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import Crypto
@available(macOS 10.15, iOS 13.2, tvOS 13.2, watchOS 6.1, *)
extension OPRF {
struct VerifiableServer<H2G: HashToGroup> {
typealias G = H2G.G
let server: OPRF.Server<H2G>
init(ciphersuite: Ciphersuite<H2G>, privateKey: G.Scalar = G.Scalar.random, v8CompatibilityMode: Bool = false, mode: OPRF.Mode) throws {
if mode != .partiallyOblivious && mode != .verifiable {
throw OPRF.Errors.incompatibleMode
}
self.server = .init(mode: mode, ciphersuite: ciphersuite, privateKey: privateKey, v8CompatibilityMode: v8CompatibilityMode)
}
var publicKey: G.Element {
server.publicKey
}
func evaluate(blindedElement: G.Element, info: Data? = nil, proofScalar: G.Scalar = G.Scalar.random) throws ->
(G.Element, DLEQProof<H2G.G.Element.Scalar>) {
let hasInfo = (info != nil)
if hasInfo && self.server.mode == .verifiable && !server.v8CompatibilityMode {
throw OPRF.Errors.invalidModeForInfo
}
let (evaluatedElement, proof) = try self.server.evaluate(blindedElement: blindedElement,
info: info,
proofScalar: proofScalar)
return (evaluatedElement, proof!)
}
internal func verifyFinalize(msg: Data,
output: Data,
info: Data?) throws -> Bool {
return try server.verifyFinalize(msg: msg, output: output, info: info)
}
}
}