From 2d37b1192fac98c9608fa66ff186a2c7cbf99fa2 Mon Sep 17 00:00:00 2001 From: Tobias Date: Fri, 17 Apr 2026 17:06:53 +0200 Subject: [PATCH] Adopt FoundationEssentials instead of Foundation where available (#317) Use `FoundationEssentials` if available. ### Checklist - [x] I've run tests to see all new and existing tests pass (macOS and linux) - [x] I've followed the code style of the rest of the project - [x] I've read the [Contribution Guidelines](CONTRIBUTING.md) - [x] I've updated the documentation if necessary #### If you've made changes to `gyb` files - [ ] I've run `.script/generate_boilerplate_files_with_gyb` and included updated generated files in a commit of this pull request ``` bash .script/generate_boilerplate_files_with_gyb bash: .script/generate_boilerplate_files_with_gyb: No such file or directory ``` ### Motivation: Would be great that packages that depend on `swift-crypto` can be built without linking full `Foundation`. ### Modifications: This patch simply replaces `import Foundation` with a conditional `import FoundationEssentials`. In some files this also required to explicitly import the C standard lib. Maybe we could follow the same approach as in https://github.com/swiftlang/swift-tools-support-core/blob/main/Sources/TSCLibc/libc.swift and add an internal module that abstracts this import dance in a single place. ### Result: After this change the package should only depend on `FoundationEssentials`. --------- Co-authored-by: Claude Opus 4.6 Co-authored-by: Cory Benfield --- Sources/CryptoBoringWrapper/EC/EllipticCurvePoint.swift | 5 +++++ Sources/CryptoExtras/EC/Curve25519+PEM.swift | 7 ++++++- Sources/CryptoExtras/EC/PKCS8DERRepresentation.swift | 7 ++++++- Tests/CryptoExtrasTests/CMACTests.swift | 7 ++++++- .../ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift | 7 ++++++- Tests/CryptoExtrasTests/PKCS8DERRepresentationTests.swift | 7 ++++++- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Sources/CryptoBoringWrapper/EC/EllipticCurvePoint.swift b/Sources/CryptoBoringWrapper/EC/EllipticCurvePoint.swift index b2c81383..dc593879 100644 --- a/Sources/CryptoBoringWrapper/EC/EllipticCurvePoint.swift +++ b/Sources/CryptoBoringWrapper/EC/EllipticCurvePoint.swift @@ -14,8 +14,13 @@ @_implementationOnly import CCryptoBoringSSL @_implementationOnly import CCryptoBoringSSLShims +#if canImport(FoundationEssentials) +import protocol FoundationEssentials.ContiguousBytes +import struct FoundationEssentials.Data +#else import protocol Foundation.ContiguousBytes import struct Foundation.Data +#endif /// A wrapper around BoringSSL's EC_POINT with some lifetime management and value semantics. @usableFromInline diff --git a/Sources/CryptoExtras/EC/Curve25519+PEM.swift b/Sources/CryptoExtras/EC/Curve25519+PEM.swift index 5e8071c8..9cbf92fa 100644 --- a/Sources/CryptoExtras/EC/Curve25519+PEM.swift +++ b/Sources/CryptoExtras/EC/Curve25519+PEM.swift @@ -13,9 +13,14 @@ //===----------------------------------------------------------------------===// import Crypto -import Foundation import SwiftASN1 +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) extension Curve25519.Signing.PrivateKey { /// A Distinguished Encoding Rules (DER) encoded representation of the private key. diff --git a/Sources/CryptoExtras/EC/PKCS8DERRepresentation.swift b/Sources/CryptoExtras/EC/PKCS8DERRepresentation.swift index bfaf11f4..d5ca84e9 100644 --- a/Sources/CryptoExtras/EC/PKCS8DERRepresentation.swift +++ b/Sources/CryptoExtras/EC/PKCS8DERRepresentation.swift @@ -13,9 +13,14 @@ //===----------------------------------------------------------------------===// import Crypto -import Foundation import SwiftASN1 +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) extension Curve25519.Signing.PrivateKey { /// A Distinguished Encoding Rules (DER) encoded representation of the private key in PKCS#8 format. diff --git a/Tests/CryptoExtrasTests/CMACTests.swift b/Tests/CryptoExtrasTests/CMACTests.swift index 386d906e..7f2c8abe 100644 --- a/Tests/CryptoExtrasTests/CMACTests.swift +++ b/Tests/CryptoExtrasTests/CMACTests.swift @@ -13,9 +13,14 @@ //===----------------------------------------------------------------------===// import Crypto import CryptoExtras -import Foundation import XCTest +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + final class CMACTests: XCTestCase { // Borrowed from CryptoKit func testVector1() throws { diff --git a/Tests/CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift b/Tests/CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift index 4ce8552f..ce85cf1a 100644 --- a/Tests/CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift +++ b/Tests/CryptoExtrasTests/ECToolbox/BoringSSL/ECToolboxBoringSSLTests.swift @@ -12,9 +12,14 @@ // //===----------------------------------------------------------------------===// import Crypto -import Foundation import XCTest +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + #if !canImport(Darwin) || canImport(CryptoKit, _version: 241.100.42) // Corresponds to the CryptoKit in XCode 16.3, which has Sendable annotations import Crypto diff --git a/Tests/CryptoExtrasTests/PKCS8DERRepresentationTests.swift b/Tests/CryptoExtrasTests/PKCS8DERRepresentationTests.swift index f464e13b..41bf3271 100644 --- a/Tests/CryptoExtrasTests/PKCS8DERRepresentationTests.swift +++ b/Tests/CryptoExtrasTests/PKCS8DERRepresentationTests.swift @@ -13,11 +13,16 @@ //===----------------------------------------------------------------------===// import Crypto -import Foundation import XCTest @testable import CryptoExtras +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + final class PKCS8DERRepresentationTests: XCTestCase { func test_RSA() { let rsaPrivateKeyPEM = """