mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c80d3aff5 | ||
|
|
25847ff705 | ||
|
|
8c018d0882 | ||
|
|
a629920495 |
No files matched your search
@@ -1,3 +0,0 @@
|
||||
# Code of Conduct
|
||||
|
||||
The code of conduct for this project can be found at https://swift.org/code-of-conduct.
|
||||
@@ -684,7 +684,7 @@ func readRawPublicNumbers(
|
||||
/// In a number of places we need to know if an EC key is compact representable. This function implements that check.
|
||||
///
|
||||
/// The check is defined in https://tools.ietf.org/id/draft-jivsov-ecc-compact-05.html#rfc.section.4.2.1. Specifically, a
|
||||
/// point is compact representable if its y coordinate is the smaller of min(y, p-y) where p is the order of the prime field.
|
||||
/// point is compact representable if its y coordinate is the smaller of min(y, p-y) where p is the prime of the base field.
|
||||
@usableFromInline
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
func _isCompactRepresentable(
|
||||
@@ -699,7 +699,7 @@ func _isCompactRepresentable(
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
let context = try! FiniteFieldArithmeticContext(fieldSize: p)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
let newY = try! context.subtract(y, from: group.order)
|
||||
let newY = try! context.subtract(y, from: p)
|
||||
|
||||
// The point is compact representable if y is less than or equal to newY.
|
||||
return y <= newY
|
||||
|
||||
@@ -108,8 +108,8 @@ extension ARC {
|
||||
let T = H2G.hashToGroup(presentationContext, domainSeparationString: Data(("HashToGroup-" + ciphersuite.domain + "Tag").utf8))
|
||||
|
||||
// Recompute m1Tag = H2G(presentationContext) - nonce * tag
|
||||
var m1Tag = T
|
||||
for _ in 0..<nonce { m1Tag = m1Tag - self.tag }
|
||||
let nonceScalar = try Group.Scalar(bytes: I2OSP(value: nonce, outputByteCount: ciphersuite.scalarByteCount), reductionIsModOrder: true)
|
||||
let m1Tag = T - nonceScalar * self.tag
|
||||
|
||||
// Create a verifier, and allocate variables for the constrained scalars.
|
||||
var verifier = Verifier<H2G>(label: ciphersuite.domain + ciphersuite.domain + "CredentialPresentation")
|
||||
|
||||
@@ -276,6 +276,9 @@ extension OpenSSLCurvePoint {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension OpenSSLCurvePoint: OPRFGroupElement {
|
||||
init(oprfRepresentation data: Data) throws {
|
||||
guard data.count == C.compressedx962PointByteCount else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
let point = try EllipticCurvePoint(x962Representation: data, on: C.group, context: C.__ffac)
|
||||
self.init(ecPoint: point)
|
||||
}
|
||||
|
||||
@@ -158,6 +158,61 @@ class ARCTests: XCTestCase {
|
||||
// try endToEndWorkflow(CurveType: P521.self)
|
||||
}
|
||||
|
||||
func testVerifyCostIsIndependentOfTheNonce() async throws {
|
||||
let ciphersuite = ARC.Ciphersuite(HashToCurveImpl<P256>.self)
|
||||
let (generatorG, generatorH) = ARC.getGenerators(suite: ciphersuite)
|
||||
let x0 = GroupImpl<P256>.Scalar.random
|
||||
let x1 = GroupImpl<P256>.Scalar.random
|
||||
let x2 = GroupImpl<P256>.Scalar.random
|
||||
let x0Blinding = GroupImpl<P256>.Scalar.random
|
||||
let serverPrivateKey = ARC.ServerPrivateKey(x0: x0, x1: x1, x2: x2, x0Blinding: x0Blinding)
|
||||
let server = ARC.Server(ciphersuite: ciphersuite, x0: x0, x1: x1, x2: x2, x0Blinding: x0Blinding)
|
||||
let requestContext = Data("test request context".utf8)
|
||||
let precredential = try ARC.Precredential(
|
||||
ciphersuite: ciphersuite,
|
||||
m1: GroupImpl<P256>.Scalar.random,
|
||||
requestContext: requestContext,
|
||||
r1: GroupImpl<P256>.Scalar.random,
|
||||
r2: GroupImpl<P256>.Scalar.random,
|
||||
serverPublicKey: server.serverPublicKey
|
||||
)
|
||||
let credential = try precredential.makeCredential(
|
||||
credentialResponse: try server.respond(credentialRequest: precredential.credentialRequest)
|
||||
)
|
||||
|
||||
// The nonce comes from the client, so presentation verification cost must not scale with nonce.
|
||||
// This test uses such a large nonce that the test would hit the timeout if verification was correlated.
|
||||
let nonce = 1 << 62
|
||||
let presentationContext = Data("0123456789".utf8)
|
||||
let presentation = try ARC.Presentation(
|
||||
credential: credential,
|
||||
presentationContext: presentationContext,
|
||||
nonce: nonce,
|
||||
generatorG: generatorG,
|
||||
generatorH: generatorH
|
||||
)
|
||||
|
||||
let verificationResult: LockedBox<Bool?> = .init(initialValue: nil)
|
||||
let finished = expectation(description: "verification finished")
|
||||
let thread = Thread {
|
||||
let result = try? presentation.verify(
|
||||
serverPrivateKey: serverPrivateKey,
|
||||
X1: x1 * generatorH,
|
||||
m2: precredential.clientSecrets.m2,
|
||||
presentationContext: presentationContext,
|
||||
presentationLimit: Int.max,
|
||||
nonce: nonce,
|
||||
generatorG: generatorG,
|
||||
generatorH: generatorH,
|
||||
ciphersuite: ciphersuite)
|
||||
verificationResult.withLockedValue { $0 = result }
|
||||
finished.fulfill()
|
||||
}
|
||||
thread.start()
|
||||
await fulfillment(of: [finished], timeout: 10)
|
||||
XCTAssertEqual(verificationResult.withLockedValue { $0 }, true)
|
||||
}
|
||||
|
||||
func testPresentationState() throws {
|
||||
let context1 = Data("context1".utf8)
|
||||
let context2 = Data("context2".utf8)
|
||||
|
||||
@@ -79,4 +79,21 @@ final class VOPRFPublicAPITests: XCTestCase {
|
||||
// [Server] Compute the PRF for the input, without blinding or proof generation.
|
||||
let _: Data = try privateKey.evaluate(input)
|
||||
}
|
||||
|
||||
func testBlindedElementRejectsUncompressedOPRFRepresentation() throws {
|
||||
/// Check we only accept compressed encoded elements, as per RFC 9497:
|
||||
///
|
||||
/// > SerializeElement(A): Implemented using the compressed Elliptic-Curve-Point-to-Octet-String method...
|
||||
/// >
|
||||
/// > DeserializeElement(buf): Implemented by attempting to deserialize a 49-byte array to a public key
|
||||
/// using the compressed Octet-String-to-Elliptic-Curve-Point method...
|
||||
///
|
||||
/// - See: https://www.rfc-editor.org/rfc/rfc9497.html#name-oprfp-384-sha-384
|
||||
let publicKey = P384._VOPRF.PrivateKey().publicKey
|
||||
let blindedElement = try publicKey.blind(Data("This is some input data".utf8)).blindedElement
|
||||
let compressed = blindedElement.oprfRepresentation
|
||||
let uncompressed = try P384.Signing.PublicKey(compressedRepresentation: compressed).x963Representation
|
||||
|
||||
XCTAssertThrowsError(try P384._VOPRF.BlindedElement(oprfRepresentation: uncompressed))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
//
|
||||
// Copyright (c) 2026 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 XCTest
|
||||
|
||||
#if canImport(CryptoKit)
|
||||
// Skip tests that require @testable imports of CryptoKit.
|
||||
#else
|
||||
@testable import Crypto
|
||||
|
||||
extension ECKeyEncodingsTests {
|
||||
func testCompactRepresentabilityIsDecidedByTheFieldPrime() throws {
|
||||
// y = n + 1, the larger of the two square roots, so not compact representable.
|
||||
let aboveMidpointX = try Data(hexString: "d1db3668128866847e66864aa97c37a04f65f6ff201a7d02345dffd738908975")
|
||||
let aboveMidpointY = try Data(hexString: "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632552")
|
||||
let aboveMidpoint = try P256.Signing.PublicKey(rawRepresentation: aboveMidpointX + aboveMidpointY)
|
||||
XCTAssertNil(aboveMidpoint.compactRepresentation)
|
||||
|
||||
// n/2 < y <= (p-1)/2, the smaller of the two square roots, so compact representable.
|
||||
let belowMidpointX = try Data(hexString: "ce2afeafa686293c6c93f644b3f6d796c659a9277ca4af6c15ef3bfdc6e00f89")
|
||||
let belowMidpointY = try Data(hexString: "7fffffff800000007fffffffffffffffde737d56d38bcf4279dce5617e3192aa")
|
||||
let belowMidpoint = try P256.Signing.PublicKey(rawRepresentation: belowMidpointX + belowMidpointY)
|
||||
XCTAssertEqual(belowMidpoint.compactRepresentation, belowMidpointX)
|
||||
}
|
||||
}
|
||||
|
||||
#endif // canImport(CryptoKit)
|
||||
Reference in new issue
Block a user