mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Update ARC API to reflect latest draft (#334)
### Motivation The latest draft of the ARC spec[0] includes moving where the presentation limit is used. Before it was baked in when a client constructed a credential request, but now it has moved to when a client makes a presentation from a finalized credential. This PR updates the internals and API surface to reflect this change. ### Modifications - Defer passing the presentation limit until a presentation is made. ### Result Align more closely with the published draft. ### API breakage It's expected that this will result in the following _breaking_ API changes: ```diff - P384._ARCV1.PublicKey.prepareCredentialRequest(requestContext:presentationLimit:) + P384._ARCV1.PublicKey.prepareCredentialRequest(requestContext:) ``` ```diff - P384._ARCV1.Credential.makePresentation(context:) + P384._ARCV1.Credential.makePresentation(context:presentationLimit:) ``` As this is behind an underscored namespace, we deem this an acceptable break. [0]: https://datatracker.ietf.org/doc/draft-yun-cfrg-arc/
This commit is contained in:
1 parent
a98e4be0dd
commit
4ac764398d
9 files changed
+174
-117
No files matched your search
@@ -24,7 +24,6 @@ extension P384 {
|
||||
/// - Seealso: [IETF Internet Draft: draft-yun-cfrg-arc-00](https://datatracker.ietf.org/doc/draft-yun-cfrg-arc).
|
||||
public enum _ARCV1 {
|
||||
internal typealias H2G = HashToCurveImpl<P384>
|
||||
// TODO: are these typealiases doing anything useful?
|
||||
fileprivate typealias Ciphersuite = ARC.Ciphersuite<H2G>
|
||||
fileprivate typealias Server = ARC.Server<H2G>
|
||||
|
||||
@@ -55,7 +54,6 @@ extension P384._ARCV1 {
|
||||
// uint8 x0Blinding[Ne];
|
||||
// } ServerPrivateKey;
|
||||
//
|
||||
// TODO: Confirm requiremnts for private key serialization format(s) and/or document binary scalar format.
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
guard rawRepresentation.count == 4 * P384.orderByteCount else {
|
||||
throw ARC.Errors.incorrectPrivateKeyDataSize
|
||||
@@ -87,7 +85,6 @@ extension P384._ARCV1 {
|
||||
// uint8 x0Blinding[Ns];
|
||||
// } ServerPrivateKey;
|
||||
//
|
||||
// TODO: Confirm requiremnts for private key serialization format(s) and/or document binary scalar format.
|
||||
public var rawRepresentation: Data {
|
||||
let serializedByteCount = 4 * P384.orderByteCount
|
||||
var result = Data(capacity: serializedByteCount)
|
||||
@@ -124,7 +121,6 @@ extension P384._ARCV1 {
|
||||
// uint8 X2[Ne];
|
||||
// } ServerPublicKey;
|
||||
//
|
||||
// TODO: Do we need other initializers over P384 elements, e.g. PEM?
|
||||
public init<D: DataProtocol>(rawRepresentation: D) throws {
|
||||
guard rawRepresentation.count == Self.serializedByteCount else { throw ARC.Errors.incorrectPublicKeyDataSize }
|
||||
|
||||
@@ -148,7 +144,6 @@ extension P384._ARCV1 {
|
||||
// uint8 X2[Ne];
|
||||
// } ServerPublicKey;
|
||||
//
|
||||
// TODO: Do we need other initializers over P384 elements, e.g. PEM?
|
||||
public var rawRepresentation: Data {
|
||||
var result = Data(capacity: Self.serializedByteCount)
|
||||
|
||||
@@ -188,7 +183,7 @@ extension P384._ARCV1 {
|
||||
public struct Precredential {
|
||||
/// This backing type binds many things together, including the server commitments, client secrets, credential
|
||||
/// request, and presentation limit.
|
||||
fileprivate var backing: ARC.Precredential<H2G>
|
||||
internal var backing: ARC.Precredential<H2G>
|
||||
|
||||
/// The credential request to be sent to the server.
|
||||
public var credentialRequest: CredentialRequest {
|
||||
@@ -227,9 +222,6 @@ extension P384._ARCV1 {
|
||||
fileprivate init(backing: ARC.Credential<H2G>) {
|
||||
self.backing = backing
|
||||
}
|
||||
|
||||
/// The presentation limit enforced for this credential.
|
||||
public var presentationLimit: Int { self.backing.presentationLimit }
|
||||
}
|
||||
|
||||
/// A presentation, created by the client from a credential, to be sent to the server to verify.
|
||||
@@ -250,10 +242,6 @@ extension P384._ARCV1 {
|
||||
self.backing.serialize()
|
||||
}
|
||||
|
||||
// TODO: Confirm if we need/want to expose this to adopters.
|
||||
// There's been some churn around nonce-management being in- or out-of-band, and we need to confirm what the
|
||||
// server needs access to in order to enforce rate-limiting. Now that nonces are sent out-of-band, does the
|
||||
// server also need access to the tag? If not, we should remove it from the API.
|
||||
public var tag: Data {
|
||||
self.backing.tag.compressedRepresentation
|
||||
}
|
||||
@@ -264,7 +252,6 @@ extension P384._ARCV1 {
|
||||
extension P384._ARCV1.PublicKey {
|
||||
internal func prepareCredentialRequest<D: DataProtocol>(
|
||||
requestContext: D,
|
||||
presentationLimit: Int,
|
||||
m1: P384._ARCV1.H2G.G.Scalar,
|
||||
r1: P384._ARCV1.H2G.G.Scalar,
|
||||
r2: P384._ARCV1.H2G.G.Scalar
|
||||
@@ -275,8 +262,7 @@ extension P384._ARCV1.PublicKey {
|
||||
requestContext: Data(requestContext),
|
||||
r1: r1,
|
||||
r2: r2,
|
||||
serverPublicKey: self.backing,
|
||||
presentationLimit: presentationLimit
|
||||
serverPublicKey: self.backing
|
||||
)
|
||||
return P384._ARCV1.Precredential(backing: precedential)
|
||||
}
|
||||
@@ -285,16 +271,13 @@ extension P384._ARCV1.PublicKey {
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - requestContext: Request context, agreed with the server.
|
||||
/// - presentationLimit: The presentation limit, agreed with the server.
|
||||
///
|
||||
/// - Returns: A precredential containing the client secrets, and request to be sent to the server.
|
||||
public func prepareCredentialRequest<D: DataProtocol>(
|
||||
requestContext: D,
|
||||
presentationLimit: Int
|
||||
requestContext: D
|
||||
) throws -> P384._ARCV1.Precredential {
|
||||
try self.prepareCredentialRequest(
|
||||
requestContext: requestContext,
|
||||
presentationLimit: presentationLimit,
|
||||
m1: .random,
|
||||
r1: .random,
|
||||
r2: .random
|
||||
@@ -334,6 +317,7 @@ extension P384._ARCV1.PublicKey {
|
||||
extension P384._ARCV1.Credential {
|
||||
internal mutating func makePresentation<D: DataProtocol>(
|
||||
context: D,
|
||||
presentationLimit: Int,
|
||||
fixedNonce: Int?,
|
||||
a: P384._ARCV1.H2G.G.Scalar,
|
||||
r: P384._ARCV1.H2G.G.Scalar,
|
||||
@@ -341,23 +325,36 @@ extension P384._ARCV1.Credential {
|
||||
) throws -> (presentation: P384._ARCV1.Presentation, nonce: Int) {
|
||||
let (presentation, nonce) = try self.backing.makePresentation(
|
||||
presentationContext: Data(context),
|
||||
a: a, r: r, z: z, optionalNonce: fixedNonce
|
||||
presentationLimit: presentationLimit,
|
||||
a: a,
|
||||
r: r,
|
||||
z: z,
|
||||
optionalNonce: fixedNonce
|
||||
)
|
||||
return (P384._ARCV1.Presentation(backing: presentation), nonce)
|
||||
}
|
||||
|
||||
/// Create a presentation to provide to a verifier.
|
||||
///
|
||||
/// - Parameter context: The presentation context agreed with the verifier.
|
||||
/// - Parameters:
|
||||
/// - context: The presentation context agreed with the verifier.
|
||||
/// - presentationLimit: The presentation limit to enforce.
|
||||
///
|
||||
/// - Returns: A presentation of this credential.
|
||||
///
|
||||
/// - Throws: An error if the presentation limit for this credential has been exceeded.
|
||||
// TODO: Should makePresentation return a nominal type instead of a tuple?
|
||||
public mutating func makePresentation<D: DataProtocol>(
|
||||
context: D
|
||||
context: D,
|
||||
presentationLimit: Int
|
||||
) throws -> (presentation: P384._ARCV1.Presentation, nonce: Int) {
|
||||
try self.makePresentation(context: context, fixedNonce: nil, a: .random, r: .random, z: .random)
|
||||
try self.makePresentation(
|
||||
context: context,
|
||||
presentationLimit: presentationLimit,
|
||||
fixedNonce: nil,
|
||||
a: .random,
|
||||
r: .random,
|
||||
z: .random
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,10 +25,9 @@ extension ARC {
|
||||
let ciphersuite: Ciphersuite<H2G>
|
||||
let generatorG: Group.Element
|
||||
let generatorH: Group.Element
|
||||
let presentationLimit: Int
|
||||
var presentationNonces: [Data: Set<Int>]
|
||||
var presentationState: PresentationState
|
||||
|
||||
init(credentialResponse: CredentialResponse<H2G>, credentialRequest: CredentialRequest<H2G>, clientSecrets: ClientSecrets<Group.Scalar>, serverPublicKey: ServerPublicKey<H2G>, ciphersuite: Ciphersuite<H2G>, generatorG: Group.Element, generatorH: Group.Element, presentationLimit: Int) throws {
|
||||
init(credentialResponse: CredentialResponse<H2G>, credentialRequest: CredentialRequest<H2G>, clientSecrets: ClientSecrets<Group.Scalar>, serverPublicKey: ServerPublicKey<H2G>, ciphersuite: Ciphersuite<H2G>, generatorG: Group.Element, generatorH: Group.Element) throws {
|
||||
// Verify credential response proof
|
||||
guard
|
||||
try credentialResponse.verify(request: credentialRequest, serverPublicKey: serverPublicKey, generatorG: generatorG, generatorH: generatorH)
|
||||
@@ -39,48 +38,74 @@ extension ARC {
|
||||
// Decrypt Enc(U') from the credential response, to get U'
|
||||
let UPrime = credentialResponse.encUPrime - credentialResponse.X0Aux - clientSecrets.r1 * credentialResponse.X1Aux - clientSecrets.r2 * credentialResponse.X2Aux
|
||||
|
||||
let presentationNonces = [Data: Set<Int>]()
|
||||
self = Self(m1: clientSecrets.m1, U: credentialResponse.U, UPrime: UPrime, X1: serverPublicKey.X1, presentationLimit: presentationLimit, presentationNonces: presentationNonces, ciphersuite: ciphersuite, generatorG: generatorG, generatorH: generatorH)
|
||||
self = Self(m1: clientSecrets.m1, U: credentialResponse.U, UPrime: UPrime, X1: serverPublicKey.X1, ciphersuite: ciphersuite, generatorG: generatorG, generatorH: generatorH, presentationState: ARC.PresentationState())
|
||||
}
|
||||
|
||||
internal init(m1: Group.Scalar, U: Group.Element, UPrime: Group.Element, X1: Group.Element, presentationLimit: Int, presentationNonces: [Data: Set<Int>], ciphersuite: Ciphersuite<H2G>, generatorG: Group.Element, generatorH: Group.Element) {
|
||||
internal init(m1: Group.Scalar, U: Group.Element, UPrime: Group.Element, X1: Group.Element, ciphersuite: Ciphersuite<H2G>, generatorG: Group.Element, generatorH: Group.Element, presentationState: PresentationState) {
|
||||
self.m1 = m1
|
||||
self.U = U
|
||||
self.UPrime = UPrime
|
||||
self.X1 = X1
|
||||
self.presentationLimit = presentationLimit
|
||||
self.presentationNonces = presentationNonces
|
||||
self.ciphersuite = ciphersuite
|
||||
self.generatorG = generatorG
|
||||
self.generatorH = generatorH
|
||||
self.presentationState = presentationState
|
||||
}
|
||||
|
||||
mutating func makePresentation(presentationContext: Data, a: Group.Scalar = Group.Scalar.random, r: Group.Scalar = Group.Scalar.random, z: Group.Scalar = Group.Scalar.random, optionalNonce: Int? = nil) throws -> (Presentation<H2G>, Int) {
|
||||
mutating func makePresentation(presentationContext: Data, presentationLimit: Int, a: Group.Scalar = Group.Scalar.random, r: Group.Scalar = Group.Scalar.random, z: Group.Scalar = Group.Scalar.random, optionalNonce: Int? = nil) throws -> (Presentation<H2G>, Int) {
|
||||
let nonce = try self.presentationState.update(presentationContext: presentationContext, presentationLimit: presentationLimit, optionalNonce: optionalNonce)
|
||||
let presentation = try Presentation<H2G>(credential: self, a: a, r: r, z: z, presentationContext: presentationContext, nonce: nonce, generatorG: self.generatorG, generatorH: self.generatorH)
|
||||
return (presentation, nonce)
|
||||
}
|
||||
}
|
||||
|
||||
struct PresentationState {
|
||||
typealias PresentationContext = Data
|
||||
typealias PresentationLimit = Int
|
||||
typealias NonceSet = Set<Int>
|
||||
var state: [PresentationContext: (PresentationLimit, NonceSet)]
|
||||
|
||||
init() {
|
||||
self.state = [PresentationContext: (PresentationLimit, NonceSet)]()
|
||||
}
|
||||
|
||||
internal init(state: [PresentationContext: (PresentationLimit, NonceSet)]) {
|
||||
self.state = state
|
||||
}
|
||||
|
||||
mutating func update(presentationContext: Data, presentationLimit: Int, optionalNonce: Int? = nil) throws -> Int {
|
||||
if presentationLimit <= 0 {
|
||||
throw ARC.Errors.invalidPresentationLimit
|
||||
}
|
||||
// If optionalNonce is set, use that nonce (eg for test vectors).
|
||||
// Otherwise, generate a random nonce that has not yet been used.
|
||||
var nonce = optionalNonce != nil ? optionalNonce! : Int.random(in: 0..<self.presentationLimit)
|
||||
var nonce = optionalNonce != nil ? optionalNonce! : Int.random(in: 0..<presentationLimit)
|
||||
|
||||
// Store the nonce in presentationNonces for that presentationContext.
|
||||
if self.presentationNonces[presentationContext] != nil {
|
||||
if self.presentationNonces[presentationContext]!.count >= self.presentationLimit {
|
||||
if self.state[presentationContext] != nil {
|
||||
let presentationContextState = self.state[presentationContext]!
|
||||
if presentationLimit != presentationContextState.0 {
|
||||
throw ARC.Errors.invalidPresentationLimit
|
||||
}
|
||||
if presentationContextState.1.count >= presentationLimit {
|
||||
throw ARC.Errors.presentationLimitExceeded
|
||||
}
|
||||
while self.presentationNonces[presentationContext]!.contains(nonce) {
|
||||
|
||||
while presentationContextState.1.contains(nonce) {
|
||||
if optionalNonce == nil {
|
||||
// Randomly generated nonce collides with existing nonce for presentationContext
|
||||
nonce = Int.random(in: 0..<self.presentationLimit)
|
||||
nonce = Int.random(in: 0..<presentationLimit)
|
||||
} else {
|
||||
// optionalNonce collides with existing nonce for presentationContext
|
||||
throw ARC.Errors.presentationLimitExceeded
|
||||
}
|
||||
}
|
||||
self.presentationNonces[presentationContext]!.insert(nonce)
|
||||
} else {
|
||||
self.presentationNonces[presentationContext] = [nonce]
|
||||
}
|
||||
|
||||
let presentation = try Presentation<H2G>(credential: self, a: a, r: r, z: z, presentationContext: presentationContext, nonce: nonce, generatorG: self.generatorG, generatorH: self.generatorH)
|
||||
return (presentation, nonce)
|
||||
self.state[presentationContext]!.1.insert(nonce)
|
||||
} else {
|
||||
self.state[presentationContext] = (presentationLimit, [nonce])
|
||||
}
|
||||
return nonce
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -226,7 +226,6 @@ extension Proof where H2G == ARCH2G {
|
||||
extension ARC.Credential where H2G == ARCH2G {
|
||||
static let scalarCount = 1
|
||||
static let pointCount = 5
|
||||
static let integerByteCount = 4
|
||||
|
||||
func serialize() throws -> Data {
|
||||
let m1 = self.m1.rawRepresentation
|
||||
@@ -235,15 +234,14 @@ extension ARC.Credential where H2G == ARCH2G {
|
||||
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)
|
||||
let presentationState = try self.presentationState.serialize()
|
||||
|
||||
return m1 + U + UPrime + X1 + genG + genH + presentationLimit + noncesData
|
||||
return m1 + U + UPrime + X1 + genG + genH + presentationState
|
||||
}
|
||||
|
||||
static func deserialize<D: DataProtocol>(credentialData: D) throws -> ARC.Credential<ARCH2G> {
|
||||
let noncesByteCount = credentialData.count - self.scalarCount * ARCCurve.orderByteCount - self.pointCount * ARCCurve.compressedx962PointByteCount - integerByteCount
|
||||
guard noncesByteCount >= 0 else {
|
||||
let stateByteCount = credentialData.count - self.scalarCount * ARCCurve.orderByteCount - self.pointCount * ARCCurve.compressedx962PointByteCount
|
||||
guard stateByteCount >= 0 else {
|
||||
throw ARC.Errors.incorrectCredentialDataSize
|
||||
}
|
||||
let credentialData = Data(credentialData)
|
||||
@@ -261,33 +259,36 @@ extension ARC.Credential where H2G == ARCH2G {
|
||||
startPointer += ARCCurve.compressedx962PointByteCount
|
||||
let genH = try ARCH2G.G.Element(oprfRepresentation: credentialData.subdata(in: startPointer..<startPointer+ARCCurve.compressedx962PointByteCount))
|
||||
startPointer += ARCCurve.compressedx962PointByteCount
|
||||
let presentationLimit = DecodeInt(value: credentialData.subdata(in: startPointer..<startPointer+self.integerByteCount))
|
||||
startPointer += self.integerByteCount
|
||||
|
||||
// Deserialize presentationNonces dictionary
|
||||
let noncesData = credentialData.subdata(in: startPointer..<startPointer+noncesByteCount)
|
||||
let presentationNonces = try ARCNonces.deserialize(noncesData: noncesData)
|
||||
// Deserialize presentationState
|
||||
let presentationStateData = credentialData.subdata(in: startPointer..<startPointer+stateByteCount)
|
||||
let presentationState = try ARC.PresentationState.deserialize(presentationStateData: presentationStateData)
|
||||
|
||||
let ciphersuite = ARC.Ciphersuite(ARCH2G.self)
|
||||
return ARC.Credential(m1: m1, U: U, UPrime: UPrime, X1: X1, presentationLimit: presentationLimit, presentationNonces: presentationNonces, ciphersuite: ciphersuite, generatorG: genG, generatorH: genH)
|
||||
return ARC.Credential(m1: m1, U: U, UPrime: UPrime, X1: X1, ciphersuite: ciphersuite, generatorG: genG, generatorH: genH, presentationState: presentationState)
|
||||
}
|
||||
}
|
||||
|
||||
// A helper for serializing a ARC credential. This will only be called client-side, and never be sent over the wire.
|
||||
// Serialize a ARC PresentationState, to help save and restore a credential.
|
||||
// This will only be called client-side, and never be sent over the wire.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
|
||||
struct ARCNonces {
|
||||
static func serialize(noncesDict: [Data: Set<Int>]) throws -> Data {
|
||||
// Convert Set<Int> to Array<Int> for encoding
|
||||
let dictForEncoding = noncesDict.mapValues {Array($0) }
|
||||
extension ARC.PresentationState {
|
||||
func serialize() throws -> Data {
|
||||
let encoder = PropertyListEncoder()
|
||||
encoder.outputFormat = .binary
|
||||
|
||||
// Convert (Int, Set<Int>) to Array<Int> for encoding
|
||||
let dictForEncoding = self.state.mapValues { [$0.0] + Array($0.1) }
|
||||
return try encoder.encode(dictForEncoding)
|
||||
}
|
||||
|
||||
static func deserialize<D: DataProtocol>(noncesData: D) throws -> [Data: Set<Int>] {
|
||||
static func deserialize<D: DataProtocol>(presentationStateData: D) throws -> ARC.PresentationState {
|
||||
let decoder = PropertyListDecoder()
|
||||
// Decode as [Data: [Int]] and convert Array<Int> back to Set<Int>
|
||||
let decodedDictionary = try decoder.decode([Data: [Int]].self, from: Data(noncesData))
|
||||
return decodedDictionary.mapValues { Set($0) }
|
||||
|
||||
let stateIntList = try decoder.decode([Data: [Int]].self, from: Data(presentationStateData))
|
||||
// Convert [Int] to (Int, Set<Int>) for decoding
|
||||
let state = stateIntList.mapValues { value in (value[0], Set(value[1...])) }
|
||||
|
||||
return ARC.PresentationState(state: state)
|
||||
}
|
||||
}
|
||||
@@ -38,23 +38,18 @@ extension ARC {
|
||||
let generatorG: Group.Element
|
||||
let generatorH: Group.Element
|
||||
let credentialRequest: CredentialRequest<H2G>
|
||||
let presentationLimit: Int
|
||||
|
||||
init(ciphersuite: Ciphersuite<H2G>, m1: Group.Scalar = Group.Scalar.random, requestContext: Data, r1: Group.Scalar = Group.Scalar.random, r2: Group.Scalar = Group.Scalar.random, serverPublicKey: ServerPublicKey<H2G>, presentationLimit: Int) throws {
|
||||
if presentationLimit <= 0 {
|
||||
throw ARC.Errors.invalidPresentationLimit
|
||||
}
|
||||
init(ciphersuite: Ciphersuite<H2G>, m1: Group.Scalar = Group.Scalar.random, requestContext: Data, r1: Group.Scalar = Group.Scalar.random, r2: Group.Scalar = Group.Scalar.random, serverPublicKey: ServerPublicKey<H2G>) throws {
|
||||
let m2 = try H2G.hashToScalar(requestContext, domainSeparationString: Data((ARC.domain + "requestContext").utf8))
|
||||
self.clientSecrets = ClientSecrets(m1: m1, m2: m2, r1: r1, r2: r2)
|
||||
self.serverPublicKey = serverPublicKey
|
||||
self.ciphersuite = ciphersuite
|
||||
(self.generatorG, self.generatorH) = ARC.getGenerators(suite: ciphersuite)
|
||||
self.credentialRequest = try CredentialRequest(clientSecrets: self.clientSecrets, generatorG: generatorG, generatorH: generatorH)
|
||||
self.presentationLimit = presentationLimit
|
||||
}
|
||||
|
||||
func makeCredential(credentialResponse: CredentialResponse<H2G>) throws -> Credential<H2G> {
|
||||
return try Credential<H2G>(credentialResponse: credentialResponse, credentialRequest: self.credentialRequest, clientSecrets: self.clientSecrets, serverPublicKey: self.serverPublicKey, ciphersuite: self.ciphersuite, generatorG: self.generatorG, generatorH: self.generatorH, presentationLimit: self.presentationLimit)
|
||||
return try Credential<H2G>(credentialResponse: credentialResponse, credentialRequest: self.credentialRequest, clientSecrets: self.clientSecrets, serverPublicKey: self.serverPublicKey, ciphersuite: self.ciphersuite, generatorG: self.generatorG, generatorH: self.generatorH)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,6 @@ final class ARCAPITests: XCTestCase {
|
||||
// [Client] Prepare a credential request using fixed values from test vector.
|
||||
let precredential = try publicKey.prepareCredentialRequest(
|
||||
requestContext: requestContext,
|
||||
presentationLimit: presentationLimit,
|
||||
m1: P384._ARCV1.H2G.G.Scalar(bytes: Data(hexString: vector.CredentialRequest.m1)),
|
||||
r1: P384._ARCV1.H2G.G.Scalar(bytes: Data(hexString: vector.CredentialRequest.r1)),
|
||||
r2: P384._ARCV1.H2G.G.Scalar(bytes: Data(hexString: vector.CredentialRequest.r2))
|
||||
@@ -110,6 +109,7 @@ final class ARCAPITests: XCTestCase {
|
||||
// [Client] Make a presentation from the credential for a presentation prefix.
|
||||
let (presentation, _) = try credential.makePresentation(
|
||||
context: presentationContext,
|
||||
presentationLimit: presentationLimit,
|
||||
fixedNonce: Int(vector.Presentation1.nonce.dropFirst(2), radix: 16)!,
|
||||
a: P384._ARCV1.H2G.G.Scalar(bytes: Data(hexString: vector.Presentation1.a)),
|
||||
r: P384._ARCV1.H2G.G.Scalar(bytes: Data(hexString: vector.Presentation1.r)),
|
||||
|
||||
@@ -36,7 +36,7 @@ class ARCEncodingTests: XCTestCase {
|
||||
let ciphersuite = ARC.Ciphersuite(HashToCurveImpl<ARCCurve>.self)
|
||||
let server = ARC.Server(ciphersuite: ciphersuite)
|
||||
let requestContext = Data("test request context".utf8)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey, presentationLimit: 1)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey)
|
||||
let request = precredential.credentialRequest
|
||||
|
||||
let requestData = request.serialize()
|
||||
@@ -56,7 +56,7 @@ class ARCEncodingTests: XCTestCase {
|
||||
let ciphersuite = ARC.Ciphersuite(HashToCurveImpl<ARCCurve>.self)
|
||||
let server = ARC.Server(ciphersuite: ciphersuite)
|
||||
let requestContext = Data("test request context".utf8)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey, presentationLimit: 1)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey)
|
||||
let request = precredential.credentialRequest
|
||||
let response = try server.respond(credentialRequest: request)
|
||||
|
||||
@@ -81,7 +81,7 @@ class ARCEncodingTests: XCTestCase {
|
||||
let ciphersuite = ARC.Ciphersuite(HashToCurveImpl<ARCCurve>.self)
|
||||
let server = ARC.Server(ciphersuite: ciphersuite)
|
||||
let requestContext = Data("test request context".utf8)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey, presentationLimit: 1)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey)
|
||||
let request = precredential.credentialRequest
|
||||
let response = try server.respond(credentialRequest: request)
|
||||
let credential = try precredential.makeCredential(credentialResponse: response)
|
||||
@@ -94,8 +94,10 @@ class ARCEncodingTests: XCTestCase {
|
||||
XCTAssert(credential.X1 == credential2.X1)
|
||||
XCTAssert(credential.generatorG == credential2.generatorG)
|
||||
XCTAssert(credential.generatorH == credential2.generatorH)
|
||||
XCTAssert(credential.presentationLimit == credential2.presentationLimit)
|
||||
XCTAssert(credential.presentationNonces == credential2.presentationNonces)
|
||||
for (key, value) in credential.presentationState.state {
|
||||
XCTAssertEqual(value.0, credential2.presentationState.state[key]?.0)
|
||||
XCTAssertEqual(value.1, credential2.presentationState.state[key]?.1)
|
||||
}
|
||||
|
||||
let credentialData2 = try credential2.serialize()
|
||||
XCTAssertEqual(credentialData, credentialData2)
|
||||
@@ -105,11 +107,11 @@ class ARCEncodingTests: XCTestCase {
|
||||
let ciphersuite = ARC.Ciphersuite(HashToCurveImpl<ARCCurve>.self)
|
||||
let server = ARC.Server(ciphersuite: ciphersuite)
|
||||
let requestContext = Data("test request context".utf8)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey, presentationLimit: 1)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, requestContext: requestContext, serverPublicKey: server.serverPublicKey)
|
||||
let request = precredential.credentialRequest
|
||||
let response = try server.respond(credentialRequest: request)
|
||||
var credential = try precredential.makeCredential(credentialResponse: response)
|
||||
let (presentation, _) = try credential.makePresentation(presentationContext: Data("test presentation context".utf8))
|
||||
let (presentation, _) = try credential.makePresentation(presentationContext: Data("test presentation context".utf8), presentationLimit: 1)
|
||||
|
||||
let presentationData = presentation.serialize()
|
||||
let presentation2 = try ARC.Presentation.deserialize(presentationData: presentationData)
|
||||
@@ -126,21 +128,27 @@ class ARCEncodingTests: XCTestCase {
|
||||
XCTAssertEqual(presentationData, presentationData2)
|
||||
}
|
||||
|
||||
func testDictionaryEncoding() throws {
|
||||
let emptyDictionary: [Data: Set<Int>] = [:]
|
||||
let smallDictionary: [Data: Set<Int>] = [Data("context1".utf8): [1, 2, 3], Data("context2".utf8): [4, 5, 6]]
|
||||
var largeDictionary: [Data: Set<Int>] = [:]
|
||||
for i in 0..<1000 {
|
||||
let key = Data("key\(i)".utf8)
|
||||
let value = Set(0...1000)
|
||||
largeDictionary[key] = value
|
||||
func testPresentationStateEncoding() throws {
|
||||
let emptyPresentationState = ARC.PresentationState()
|
||||
let smallPresentationState = ARC.PresentationState(state: [Data("context1".utf8): (4, [1, 2, 3]), Data("context2".utf8): (10, [4, 5, 6])])
|
||||
var largePresentationState = ARC.PresentationState()
|
||||
for presentationLimit in 1..<100 {
|
||||
let presentationContext = Data("presentationContext\(presentationLimit)".utf8)
|
||||
for nonce in 0..<presentationLimit {
|
||||
let selectedNonce = try largePresentationState.update(presentationContext:presentationContext, presentationLimit: presentationLimit, optionalNonce: nonce)
|
||||
XCTAssertEqual(selectedNonce, nonce)
|
||||
}
|
||||
}
|
||||
|
||||
for dictionary in [emptyDictionary, smallDictionary, largeDictionary] {
|
||||
let serializedDictionary = try ARCNonces.serialize(noncesDict: dictionary)
|
||||
XCTAssertNotNil(serializedDictionary, "Serialized data should not be nil")
|
||||
let deserializedDictionary = try ARCNonces.deserialize(noncesData: serializedDictionary)
|
||||
XCTAssertEqual(dictionary, deserializedDictionary, "Deserialized dictionary should match the original")
|
||||
for state in [emptyPresentationState, smallPresentationState, largePresentationState] {
|
||||
let serializedState = try state.serialize()
|
||||
XCTAssertNotNil(serializedState, "Serialized state should not be nil")
|
||||
|
||||
let deserializedState = try ARC.PresentationState.deserialize(presentationStateData: serializedState)
|
||||
for (key, value) in state.state {
|
||||
XCTAssertEqual(value.0, deserializedState.state[key]?.0)
|
||||
XCTAssertEqual(value.1, deserializedState.state[key]?.1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,10 +38,7 @@ final class ARCPublicAPITests: XCTestCase {
|
||||
let publicKey = try P384._ARCV1.PublicKey(rawRepresentation: publicKeyBytes)
|
||||
|
||||
// [Client] Prepare a credential request.
|
||||
let precredential = try publicKey.prepareCredentialRequest(
|
||||
requestContext: requestContext,
|
||||
presentationLimit: presentationLimit
|
||||
)
|
||||
let precredential = try publicKey.prepareCredentialRequest(requestContext: requestContext)
|
||||
|
||||
// [Client -> Issuer] Send the credential request.
|
||||
let credentialRequestBytes = precredential.credentialRequest.rawRepresentation
|
||||
@@ -59,14 +56,15 @@ final class ARCPublicAPITests: XCTestCase {
|
||||
let _ = try P384._ARCV1.CredentialResponse(rawRepresentation: credentialResponseBytes)
|
||||
|
||||
// [Client] Generate a credential.
|
||||
// NOTE: This is a var because it enforces the presentation limit for each presentation prefix.
|
||||
// NOTE: This is a var because it enforces the presentation limits for each presentation prefix.
|
||||
var credential = try publicKey.finalize(credentialResponse, for: precredential)
|
||||
|
||||
// [Client] Make a presentation from the credential for a presentation prefix.
|
||||
let (presentation, nonce) = try credential.makePresentation(context: presentationContext)
|
||||
|
||||
// TODO: The shared code doesn't expose fixing randomness in makePresentation so we cannot assert things about it
|
||||
// Instea
|
||||
// NOTE: On first presentation, the presentation limit provided is now set, and enforced going forward.
|
||||
let (presentation, nonce) = try credential.makePresentation(
|
||||
context: presentationContext,
|
||||
presentationLimit: presentationLimit
|
||||
)
|
||||
|
||||
// [Client -> Verifier] Send the presentation.
|
||||
let presentationBytes = presentation.rawRepresentation
|
||||
@@ -95,18 +93,17 @@ final class ARCPublicAPITests: XCTestCase {
|
||||
let presentationContext = Data("shared presentation context".utf8)
|
||||
let presentationLimit = 2
|
||||
|
||||
let precredential = try publicKey.prepareCredentialRequest(
|
||||
requestContext: requestContext,
|
||||
presentationLimit: presentationLimit
|
||||
)
|
||||
let precredential = try publicKey.prepareCredentialRequest(requestContext: requestContext)
|
||||
let credentialResponse = try privateKey.issue(precredential.credentialRequest)
|
||||
var credential = try publicKey.finalize(credentialResponse, for: precredential)
|
||||
|
||||
for _ in 0..<presentationLimit {
|
||||
_ = try credential.makePresentation(context: presentationContext)
|
||||
_ = try credential.makePresentation(context: presentationContext, presentationLimit: presentationLimit)
|
||||
}
|
||||
|
||||
XCTAssertThrowsError(try credential.makePresentation(context: presentationContext)) { error in
|
||||
XCTAssertThrowsError(
|
||||
try credential.makePresentation(context: presentationContext, presentationLimit: presentationLimit)
|
||||
) { error in
|
||||
XCTAssertEqual(String(describing: error), "presentationLimitExceeded")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ class ARCTestVectors: XCTestCase {
|
||||
let m2 = try scalarFromString(CurveType: Curve.self, value: tv.CredentialRequest.m2)
|
||||
let r1 = try scalarFromString(CurveType: Curve.self, value: tv.CredentialRequest.r1)
|
||||
let r2 = try scalarFromString(CurveType: Curve.self, value: tv.CredentialRequest.r2)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, m1: m1, requestContext: requestContext, r1: r1, r2: r2, serverPublicKey: server.serverPublicKey, presentationLimit: 2)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, m1: m1, requestContext: requestContext, r1: r1, r2: r2, serverPublicKey: server.serverPublicKey)
|
||||
XCTAssertEqual(precredential.credentialRequest.m1Enc.oprfRepresentation.hexString, tv.CredentialRequest.m1_enc)
|
||||
XCTAssertEqual(precredential.credentialRequest.m2Enc.oprfRepresentation.hexString, tv.CredentialRequest.m2_enc)
|
||||
XCTAssertEqual(precredential.clientSecrets.m2.rawRepresentation.hexString, tv.CredentialRequest.m2)
|
||||
@@ -212,7 +212,7 @@ class ARCTestVectors: XCTestCase {
|
||||
let r_1 = try scalarFromString(CurveType: Curve.self, value: tv.Presentation1.r)
|
||||
let z_1 = try scalarFromString(CurveType: Curve.self, value: tv.Presentation1.z)
|
||||
let nonce_1 = Int(tv.Presentation1.nonce.replacingOccurrences(of: "0x", with: ""), radix: 16)!
|
||||
let (presentation1, nonce_1_returned) = try credential.makePresentation(presentationContext: presentationContext1, a: a_1, r: r_1, z: z_1, optionalNonce: nonce_1)
|
||||
let (presentation1, nonce_1_returned) = try credential.makePresentation(presentationContext: presentationContext1, presentationLimit: 2, a: a_1, r: r_1, z: z_1, optionalNonce: nonce_1)
|
||||
XCTAssertEqual(nonce_1, nonce_1_returned)
|
||||
XCTAssertEqual(presentation1.U.oprfRepresentation.hexString, tv.Presentation1.U)
|
||||
XCTAssertEqual(presentation1.UPrimeCommit.oprfRepresentation.hexString, tv.Presentation1.U_prime_commit)
|
||||
@@ -231,7 +231,7 @@ class ARCTestVectors: XCTestCase {
|
||||
let r_2 = try scalarFromString(CurveType: Curve.self, value: tv.Presentation2.r)
|
||||
let z_2 = try scalarFromString(CurveType: Curve.self, value: tv.Presentation2.z)
|
||||
let nonce_2 = Int(tv.Presentation2.nonce.replacingOccurrences(of: "0x", with: ""), radix: 16)!
|
||||
let (presentation2, nonce_2_returned) = try credential.makePresentation(presentationContext: presentationContext1, a: a_2, r: r_2, z: z_2, optionalNonce: nonce_2)
|
||||
let (presentation2, nonce_2_returned) = try credential.makePresentation(presentationContext: presentationContext1, presentationLimit: 2, a: a_2, r: r_2, z: z_2, optionalNonce: nonce_2)
|
||||
XCTAssertEqual(nonce_2, nonce_2_returned)
|
||||
XCTAssertEqual(presentation2.U.oprfRepresentation.hexString, tv.Presentation2.U)
|
||||
XCTAssertEqual(presentation2.UPrimeCommit.oprfRepresentation.hexString, tv.Presentation2.U_prime_commit)
|
||||
|
||||
@@ -39,7 +39,7 @@ class ARCTests: XCTestCase {
|
||||
let m1 = GroupImpl<Curve>.Scalar.random
|
||||
let r1 = GroupImpl<Curve>.Scalar.random
|
||||
let r2 = GroupImpl<Curve>.Scalar.random
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, m1: m1, requestContext: requestContext, r1: r1, r2: r2, serverPublicKey: serverPublicKey, presentationLimit: presentationLimit)
|
||||
let precredential = try ARC.Precredential(ciphersuite: ciphersuite, m1: m1, requestContext: requestContext, r1: r1, r2: r2, serverPublicKey: serverPublicKey)
|
||||
|
||||
// Client makes an CredentialRequest using its private attributes.
|
||||
let request = precredential.credentialRequest
|
||||
@@ -61,16 +61,16 @@ class ARCTests: XCTestCase {
|
||||
// Client makes two Presentations from the Credential.
|
||||
// Note that in practice, the definition of presentationContext would depend on the use case of the tag (e.g. rate limiting).
|
||||
let presentationContext = Data("0123456789".utf8)
|
||||
let (presentation1, nonce1) = try credential.makePresentation(presentationContext: presentationContext)
|
||||
let (presentation2, nonce2) = try credential.makePresentation(presentationContext: presentationContext)
|
||||
let (presentation1, nonce1) = try credential.makePresentation(presentationContext: presentationContext, presentationLimit: presentationLimit)
|
||||
let (presentation2, nonce2) = try credential.makePresentation(presentationContext: presentationContext, presentationLimit: presentationLimit)
|
||||
XCTAssertNotNil(presentation1)
|
||||
XCTAssertNotNil(presentation2)
|
||||
|
||||
// We hit the limit for the presentationContext, and should not receive any new presentations
|
||||
XCTAssertThrowsError(try credential.makePresentation(presentationContext: presentationContext), error: ARC.Errors.presentationLimitExceeded)
|
||||
XCTAssertThrowsError(try credential.makePresentation(presentationContext: presentationContext, presentationLimit: presentationLimit), error: ARC.Errors.presentationLimitExceeded)
|
||||
// But we can make more presentations under a different presentationContext
|
||||
let newPresentationContext = Data("ABCDEF".utf8)
|
||||
let (presentation3, nonce3) = try credential.makePresentation(presentationContext: newPresentationContext)
|
||||
let (presentation3, nonce3) = try credential.makePresentation(presentationContext: newPresentationContext, presentationLimit: presentationLimit)
|
||||
XCTAssertNotNil(presentation3)
|
||||
|
||||
// Server verifies Presentation1 with its server keys.
|
||||
@@ -155,4 +155,38 @@ class ARCTests: XCTestCase {
|
||||
try endToEndWorkflow(CurveType: P384.self)
|
||||
// try endToEndWorkflow(CurveType: P521.self)
|
||||
}
|
||||
|
||||
func testPresentationState() throws {
|
||||
let context1 = Data("context1".utf8)
|
||||
let context2 = Data("context2".utf8)
|
||||
var smallPresentationState = ARC.PresentationState(state: [context1: (4, [0, 1, 2]), context2: (10, [3, 4, 5, 6])])
|
||||
|
||||
// Test that a new nonce is selected correctly
|
||||
XCTAssertEqual(3, try smallPresentationState.update(presentationContext: context1, presentationLimit: 4))
|
||||
XCTAssertEqual(0, try smallPresentationState.update(presentationContext: Data("context 3".utf8), presentationLimit: 1))
|
||||
XCTAssert(try smallPresentationState.update(presentationContext: context2, presentationLimit: 10) < 10)
|
||||
// Test that exceeding the rate limit for a presentationContext throws an error
|
||||
XCTAssertThrowsError(try smallPresentationState.update(presentationContext: context1, presentationLimit: 4), error: ARC.Errors.presentationLimitExceeded)
|
||||
// Test that reusing a nonce for a presentationContext throws an error
|
||||
XCTAssertThrowsError(try smallPresentationState.update(presentationContext: context2, presentationLimit: 10, optionalNonce: 3), error: ARC.Errors.presentationLimitExceeded)
|
||||
// Test that using an incorrect presentationLimit throws an error
|
||||
XCTAssertThrowsError(try smallPresentationState.update(presentationContext: context2, presentationLimit: 9), error: ARC.Errors.invalidPresentationLimit)
|
||||
XCTAssertThrowsError(try smallPresentationState.update(presentationContext: Data("context 4".utf8), presentationLimit: 0), error: ARC.Errors.invalidPresentationLimit)
|
||||
|
||||
var largePresentationState = ARC.PresentationState()
|
||||
for presentationLimit in 1..<100 {
|
||||
let presentationContext = Data("presentationContext\(presentationLimit)".utf8)
|
||||
for _ in 0..<presentationLimit {
|
||||
let nonce = try largePresentationState.update(presentationContext:presentationContext, presentationLimit: presentationLimit)
|
||||
XCTAssert(nonce < presentationLimit)
|
||||
// Test that reusing a nonce for a presentationContext throws an error
|
||||
XCTAssertThrowsError(try largePresentationState.update(presentationContext: presentationContext, presentationLimit: presentationLimit, optionalNonce: nonce), error: ARC.Errors.presentationLimitExceeded)
|
||||
}
|
||||
// Test that exceeding the rate limit for a presentationContext throws an error
|
||||
XCTAssertThrowsError(try largePresentationState.update(presentationContext: presentationContext, presentationLimit: presentationLimit), error: ARC.Errors.presentationLimitExceeded)
|
||||
XCTAssertEqual(largePresentationState.state[presentationContext]!.1.count, presentationLimit)
|
||||
// Test that using an incorrect presentationLimit throws an error
|
||||
XCTAssertThrowsError(try largePresentationState.update(presentationContext: presentationContext, presentationLimit: presentationLimit-1), error: ARC.Errors.invalidPresentationLimit)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user