//===----------------------------------------------------------------------===// // // 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, watchOS 6, tvOS 13, *) typealias ARCCurve = P384 @available(macOS 10.15, iOS 13.2, tvOS 13.2, watchOS 6.1, *) typealias ARCH2G = HashToCurveImpl @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) internal func DecodeInt(value: Data) -> Int { @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) var result = Int(0) for i in 0.. Data { return I2OSP(value: value, outputByteCount: 4) } @available(macOS 10.15, iOS 13.2, tvOS 13.2, watchOS 6.1, *) extension ARC.CredentialRequest where H2G == ARCH2G { static let scalarCount = 5 static let pointCount = 2 static let serializedByteCount = pointCount * ARCCurve.compressedx962PointByteCount + scalarCount * ARCCurve.orderByteCount func serialize() -> Data { let m1Enc = self.m1Enc.compressedRepresentation let m2Enc = self.m2Enc.compressedRepresentation let proofData = self.proof.serialize() return m1Enc + m2Enc + proofData } static func deserialize(requestData: D) throws -> ARC.CredentialRequest { guard requestData.count == self.serializedByteCount else { throw ARC.Errors.incorrectRequestDataSize } let requestData = Data(requestData) var startPointer = 0 let m1Enc = try ARCH2G.G.Element(oprfRepresentation: requestData.subdata(in: startPointer.. Data { let U = self.U.compressedRepresentation let encUPrime = self.encUPrime.compressedRepresentation let X0Aux = self.X0Aux.compressedRepresentation let X1Aux = self.X1Aux.compressedRepresentation let X2Aux = self.X2Aux.compressedRepresentation let HAux = self.HAux.compressedRepresentation let responsePoints = U + encUPrime + X0Aux + X1Aux + X2Aux + HAux let proofData = self.proof.serialize() return responsePoints + proofData } static func deserialize(responseData: D) throws -> ARC.CredentialResponse { guard responseData.count == self.serializedByteCount else { throw ARC.Errors.incorrectResponseDataSize } let responseData = Data(responseData) var startPointer = 0 let U = try ARCH2G.G.Element(oprfRepresentation: responseData.subdata(in: startPointer.. Data { // Serialize presentation elements let U = self.U.compressedRepresentation let UPrimeCommit = self.UPrimeCommit.compressedRepresentation let m1Commit = self.m1Commit.compressedRepresentation let tag = self.tag.compressedRepresentation let presentationProofData = self.proof.serialize() return U + UPrimeCommit + m1Commit + tag + presentationProofData } static func deserialize(presentationData: D) throws -> ARC.Presentation { guard presentationData.count == self.serializedByteCount else { throw ARC.Errors.incorrectPresentationDataSize } let presentationData = Data(presentationData) var startPointer = 0 // Deserialize presentation elements let U = try ARCH2G.G.Element(oprfRepresentation: presentationData.subdata(in: startPointer.. Data { // Serialize server commitment elements let X0 = self.X0.compressedRepresentation let X1 = self.X1.compressedRepresentation let X2 = self.X2.compressedRepresentation return X0 + X1 + X2 } static func deserialize(serverPublicKeyData: D) throws -> ARC.ServerPublicKey { guard serverPublicKeyData.count == self.pointCount * ARCCurve.compressedx962PointByteCount else { throw ARC.Errors.incorrectServerCommitmentsSize } let serverPublicKeyData = Data(serverPublicKeyData) var startPointer = 0 // Deserialize server commitment elements let X0 = try ARCH2G.G.Element(oprfRepresentation: serverPublicKeyData.subdata(in: startPointer.. Data { let scalarCount = self.responses.count + 1 var result = Data(capacity: scalarCount * ARCCurve.orderByteCount) // Serialize challenge result.append(self.challenge.rawRepresentation) // Serialize responses for response in self.responses { result.append(response.rawRepresentation) } return result } static func deserialize(proofData: D, scalarCount: Int) throws -> Proof { guard proofData.count == scalarCount * ARCCurve.orderByteCount else { throw ARC.Errors.incorrectProofDataSize } let proofData = Data(proofData) var startPointer = 0 // Deserialize challenge let challenge = try GroupImpl.Scalar(bytes: proofData.subdata(in: startPointer...Scalar] = [] for _ in (0...Scalar(bytes: proofData.subdata(in: startPointer.. Data { let m1 = self.m1.rawRepresentation let U = self.U.compressedRepresentation let UPrime = self.UPrime.compressedRepresentation let X1 = self.X1.compressedRepresentation let genG = self.generatorG.compressedRepresentation let genH = self.generatorH.compressedRepresentation let presentationLimit = EncodeInt(value: self.presentationLimit) let noncesData = try ARCNonces.serialize(noncesDict: self.presentationNonces) return m1 + U + UPrime + X1 + genG + genH + presentationLimit + noncesData } static func deserialize(credentialData: D) throws -> ARC.Credential { let noncesByteCount = credentialData.count - self.scalarCount * ARCCurve.orderByteCount - self.pointCount * ARCCurve.compressedx962PointByteCount - integerByteCount guard noncesByteCount >= 0 else { throw ARC.Errors.incorrectCredentialDataSize } let credentialData = Data(credentialData) var startPointer = 0 let m1 = try GroupImpl.Scalar(bytes: credentialData.subdata(in: startPointer..]) throws -> Data { // Convert Set to Array for encoding let dictForEncoding = noncesDict.mapValues {Array($0) } let encoder = PropertyListEncoder() encoder.outputFormat = .binary return try encoder.encode(dictForEncoding) } static func deserialize(noncesData: D) throws -> [Data: Set] { let decoder = PropertyListDecoder() // Decode as [Data: [Int]] and convert Array back to Set let decodedDictionary = try decoder.decode([Data: [Int]].self, from: Data(noncesData)) return decodedDictionary.mapValues { Set($0) } } }