From 3280a5f72874d2f73bf25551bbb64f524c76d834 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 19 Nov 2026 14:46:02 +0000 Subject: [PATCH] Merge commit from fork --- .../Keys/EC/BoringSSL/X25519Keys_boring.swift | 25 +++++----- .../ECDH/BoringSSL/X25519Tests_boring.swift | 50 +++++++++++++++++++ 2 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 Tests/CryptoTests/ECDH/BoringSSL/X25519Tests_boring.swift diff --git a/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift index 91c8ffc7..625b5ae2 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift @@ -121,10 +121,10 @@ extension Curve25519.KeyAgreement { ) throws -> SharedSecret { - let sharedSecret = SecureBytes( + let sharedSecret = try SecureBytes( unsafeUninitializedCapacity: Curve25519.KeyAgreement.keySizeBytes ) { secretPointer, secretSize in - self.key.withUnsafeBytes { privateKeyPointer in + try self.key.withUnsafeBytes { privateKeyPointer in // We precondition on all of these sizes because bounds checking is cool. // These are fatal instead of guards because we allocated the secret (so it must be right), // we either allocated the private key or validated it on construction (so it must be right), @@ -133,16 +133,17 @@ extension Curve25519.KeyAgreement { precondition(privateKeyPointer.count == Curve25519.KeyAgreement.keySizeBytes) precondition(publicKeyShare.keyBytes.count == Curve25519.KeyAgreement.keySizeBytes) - // We don't check the return code here. This return code only validates that the generated secret is not the weak all-zero - // secret (it is not possible for BoringSSL's X25519 implementation to fail, which is nice). There is currently what I would - // politely describe as a "lack of consensus" as to whether crypto libraries should reject this secret. CryptoKit on Apple - // platforms currently does not, so for the sake of conformance with our peer implementation I will also refuse to check it. - // We may elect to revisit this decision if the security best-practice thinking changes. - CCryptoBoringSSL_X25519( - secretPointer.baseAddress, - privateKeyPointer.baseAddress, - publicKeyShare.keyBytes - ) + // BoringSSL returns 0 when the peer's public key has low order, which makes the shared secret + // the weak all-zero value. CryptoKit rejects that key agreement, so we do too. + guard + CCryptoBoringSSL_X25519( + secretPointer.baseAddress, + privateKeyPointer.baseAddress, + publicKeyShare.keyBytes + ) == 1 + else { + throw CryptoKitError.internalBoringSSLError() + } } secretSize = Curve25519.KeyAgreement.keySizeBytes // We always use all of it. diff --git a/Tests/CryptoTests/ECDH/BoringSSL/X25519Tests_boring.swift b/Tests/CryptoTests/ECDH/BoringSSL/X25519Tests_boring.swift new file mode 100644 index 00000000..43ade3e4 --- /dev/null +++ b/Tests/CryptoTests/ECDH/BoringSSL/X25519Tests_boring.swift @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// 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 X25519Tests { + func testKeyAgreementWithLowOrderPublicKeyThrows() throws { + // The vectors whose shared secret is all-zero: the seven low-order u-coordinates, which are + // 0, 1, both points of order 8, p-1, p and p+1, plus a non-canonical spelling of each. + try wycheproofTest( + bundleType: self, + jsonName: "x25519_test", + testFunction: { (group: ECDHTestGroup) in + let zeroSharedSecretTests = group.tests.filter { $0.flags.contains("ZeroSharedSecret") } + XCTAssert(!zeroSharedSecretTests.isEmpty) + + for testVector in zeroSharedSecretTests { + // The public key itself is accepted, as it is by CryptoKit; only the agreement fails. + let publicKey = try Curve25519.KeyAgreement.PublicKey( + rawRepresentation: Array(hexString: testVector.publicKey) + ) + let privateKey = try Curve25519.KeyAgreement.PrivateKey( + rawRepresentation: Array(hexString: testVector.privateKey) + ) + XCTAssertThrowsError( + try privateKey.sharedSecretFromKeyAgreement(with: publicKey), + "tcId \(testVector.tcId)" + ) + } + } + ) + } +} + +#endif // canImport(CryptoKit)