Merge commit from fork

This commit is contained in:
Si Beaumont authored and GitHub committed 2026-11-19 14:46:02 +00:00
1 parent 1fa43495bc
commit 3280a5f728
2 files changed
+63 -12

No files matched your search

@@ -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.
@@ -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)