mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
+6








431013afaf
This change updates Crypto to WWDC25 equivalent code. Each of these changes were individually merged to the WWDC25 branch. This includes: - #359 - #363 - #365 - #367 - #368 - #397 - #403 - #410 - #407 - #401 - #415 --------- Co-authored-by: YourMJK <37852512+YourMJK@users.noreply.github.com> Co-authored-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Rick Newton-Rogers <rnro@apple.com> Co-authored-by: Raphael <josephnoir@users.noreply.github.com> Co-authored-by: Evan Wilde <ewilde@apple.com> Co-authored-by: Tim Condon <0xTim@users.noreply.github.com> Co-authored-by: Gus Cairo <gus.cairo@apple.com> Co-authored-by: George Barnett <gbarnett@apple.com> Co-authored-by: Fabrice de Gans <Steelskin@users.noreply.github.com> Co-authored-by: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Co-authored-by: Jesse L. Zamora <xtremekforever@gmail.com> Co-authored-by: aryan-25 <aryan_shah@apple.com> Co-authored-by: Gwynne Raskind <gwynne@vapor.codes> Co-authored-by: 3405691582 <dsk@google.com> Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com>
104 lines
5.4 KiB
Swift
104 lines
5.4 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 Crypto
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
|
|
@available(macOS 10.15, iOS 13.2, tvOS 13.2, watchOS 6.1, macCatalyst 13.2, visionOS 1.2, *)
|
|
extension OPRF {
|
|
struct VerifiableClient<H2G: HashToGroup> {
|
|
fileprivate let client: OPRF.Client<H2G>
|
|
typealias G = H2G.G
|
|
|
|
init(ciphersuite: Ciphersuite<H2G>, v8CompatibilityMode: Bool = false, mode: OPRF.Mode) throws {
|
|
if mode != .partiallyOblivious && mode != .verifiable {
|
|
throw OPRF.Errors.incompatibleMode
|
|
}
|
|
|
|
self.client = .init(mode: mode, ciphersuite: ciphersuite, v8CompatibilityMode: v8CompatibilityMode)
|
|
}
|
|
|
|
func blindMessage(_ message: Data, blind: G.Scalar = G.Scalar.random) -> (blind: G.Scalar, blindedElement: G.Element) {
|
|
self.client.blindMessage(message, blind: blind)
|
|
}
|
|
|
|
fileprivate func v8Finalize(message: Data, info: Data?, blind: G.Scalar, evaluatedElement: G.Element, proof: DLEQProof<G.Scalar>, publicKey: G.Element) throws -> Data {
|
|
precondition(self.client.mode == .verifiable)
|
|
let setupCtx = setupContext(mode: client.mode, suite: client.ciphersuite, v8CompatibilityMode: self.client.v8CompatibilityMode)
|
|
let contextDST = "Context-".data(using: .utf8)! + setupCtx
|
|
|
|
let ctx = contextDST + I2OSP(value: (info?.count ?? 0), outputByteCount: 2) + (info ?? Data())
|
|
|
|
let m = try H2G.hashToScalar(ctx, domainSeparationString: setupCtx)
|
|
let t = m * G.Element.generator
|
|
|
|
let u = publicKey + t
|
|
|
|
let blindedElement = self.blindMessage(message, blind: blind).blindedElement
|
|
guard try DLEQ<H2G>.verifyProof(A: H2G.G.Element.generator, B: u,
|
|
CDs: [(C: evaluatedElement, D: blindedElement)],
|
|
proof: proof,
|
|
dst: setupContext(mode: client.mode, suite: client.ciphersuite, v8CompatibilityMode: self.client.v8CompatibilityMode), v8CompatibilityMode: self.client.v8CompatibilityMode) else {
|
|
throw OPRF.Errors.invalidProof
|
|
}
|
|
|
|
return try self.client.finalize(message: message, info: info, blind: blind, evaluatedElement: evaluatedElement)
|
|
|
|
}
|
|
|
|
func finalize(message: Data, info: Data?, blind: G.Scalar, evaluatedElement: G.Element, proof: DLEQProof<G.Scalar>, publicKey: G.Element) throws -> Data {
|
|
if self.client.v8CompatibilityMode { return try v8Finalize(message: message, info: info, blind: blind, evaluatedElement: evaluatedElement, proof: proof, publicKey: publicKey) }
|
|
|
|
let hasInfo = (info != nil)
|
|
if hasInfo && (self.client.mode == .verifiable) {
|
|
throw OPRF.Errors.invalidModeForInfo
|
|
}
|
|
|
|
let setupCtx = setupContext(mode: client.mode, suite: client.ciphersuite, v8CompatibilityMode: self.client.v8CompatibilityMode)
|
|
let blindedElement = self.blindMessage(message, blind: blind).blindedElement
|
|
|
|
if self.client.mode == .verifiable {
|
|
guard try DLEQ<H2G>.verifyProof(A: H2G.G.Element.generator, B: publicKey,
|
|
CDs: [(C: blindedElement, D: evaluatedElement)],
|
|
proof: proof,
|
|
dst: setupContext(mode: client.mode, suite: client.ciphersuite, v8CompatibilityMode: self.client.v8CompatibilityMode), v8CompatibilityMode: self.client.v8CompatibilityMode) else {
|
|
throw OPRF.Errors.invalidProof
|
|
}
|
|
|
|
return try self.client.finalize(message: message, info: info, blind: blind, evaluatedElement: evaluatedElement)
|
|
}
|
|
|
|
precondition(self.client.mode == .partiallyOblivious)
|
|
let framedInfo = Data("Info".utf8) + I2OSP(value: info!.count, outputByteCount: 2) + info!
|
|
|
|
let m = try H2G.hashToScalar(framedInfo, domainSeparationString: setupCtx)
|
|
let T = m * G.Element.generator
|
|
|
|
let tweakedKey = T + publicKey
|
|
guard try DLEQ<H2G>.verifyProof(A: H2G.G.Element.generator, B: tweakedKey,
|
|
CDs: [(C: evaluatedElement, D: blindedElement)],
|
|
proof: proof,
|
|
dst: setupContext(mode: client.mode, suite: client.ciphersuite, v8CompatibilityMode: self.client.v8CompatibilityMode), v8CompatibilityMode: self.client.v8CompatibilityMode) else {
|
|
throw OPRF.Errors.invalidProof
|
|
}
|
|
|
|
return try self.client.finalize(message: message, info: info, blind: blind, evaluatedElement: evaluatedElement)
|
|
}
|
|
|
|
}
|
|
}
|