From 8ee9962f379d3d4a1729771eb0bc9b72f132285c Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Tue, 23 Jun 2026 15:21:37 +0100 Subject: [PATCH] [5.x] Align with CryptoKit APIs from Xcode 27 beta 2 (#444) ## Motivation Xcode 27 beta 2 is now available[^1] with so we can tag a compatible release with the cross-platform API here. As part of this release, we'd like to take the opportunity to streamline Swift Crypto both for our adopters and the maintainers. Historically Swift Crypto has had to release a new major version each time there is a new CryptoKit release. This happens when non-frozen CryptoKit enums are extended with new cases, which is a source-breaking change in Swift Crypto, which is not built with library evolution mode. This presents churn for adopters, even those who do not even switch over these enums, who need to widen their dependency range. For example, when Swift Crypto 4.0.0 was released, many adopters updated their version constraint to `"1.0.0"..<"5.0.0"`. However, Swift now supports extensible enums[^2] in all versions that Swift Crypto 5 will support. This presents an opportunity for Swift Crypto to label such enums as `@nonexhaustive`, which should mitigate the need for future major bumps for this reason. ## Modifications - Update shared API surface to align with CryptoKit - Simplified conditional compilation exported-import of CryptoKit - Drop support for developing the BoringSSL backend on macOS - Remove `.gyb` files after synchronisation - Removed many conditional compilation branches that will never be taken - Apply `@nonexhaustive` to all public non-frozen enums - Drop `@available` annotations in Crypto (now unused in all supported compilation modes) ## Result - Aligned with CryptoKit APIs in latest Xcode beta release. - Hopefully Swift Crypto 5.0 will be the last SemVer major release for a while. - Development of BoringSSL backend must now be done on target platform, e.g. Dev Container. [^1]: https://developer.apple.com/download/all/?q=Xcode%2027 [^2]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0487-extensible-enums.md --- Package.swift | 76 +-- README.md | 18 +- Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift | 39 +- .../AES/GCM/BoringSSL/AES-GCM_boring.swift | 4 +- .../BoringSSL/ChaChaPoly_boring.swift | 4 +- .../Crypto/AEADs/ChachaPoly/ChaChaPoly.swift | 37 +- Sources/Crypto/AEADs/Cipher.swift | 13 +- Sources/Crypto/AEADs/Nonces.swift | 32 +- Sources/Crypto/AEADs/Nonces.swift.gyb | 199 -------- Sources/Crypto/ASN1/ASN1.swift | 10 +- .../ASN1/Basic ASN1 Types/ASN1Any.swift | 10 +- .../ASN1/Basic ASN1 Types/ASN1BitString.swift | 10 +- .../ASN1/Basic ASN1 Types/ASN1Boolean.swift | 10 +- .../Basic ASN1 Types/ASN1Identifier.swift | 10 +- .../ASN1/Basic ASN1 Types/ASN1Integer.swift | 10 +- .../ASN1/Basic ASN1 Types/ASN1Null.swift | 10 +- .../Basic ASN1 Types/ASN1OctetString.swift | 10 +- .../ASN1/Basic ASN1 Types/ASN1Strings.swift | 10 +- .../Basic ASN1 Types/ArraySliceBigint.swift | 10 +- .../Basic ASN1 Types/GeneralizedTime.swift | 10 +- .../Basic ASN1 Types/ObjectIdentifier.swift | 10 +- Sources/Crypto/ASN1/ECDSASignature.swift | 14 +- Sources/Crypto/ASN1/PEMDocument.swift | 14 +- Sources/Crypto/ASN1/PKCS8PrivateKey.swift | 10 +- Sources/Crypto/ASN1/SEC1PrivateKey.swift | 14 +- .../Crypto/ASN1/SubjectPublicKeyInfo.swift | 14 +- Sources/Crypto/CMakeLists.txt | 5 - Sources/Crypto/CryptoKitErrors.swift | 30 +- .../Digests/BoringSSL/Digest_boring.swift | 4 +- Sources/Crypto/Digests/Digest.swift | 47 +- Sources/Crypto/Digests/Digests.swift | 43 +- Sources/Crypto/Digests/Digests.swift.gyb | 250 ---------- Sources/Crypto/Digests/HashFunctions.swift | 49 +- .../Crypto/Digests/HashFunctions_SHA2.swift | 50 +- .../Crypto/Digests/HashFunctions_SHA3.swift | 8 +- Sources/Crypto/Digests/XKCP/Digest_xkcp.swift | 4 +- .../Crypto/HPKE/Ciphersuite/HPKE-AEAD.swift | 16 +- .../HPKE/Ciphersuite/HPKE-Ciphersuite.swift | 16 +- .../Crypto/HPKE/Ciphersuite/HPKE-KDF.swift | 19 +- .../Ciphersuite/HPKE-KexKeyDerivation.swift | 24 +- .../Ciphersuite/HPKE-LabeledExtract.swift | 34 +- .../Crypto/HPKE/Ciphersuite/HPKE-Utils.swift | 13 +- .../Ciphersuite/KEM/Conformances/DHKEM.swift | 72 +-- .../Conformances/HPKE-KEM-Curve25519.swift | 20 +- .../KEM/Conformances/HPKE-NIST-EC-KEMs.swift | 48 +- .../HPKE/Ciphersuite/KEM/HPKE-KEM.swift | 21 +- Sources/Crypto/HPKE/HPKE-Errors.swift | 15 +- Sources/Crypto/HPKE/HPKE.swift | 36 +- .../HPKE/Key Schedule/HPKE-Context.swift | 16 +- .../HPKE/Key Schedule/HPKE-KeySchedule.swift | 14 +- Sources/Crypto/HPKE/Modes/HPKE-Modes.swift | 16 +- Sources/Crypto/Insecure/Insecure.swift | 15 +- .../Insecure/Insecure_HashFunctions.swift | 20 +- .../Crypto/KEM/BoringSSL/MLKEM_boring.swift | 4 +- .../KEM/BoringSSL/MLKEM_boring.swift.gyb | 4 +- .../Crypto/KEM/BoringSSL/MLKEM_wrapper.swift | 4 +- .../Crypto/KEM/BoringSSL/XWing_boring.swift | 4 +- Sources/Crypto/KEM/KEM-Errors.swift | 15 +- Sources/Crypto/KEM/KEM.swift | 30 +- Sources/Crypto/KEM/MLKEM.swift | 19 +- Sources/Crypto/KEM/MLKEM.swift.gyb | 205 -------- Sources/Crypto/KEM/XWing.swift | 46 +- .../Key Agreement/BoringSSL/ECDH_boring.swift | 4 +- Sources/Crypto/Key Agreement/DH.swift | 44 +- Sources/Crypto/Key Agreement/ECDH.swift | 442 +----------------- Sources/Crypto/Key Agreement/ECDH.swift.gyb | 385 --------------- Sources/Crypto/Key Derivation/ANSIx963.swift | 8 +- Sources/Crypto/Key Derivation/HKDF.swift | 61 +-- Sources/Crypto/Key Wrapping/AESWrap.swift | 19 +- .../BoringSSL/AESWrap_boring.swift | 4 +- .../Keys/EC/BoringSSL/Ed25519_boring.swift | 4 +- .../EC/BoringSSL/NISTCurvesKeys_boring.swift | 4 +- .../Keys/EC/BoringSSL/X25519Keys_boring.swift | 4 +- Sources/Crypto/Keys/EC/Curve25519.swift | 11 +- Sources/Crypto/Keys/EC/Ed25519Keys.swift | 21 +- Sources/Crypto/Keys/EC/NISTCurvesKeys.swift | 32 +- Sources/Crypto/Keys/EC/X25519Keys.swift | 31 +- .../Crypto/Keys/Symmetric/SymmetricKeys.swift | 36 +- .../HMAC/HMAC.swift | 40 +- .../MACFunctions.swift | 28 +- .../MessageAuthenticationCode.swift | 25 +- Sources/Crypto/PRF/AES.swift | 16 +- .../BoringSSL/ECDSASignature_boring.swift | 4 +- .../Signatures/BoringSSL/ECDSA_boring.swift | 4 +- .../Signatures/BoringSSL/EdDSA_boring.swift | 4 +- .../Signatures/BoringSSL/MLDSA_boring.swift | 4 +- .../BoringSSL/MLDSA_boring.swift.gyb | 4 +- .../Signatures/BoringSSL/MLDSA_wrapper.swift | 4 +- Sources/Crypto/Signatures/ECDSA.swift | 53 +-- Sources/Crypto/Signatures/ECDSA.swift.gyb | 234 ---------- Sources/Crypto/Signatures/Ed25519.swift | 23 +- Sources/Crypto/Signatures/MLDSA.swift | 17 +- Sources/Crypto/Signatures/MLDSA.swift.gyb | 197 -------- Sources/Crypto/Signatures/Signature.swift | 13 +- Sources/Crypto/Util/ArraySpanHelpers.swift | 10 +- .../BoringSSL/CryptoKitErrors_boring.swift | 4 +- .../InlineArray+withBytes_boring.swift | 2 +- .../Crypto/Util/BoringSSL/RNG_boring.swift | 4 +- Sources/Crypto/Util/Data+ArraySpan.swift | 29 +- Sources/Crypto/Util/PrettyBytes.swift | 18 +- Sources/Crypto/Util/SafeCompare.swift | 18 +- Sources/Crypto/Util/SecureBytes.swift | 133 +----- Sources/Crypto/Util/Zeroization.swift | 33 +- Sources/Crypto/vendored-sources.txt | 143 ++++++ Sources/CryptoExtras/CMakeLists.txt | 4 - .../CryptoExtras/ECToolbox/ECToolbox.swift | 9 - Tests/CryptoTests/ASN1/ASN1Tests.swift | 7 +- .../ASN1/GeneralizedTimeTests.swift | 7 +- .../AES-GCM-Runner.swift | 21 +- .../ChaChaPoly-Runner.swift | 24 +- Tests/CryptoTests/Digests/DigestsTests.swift | 39 +- Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift | 13 +- .../BoringSSL/secpECDH_Runner_boring.swift | 13 +- Tests/CryptoTests/ECDH/X25519-Runner.swift | 9 +- Tests/CryptoTests/ECDH/secpECDH_Runner.swift | 17 +- .../Encodings/BoringSSL/DERTests_boring.swift | 8 +- Tests/CryptoTests/Encodings/DERTests.swift | 15 +- .../Encodings/ECKeyEncodingsTests.swift | 9 +- .../HPKE/HPKETests-TestVectors.swift | 14 +- Tests/CryptoTests/HPKE/HPKETests.swift | 24 +- .../KEM/Boring/XWingTests_boring.swift | 8 +- Tests/CryptoTests/KEM/MLKEMKeyGenTests.swift | 14 +- Tests/CryptoTests/KEM/MLKEMTests.swift | 67 +-- Tests/CryptoTests/KEM/XWingTests.swift | 16 +- .../Key Derivation/HKDFTests.swift | 9 +- .../Key Derivation/SharedSecretTests.swift | 9 +- .../Key Derivation/X963KDFTests.swift | 9 +- .../Key Wrapping/KeyWrapping.swift | 9 +- Tests/CryptoTests/MAC/HMACTests.swift | 9 +- .../SecureBytes/SecureBytesTests.swift | 12 +- .../ECDSA/ECDSASignatureTests.swift | 9 +- .../ECDSA/RawECDSASignaturesTests.swift | 9 +- .../Signatures/EdDSA/Ed25519-Runner.swift | 15 +- .../Signatures/MLDSA/MLDSAKeyGenTests.swift | 14 +- .../Signatures/MLDSA/MLDSATests.swift | 10 +- .../SymmetricKeyTests/SymmetricKeyTests.swift | 15 +- Tests/CryptoTests/Utils/Boring/CTRDRBG.swift | 6 +- .../Utils/Boring/SequenceDRBG.swift | 6 +- Tests/CryptoTests/Utils/PrettyBytes.swift | 18 +- Tests/CryptoTests/Utils/RFCVector.swift | 27 +- Tests/CryptoTests/Utils/SplitData.swift | 1 + Tests/CryptoTests/Utils/Wycheproof.swift | 6 +- Tests/CryptoTests/Utils/XCTestUtils.swift | 1 + 143 files changed, 623 insertions(+), 3927 deletions(-) delete mode 100644 Sources/Crypto/AEADs/Nonces.swift.gyb delete mode 100644 Sources/Crypto/Digests/Digests.swift.gyb delete mode 100644 Sources/Crypto/KEM/MLKEM.swift.gyb delete mode 100644 Sources/Crypto/Key Agreement/ECDH.swift.gyb delete mode 100644 Sources/Crypto/Signatures/ECDSA.swift.gyb delete mode 100644 Sources/Crypto/Signatures/MLDSA.swift.gyb create mode 100644 Sources/Crypto/vendored-sources.txt diff --git a/Package.swift b/Package.swift index b1f86f88..5eae0628 100644 --- a/Package.swift +++ b/Package.swift @@ -26,54 +26,22 @@ import PackageDescription import class Foundation.ProcessInfo -// To develop this on Apple platforms, set this to true -let development = false +// NOTE: To develop the the non-Darwin Crypto target on macOS, use a Dev Container. +let nonDarwinPlatforms: [Platform] = [ + .linux, + .android, + .windows, + .wasi, + .openbsd, + // The SwiftPM Platform symbol is not yet public but the underlying platform name is set. + // -- https://github.com/swiftlang/swift-package-manager/blob/swift-6.2.3-RELEASE/Sources/PackageDescription/SupportedPlatforms.swift#L75 + .custom("freebsd"), +] -// Ideally, we should use `.when(platforms:)` to set `swiftSettings` and -// `dependencies` like on other platforms. However, `Platform.freebsd` is not -// yet available, and therefore we guard the settings behind this boolean. -#if os(FreeBSD) -let isFreeBSD = true -#else -let isFreeBSD = false -#endif - -let swiftSettings: [SwiftSetting] -let dependencies: [Target.Dependency] -if development || isFreeBSD { - swiftSettings = [ - .define("CRYPTO_IN_SWIFTPM"), - .define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"), - .enableExperimentalFeature("Lifetimes"), - ] - dependencies = [ - "CCryptoBoringSSL", - "CCryptoBoringSSLShims", - "CryptoBoringWrapper", - "CXKCP", - "CXKCPShims", - ] -} else { - let platforms: [Platform] = [ - Platform.linux, - Platform.android, - Platform.windows, - Platform.wasi, - Platform.openbsd, - ] - swiftSettings = [ - .define("CRYPTO_IN_SWIFTPM"), - .define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API", .when(platforms: platforms)), - .enableExperimentalFeature("Lifetimes"), - ] - dependencies = [ - .target(name: "CCryptoBoringSSL", condition: .when(platforms: platforms)), - .target(name: "CCryptoBoringSSLShims", condition: .when(platforms: platforms)), - .target(name: "CryptoBoringWrapper", condition: .when(platforms: platforms)), - .target(name: "CXKCP", condition: .when(platforms: platforms)), - .target(name: "CXKCPShims", condition: .when(platforms: platforms)), - ] -} +let swiftSettings: [SwiftSetting] = [ + .define("CRYPTO_IN_SWIFTPM"), + .enableExperimentalFeature("Lifetimes"), +] // This doesn't work when cross-compiling: the privacy manifest will be included in the Bundle and // Foundation will be linked. This is, however, strictly better than unconditionally adding the @@ -165,16 +133,16 @@ let package = Package( ), .target( name: "Crypto", - dependencies: dependencies, + dependencies: [ + .target(name: "CCryptoBoringSSL", condition: .when(platforms: nonDarwinPlatforms)), + .target(name: "CCryptoBoringSSLShims", condition: .when(platforms: nonDarwinPlatforms)), + .target(name: "CryptoBoringWrapper", condition: .when(platforms: nonDarwinPlatforms)), + .target(name: "CXKCP", condition: .when(platforms: nonDarwinPlatforms)), + .target(name: "CXKCPShims", condition: .when(platforms: nonDarwinPlatforms)), + ], exclude: privacyManifestExclude + [ "CMakeLists.txt", - "AEADs/Nonces.swift.gyb", - "Digests/Digests.swift.gyb", - "Key Agreement/ECDH.swift.gyb", - "Signatures/ECDSA.swift.gyb", - "Signatures/MLDSA.swift.gyb", "Signatures/BoringSSL/MLDSA_boring.swift.gyb", - "KEM/MLKEM.swift.gyb", "KEM/BoringSSL/MLKEM_boring.swift.gyb", ], resources: privacyManifestResource, diff --git a/README.md b/README.md index 75aa4043..5fa81a41 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ Swift Crypto is an open-source implementation of a substantial portion of the AP Swift Crypto is available as a Swift Package Manager package. To use it, add the following dependency in your `Package.swift`: ```swift -// swift-crypto 1.x, 2.x, 3.x, and 4.x are almost API compatible, so most clients +// swift-crypto 1.x, 2.x, 3.x, 4.x, and 5.x are almost API compatible, so most clients // should allow any of them -.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "5.0.0"), +.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "6.0.0"), ``` and to your target, add `Crypto` to your dependencies. You can then `import Crypto` to get access to Swift Crypto's functionality. @@ -28,7 +28,7 @@ Swift Crypto compiles in two distinct modes depending on the platform for which When building Swift Crypto for use on an Apple platform where CryptoKit is already available, Swift Crypto compiles its entire API surface down to nothing and simply re-exports the API of CryptoKit. This means that when using Apple platforms Swift Crypto simply delegates all work to the core implementation of CryptoKit, as though Swift Crypto was not even there. -When building Swift Crypto for use on Linux or Windows, Swift Crypto builds substantially more code. In particular, we build: +When building Swift Crypto for use on other platforms, Swift Crypto builds substantially more code. In particular, we build: 1. A vendored copy of BoringSSL's libcrypto. 2. The common API of Swift Crypto and CryptoKit. @@ -108,7 +108,7 @@ If you believe you have identified a vulnerability in Swift Crypto, please [repo ### Swift versions -The most recent versions of Swift Crypto support Swift 6 and newer. The minimum Swift version supported by Swift Crypto releases are detailed below: +The most recent versions of Swift Crypto support Swift 6.2 and newer. The minimum Swift version supported by Swift Crypto releases are detailed below: Swift Crypto | Minimum Swift Version --------------------|---------------------- @@ -120,7 +120,9 @@ Swift Crypto | Minimum Swift Version `3.3.0 ..< 3.8.0` | 5.8 `3.9.0 ..< 3.13.0` | 5.9 `3.13.0 ..< 4.0.0` | 5.10 -`4.0.0 ...` | 6.0 +`4.0.0 ..< 4.4.0` | 6.0 +`4.4.0 ..< 5.0.0` | 6.1 +`5.0.0 ...` | 6.2 ### Compatibility @@ -136,13 +138,15 @@ Swift Crypto 3.0.0 was released in September 2023. Again the only breaking chang Swift Crypto 4.0.0 was released in September 2025. Again the only breaking change was the addition of new cases in the `CryptoError` enumeration, so most users can safely depend on the 1.0.0, 2.0.0, 3.0.0, or 4.0.0 series of releases. Note that in this release `_CryptoExtras` was renamed to `CryptoExtras`. The old products and targets have been kept around for compatibility reasons, but users depending only on new code should use `CryptoExtras` not `_CryptoExtras`. +Swift Crypto 5.0.0 was released in September 2026. Because this version is only supported on Swift versions that support extensible enums, this release has mechanically marked all non-frozen public enums in CryptoKit as `@nonexhaustive`, which should mitigate the need for any future major releases of Swift Crypto. Most users will be able to depend on the 1.0.0, 2.0.0, 3.0.0, 4.0.0 or 5.0.0 series of releases. + To do so, please use the following dependency in your `Package.swift`: ```swift -.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "5.0.0"), +.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "6.0.0"), ``` ### Developing Swift Crypto on macOS -Swift Crypto normally defers to the OS implementation of CryptoKit on macOS. Naturally, this makes developing Swift Crypto on macOS tricky. To get Swift Crypto to build the open source implementation on macOS, in `Package.swift`, change `let development = false` to `let development = true`, as this will force Swift Crypto to build its public API. +Swift Crypto normally defers to the OS implementation of CryptoKit on macOS. Naturally, this makes developing Swift Crypto on macOS tricky. To get Swift Crypto to build the open source implementation on macOS, in `Package.swift`, use a Linux container. A Dev Container config is provided in the repo to make this easier. diff --git a/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift b/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift index d02f6c29..9e3b6d2a 100644 --- a/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift +++ b/Sources/Crypto/AEADs/AES/GCM/AES-GCM.swift @@ -11,47 +11,28 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias AESGCMImpl = CoreCryptoGCMImpl -#else typealias AESGCMImpl = OpenSSLAESGCMImpl -#endif -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension AES { /// The Advanced Encryption Standard (AES) Galois Counter Mode (GCM) cipher /// suite. + @nonexhaustive public enum GCM: Cipher, Sendable { static let tagByteCount = 16 static let defaultNonceByteCount = 12 - #if !CRYPTOKIT_STATIC_LIBRARY /// Fixed-length array of tagByteCount bytes. typealias TagStorage = [16 of UInt8] - #endif /// Secures the given plaintext message with encryption and an /// authentication tag that covers both the encrypted data and @@ -91,7 +72,6 @@ extension AES { /// - nonce: The nonce the sealing process requires. /// - authenticatedData: Additional data to be authenticated, if provided. /// - tag: receives the 16-byte authentication tag - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) #if swift(<6.3) @_lifetime(message: copy message) #endif @@ -162,7 +142,6 @@ extension AES { /// - authenticatedData: Additional data that was authenticated. /// /// The call throws an error if decryption or authentication fail. - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) #if swift(<6.3) @_lifetime(message: copy message) #endif @@ -207,11 +186,6 @@ extension AES { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension AES.GCM { /// A secure container for your data that you can access using a cipher. /// @@ -227,11 +201,6 @@ extension AES.GCM { /// /// The receiver uses another instance of the same cipher, like the /// ``open(_:using:)`` method, to open the box. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif public struct SealedBox: AEADSealedBox, Sendable { private let combinedRepresentation: Data private let nonceByteCount: Int @@ -326,4 +295,4 @@ extension AES.GCM { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift b/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift index f2a55072..7e18dd30 100644 --- a/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift +++ b/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -160,4 +160,4 @@ enum OpenSSLAESGCMImpl { } } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift b/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift index 06011986..ebc111a2 100644 --- a/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift +++ b/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -242,4 +242,4 @@ enum OpenSSLChaChaPolyImpl { } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift b/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift index fa699c3b..db63ca5c 100644 --- a/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift +++ b/Sources/Crypto/AEADs/ChachaPoly/ChaChaPoly.swift @@ -11,38 +11,21 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias ChaChaPolyImpl = CoreCryptoChaChaPolyImpl -#else typealias ChaChaPolyImpl = OpenSSLChaChaPolyImpl -#endif -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// An implementation of the ChaCha20-Poly1305 cipher. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum ChaChaPoly: Cipher, Sendable { static let tagByteCount = 16 static let keyBitsCount = 256 @@ -87,7 +70,6 @@ public enum ChaChaPoly: Cipher, Sendable { /// - nonce: The nonce the sealing process requires. /// - authenticatedData: Additional data to be authenticated. /// - tag: Will be updated with the 16-byte authentication tag. - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) #if swift(<6.3) @_lifetime(message: copy message) #endif @@ -157,7 +139,6 @@ public enum ChaChaPoly: Cipher, Sendable { /// - authenticatedData: Additional data that was authenticated. /// /// The call throws an error if decryption or authentication fail. - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) #if swift(<6.3) @_lifetime(message: copy message) #endif @@ -187,11 +168,6 @@ public enum ChaChaPoly: Cipher, Sendable { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension ChaChaPoly { /// A secure container for your data that you access using a cipher. /// @@ -208,11 +184,6 @@ extension ChaChaPoly { /// The receiver uses another instance of the same cipher, like the /// ``open(_:using:)`` method, to open the box. @frozen - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif public struct SealedBox: AEADSealedBox, Sendable { /// A combined element composed of the tag, the nonce, and the /// ciphertext. @@ -272,4 +243,4 @@ extension ChaChaPoly { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/AEADs/Cipher.swift b/Sources/Crypto/AEADs/Cipher.swift index 34e279de..518b9cd4 100644 --- a/Sources/Crypto/AEADs/Cipher.swift +++ b/Sources/Crypto/AEADs/Cipher.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif protocol AEADSealedBox: Sendable { @@ -46,11 +42,6 @@ protocol AEADSealedBox: Sendable { } /// A type representing authenticated encryption with associated data. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif protocol Cipher { associatedtype Key associatedtype SealedBox: AEADSealedBox diff --git a/Sources/Crypto/AEADs/Nonces.swift b/Sources/Crypto/AEADs/Nonces.swift index fb029b33..4d4591be 100644 --- a/Sources/Crypto/AEADs/Nonces.swift +++ b/Sources/Crypto/AEADs/Nonces.swift @@ -11,19 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif // MARK: - Generated file, do NOT edit // any edits of this file WILL be overwritten and thus discarded // see section `gyb` in `README` for details. @@ -32,22 +28,12 @@ public import Foundation // MARK: - AES.GCM + Nonce -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension AES.GCM { /// A value used once during a cryptographic operation and then discarded. /// /// Don’t reuse the same nonce for multiple calls to encryption APIs. It’s critical /// that nonces are unique per call to encryption APIs in order to protect the /// integrity of the encryption. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif public struct Nonce: ContiguousBytes, Sequence, Sendable { typealias Storage = ContiguousArray @@ -84,7 +70,6 @@ extension AES.GCM { /// - Parameters: /// - bytes: The bytes that represent the nonce. /// The initializer throws an error if the data has a length smaller than 12 bytes. - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) public init(copying bytes: RawSpan) throws(CryptoKitMetaError) { if bytes.byteCount < AES.GCM.defaultNonceByteCount { throw error(CryptoKitError.incorrectParameterSize) @@ -167,22 +152,12 @@ extension AES.GCM { } // MARK: - ChaChaPoly + Nonce -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension ChaChaPoly { /// A value used once during a cryptographic operation and then discarded. /// /// Don’t reuse the same nonce for multiple calls to encryption APIs. It’s critical /// that nonces are unique per call to encryption APIs in order to protect the /// integrity of the encryption. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif public struct Nonce: ContiguousBytes, Sequence, Sendable { typealias Storage = [12 of UInt8] @@ -219,7 +194,6 @@ extension ChaChaPoly { /// - Parameters: /// - bytes: The bytes that represent the nonce. /// The initializer throws an error if the data isn't 12 bytes long. - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) public init(copying bytes: RawSpan) throws(CryptoKitMetaError) { if bytes.byteCount != ChaChaPoly.nonceByteCount { throw error(CryptoKitError.incorrectParameterSize) @@ -299,4 +273,4 @@ extension ChaChaPoly { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/AEADs/Nonces.swift.gyb b/Sources/Crypto/AEADs/Nonces.swift.gyb deleted file mode 100644 index 156fab55..00000000 --- a/Sources/Crypto/AEADs/Nonces.swift.gyb +++ /dev/null @@ -1,199 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftCrypto open source project -// -// Copyright (c) 2019-2020 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 -// -//===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@_exported import CryptoKit -#else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else -#if canImport(FoundationEssentials) -public import FoundationEssentials -#else -public import Foundation -#endif -#endif -// MARK: - Generated file, do NOT edit -// any edits of this file WILL be overwritten and thus discarded -// see section `gyb` in `README` for details. - -%{ -ciphers = [{"name": "AES.GCM", "recommendedNonceSize": "AES.GCM.defaultNonceByteCount", "nonceValidation": "< AES.GCM.defaultNonceByteCount", "dataDescription": "/// - data: A data representation of the nonce.\n/// The initializer throws an error if the data has a length smaller than 12 bytes.", "bytesDescription": "/// - bytes: The bytes that represent the nonce.\n/// The initializer throws an error if the data has a length smaller than 12 bytes.", "fixedLength": None}] - -if "NO_CHACHAPOLY" in globals(): - pass -else: - ciphers.append({"name": "ChaChaPoly", "recommendedNonceSize": "ChaChaPoly.nonceByteCount", "nonceValidation": "!= ChaChaPoly.nonceByteCount", "dataDescription": "/// - data: A 12-byte data representation of the nonce.\n/// The initializer throws an error if the data isn't 12 bytes long.", "bytesDescription": "/// - bytes: The bytes that represent the nonce.\n/// The initializer throws an error if the data isn't 12 bytes long.", "fixedLength" : 12}) -}% - - -% for cipher in ciphers: -%{ -name = cipher["name"] -nonceSize = cipher["recommendedNonceSize"] -nonceValidation = cipher["nonceValidation"] -dataDescription = cipher["dataDescription"] -bytesDescription = cipher["bytesDescription"] - -if "NO_INLINEARRAY" in globals(): - fixedLength = None -else: - fixedLength = cipher["fixedLength"] -}% - -// MARK: - ${name} + Nonce -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -extension ${name} { - /// A value used once during a cryptographic operation and then discarded. - /// - /// Don’t reuse the same nonce for multiple calls to encryption APIs. It’s critical - /// that nonces are unique per call to encryption APIs in order to protect the - /// integrity of the encryption. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif - public struct Nonce: ContiguousBytes, Sequence, Sendable { -% if fixedLength is None: - typealias Storage = ContiguousArray -% else: - typealias Storage = [${fixedLength} of UInt8] -% end - - let storage: Storage - - /// Creates a new random nonce. - /// - /// The default nonce is a 12-byte random nonce. - public init() { - self.storage = Self.randomNonceStorage - } - - /// Creates a nonce from the given data. - /// - /// Unless your use case calls for a nonce with a specific value, use the - /// ``init()`` method to instead create a random nonce. - /// - /// - Parameters: -${dataDescription} - internal init(data: RawSpan) throws(CryptoKitMetaError) { - if data.byteCount ${nonceValidation} { - throw error(CryptoKitError.incorrectParameterSize) - } - - self.storage = Storage(copying: data) - } - - /// Creates a nonce from the given data. - /// - /// Unless your use case calls for a nonce with a specific value, use the - /// ``init()`` method to instead create a random nonce. - /// - /// - Parameters: -${bytesDescription} - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) - public init(copying bytes: RawSpan) throws(CryptoKitMetaError) { - if bytes.byteCount ${nonceValidation} { - throw error(CryptoKitError.incorrectParameterSize) - } - - self.storage = Storage(copying: bytes) - } - - /// Creates a nonce from the given data. - /// - /// Unless your use case calls for a nonce with a specific value, use the - /// ``init()`` method to instead create a random nonce. - /// - /// - Parameters: -${dataDescription} - public init(data: D) throws(CryptoKitMetaError) { - if data.count ${nonceValidation} { - throw error(CryptoKitError.incorrectParameterSize) - } - -% if fixedLength is None: - self.storage = Storage(data) -% else: - self.storage = Storage(copying: data) -% end - } - - /// Calls the given closure with a pointer to the underlying bytes of the array’s - /// contiguous storage. - /// - /// - Parameters: - /// - body: A closure with an `UnsafeRawBufferPointer` parameter that points to the - /// contiguous storage for the array. The system creates the storage if it doesn’t - /// exist. If body has a return value, that value is also used as the return value - /// for the ``withUnsafeBytes(_:)`` method. The argument is valid only for - /// the duration of the closure’s execution. - /// - /// - Returns: The return value, if any, of the body closure parameter. -#if !hasFeature(Embedded) - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - return try self.bytes.withUnsafeBytes(body) - } -#else - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R { - return try self.bytes.withUnsafeBytes(body) - } -#endif - - /// The bytes stored in the nonce. - public var bytes: RawSpan { - get { - storage.span.bytes - } - } - - /// The number of bytes stored in the nonce. - public var count: Int { storage.count } - - /// Returns an iterator over the elements of the nonce. - public func makeIterator() -> Array.Iterator { - self.withUnsafeBytes({ (buffPtr) in - return Array(buffPtr).makeIterator() - }) - } - - /// Storage for a new, random nonce. - static var randomNonceStorage: Storage { -% if fixedLength is None: - var data = Storage(repeating: 0, count: ${nonceSize}) - assert(data.count == ${nonceSize}) -% else: - var data = Storage(repeating: 0) -% end - - var mutableSpan = data.mutableSpan - var mutableBytes = mutableSpan.mutableBytes - initializeRandomNonce(into: &mutableBytes) - - return data - } - - /// Initialize the given mutable span with random bytes. - static func initializeRandomNonce(into data: inout MutableRawSpan) { - data.initializeWithRandomBytes(count: data.byteCount) - } - } -} -% end -#endif // Linux or !SwiftPM diff --git a/Sources/Crypto/ASN1/ASN1.swift b/Sources/Crypto/ASN1/ASN1.swift index 695b5060..3c69c0a0 100644 --- a/Sources/Crypto/ASN1/ASN1.swift +++ b/Sources/Crypto/ASN1/ASN1.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif // This module implements "just enough" ASN.1. Specifically, we implement exactly enough ASN.1 DER parsing to handle // the following use-cases: @@ -744,4 +740,4 @@ extension FixedWidthInteger { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Any.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Any.swift index abda265b..edaafa0e 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Any.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Any.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// An ASN1 ANY represents...well, anything. @@ -77,4 +73,4 @@ extension ASN1ImplicitlyTaggable { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1BitString.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1BitString.swift index 0e4c8a9b..bd195d89 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1BitString.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1BitString.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// A bitstring is a representation of...well...some bits. @@ -80,4 +76,4 @@ extension ASN1.ASN1BitString: ContiguousBytes { #endif } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Boolean.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Boolean.swift index 859638d7..5b1ffe52 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Boolean.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Boolean.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension Bool: ASN1ImplicitlyTaggable { static var defaultIdentifier: ASN1.ASN1Identifier { @@ -64,4 +60,4 @@ extension Bool: ASN1ImplicitlyTaggable { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Identifier.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Identifier.swift index cdf3c8e8..c3314c19 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Identifier.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Identifier.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// An `ASN1Identifier` is a representation of the abstract notion of an ASN.1 identifier. Identifiers have a number of properties that relate to both the specific @@ -155,4 +151,4 @@ extension ASN1.ASN1Identifier: CustomStringConvertible { } #endif -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Integer.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Integer.swift index 61f0ab8b..7cdb672e 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Integer.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Integer.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif /// A protocol that represents any internal object that can present itself as a INTEGER, or be parsed from /// a INTEGER. @@ -279,4 +275,4 @@ extension UInt8 { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Null.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Null.swift index 028eb3a2..ca5041f2 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Null.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Null.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// An ASN1 NULL represents nothing. @@ -51,4 +47,4 @@ extension ASN1 { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1OctetString.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1OctetString.swift index c301eadd..ce8bf46b 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1OctetString.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1OctetString.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// An octet string is a representation of a string of octets. @@ -73,4 +69,4 @@ extension ASN1.ASN1OctetString: ContiguousBytes { #endif } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Strings.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Strings.swift index 2c61a51f..61eb5509 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Strings.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ASN1Strings.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif private func contents(of string: StaticString) -> ArraySlice { if string.hasPointerRepresentation { @@ -291,4 +287,4 @@ extension ASN1.ASN1BMPString: ExpressibleByStringLiteral { #endif -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ArraySliceBigint.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ArraySliceBigint.swift index 4cbdd658..4937b2eb 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ArraySliceBigint.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ArraySliceBigint.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif // For temporary purposes we pretend that ArraySlice is our "bigint" type. We don't really need anything else. extension ArraySlice: ASN1Serializable where Element == UInt8 { } @@ -48,4 +44,4 @@ extension ArraySlice: ASN1IntegerRepresentable where Element == UInt8 { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/GeneralizedTime.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/GeneralizedTime.swift index a9f2fe3a..947669e0 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/GeneralizedTime.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/GeneralizedTime.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { struct GeneralizedTime: ASN1ImplicitlyTaggable, Hashable { @@ -413,4 +409,4 @@ extension Int { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/Basic ASN1 Types/ObjectIdentifier.swift b/Sources/Crypto/ASN1/Basic ASN1 Types/ObjectIdentifier.swift index 0e7d8ab0..5ca10b5c 100644 --- a/Sources/Crypto/ASN1/Basic ASN1 Types/ObjectIdentifier.swift +++ b/Sources/Crypto/ASN1/Basic ASN1 Types/ObjectIdentifier.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// An Object Identifier is a representation of some kind of object: really any kind of object. @@ -224,4 +220,4 @@ extension UInt { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/ECDSASignature.swift b/Sources/Crypto/ASN1/ECDSASignature.swift index 6002e3ab..aa2477fa 100644 --- a/Sources/Crypto/ASN1/ECDSASignature.swift +++ b/Sources/Crypto/ASN1/ECDSASignature.swift @@ -11,24 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// An ECDSA signature is laid out as follows: @@ -70,4 +62,4 @@ extension ASN1 { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/PEMDocument.swift b/Sources/Crypto/ASN1/PEMDocument.swift index 08ce5c48..16d62811 100644 --- a/Sources/Crypto/ASN1/PEMDocument.swift +++ b/Sources/Crypto/ASN1/PEMDocument.swift @@ -11,24 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { /// A PEM document is some data, and a discriminator type that is used to advertise the content. @@ -129,4 +121,4 @@ extension Substring { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/PKCS8PrivateKey.swift b/Sources/Crypto/ASN1/PKCS8PrivateKey.swift index 90665838..901f1f3b 100644 --- a/Sources/Crypto/ASN1/PKCS8PrivateKey.swift +++ b/Sources/Crypto/ASN1/PKCS8PrivateKey.swift @@ -11,19 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { // A PKCS#8 private key is one of two formats, depending on the version: @@ -109,4 +105,4 @@ extension ASN1 { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/SEC1PrivateKey.swift b/Sources/Crypto/ASN1/SEC1PrivateKey.swift index d6108927..0ee4a593 100644 --- a/Sources/Crypto/ASN1/SEC1PrivateKey.swift +++ b/Sources/Crypto/ASN1/SEC1PrivateKey.swift @@ -11,24 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { // For private keys, SEC 1 uses: @@ -121,4 +113,4 @@ extension ASN1 { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/ASN1/SubjectPublicKeyInfo.swift b/Sources/Crypto/ASN1/SubjectPublicKeyInfo.swift index 779cf700..3e5898ce 100644 --- a/Sources/Crypto/ASN1/SubjectPublicKeyInfo.swift +++ b/Sources/Crypto/ASN1/SubjectPublicKeyInfo.swift @@ -11,24 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension ASN1 { struct SubjectPublicKeyInfo: ASN1ImplicitlyTaggable { @@ -134,4 +126,4 @@ extension ASN1.RFC5480AlgorithmIdentifier { parameters: try! .init(erasing: ASN1.ASN1ObjectIdentifier.NamedCurves.secp521r1)) } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/CMakeLists.txt b/Sources/Crypto/CMakeLists.txt index 0474eacc..9800af02 100644 --- a/Sources/Crypto/CMakeLists.txt +++ b/Sources/Crypto/CMakeLists.txt @@ -115,11 +115,6 @@ add_library(Crypto target_compile_definitions(Crypto PRIVATE "$<$:CRYPTO_IN_SWIFTPM>") -if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") - target_compile_definitions(Crypto PRIVATE - "$<$:CRYPTO_IN_SWIFTPM_FORCE_BUILD_API>") -endif() - target_include_directories(Crypto PRIVATE $ $ diff --git a/Sources/Crypto/CryptoKitErrors.swift b/Sources/Crypto/CryptoKitErrors.swift index 8c94b8f7..b811d510 100644 --- a/Sources/Crypto/CryptoKitErrors.swift +++ b/Sources/Crypto/CryptoKitErrors.swift @@ -11,19 +11,12 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else /// General cryptography errors used by CryptoKit. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum CryptoKitError: Error { /// The key size is incorrect. case incorrectKeySize @@ -35,29 +28,17 @@ public enum CryptoKitError: Error { /// action. case underlyingCoreCryptoError(error: Int32) /// The framework can't wrap the specified key. - @available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *) case wrapFailure /// The framework can't unwrap the specified key. - @available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *) case unwrapFailure /// The parameter is invalid. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif case invalidParameter } -@available(iOS 17.4, macOS 14.4, watchOS 10.4, tvOS 17.4, macCatalyst 17.4, *) extension CryptoKitError: Equatable, Hashable {} /// Errors from decoding ASN.1 content. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum CryptoKitASN1Error: Equatable, Error, Hashable { /// The ASN.1 tag for this field is invalid or unsupported. case invalidFieldIdentifier @@ -107,6 +88,7 @@ struct RSAPSSSPKIError: Error { #endif #if hasFeature(Embedded) +@nonexhaustive public enum CryptoKitMetaError: Error { case cryptoKitError(underlyingError: CryptoKitError) case asn1Error(underlyingError: CryptoKitASN1Error) @@ -122,7 +104,7 @@ internal func error(_ error: CryptoKitASN1Error) -> CryptoKitMetaError { internal func error(_ error: RSAPSSSPKIErrors) -> CryptoKitMetaError { .rsapssspkiError(underlyingError: RSAPSSSPKIError(error: error)) } -#else /* !hasFeature(Embedded) */ +#else public typealias CryptoKitMetaError = any Error internal func error(_ error: CryptoKitError) -> CryptoKitError { error } internal func error(_ error: CryptoKitASN1Error) -> CryptoKitASN1Error { error } diff --git a/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift b/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift index 52093db1..724033e4 100644 --- a/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift +++ b/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -210,4 +210,4 @@ private final class DigestContext { withUnsafeMutablePointer(to: &self.context) { $0.zeroize() } } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Digests/Digest.swift b/Sources/Crypto/Digests/Digest.swift index e47215d3..d7e3463d 100644 --- a/Sources/Crypto/Digests/Digest.swift +++ b/Sources/Crypto/Digests/Digest.swift @@ -11,44 +11,26 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif #if hasFeature(Embedded) /// A type that represents the output of a hash. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif @preconcurrency public protocol Digest: Hashable, Sendable, ContiguousBytes, Sequence where Element == UInt8 { /// The number of bytes in the digest. static var byteCount: Int { get } } -#else // hasFeature(Embedded) +#else /// A type that represents the output of a hash. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif @preconcurrency public protocol Digest: Hashable, Sendable, ContiguousBytes, CustomStringConvertible, Sequence where Element == UInt8 { /// The number of bytes in the digest. @@ -56,20 +38,10 @@ public protocol Digest: Hashable, Sendable, ContiguousBytes, CustomStringConvert } #endif // hasFeature(Embedded) -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif protocol DigestPrivate: Digest { init?(initializingWith body: (inout OutputRawSpan) -> ()) } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension DigestPrivate { @inlinable init?(copying bytes: RawSpan) { @@ -79,11 +51,6 @@ extension DigestPrivate { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Digest { public func makeIterator() -> Array.Iterator { self.withUnsafeBytes({ (buffPtr) in @@ -93,11 +60,6 @@ extension Digest { } // We want to implement constant-time comparison for digests. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Digest { /// Determines whether two digests are equal. /// @@ -130,7 +92,6 @@ extension Digest { } #if !hasFeature(Embedded) -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension Digest { public var description: String { return "\(Self.self): \(Array(self).hexString)" @@ -138,4 +99,4 @@ extension Digest { } #endif -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Digests/Digests.swift b/Sources/Crypto/Digests/Digests.swift index 46f7c080..396218cb 100644 --- a/Sources/Crypto/Digests/Digests.swift +++ b/Sources/Crypto/Digests/Digests.swift @@ -11,7 +11,8 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else // MARK: - Generated file, do NOT edit @@ -22,11 +23,6 @@ // MARK: - SHA256Digest + DigestPrivate /// The output of a Secure Hashing Algorithm 2 (SHA-2) hash with a 256-bit digest. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SHA256Digest: DigestPrivate { let bytes: (UInt64, UInt64, UInt64, UInt64) @@ -110,11 +106,6 @@ public struct SHA256Digest: DigestPrivate { // MARK: - SHA384Digest + DigestPrivate /// The output of a Secure Hashing Algorithm 2 (SHA-2) hash with a 384-bit digest. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SHA384Digest: DigestPrivate { let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64) @@ -200,11 +191,6 @@ public struct SHA384Digest: DigestPrivate { // MARK: - SHA512Digest + DigestPrivate /// The output of a Secure Hashing Algorithm 2 (SHA-2) hash with a 512-bit digest. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SHA512Digest: DigestPrivate { let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64) @@ -289,19 +275,9 @@ public struct SHA512Digest: DigestPrivate { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Insecure { // MARK: - SHA1Digest + DigestPrivate /// The output of a SHA1 hash. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SHA1Digest: DigestPrivate { let bytes: (UInt64, UInt64, UInt64) @@ -381,19 +357,9 @@ public struct SHA1Digest: DigestPrivate { } } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Insecure { // MARK: - MD5Digest + DigestPrivate /// The output of a MD5 hash. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct MD5Digest: DigestPrivate { let bytes: (UInt64, UInt64) @@ -478,7 +444,6 @@ public struct MD5Digest: DigestPrivate { // MARK: - SHA3_256Digest + DigestPrivate /// The output of a Secure Hashing Algorithm 3 (SHA-2) hash with a 256-bit digest. -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public struct SHA3_256Digest: DigestPrivate { let bytes: (UInt64, UInt64, UInt64, UInt64) @@ -560,7 +525,6 @@ public struct SHA3_256Digest: DigestPrivate { } // MARK: - SHA3_384Digest + DigestPrivate /// The output of a Secure Hashing Algorithm 3 (SHA-2) hash with a 384-bit digest. -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public struct SHA3_384Digest: DigestPrivate { let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64) @@ -644,7 +608,6 @@ public struct SHA3_384Digest: DigestPrivate { } // MARK: - SHA3_512Digest + DigestPrivate /// The output of a Secure Hashing Algorithm 3 (SHA-2) hash with a 512-bit digest. -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public struct SHA3_512Digest: DigestPrivate { let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64) @@ -729,4 +692,4 @@ public struct SHA3_512Digest: DigestPrivate { } } #endif // !CRYPTOKIT_IN_SEP -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Digests/Digests.swift.gyb b/Sources/Crypto/Digests/Digests.swift.gyb deleted file mode 100644 index 562132e0..00000000 --- a/Sources/Crypto/Digests/Digests.swift.gyb +++ /dev/null @@ -1,250 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftCrypto open source project -// -// Copyright (c) 2019-2020 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 -// -//===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@_exported import CryptoKit -#else -// MARK: - Generated file, do NOT edit -// any edits of this file WILL be overwritten and thus discarded -// see section `gyb` in `README` for details. - -%{ -digests_and_length = [{"name": "SHA256", "count": 32},{"name": "SHA384","count":48},{"name":"SHA512", "count": 64}] -if "NO_INSECURE_DIGEST" in globals(): - pass -else: - digests_and_length.extend([{"name":"SHA1", "count":20, "prefix":"Insecure "},{"name":"MD5", "count":16, "prefix":"Insecure "}]) -}% - -% for HF in digests_and_length: -%{ - name = HF["name"] - byteCount = HF["count"] - wordsCount = (byteCount*8)//64 + (0 if ((byteCount*8)%64) == 0 else 1) -}% -%{ - if "prefix" in HF.keys(): - protocol_prefix = "" - protocol_prefix = protocol_prefix + "#if !CRYPTOKIT_STATIC_LIBRARY\n" - protocol_prefix = protocol_prefix + "@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)\n" - protocol_prefix = protocol_prefix + "#else // CRYPTOKIT_STATIC_LIBRARY\n" - protocol_prefix = protocol_prefix + "@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)\n" - protocol_prefix = protocol_prefix + "#endif\n" - protocol_prefix = protocol_prefix + "extension " + HF["prefix"] + "{" - protocol_suffix = "}" - parent = HF["prefix"].strip() + "/" + name - else: - protocol_prefix = "" - protocol_suffix = "" - parent = name - - if name == "SHA1" or name == "MD5": - abstract = "The output of a " + name + " hash." - else: - abstract = "The output of a Secure Hashing Algorithm 2 (SHA-2) hash with a " + name[-3:] + "-bit digest." -}% -${protocol_prefix} -// MARK: - ${name}Digest + DigestPrivate -/// ${abstract} -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -public struct ${name}Digest: DigestPrivate { - let bytes: (${(wordsCount-1)*"UInt64, "+"UInt64"}) - - init?(initializingWith body: (inout OutputRawSpan) -> ()) { - var bytes = (${(wordsCount-1)*"UInt64(0), "+"UInt64(0)"}) - let initialized = withUnsafeMutableBytes(of: &bytes) { targetPtr in - var output = OutputRawSpan(buffer: targetPtr, initializedCount: 0) - body(&output) - return output.finalize(for: targetPtr) - } - guard initialized == ${byteCount} else { - return nil - } - self.bytes = bytes - } - - /// The number of bytes in the digest. - public static var byteCount: Int { - return ${byteCount} - } - - /// Invokes the given closure with a buffer pointer covering the raw bytes of - /// the digest. - /// - /// - Parameters: - /// - body: A closure that takes a raw buffer pointer to the bytes of the digest - /// and returns the digest. - /// - /// - Returns: The digest, as returned from the body closure. -#if !hasFeature(Embedded) - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - return try Swift.withUnsafeBytes(of: bytes) { - let boundsCheckedPtr = UnsafeRawBufferPointer(start: $0.baseAddress, - count: Self.byteCount) - return try body(boundsCheckedPtr) - } - } -#else - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R { - return try Swift.withUnsafeBytes(of: bytes) { ptr throws(E) -> R in - let boundsCheckedPtr = UnsafeRawBufferPointer(start: ptr.baseAddress, - count: Self.byteCount) - return try body(boundsCheckedPtr) - } - } -#endif - - private func toArray() -> ArraySlice { - var array = [UInt8]() - array.appendByte(bytes.0) - % for index in range(wordsCount-1): - array.appendByte(bytes.${index+1}) - % end - return array.prefix(${name}Digest.byteCount) - } - -#if !hasFeature(Embedded) - /// A human-readable description of the digest. - public var description: String { - return "\("${name}") digest: \(toArray().hexString)" - } -#endif - - /// Hashes the essential components of the digest by feeding them into the - /// given hash function. - /// - /// This method is part of the digest’s conformance to Swift standard library’s - /// protocol, making - /// it possible to compare digests. Don’t confuse that hashing with the - /// cryptographically secure hashing that you use to create the digest in the - /// first place by, for example, calling ``${parent}/hash(data:)``. - /// - /// - Parameters: - /// - hasher: The hash function to use when combining the components of - /// the digest. - public func hash(into hasher: inout Hasher) { - self.withUnsafeBytes { hasher.combine(bytes: $0) } - } -} -${protocol_suffix} -% end - - -#if !CRYPTOKIT_IN_SEP -%{ -digests_and_length = [ - {"name": "SHA3_256", "count": 32}, - {"name": "SHA3_384", "count": 48}, - {"name": "SHA3_512", "count": 64} -] -}% - -% for HF in digests_and_length: -%{ - name = HF["name"] - byteCount = HF["count"] - wordsCount = (byteCount*8)//64 + (0 if ((byteCount*8)%64) == 0 else 1) -}% -%{ - parent = name - abstract = "The output of a Secure Hashing Algorithm 3 (SHA-2) hash with a " + name[-3:] + "-bit digest." -}% -// MARK: - ${name}Digest + DigestPrivate -/// ${abstract} -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -public struct ${name}Digest: DigestPrivate { - let bytes: (${(wordsCount-1)*"UInt64, "+"UInt64"}) - - init?(initializingWith body: (inout OutputRawSpan) -> ()) { - var bytes = (${(wordsCount-1)*"UInt64(0), "+"UInt64(0)"}) - let initialized = withUnsafeMutableBytes(of: &bytes) { targetPtr in - var output = OutputRawSpan(buffer: targetPtr, initializedCount: 0) - body(&output) - return output.finalize(for: targetPtr) - } - guard initialized == ${byteCount} else { - return nil - } - self.bytes = bytes - } - - /// The number of bytes in the digest. - public static var byteCount: Int { - return ${byteCount} - } - - /// Invokes the given closure with a buffer pointer covering the raw bytes of - /// the digest. - /// - /// - Parameters: - /// - body: A closure that takes a raw buffer pointer to the bytes of the digest - /// and returns the digest. - /// - /// - Returns: The digest, as returned from the body closure. -#if !hasFeature(Embedded) - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - return try Swift.withUnsafeBytes(of: bytes) { - let boundsCheckedPtr = UnsafeRawBufferPointer(start: $0.baseAddress, - count: Self.byteCount) - return try body(boundsCheckedPtr) - } - } -#else - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R { - return try Swift.withUnsafeBytes(of: bytes) { ptr throws(E) -> R in - let boundsCheckedPtr = UnsafeRawBufferPointer(start: ptr.baseAddress, - count: Self.byteCount) - return try body(boundsCheckedPtr) - } - } -#endif - - private func toArray() -> ArraySlice { - var array = [UInt8]() - array.appendByte(bytes.0) - % for index in range(wordsCount-1): - array.appendByte(bytes.${index+1}) - % end - return array.prefix(upTo: ${name}Digest.byteCount) - } - -#if !hasFeature(Embedded) - /// A human-readable description of the digest. - public var description: String { - return "\("${name}") digest: \(toArray().hexString)" - } -#endif - - /// Hashes the essential components of the digest by feeding them into the - /// given hash function. - /// - /// This method is part of the digest’s conformance to Swift standard library’s - /// protocol, making - /// it possible to compare digests. Don’t confuse that hashing with the - /// cryptographically secure hashing that you use to create the digest in the - /// first place by, for example, calling ``${parent}/hash(data:)``. - /// - /// - Parameters: - /// - hasher: The hash function to use when combining the components of - /// the digest. - public func hash(into hasher: inout Hasher) { - self.withUnsafeBytes { hasher.combine(bytes: $0) } - } -} -% end -#endif // !CRYPTOKIT_IN_SEP -#endif // Linux or !SwiftPM diff --git a/Sources/Crypto/Digests/HashFunctions.swift b/Sources/Crypto/Digests/HashFunctions.swift index bab0f409..3ff1a3cd 100644 --- a/Sources/Crypto/Digests/HashFunctions.swift +++ b/Sources/Crypto/Digests/HashFunctions.swift @@ -11,37 +11,18 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias DigestImpl = CoreCryptoDigestImpl -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias DigestImplSHA3 = CoreCryptoDigestImpl -#else typealias DigestImpl = OpenSSLDigestImpl typealias DigestImplSHA3 = XKCPDigestImpl -#endif -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// A type that performs cryptographically secure hashing. /// @@ -61,30 +42,11 @@ public import Foundation /// authentication code (MAC) like ``HMAC`` instead. MACs rely on hashing, but /// incorporate a secret cryptographic key into the digest computation. Only a /// user that has the key can generate a valid MAC. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HashFunction: Sendable { /// The number of bytes that represents the hash function’s internal state. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.13, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, visionOS 1.0, *) - #endif static var blockByteCount: Int { get } - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - /// The type of the digest returned by the hash function. - #if CRYPTOKIT_STATIC_LIBRARY - associatedtype Digest: CryptoKit_Static.Digest - #else - associatedtype Digest: CryptoKit.Digest - #endif - #else associatedtype Digest: Crypto.Digest - #endif /// Creates a hash function. /// @@ -139,11 +101,6 @@ extension HashFunction { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension HashFunction { /// Computes a digest of the buffer. /// @@ -216,4 +173,4 @@ extension HashFunction { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Digests/HashFunctions_SHA2.swift b/Sources/Crypto/Digests/HashFunctions_SHA2.swift index 8d10a262..3e406527 100644 --- a/Sources/Crypto/Digests/HashFunctions_SHA2.swift +++ b/Sources/Crypto/Digests/HashFunctions_SHA2.swift @@ -11,7 +11,8 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else /// An implementation of Secure Hashing Algorithm 2 (SHA-2) hashing with a @@ -26,11 +27,6 @@ /// in memory, you can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public typealias SHA2_256 = SHA256 /// An implementation of Secure Hashing Algorithm 2 (SHA-2) hashing with a @@ -45,18 +41,8 @@ public typealias SHA2_256 = SHA256 /// in memory, you can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SHA256: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal state. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.13, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, visionOS 1.0, *) - #endif public static let blockByteCount: Int = 64 /// The number of bytes in a SHA256 digest. public static let byteCount: Int = 32 @@ -126,11 +112,6 @@ public struct SHA256: HashFunctionImplementationDetails, Sendable { /// in memory, you can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public typealias SHA2_384 = SHA384 /// An implementation of Secure Hashing Algorithm 2 (SHA-2) hashing with a @@ -145,18 +126,8 @@ public typealias SHA2_384 = SHA384 /// in memory, you can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SHA384: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal state. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.13, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, visionOS 1.0, *) - #endif public static let blockByteCount: Int = 128 /// The number of bytes in a SHA384 digest. public static let byteCount: Int = 48 @@ -227,11 +198,6 @@ public struct SHA384: HashFunctionImplementationDetails, Sendable { /// in memory, you can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public typealias SHA2_512 = SHA512 /// An implementation of Secure Hashing Algorithm 2 (SHA-2) hashing with a @@ -246,18 +212,8 @@ public typealias SHA2_512 = SHA512 /// in memory, you can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SHA512: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal state. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.13, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, visionOS 1.0, *) - #endif public static let blockByteCount: Int = 128 /// The number of bytes in a SHA512 digest. public static let byteCount: Int = 64 @@ -315,4 +271,4 @@ public struct SHA512: HashFunctionImplementationDetails, Sendable { return impl.finalize() } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Digests/HashFunctions_SHA3.swift b/Sources/Crypto/Digests/HashFunctions_SHA3.swift index e7755ee3..f36a8ae6 100644 --- a/Sources/Crypto/Digests/HashFunctions_SHA3.swift +++ b/Sources/Crypto/Digests/HashFunctions_SHA3.swift @@ -11,7 +11,8 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else #if !CRYPTOKIT_IN_SEP @@ -26,7 +27,6 @@ /// applications can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public struct SHA3_256: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal state. public static let blockByteCount: Int = 136 @@ -97,7 +97,6 @@ public struct SHA3_256: HashFunctionImplementationDetails, Sendable { /// applications can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public struct SHA3_384: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal state. public static let blockByteCount: Int = 104 @@ -168,7 +167,6 @@ public struct SHA3_384: HashFunctionImplementationDetails, Sendable { /// applications can compute the digest iteratively by creating a new hash /// instance, calling the ``update(data:)`` method repeatedly with blocks of /// data, and then calling the ``finalize()`` method to get the result. -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public struct SHA3_512: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal state. public static let blockByteCount: Int = 72 @@ -228,4 +226,4 @@ public struct SHA3_512: HashFunctionImplementationDetails, Sendable { } } #endif // !CRYPTOKIT_IN_SEP -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Digests/XKCP/Digest_xkcp.swift b/Sources/Crypto/Digests/XKCP/Digest_xkcp.swift index cb40f203..7c927594 100644 --- a/Sources/Crypto/Digests/XKCP/Digest_xkcp.swift +++ b/Sources/Crypto/Digests/XKCP/Digest_xkcp.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CXKCP @@ -157,4 +157,4 @@ private final class XKCPDigestContext { withUnsafeMutablePointer(to: &self.context) { $0.zeroize() } } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/HPKE-AEAD.swift b/Sources/Crypto/HPKE/Ciphersuite/HPKE-AEAD.swift index 85d9c178..4a2bd501 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/HPKE-AEAD.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/HPKE-AEAD.swift @@ -11,28 +11,20 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif - -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { /// The authenticated encryption with associated data (AEAD) algorithms to use in HPKE. + @nonexhaustive public enum AEAD: CaseIterable, Hashable, Sendable { /// An Advanced Encryption Standard cipher in Galois/Counter Mode with a key length of 128 bits. case AES_GCM_128 @@ -129,4 +121,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/HPKE-Ciphersuite.swift b/Sources/Crypto/HPKE/Ciphersuite/HPKE-Ciphersuite.swift index 6ab4c5fc..b96591c5 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/HPKE-Ciphersuite.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/HPKE-Ciphersuite.swift @@ -11,25 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif - -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { /// Cipher suites to use in hybrid public key encryption (HPKE). @@ -53,7 +44,6 @@ extension HPKE { public static let Curve25519_SHA256_ChachaPoly = Ciphersuite(kem: .Curve25519_HKDF_SHA256, kdf: .HKDF_SHA256, aead: .chaChaPoly) /// A cipher suite for HPKE that uses the X-Wing KEM (ML-KEM-768 with X25519), SHA-2 key derivation /// with a 256-bit digest, and the Advanced Encryption Standard cipher in Galois/Counter Mode with a key length of 256 bits. - @available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public static let XWingMLKEM768X25519_SHA256_AES_GCM_256 = Ciphersuite(kem: .XWingMLKEM768X25519, kdf: .HKDF_SHA256, aead: .AES_GCM_256) fileprivate static let ciphersuiteLabel = Data("HPKE".utf8) @@ -87,4 +77,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/HPKE-KDF.swift b/Sources/Crypto/HPKE/Ciphersuite/HPKE-KDF.swift index 3b309718..d45f65c8 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/HPKE-KDF.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/HPKE-KDF.swift @@ -11,31 +11,20 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { /// The key derivation functions to use in HPKE. + @nonexhaustive public enum KDF: CaseIterable, Hashable, Sendable { /// An HMAC-based key derivation function that uses SHA-2 hashing with a 256-bit digest. case HKDF_SHA256 @@ -114,4 +103,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/HPKE-KexKeyDerivation.swift b/Sources/Crypto/HPKE/Ciphersuite/HPKE-KexKeyDerivation.swift index 84491e53..aea30291 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/HPKE-KexKeyDerivation.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/HPKE-KexKeyDerivation.swift @@ -11,46 +11,28 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif private let suiteIDLabel = Data("KEM".utf8) -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { struct KexUtils { static func ExtractAndExpand(dh: ContiguousBytes, enc: Data, pkRm: Data, pkSm: Data? = nil, kem: HPKE.KEM, kdf: HPKE.KDF) -> SymmetricKey { var suiteID = suiteIDLabel suiteID.append(kem.identifier) - #if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API - #if CRYPTOKIT_STATIC_LIBRARY - return CryptoKit_Static.ExtractAndExpand(zz: dh, kemContext: kemContext(enc: enc, pkRm: pkRm, pkSm: pkSm), - suiteID: suiteID, kem: kem, kdf: kdf) - #else - return CryptoKit.ExtractAndExpand(zz: dh, kemContext: kemContext(enc: enc, pkRm: pkRm, pkSm: pkSm), - suiteID: suiteID, kem: kem, kdf: kdf) - #endif - #else return Crypto.ExtractAndExpand(zz: dh, kemContext: kemContext(enc: enc, pkRm: pkRm, pkSm: pkSm), suiteID: suiteID, kem: kem, kdf: kdf) - #endif } static func kemContext(enc: Data, pkRm: Data, pkSm: Data? = nil) -> Data { @@ -63,4 +45,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/HPKE-LabeledExtract.swift b/Sources/Crypto/HPKE/Ciphersuite/HPKE-LabeledExtract.swift index d6f349e4..c468e997 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/HPKE-LabeledExtract.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/HPKE-LabeledExtract.swift @@ -11,19 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif private let protocolLabel = Data("HPKE-v1".utf8) @@ -36,11 +33,6 @@ extension Data { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif internal func ExtractAndExpand(zz: ContiguousBytes, kemContext: Data, suiteID: Data, kem: HPKE.KEM, kdf: HPKE.KDF) -> SymmetricKey { let eaePrk = LabeledExtract(salt: Data(), label: eaePRKLabel, ikm: zz, suiteID: suiteID, kdf: kdf) @@ -48,11 +40,6 @@ internal func ExtractAndExpand(zz: ContiguousBytes, kemContext: Data, suiteID: D info: kemContext, outputByteCount: kem.nSecret, suiteID: suiteID, kdf: kdf) } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif internal func LabeledExtract(salt: Data?, label: Data, ikm: ContiguousBytes?, suiteID: Data, kdf: HPKE.KDF) -> SymmetricKey { var labeled_ikm = protocolLabel labeled_ikm.append(suiteID) @@ -61,11 +48,6 @@ internal func LabeledExtract(salt: Data?, label: Data, ikm: ContiguousBytes?, su return kdf.extract(salt: salt ?? Data(), ikm: SymmetricKey(data: labeled_ikm)) } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif internal func LabeledExpand(prk: SymmetricKey, label: Data, info: Info, outputByteCount: UInt16, suiteID: Data, kdf: HPKE.KDF) -> SymmetricKey { var labeled_info = I2OSP(value: Int(outputByteCount), outputByteCount: 2) labeled_info.append(protocolLabel) @@ -75,22 +57,12 @@ internal func LabeledExpand(prk: SymmetricKey, label: Data, return kdf.expand(prk: prk, info: labeled_info, outputByteCount: Int(outputByteCount)) } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif internal func NonSecretOutputLabeledExtract(salt: Data?, label: Data, ikm: ContiguousBytes?, suiteID: Data, kdf: HPKE.KDF) -> Data { return Data(unsafeFromContiguousBytes: LabeledExtract(salt: salt, label: label, ikm: ikm, suiteID: suiteID, kdf: kdf)) } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif internal func NonSecretOutputLabeledExpand(prk: SymmetricKey, label: Data, info: Data, outputByteCount: UInt16, suiteID: Data, kdf: HPKE.KDF) -> Data { return Data(unsafeFromContiguousBytes: LabeledExpand(prk: prk, label: label, info: info, outputByteCount: outputByteCount, suiteID: suiteID, kdf: kdf)) } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/HPKE-Utils.swift b/Sources/Crypto/HPKE/Ciphersuite/HPKE-Utils.swift index 765debd4..89bc056f 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/HPKE-Utils.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/HPKE-Utils.swift @@ -11,13 +11,11 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) #if os(Windows) import ucrt @@ -36,18 +34,13 @@ import FoundationEssentials #else import Foundation #endif -#endif internal func I2OSP(value: Int, outputByteCount: Int) -> Data { precondition(outputByteCount > 0, "Cannot I2OSP with no output length.") precondition(value >= 0, "I2OSP requires a non-null value.") - #if !CRYPTOKIT_NO_ACCESS_TO_FOUNDATION let requiredBytes = Int(ceil(log2(Double(max(value, 1) + 1)) / 8)) - #else - let requiredBytes = Int((log2_bridge(Double(max(value, 1) + 1)) / 8).rounded(.up)) - #endif precondition(outputByteCount >= requiredBytes) @@ -60,4 +53,4 @@ internal func I2OSP(value: Int, outputByteCount: Int) -> Data { return data } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/DHKEM.swift b/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/DHKEM.swift index 0356c6b2..0f34c661 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/DHKEM.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/DHKEM.swift @@ -11,30 +11,18 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// A type that ``HPKE`` uses to encode the public key. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HPKEPublicKeySerialization: Sendable { /// Creates a public key from an encoded representation. @@ -53,11 +41,6 @@ public protocol HPKEPublicKeySerialization: Sendable { } /// A type that represents the public key in a Diffie-Hellman key exchange. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HPKEDiffieHellmanPublicKey: HPKEPublicKeySerialization, Sendable where EphemeralPrivateKey.PublicKey == Self { /// The type of the ephemeral private key. @@ -65,11 +48,6 @@ public protocol HPKEDiffieHellmanPublicKey: HPKEPublicKeySerialization, Sendable } /// A type that represents the public key in HPKE -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HPKEKEMPublicKey: KEMPublicKey, HPKEPublicKeySerialization where EphemeralPrivateKey.PublicKey == Self { /// The type of the ephemeral private key. @@ -77,29 +55,14 @@ public protocol HPKEKEMPublicKey: KEMPublicKey, HPKEPublicKeySerialization where } /// A type that represents the private key in a Diffie-Hellman key exchange. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HPKEDiffieHellmanPrivateKey: Sendable, DiffieHellmanKeyAgreement where PublicKey: HPKEDiffieHellmanPublicKey {} /// A type that represents the private key in HPKE. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HPKEKEMPrivateKey: Sendable, KEMPrivateKey where PublicKey: HPKEKEMPublicKey {} /// A type that represents the generation of private keys in a Diffie-Hellman key exchange. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HPKEDiffieHellmanPrivateKeyGeneration: HPKEDiffieHellmanPrivateKey, Sendable { /// Creates a private key generator. @@ -107,11 +70,6 @@ public protocol HPKEDiffieHellmanPrivateKeyGeneration: HPKEDiffieHellmanPrivateK } /// A type that represents the generation of private keys in HPKE -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol HPKEKEMPrivateKeyGeneration: HPKEKEMPrivateKey, Sendable { /// Creates a private key generator. @@ -120,30 +78,13 @@ public protocol HPKEKEMPrivateKeyGeneration: HPKEKEMPrivateKey, Sendable { extension HPKE { /// A container for Diffie-Hellman key encapsulation mechanisms (KEMs). - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif + @nonexhaustive public enum DHKEM: Sendable { - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif struct PublicKey: KEMPublicKey where DHPK == DHPK.EphemeralPrivateKey.PublicKey { let kem: HPKE.KEM let key: DHPK - #if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API - #if CRYPTOKIT_STATIC_LIBRARY - typealias EncapsulationResult = CryptoKit_Static.KEM.EncapsulationResult - #else - typealias EncapsulationResult = CryptoKit.KEM.EncapsulationResult - #endif - #else typealias EncapsulationResult = Crypto.KEM.EncapsulationResult - #endif init(_ publicKey: DHPK, kem: HPKE.KEM) throws { // TODO: Validate Ciphersuite Mismatches @@ -167,11 +108,6 @@ extension HPKE { } } - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #endif struct PrivateKey: KEMPrivateKey { let kem: HPKE.KEM let key: DHSK @@ -232,4 +168,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-KEM-Curve25519.swift b/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-KEM-Curve25519.swift index 108cbe8a..e8c02175 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-KEM-Curve25519.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-KEM-Curve25519.swift @@ -11,33 +11,19 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif - -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension Curve25519.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration {} -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension Curve25519.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { /// The type of the ephemeral private key associated with this public key. public typealias EphemeralPrivateKey = Curve25519.KeyAgreement.PrivateKey @@ -80,4 +66,4 @@ extension Curve25519.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { public typealias HPKEEphemeralPrivateKey = Curve25519.KeyAgreement.PrivateKey } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-NIST-EC-KEMs.swift b/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-NIST-EC-KEMs.swift index 69992f98..f73dc930 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-NIST-EC-KEMs.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/KEM/Conformances/HPKE-NIST-EC-KEMs.swift @@ -11,29 +11,17 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension P256.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration { /// Creates a NIST P-256 elliptic curve private key for use with Diffie-Hellman key exchange. public init() { @@ -41,12 +29,6 @@ extension P256.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif - extension P256.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { /// The type of the ephemeral private key associated with this public key. public typealias EphemeralPrivateKey = P256.KeyAgreement.PrivateKey @@ -86,12 +68,6 @@ extension P256.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { public typealias HPKEEphemeralPrivateKey = P256.KeyAgreement.PrivateKey } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif - extension P384.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration { /// Creates a NIST P-384 elliptic curve private key for use with Diffie-Hellman key exchange. public init() { @@ -99,12 +75,6 @@ extension P384.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif - extension P384.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { /// The type of the ephemeral private key associated with this public key. public typealias EphemeralPrivateKey = P384.KeyAgreement.PrivateKey @@ -144,12 +114,6 @@ extension P384.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif - extension P521.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration { /// Creates a NIST P-521 elliptic curve private key for use with Diffie-Hellman key exchange. public init() { @@ -157,12 +121,6 @@ extension P521.KeyAgreement.PrivateKey: HPKEDiffieHellmanPrivateKeyGeneration { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif - extension P521.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { /// The type of the ephemeral private key associated with this public key. public typealias EphemeralPrivateKey = P521.KeyAgreement.PrivateKey @@ -205,4 +163,4 @@ extension P521.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey { public typealias HPKEEphemeralPrivateKey = P521.KeyAgreement.PrivateKey } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Ciphersuite/KEM/HPKE-KEM.swift b/Sources/Crypto/HPKE/Ciphersuite/KEM/HPKE-KEM.swift index 0d6e7326..a0cbf575 100644 --- a/Sources/Crypto/HPKE/Ciphersuite/KEM/HPKE-KEM.swift +++ b/Sources/Crypto/HPKE/Ciphersuite/KEM/HPKE-KEM.swift @@ -11,34 +11,22 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { /// The key encapsulation mechanisms to use in HPKE. /// /// The module-lattice key encapsulation mechanism (ML-KEM) is designed to offer increased security in situations /// where an adversary uses a quantum computer. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif + @nonexhaustive public enum KEM: CaseIterable, Hashable, Sendable { public static var allCases: [HPKE.KEM] { var cases = [KEM.P256_HKDF_SHA256, KEM.P384_HKDF_SHA384, KEM.P521_HKDF_SHA512, KEM.Curve25519_HKDF_SHA256] @@ -62,7 +50,6 @@ extension HPKE { case Curve25519_HKDF_SHA256 /// A key encapsulation mechanism using the X-Wing (ML-KEM-768 with X25519) key encapsulation mechanism /// and SHA-2 hashing with a 256-bit digest. - @available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) case XWingMLKEM768X25519 /// Return the KEM algorithm identifier as defined in section 7.1 of [RFC 9180](https://www.ietf.org/rfc/rfc9180.pdf). @@ -113,4 +100,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/HPKE-Errors.swift b/Sources/Crypto/HPKE/HPKE-Errors.swift index 7190a68d..14d6a129 100644 --- a/Sources/Crypto/HPKE/HPKE-Errors.swift +++ b/Sources/Crypto/HPKE/HPKE-Errors.swift @@ -11,27 +11,20 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { /// Hybrid public key encryption (HPKE) errors that CryptoKit uses. + @nonexhaustive public enum Errors: Error { /// The parameters for initializing an HPKE sender or receiver are inconsistent. case inconsistentParameters @@ -52,4 +45,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/HPKE.swift b/Sources/Crypto/HPKE/HPKE.swift index 01e04c62..7305decf 100644 --- a/Sources/Crypto/HPKE/HPKE.swift +++ b/Sources/Crypto/HPKE/HPKE.swift @@ -11,22 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif -#else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem #else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// A container for hybrid public key encryption (HPKE) operations. /// @@ -57,18 +50,9 @@ public import Foundation /// /// ### Handling errors /// - ``Errors`` -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum HPKE: Sendable {} -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { /// Static constant used to store the fixed-string label for the HPKE export API /// See: https://datatracker.ietf.org/doc/html/rfc9180#name-secret-export @@ -82,11 +66,6 @@ extension HPKE { /// in turn to retrieve its ciphertext. The recipient of the messages needs to process them in the /// same order as the `Sender`, using the same encryption mode, cipher suite, and key schedule information /// (`info`), as well as the `Sender`'s ``encapsulatedKey``. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public struct Sender: Sendable { private var context: Context /// The encapsulated symmetric key that the recipient uses to decrypt messages. @@ -141,7 +120,6 @@ extension HPKE { /// - ciphersuite: The cipher suite that defines the cryptographic algorithms to use. /// - info: Data that the key derivation function uses to compute the symmetric key material. The sender and the recipient need to use the same `info` data. /// - Note: The system throws errors from ``CryptoKit/HPKE/Errors`` when it encounters them. - @available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public init(recipientKey: PK, ciphersuite: Ciphersuite, info: Data) throws { self.context = try Context(senderRoleWithCiphersuite: ciphersuite, mode: .base, psk: nil, pskID: nil, pkR: recipientKey, info: info) self.encapsulatedKey = context.encapsulated @@ -242,11 +220,6 @@ extension HPKE { /// same order as the `Sender`, using the same cipher suite, encryption mode, and key schedule information /// (`info` data). /// Use a separate `Recipient` instance for each stream of messages. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif public struct Recipient: Sendable { private var context: Context @@ -295,7 +268,6 @@ extension HPKE { /// - info: Data that the key derivation function uses to compute the symmetric key material. The sender and the recipient need to use the same `info` data. /// - encapsulatedKey: The encapsulated symmetric key that the sender provides. /// - Note: The system throws errors from ``CryptoKit/HPKE/Errors`` when it encounters them. - @available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) public init(privateKey: SK, ciphersuite: Ciphersuite, info: Data, encapsulatedKey: Data) throws { self.context = try Context(recipientRoleWithCiphersuite: ciphersuite, mode: .base, enc: encapsulatedKey, psk: nil, pskID: nil, skR: privateKey, info: info, pkS: nil) } @@ -386,4 +358,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Key Schedule/HPKE-Context.swift b/Sources/Crypto/HPKE/Key Schedule/HPKE-Context.swift index 6d625cd6..3a0a7212 100644 --- a/Sources/Crypto/HPKE/Key Schedule/HPKE-Context.swift +++ b/Sources/Crypto/HPKE/Key Schedule/HPKE-Context.swift @@ -11,25 +11,17 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { struct Context: Sendable { var keySchedule: KeySchedule @@ -44,7 +36,6 @@ extension HPKE { sharedSecret: encapsulationResult.sharedSecret, info: info, psk: psk, pskID: pskID, ciphersuite: ciphersuite) } - @available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) init(senderRoleWithCiphersuite ciphersuite: Ciphersuite, mode: Mode, psk: SymmetricKey?, pskID: Data?, pkR: PublicKey, info: Data) throws { let encapsulationResult = try pkR.encapsulate() encapsulated = encapsulationResult.encapsulated @@ -77,7 +68,6 @@ extension HPKE { self.keySchedule = try KeySchedule(mode: mode, sharedSecret: sharedSecret, info: info, psk: psk, pskID: pskID, ciphersuite: ciphersuite) } - @available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) init(recipientRoleWithCiphersuite ciphersuite: Ciphersuite, mode: Mode, enc: Data, psk: SymmetricKey?, pskID: Data?, skR: PrivateKey, info: Data, pkS: PrivateKey.PublicKey?) throws { let sharedSecret = try skR.decapsulate(enc) self.encapsulated = enc @@ -86,4 +76,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Key Schedule/HPKE-KeySchedule.swift b/Sources/Crypto/HPKE/Key Schedule/HPKE-KeySchedule.swift index f1f81103..2d63daaf 100644 --- a/Sources/Crypto/HPKE/Key Schedule/HPKE-KeySchedule.swift +++ b/Sources/Crypto/HPKE/Key Schedule/HPKE-KeySchedule.swift @@ -11,25 +11,17 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { internal struct KeySchedule: Sendable { fileprivate static let pksIDHashLabel = Data("psk_id_hash".utf8) @@ -166,4 +158,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/HPKE/Modes/HPKE-Modes.swift b/Sources/Crypto/HPKE/Modes/HPKE-Modes.swift index 48b2d2c1..e0f6e314 100644 --- a/Sources/Crypto/HPKE/Modes/HPKE-Modes.swift +++ b/Sources/Crypto/HPKE/Modes/HPKE-Modes.swift @@ -11,22 +11,12 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension HPKE { - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 17.0, macOS 10.15, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal enum Mode: CaseIterable { case base case psk @@ -48,4 +38,4 @@ extension HPKE { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Insecure/Insecure.swift b/Sources/Crypto/Insecure/Insecure.swift index f87b34fb..0c4163c1 100644 --- a/Sources/Crypto/Insecure/Insecure.swift +++ b/Sources/Crypto/Insecure/Insecure.swift @@ -11,22 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else /// A container for older, cryptographically insecure algorithms. /// /// - Important: These algorithms aren’t considered cryptographically secure, /// but the framework provides them for backward compatibility with older /// services that require them. For new services, avoid these algorithms. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum Insecure: Sendable {} -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Insecure/Insecure_HashFunctions.swift b/Sources/Crypto/Insecure/Insecure_HashFunctions.swift index 01e31641..95dcbc0d 100644 --- a/Sources/Crypto/Insecure/Insecure_HashFunctions.swift +++ b/Sources/Crypto/Insecure/Insecure_HashFunctions.swift @@ -11,14 +11,10 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Insecure { /// An implementation of SHA1 hashing. /// @@ -39,11 +35,6 @@ extension Insecure { public struct SHA1: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal /// state. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.13, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, visionOS 1.0, *) - #endif public static let blockByteCount: Int = 64 /// The number of bytes in a SHA1 digest. @@ -124,11 +115,6 @@ extension Insecure { public struct MD5: HashFunctionImplementationDetails, Sendable { /// The number of bytes that represents the hash function’s internal /// state. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.13, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, visionOS 1.0, *) - #endif public static let blockByteCount: Int = 64 /// The number of bytes in an MD5 digest. public static let byteCount: Int = 16 @@ -189,4 +175,4 @@ extension Insecure { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift b/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift index 4a4fab70..efb8b7e7 100644 --- a/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift +++ b/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift @@ -16,7 +16,7 @@ // any edits of this file WILL be overwritten and thus discarded // see section `gyb` in `README` for details. -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -573,4 +573,4 @@ enum MLKEM { static let sharedSecretByteCount = 32 } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift.gyb b/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift.gyb index 112376e5..3cf87085 100644 --- a/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift.gyb +++ b/Sources/Crypto/KEM/BoringSSL/MLKEM_boring.swift.gyb @@ -16,7 +16,7 @@ // any edits of this file WILL be overwritten and thus discarded // see section `gyb` in `README` for details. -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -306,4 +306,4 @@ enum MLKEM { static let sharedSecretByteCount = 32 } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/BoringSSL/MLKEM_wrapper.swift b/Sources/Crypto/KEM/BoringSSL/MLKEM_wrapper.swift index 4400dbd7..61c11132 100644 --- a/Sources/Crypto/KEM/BoringSSL/MLKEM_wrapper.swift +++ b/Sources/Crypto/KEM/BoringSSL/MLKEM_wrapper.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -289,4 +289,4 @@ struct OpenSSLMLKEMPrivateKeyImpl: B } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift b/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift index 5ca369ca..4260631f 100644 --- a/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift +++ b/Sources/Crypto/KEM/BoringSSL/XWing_boring.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -260,4 +260,4 @@ extension OpenSSLXWingPrivateKeyImpl { } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/KEM-Errors.swift b/Sources/Crypto/KEM/KEM-Errors.swift index 78e8e735..67d3b5ad 100644 --- a/Sources/Crypto/KEM/KEM-Errors.swift +++ b/Sources/Crypto/KEM/KEM-Errors.swift @@ -11,27 +11,20 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension KEM { /// Errors that CryptoKit throws when it encounters problems in key encapsulation mechanism (KEM) operations. + @nonexhaustive public enum Errors: Error { /// The public key CryptoKit receives when it initializes a key encapsulation operation doesn't match the expected value. case publicKeyMismatchDuringInitialization @@ -41,4 +34,4 @@ extension KEM { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/KEM.swift b/Sources/Crypto/KEM/KEM.swift index c36b3417..915d7090 100644 --- a/Sources/Crypto/KEM/KEM.swift +++ b/Sources/Crypto/KEM/KEM.swift @@ -11,32 +11,21 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// A key encapsulation mechanism. /// /// Use a key encapsulation mechanism (KEM) to protect a symmetric cryptographic key that you share with another party. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else //CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum KEM: Sendable { /// The result of a key encapsulation operation. public struct EncapsulationResult: Sendable { @@ -54,11 +43,6 @@ public enum KEM: Sendable { } /// The public key for a key encapsulation mechanism. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else //CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol KEMPublicKey: Sendable { /// Generates and encapsulates a shared secret. @@ -70,11 +54,6 @@ public protocol KEMPublicKey: Sendable { } /// The private key for a key encapsulation mechanism. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else //CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol KEMPrivateKey: Sendable { associatedtype PublicKey: KEMPublicKey @@ -96,7 +75,6 @@ public protocol KEMPrivateKey: Sendable { } /// A one-time private key for a key encapsulation mechanism, which can only decapsulate once but it does so faster. -@available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) @preconcurrency public protocol KEMOneTimePrivateKey: ~Copyable, Sendable { associatedtype PublicKey: KEMPublicKey @@ -116,4 +94,4 @@ public protocol KEMOneTimePrivateKey: ~Copyable, Sendable { /// The associated public key. var publicKey: PublicKey { get } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/MLKEM.swift b/Sources/Crypto/KEM/MLKEM.swift index 14d7d20b..2cccab71 100644 --- a/Sources/Crypto/KEM/MLKEM.swift +++ b/Sources/Crypto/KEM/MLKEM.swift @@ -11,7 +11,8 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else #if canImport(FoundationEssentials) @@ -20,21 +21,14 @@ public import FoundationEssentials public import Foundation #endif -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -typealias MLKEMPublicKeyImpl = CoreCryptoMLKEMPublicKeyImpl -typealias MLKEMPrivateKeyImpl = CoreCryptoMLKEMPrivateKeyImpl -#else typealias MLKEMPublicKeyImpl = OpenSSLMLKEMPublicKeyImpl typealias MLKEMPrivateKeyImpl = OpenSSLMLKEMPrivateKeyImpl -#endif /// The Module-Lattice key encapsulation mechanism (KEM). -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) +@nonexhaustive public enum MLKEM768: Sendable {} -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) extension MLKEM768 { /// A public key you use to encapsulate shared secrets with the Module-Lattice key encapsulation mechanism. public struct PublicKey: KEMPublicKey, Sendable { @@ -151,7 +145,6 @@ extension MLKEM768 { } } - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) /// A one-time-use private key to decapsulate a shared secret with the Module-Lattice key encapsulation mechanism. /// /// The associated decapsulation function can be multiple times faster than the one implemented for PrivateKey, @@ -194,10 +187,9 @@ extension MLKEM768 { /// The Module-Lattice key encapsulation mechanism (KEM). -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) +@nonexhaustive public enum MLKEM1024: Sendable {} -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) extension MLKEM1024 { /// A public key you use to encapsulate shared secrets with the Module-Lattice key encapsulation mechanism. public struct PublicKey: KEMPublicKey, Sendable { @@ -314,7 +306,6 @@ extension MLKEM1024 { } } - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) /// A one-time-use private key to decapsulate a shared secret with the Module-Lattice key encapsulation mechanism. /// /// The associated decapsulation function can be multiple times faster than the one implemented for PrivateKey, @@ -356,4 +347,4 @@ extension MLKEM1024 { } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/KEM/MLKEM.swift.gyb b/Sources/Crypto/KEM/MLKEM.swift.gyb deleted file mode 100644 index cf423df4..00000000 --- a/Sources/Crypto/KEM/MLKEM.swift.gyb +++ /dev/null @@ -1,205 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftCrypto open source project -// -// Copyright (c) 2019-2020 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 -// -//===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@_exported import CryptoKit -#else -#if canImport(FoundationEssentials) -public import FoundationEssentials -#else -public import Foundation -#endif -%{ - MLKEM_VARIANTS = [{"name": "MLKEM768", "ccinfo": "cckem_mlkem768()!"}, {"name": "MLKEM1024", "ccinfo": "cckem_mlkem1024()!"}] -}% - -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -typealias MLKEMPublicKeyImpl = CoreCryptoMLKEMPublicKeyImpl -typealias MLKEMPrivateKeyImpl = CoreCryptoMLKEMPrivateKeyImpl -#else -typealias MLKEMPublicKeyImpl = OpenSSLMLKEMPublicKeyImpl -typealias MLKEMPrivateKeyImpl = OpenSSLMLKEMPrivateKeyImpl -#endif - -% for MLKEM_VARIANT in MLKEM_VARIANTS: -%{ - NAME = MLKEM_VARIANT["name"] - INFO = MLKEM_VARIANT["ccinfo"] -}% - -/// The Module-Lattice key encapsulation mechanism (KEM). -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -public enum ${NAME}: Sendable {} - -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -extension ${NAME} { - /// A public key you use to encapsulate shared secrets with the Module-Lattice key encapsulation mechanism. - public struct PublicKey: KEMPublicKey, Sendable { - var impl: MLKEMPublicKeyImpl<${NAME}> - - /// Initializes a public key from a raw representation. - /// - Parameter rawRepresentation: Data that represents the public key. - public init(rawRepresentation: D) throws { - self.impl = try MLKEMPublicKeyImpl(rawRepresentation: rawRepresentation) - } - - /// A serialized representation of the public key. - public var rawRepresentation: Data { - get { - return self.impl.rawRepresentation - } - } - - /// Generates and encapsulates a shared secret. - /// - /// - Returns: an encapsulated shared secret, that you decapsulate by calling ``${NAME}/PrivateKey/decapsulate(_:)`` on the corresponding private key. - public func encapsulate() throws -> KEM.EncapsulationResult { - return try self.impl.encapsulate() - } - - func encapsulateWithSeed(encapSeed: Data) throws -> KEM.EncapsulationResult { - return try self.impl.encapsulateWithSeed(encapSeed) - } - } - - /// A private key you use to decapsulate shared secrets with the Module-Lattice key encapsulation mechanism. - public struct PrivateKey: KEMPrivateKey { - internal let impl: MLKEMPrivateKeyImpl<${NAME}> - - internal init(_ impl: MLKEMPrivateKeyImpl<${NAME}>) { - self.impl = impl - } - - /// Generates a new, random private key. - public static func generate() throws -> ${NAME}.PrivateKey { - let impl = try MLKEMPrivateKeyImpl<${NAME}>.generatePrivateKey() - return PrivateKey(impl) - } - - static func generateWithSeed(_ seed: Data) throws -> ${NAME}.PrivateKey { - let impl = try MLKEMPrivateKeyImpl<${NAME}>.generateWithSeed(seed) - return PrivateKey(impl) - } - - /// Initializes a random private key. - public init() throws { - self = try PrivateKey.generate() - } - - /// Initializes a private key from a seed representation and optional public key. - /// - /// - Parameters: - /// - seedRepresentation: The seed representation `d||z`, as specified in the `ML-KEM.KeyGen_internal(d,z)` algorithm (Algorithm 16) of FIPS 203. - /// - publicKey: An optional public key. Pass this to check that the initialized private key is consistent with the public key. The initializer throws if the public key doesn't match the expected value. - public init(seedRepresentation: D, publicKey: ${NAME}.PublicKey?) throws { - var publicKeyRawRepresentation: Data? = nil - if publicKey != nil { - publicKeyRawRepresentation = publicKey!.rawRepresentation - } - self.impl = try MLKEMPrivateKeyImpl<${NAME}>(seedRepresentation: seedRepresentation, publicKeyRawRepresentation: publicKeyRawRepresentation) - } - - /// The private key's seed representation. - /// - /// The seed is `d||z`, as specified in the algorithm `ML-KEM.KeyGen_internal(d,z)` (Algorithm 16) of FIPS 203. - public var seedRepresentation: Data { - get { - return self.impl.seedRepresentation - } - } - - /// Decapsulate a shared secret. - /// - /// - Parameters: - /// - encapsulated: An encapsulated shared secret, that you get by calling ``${NAME}/PublicKey/encapsulate()`` on the corresponding public key. - /// - Returns: The shared secret. - public func decapsulate(_ encapsulated: D) throws -> SymmetricKey { - return try impl.decapsulate(encapsulated: encapsulated) - } - - /// The corresponding public key. - public var publicKey: ${NAME}.PublicKey { - get { - self.impl.publicKey - } - } - - /// Initializes a private key from an integrity-checked representation. - /// - /// - Parameter integrityCheckedRepresentation: A representation of the private key that includes the seed value, and a hash of the corresponding public key. - public init(integrityCheckedRepresentation: D) throws { - guard integrityCheckedRepresentation.count == MLKEMPrivateKeyImpl<${NAME}>.seedSize + 32 else { - throw KEM.Errors.invalidSeed - } - let seed = Data(integrityCheckedRepresentation).subdata(in: 0...seedSize) - let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: MLKEMPrivateKeyImpl<${NAME}>.seedSize..(seedRepresentation: seed, publicKeyHash: publicKeyHash) - } - - /// An integrity-checked representation of the private key. - /// - /// This representation includes the seed value, and a hash of the corresponding public key. - public var integrityCheckedRepresentation: Data { - get { - return self.impl.integrityCheckedRepresentation - } - } - } - - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) - /// A one-time-use private key to decapsulate a shared secret with the Module-Lattice key encapsulation mechanism. - /// - /// The associated decapsulation function can be multiple times faster than the one implemented for PrivateKey, - /// but this private key can only be used to decapsulate a shared secret once. - public struct OneTimePrivateKey: KEMOneTimePrivateKey, ~Copyable { - internal let impl: MLKEMPrivateKeyImpl<${NAME}> - - internal init(_ impl: MLKEMPrivateKeyImpl<${NAME}>) { - self.impl = impl - } - - /// Generates a new, random one-time-use private key. - public static func generate() throws -> ${NAME}.OneTimePrivateKey { - let impl = try MLKEMPrivateKeyImpl<${NAME}>.generatePrivateKey() - return OneTimePrivateKey(impl) - } - - /// Initializes a random one-time-use private key. - public init() throws { - self = try OneTimePrivateKey.generate() - } - - /// Decapsulate a shared secret. - /// - /// - Parameters: - /// - encapsulated: An encapsulated shared secret, that you get by calling ``${NAME}/PublicKey/encapsulate()`` on the corresponding public key. - /// - Returns: The shared secret. - public consuming func decapsulate(_ encapsulated: D) throws -> SymmetricKey { - return try impl.decapsulate(encapsulated: encapsulated) - } - - /// The corresponding public key. - public var publicKey: ${NAME}.PublicKey { - get { - self.impl.publicKey - } - } - } -} - -% end - -#endif // Linux or !SwiftPM diff --git a/Sources/Crypto/KEM/XWing.swift b/Sources/Crypto/KEM/XWing.swift index 2ebc7944..0cdb7ef6 100644 --- a/Sources/Crypto/KEM/XWing.swift +++ b/Sources/Crypto/KEM/XWing.swift @@ -11,54 +11,29 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else +#if canImport(CryptoKit) @_exported import CryptoKit -#endif #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) -typealias XWingPublicKeyImpl = CoreCryptoXWingPublicKeyImpl -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) -typealias XWingPrivateKeyImpl = CoreCryptoXWingPrivateKeyImpl -#else -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) typealias XWingPublicKeyImpl = OpenSSLXWingPublicKeyImpl -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) typealias XWingPrivateKeyImpl = OpenSSLXWingPrivateKeyImpl -#endif /// The X-Wing (ML-KEM768 with X25519) Key Encapsulation Mechanism, defined in /// https://datatracker.ietf.org/doc/html/draft-connolly-cfrg-xwing-kem-06 -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum XWingMLKEM768X25519: Sendable {} -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension XWingMLKEM768X25519 { public struct PublicKey: KEMPublicKey { var impl: XWingPublicKeyImpl @@ -120,7 +95,6 @@ extension XWingMLKEM768X25519 { } } - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) /// A one-time-use private key to decapsulate a shared secret with the X-Wing key encapsulation mechanism. /// /// The associated decapsulation function can be multiple times faster than the one implemented for PrivateKey, @@ -156,11 +130,6 @@ extension XWingMLKEM768X25519 { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension XWingMLKEM768X25519.PrivateKey: HPKEKEMPrivateKeyGeneration { public init() throws { self = try Self.generate() @@ -190,11 +159,6 @@ extension XWingMLKEM768X25519.PrivateKey: HPKEKEMPrivateKeyGeneration { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif extension XWingMLKEM768X25519.PublicKey: HPKEKEMPublicKey { /// The type of the ephemeral private key associated with this public key. public typealias EphemeralPrivateKey = XWingMLKEM768X25519.PrivateKey @@ -237,4 +201,4 @@ extension XWingMLKEM768X25519.PublicKey: HPKEKEMPublicKey { public typealias HPKEEphemeralPrivateKey = XWingMLKEM768X25519.PrivateKey } #endif -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift b/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift index 932d61b4..19c9e983 100644 --- a/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift +++ b/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -45,4 +45,4 @@ extension P521.KeyAgreement.PrivateKey { return SharedSecret(ss: key) } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Key Agreement/DH.swift b/Sources/Crypto/Key Agreement/DH.swift index 7a864b6b..31f3eb27 100644 --- a/Sources/Crypto/Key Agreement/DH.swift +++ b/Sources/Crypto/Key Agreement/DH.swift @@ -11,27 +11,18 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// A Diffie-Hellman Key Agreement Key -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 17.0, macOS 14.0, watchOS 10.0, tvOS 17.0, macCatalyst 17.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) -#endif @preconcurrency public protocol DiffieHellmanKeyAgreement: Sendable { /// The public key share type to perform the DH Key Agreement @@ -63,18 +54,9 @@ public protocol DiffieHellmanKeyAgreement: Sendable { /// symmetric key suitable for creating a message authentication code like /// ``HMAC``, or for opening and closing a sealed box with a cipher like /// ``ChaChaPoly`` or ``AES``. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SharedSecret: ContiguousBytes, Sendable { var ss: SecureBytes - internal init(withExternalSS ss: SS) { - self.ss = SecureBytes(bytes: ss) - } - internal init(ss: SecureBytes){ self.ss = ss } @@ -132,11 +114,6 @@ public struct SharedSecret: ContiguousBytes, Sendable { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SharedSecret: Hashable { public func hash(into hasher: inout Hasher) { ss.withUnsafeBytes { hasher.combine(bytes: $0) } @@ -144,11 +121,6 @@ extension SharedSecret: Hashable { } // We want to implement constant-time comparison for digests. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SharedSecret: Equatable { public static func == (lhs: Self, rhs: Self) -> Bool { return safeCompare(lhs, rhs) @@ -174,11 +146,6 @@ extension SharedSecret: Equatable { } #if !hasFeature(Embedded) -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SharedSecret: CustomStringConvertible { public var description: String { return "\(Self.self): \(ss.hexString)" @@ -186,11 +153,6 @@ extension SharedSecret: CustomStringConvertible { } #endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension HashFunction { // A wrapper function to keep the unsafe code in one place. mutating func update(_ secret: SharedSecret) { @@ -205,4 +167,4 @@ extension HashFunction { } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Key Agreement/ECDH.swift b/Sources/Crypto/Key Agreement/ECDH.swift index 8ee65ca7..6dc51272 100644 --- a/Sources/Crypto/Key Agreement/ECDH.swift +++ b/Sources/Crypto/Key Agreement/ECDH.swift @@ -11,52 +11,29 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias NISTCurvePublicKeyImpl = CoreCryptoNISTCurvePublicKeyImpl -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias NISTCurvePrivateKeyImpl = CoreCryptoNISTCurvePrivateKeyImpl -#else typealias NISTCurvePublicKeyImpl = OpenSSLNISTCurvePublicKeyImpl typealias NISTCurvePrivateKeyImpl = OpenSSLNISTCurvePrivateKeyImpl -#endif -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif // MARK: - Generated file, do NOT edit // any edits of this file WILL be overwritten and thus discarded // see section `gyb` in `README` for details. // MARK: - P256 + Signing -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P256 { /// A mechanism used to create or verify a cryptographic signature using /// the NIST P-256 elliptic curve digital signature algorithm (ECDSA). + @nonexhaustive public enum Signing: Sendable { /// A P-256 public key used to verify cryptographic signatures. @@ -97,11 +74,6 @@ extension P256 { /// - Parameters: /// - compressedRepresentation: A compressed representation of the key as a collection /// of contiguous bytes. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public init(compressedRepresentation: Bytes) throws(CryptoKitMetaError) { impl = try NISTCurvePublicKeyImpl(compressedRepresentation: compressedRepresentation) } @@ -112,11 +84,6 @@ extension P256 { /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) guard pem.type == "PUBLIC KEY" else { @@ -131,11 +98,6 @@ extension P256 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) let parsed = try ASN1.SubjectPublicKeyInfo(asn1Encoded: bytes) @@ -156,19 +118,9 @@ extension P256 { public var x963Representation: Data { impl.x963Representation } /// A compressed representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public var compressedRepresentation: Data { impl.compressedRepresentation } /// A Distinguished Encoding Rules (DER) encoded representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let spki = ASN1.SubjectPublicKeyInfo(algorithmIdentifier: .ecdsaP256, key: Array(self.x963Representation)) var serializer = ASN1.Serializer() @@ -180,11 +132,6 @@ extension P256 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -227,26 +174,12 @@ extension P256 { impl = try NISTCurvePrivateKeyImpl(data: rawRepresentation) } - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal init(seed: Data, compactRepresentable: Bool) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(seed: seed, compactRepresentable: compactRepresentable) - } - #if !hasFeature(Embedded) /// Creates a P-256 private key for signing from a Privacy-Enhanced Mail /// PEM) representation. /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) @@ -268,11 +201,6 @@ extension P256 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) @@ -304,11 +232,6 @@ extension P256 { public var x963Representation: Data { impl.x963Representation } /// A Distinguished Encoding Rules (DER) encoded representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let pkey = ASN1.PKCS8PrivateKey(algorithm: .ecdsaP256, privateKey: Array(self.rawRepresentation), publicKey: Array(self.publicKey.x963Representation)) var serializer = ASN1.Serializer() @@ -320,11 +243,6 @@ extension P256 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -334,16 +252,12 @@ extension P256 { } } // MARK: - P256 + KeyAgreement -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P256 { /// A mechanism used to create a shared secret between two users by /// performing NIST P-256 elliptic curve Diffie Hellman (ECDH) key /// exchange. + @nonexhaustive public enum KeyAgreement: Sendable { /// A P-256 public key used for key agreement. @@ -384,11 +298,6 @@ extension P256 { /// - Parameters: /// - compressedRepresentation: A compressed representation of the key as a collection /// of contiguous bytes. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public init(compressedRepresentation: Bytes) throws(CryptoKitMetaError) { impl = try NISTCurvePublicKeyImpl(compressedRepresentation: compressedRepresentation) } @@ -399,11 +308,6 @@ extension P256 { /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) guard pem.type == "PUBLIC KEY" else { @@ -418,11 +322,6 @@ extension P256 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) let parsed = try ASN1.SubjectPublicKeyInfo(asn1Encoded: bytes) @@ -443,19 +342,9 @@ extension P256 { public var x963Representation: Data { impl.x963Representation } /// A compressed representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public var compressedRepresentation: Data { impl.compressedRepresentation } /// A Distinguished Encoding Rules (DER) encoded representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let spki = ASN1.SubjectPublicKeyInfo(algorithmIdentifier: .ecdsaP256, key: Array(self.x963Representation)) var serializer = ASN1.Serializer() @@ -467,11 +356,6 @@ extension P256 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -514,26 +398,12 @@ extension P256 { impl = try NISTCurvePrivateKeyImpl(data: rawRepresentation) } - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal init(seed: Data, compactRepresentable: Bool) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(seed: seed, compactRepresentable: compactRepresentable) - } - #if !hasFeature(Embedded) /// Creates a P-256 private key for key agreement from a Privacy-Enhanced Mail /// PEM) representation. /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) @@ -555,11 +425,6 @@ extension P256 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) @@ -591,11 +456,6 @@ extension P256 { public var x963Representation: Data { impl.x963Representation } /// A Distinguished Encoding Rules (DER) encoded representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let pkey = ASN1.PKCS8PrivateKey(algorithm: .ecdsaP256, privateKey: Array(self.rawRepresentation), publicKey: Array(self.publicKey.x963Representation)) var serializer = ASN1.Serializer() @@ -607,11 +467,6 @@ extension P256 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -621,15 +476,11 @@ extension P256 { } } // MARK: - P384 + Signing -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P384 { /// A mechanism used to create or verify a cryptographic signature using /// the NIST P-384 elliptic curve digital signature algorithm (ECDSA). + @nonexhaustive public enum Signing: Sendable { /// A P-384 public key used to verify cryptographic signatures. @@ -670,11 +521,6 @@ extension P384 { /// - Parameters: /// - compressedRepresentation: A compressed representation of the key as a collection /// of contiguous bytes. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public init(compressedRepresentation: Bytes) throws(CryptoKitMetaError) { impl = try NISTCurvePublicKeyImpl(compressedRepresentation: compressedRepresentation) } @@ -685,11 +531,6 @@ extension P384 { /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) guard pem.type == "PUBLIC KEY" else { @@ -704,11 +545,6 @@ extension P384 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) let parsed = try ASN1.SubjectPublicKeyInfo(asn1Encoded: bytes) @@ -729,19 +565,9 @@ extension P384 { public var x963Representation: Data { impl.x963Representation } /// A compressed representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public var compressedRepresentation: Data { impl.compressedRepresentation } /// A Distinguished Encoding Rules (DER) encoded representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let spki = ASN1.SubjectPublicKeyInfo(algorithmIdentifier: .ecdsaP384, key: Array(self.x963Representation)) var serializer = ASN1.Serializer() @@ -753,11 +579,6 @@ extension P384 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -800,26 +621,12 @@ extension P384 { impl = try NISTCurvePrivateKeyImpl(data: rawRepresentation) } - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal init(seed: Data, compactRepresentable: Bool) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(seed: seed, compactRepresentable: compactRepresentable) - } - #if !hasFeature(Embedded) /// Creates a P-384 private key for signing from a Privacy-Enhanced Mail /// PEM) representation. /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) @@ -841,11 +648,6 @@ extension P384 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) @@ -877,11 +679,6 @@ extension P384 { public var x963Representation: Data { impl.x963Representation } /// A Distinguished Encoding Rules (DER) encoded representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let pkey = ASN1.PKCS8PrivateKey(algorithm: .ecdsaP384, privateKey: Array(self.rawRepresentation), publicKey: Array(self.publicKey.x963Representation)) var serializer = ASN1.Serializer() @@ -893,11 +690,6 @@ extension P384 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -907,16 +699,12 @@ extension P384 { } } // MARK: - P384 + KeyAgreement -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P384 { /// A mechanism used to create a shared secret between two users by /// performing NIST P-384 elliptic curve Diffie Hellman (ECDH) key /// exchange. + @nonexhaustive public enum KeyAgreement: Sendable { /// A P-384 public key used for key agreement. @@ -957,11 +745,6 @@ extension P384 { /// - Parameters: /// - compressedRepresentation: A compressed representation of the key as a collection /// of contiguous bytes. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public init(compressedRepresentation: Bytes) throws(CryptoKitMetaError) { impl = try NISTCurvePublicKeyImpl(compressedRepresentation: compressedRepresentation) } @@ -972,11 +755,6 @@ extension P384 { /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) guard pem.type == "PUBLIC KEY" else { @@ -991,11 +769,6 @@ extension P384 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) let parsed = try ASN1.SubjectPublicKeyInfo(asn1Encoded: bytes) @@ -1016,19 +789,9 @@ extension P384 { public var x963Representation: Data { impl.x963Representation } /// A compressed representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public var compressedRepresentation: Data { impl.compressedRepresentation } /// A Distinguished Encoding Rules (DER) encoded representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let spki = ASN1.SubjectPublicKeyInfo(algorithmIdentifier: .ecdsaP384, key: Array(self.x963Representation)) var serializer = ASN1.Serializer() @@ -1040,11 +803,6 @@ extension P384 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -1087,26 +845,12 @@ extension P384 { impl = try NISTCurvePrivateKeyImpl(data: rawRepresentation) } - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal init(seed: Data, compactRepresentable: Bool) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(seed: seed, compactRepresentable: compactRepresentable) - } - #if !hasFeature(Embedded) /// Creates a P-384 private key for key agreement from a Privacy-Enhanced Mail /// PEM) representation. /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) @@ -1128,11 +872,6 @@ extension P384 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) @@ -1164,11 +903,6 @@ extension P384 { public var x963Representation: Data { impl.x963Representation } /// A Distinguished Encoding Rules (DER) encoded representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let pkey = ASN1.PKCS8PrivateKey(algorithm: .ecdsaP384, privateKey: Array(self.rawRepresentation), publicKey: Array(self.publicKey.x963Representation)) var serializer = ASN1.Serializer() @@ -1180,11 +914,6 @@ extension P384 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -1194,15 +923,11 @@ extension P384 { } } // MARK: - P521 + Signing -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P521 { /// A mechanism used to create or verify a cryptographic signature using /// the NIST P-521 elliptic curve digital signature algorithm (ECDSA). + @nonexhaustive public enum Signing: Sendable { /// A P-521 public key used to verify cryptographic signatures. @@ -1243,11 +968,6 @@ extension P521 { /// - Parameters: /// - compressedRepresentation: A compressed representation of the key as a collection /// of contiguous bytes. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public init(compressedRepresentation: Bytes) throws(CryptoKitMetaError) { impl = try NISTCurvePublicKeyImpl(compressedRepresentation: compressedRepresentation) } @@ -1258,11 +978,6 @@ extension P521 { /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) guard pem.type == "PUBLIC KEY" else { @@ -1277,11 +992,6 @@ extension P521 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) let parsed = try ASN1.SubjectPublicKeyInfo(asn1Encoded: bytes) @@ -1302,19 +1012,9 @@ extension P521 { public var x963Representation: Data { impl.x963Representation } /// A compressed representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public var compressedRepresentation: Data { impl.compressedRepresentation } /// A Distinguished Encoding Rules (DER) encoded representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let spki = ASN1.SubjectPublicKeyInfo(algorithmIdentifier: .ecdsaP521, key: Array(self.x963Representation)) var serializer = ASN1.Serializer() @@ -1326,11 +1026,6 @@ extension P521 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -1373,26 +1068,12 @@ extension P521 { impl = try NISTCurvePrivateKeyImpl(data: rawRepresentation) } - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal init(seed: Data, compactRepresentable: Bool) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(seed: seed, compactRepresentable: compactRepresentable) - } - #if !hasFeature(Embedded) /// Creates a P-521 private key for signing from a Privacy-Enhanced Mail /// PEM) representation. /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) @@ -1414,11 +1095,6 @@ extension P521 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) @@ -1450,11 +1126,6 @@ extension P521 { public var x963Representation: Data { impl.x963Representation } /// A Distinguished Encoding Rules (DER) encoded representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let pkey = ASN1.PKCS8PrivateKey(algorithm: .ecdsaP521, privateKey: Array(self.rawRepresentation), publicKey: Array(self.publicKey.x963Representation)) var serializer = ASN1.Serializer() @@ -1466,11 +1137,6 @@ extension P521 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -1480,16 +1146,12 @@ extension P521 { } } // MARK: - P521 + KeyAgreement -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P521 { /// A mechanism used to create a shared secret between two users by /// performing NIST P-521 elliptic curve Diffie Hellman (ECDH) key /// exchange. + @nonexhaustive public enum KeyAgreement: Sendable { /// A P-521 public key used for key agreement. @@ -1530,11 +1192,6 @@ extension P521 { /// - Parameters: /// - compressedRepresentation: A compressed representation of the key as a collection /// of contiguous bytes. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public init(compressedRepresentation: Bytes) throws(CryptoKitMetaError) { impl = try NISTCurvePublicKeyImpl(compressedRepresentation: compressedRepresentation) } @@ -1545,11 +1202,6 @@ extension P521 { /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) guard pem.type == "PUBLIC KEY" else { @@ -1564,11 +1216,6 @@ extension P521 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) let parsed = try ASN1.SubjectPublicKeyInfo(asn1Encoded: bytes) @@ -1589,19 +1236,9 @@ extension P521 { public var x963Representation: Data { impl.x963Representation } /// A compressed representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif public var compressedRepresentation: Data { impl.compressedRepresentation } /// A Distinguished Encoding Rules (DER) encoded representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let spki = ASN1.SubjectPublicKeyInfo(algorithmIdentifier: .ecdsaP521, key: Array(self.x963Representation)) var serializer = ASN1.Serializer() @@ -1613,11 +1250,6 @@ extension P521 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -1660,26 +1292,12 @@ extension P521 { impl = try NISTCurvePrivateKeyImpl(data: rawRepresentation) } - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal init(seed: Data, compactRepresentable: Bool) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(seed: seed, compactRepresentable: compactRepresentable) - } - #if !hasFeature(Embedded) /// Creates a P-521 private key for key agreement from a Privacy-Enhanced Mail /// PEM) representation. /// /// - Parameters: /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(pemRepresentation: String) throws(CryptoKitMetaError) { let pem = try ASN1.PEMDocument(pemString: pemRepresentation) @@ -1701,11 +1319,6 @@ extension P521 { /// /// - Parameters: /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { let bytes = Array(derRepresentation) @@ -1737,11 +1350,6 @@ extension P521 { public var x963Representation: Data { impl.x963Representation } /// A Distinguished Encoding Rules (DER) encoded representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var derRepresentation: Data { let pkey = ASN1.PKCS8PrivateKey(algorithm: .ecdsaP521, privateKey: Array(self.rawRepresentation), publicKey: Array(self.publicKey.x963Representation)) var serializer = ASN1.Serializer() @@ -1753,11 +1361,6 @@ extension P521 { #if !hasFeature(Embedded) /// A Privacy-Enhanced Mail (PEM) representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public var pemRepresentation: String { let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) return pemDocument.pemString @@ -1768,11 +1371,6 @@ extension P521 { } // MARK: - P256 + DH -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P256.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement { /// Computes a shared secret with the provided public key from another party. /// @@ -1781,19 +1379,10 @@ extension P256.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement { /// key from this user to create the shared secret. /// - Returns: The computed shared secret. public func sharedSecretFromKeyAgreement(with publicKeyShare: P256.KeyAgreement.PublicKey) throws(CryptoKitMetaError) -> SharedSecret { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare) - #else return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare) - #endif } } // MARK: - P384 + DH -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P384.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement { /// Computes a shared secret with the provided public key from another party. /// @@ -1802,19 +1391,10 @@ extension P384.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement { /// key from this user to create the shared secret. /// - Returns: The computed shared secret. public func sharedSecretFromKeyAgreement(with publicKeyShare: P384.KeyAgreement.PublicKey) throws(CryptoKitMetaError) -> SharedSecret { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare) - #else return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare) - #endif } } // MARK: - P521 + DH -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension P521.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement { /// Computes a shared secret with the provided public key from another party. /// @@ -1823,11 +1403,7 @@ extension P521.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement { /// key from this user to create the shared secret. /// - Returns: The computed shared secret. public func sharedSecretFromKeyAgreement(with publicKeyShare: P521.KeyAgreement.PublicKey) throws(CryptoKitMetaError) -> SharedSecret { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare) - #else return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare) - #endif } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Key Agreement/ECDH.swift.gyb b/Sources/Crypto/Key Agreement/ECDH.swift.gyb deleted file mode 100644 index 8c135ecb..00000000 --- a/Sources/Crypto/Key Agreement/ECDH.swift.gyb +++ /dev/null @@ -1,385 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftCrypto open source project -// -// Copyright (c) 2019-2020 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 -// -//===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@_exported import CryptoKit -#else -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias NISTCurvePublicKeyImpl = CoreCryptoNISTCurvePublicKeyImpl -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -typealias NISTCurvePrivateKeyImpl = CoreCryptoNISTCurvePrivateKeyImpl -#else -typealias NISTCurvePublicKeyImpl = OpenSSLNISTCurvePublicKeyImpl -typealias NISTCurvePrivateKeyImpl = OpenSSLNISTCurvePrivateKeyImpl -#endif - -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else -#if canImport(FoundationEssentials) -public import FoundationEssentials -#else -public import Foundation -#endif -#endif - -// MARK: - Generated file, do NOT edit -// any edits of this file WILL be overwritten and thus discarded -// see section `gyb` in `README` for details. - -%{ - NIST_CURVES = ["P256", "P384", "P521"] - CURVES_FUNC = ["Signing", "KeyAgreement"] -}% -% for CURVE in NIST_CURVES: -%{ DISPLAY_CURVE = CURVE[:1] + "-" + CURVE[1:] }% -% for FUNC in CURVES_FUNC: -// MARK: - ${CURVE} + ${FUNC} -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -extension ${CURVE} { - - % if FUNC == "Signing": - /// A mechanism used to create or verify a cryptographic signature using - /// the NIST ${DISPLAY_CURVE} elliptic curve digital signature algorithm (ECDSA). - % else: - /// A mechanism used to create a shared secret between two users by - /// performing NIST ${DISPLAY_CURVE} elliptic curve Diffie Hellman (ECDH) key - /// exchange. - % end - public enum ${FUNC}: Sendable { - - % if FUNC == "Signing": - %{ READABLE_FUNC = "signing" }% - /// A ${DISPLAY_CURVE} public key used to verify cryptographic signatures. - % else: - %{ READABLE_FUNC = "key agreement" }% - /// A ${DISPLAY_CURVE} public key used for key agreement. - % end - public struct PublicKey: NISTECPublicKey, Sendable { - var impl: NISTCurvePublicKeyImpl<${CURVE}> - - /// Creates a ${DISPLAY_CURVE} public key for ${READABLE_FUNC} from a collection of bytes. - /// - /// - Parameters: - /// - rawRepresentation: A raw representation of the key as a collection of - /// contiguous bytes. - public init(rawRepresentation: D) throws(CryptoKitMetaError) { - impl = try NISTCurvePublicKeyImpl(rawRepresentation: rawRepresentation) - } - - /// Creates a ${DISPLAY_CURVE} public key for ${READABLE_FUNC} from a compact - /// representation of the key. - /// - /// - Parameters: - /// - compactRepresentation: A compact representation of the key - /// as a collection of contiguous bytes. - public init(compactRepresentation: Bytes) throws(CryptoKitMetaError) { - impl = try NISTCurvePublicKeyImpl(compactRepresentation: compactRepresentation) - } - - /// Creates a ${DISPLAY_CURVE} public key for ${READABLE_FUNC} from an ANSI x9.63 - /// representation. - /// - /// - Parameters: - /// - x963Representation: An ANSI x9.63 representation of the key. - public init(x963Representation: Bytes) throws(CryptoKitMetaError) { - impl = try NISTCurvePublicKeyImpl(x963Representation: x963Representation) - } - - /// Creates a ${DISPLAY_CURVE} public key for ${READABLE_FUNC} from a compressed representation of - /// the key. - /// - /// - Parameters: - /// - compressedRepresentation: A compressed representation of the key as a collection - /// of contiguous bytes. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - public init(compressedRepresentation: Bytes) throws(CryptoKitMetaError) { - impl = try NISTCurvePublicKeyImpl(compressedRepresentation: compressedRepresentation) - } - -#if !hasFeature(Embedded) - /// Creates a ${DISPLAY_CURVE} public key for ${READABLE_FUNC} from a Privacy-Enhanced Mail - /// (PEM) representation. - /// - /// - Parameters: - /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public init(pemRepresentation: String) throws(CryptoKitMetaError) { - let pem = try ASN1.PEMDocument(pemString: pemRepresentation) - guard pem.type == "PUBLIC KEY" else { - throw CryptoKitASN1Error.invalidPEMDocument - } - self = try .init(derRepresentation: pem.derBytes) - } -#endif - - /// Creates a ${DISPLAY_CURVE} public key for ${READABLE_FUNC} from a Distinguished Encoding - /// Rules (DER) encoded representation. - /// - /// - Parameters: - /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { - let bytes = Array(derRepresentation) - let parsed = try ASN1.SubjectPublicKeyInfo(asn1Encoded: bytes) - self = try .init(x963Representation: parsed.key) - } - - init(impl: NISTCurvePublicKeyImpl<${CURVE}>) { - self.impl = impl - } - - /// A compact representation of the public key. - public var compactRepresentation: Data? { impl.compactRepresentation } - - /// A full representation of the public key. - public var rawRepresentation: Data { impl.rawRepresentation } - - /// An ANSI x9.63 representation of the public key. - public var x963Representation: Data { impl.x963Representation } - - /// A compressed representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 9.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - public var compressedRepresentation: Data { impl.compressedRepresentation } - - /// A Distinguished Encoding Rules (DER) encoded representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public var derRepresentation: Data { - let spki = ASN1.SubjectPublicKeyInfo(algorithmIdentifier: .ecdsa${CURVE}, key: Array(self.x963Representation)) - var serializer = ASN1.Serializer() - - // Serializing these keys can't throw - try! serializer.serialize(spki) - return Data(serializer.serializedBytes) - } - -#if !hasFeature(Embedded) - /// A Privacy-Enhanced Mail (PEM) representation of the public key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public var pemRepresentation: String { - let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) - return pemDocument.pemString - } -#endif - } - - % if FUNC == "Signing": - /// A ${DISPLAY_CURVE} private key used to create cryptographic signatures. - % else: - /// A ${DISPLAY_CURVE} private key used for key agreement. - % end - public struct PrivateKey: NISTECPrivateKey, Sendable { - let impl: NISTCurvePrivateKeyImpl<${CURVE}> - - /// Creates a random ${DISPLAY_CURVE} private key for ${READABLE_FUNC}. - /// - /// Keys that use a compact point encoding enable shorter public keys, but aren’t - /// compliant with FIPS certification. If your app requires FIPS certification, - /// create a key with ``init(rawRepresentation:)``. - /// - /// - Parameters: - /// - compactRepresentable: A Boolean value that indicates whether CryptoKit - /// creates the key with the structure to enable compact point encoding. - public init(compactRepresentable: Bool = true) { - impl = NISTCurvePrivateKeyImpl(compactRepresentable: compactRepresentable) - } - - /// Creates a ${DISPLAY_CURVE} private key for ${READABLE_FUNC} from an ANSI x9.63 - /// representation. - /// - /// - Parameters: - /// - x963Representation: An ANSI x9.63 representation of the key. - public init(x963Representation: Bytes) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(x963: x963Representation) - } - - /// Creates a ${DISPLAY_CURVE} private key for ${READABLE_FUNC} from a collection of bytes. - /// - /// - Parameters: - /// - rawRepresentation: A raw representation of the key as a collection of - /// contiguous bytes. - public init(rawRepresentation: Bytes) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(data: rawRepresentation) - } - - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 13.0, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 16.0, macOS 10.13, watchOS 8.0, tvOS 16.0, macCatalyst 16.0, visionOS 1.0, *) - #endif - internal init(seed: Data, compactRepresentable: Bool) throws(CryptoKitMetaError) { - impl = try NISTCurvePrivateKeyImpl(seed: seed, compactRepresentable: compactRepresentable) - } - -#if !hasFeature(Embedded) - /// Creates a ${DISPLAY_CURVE} private key for ${READABLE_FUNC} from a Privacy-Enhanced Mail - /// PEM) representation. - /// - /// - Parameters: - /// - pemRepresentation: A PEM representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public init(pemRepresentation: String) throws(CryptoKitMetaError) { - let pem = try ASN1.PEMDocument(pemString: pemRepresentation) - - switch pem.type { - case "EC PRIVATE KEY": - let parsed = try ASN1.SEC1PrivateKey(asn1Encoded: Array(pem.derBytes)) - self = try .init(rawRepresentation: parsed.privateKey) - case "PRIVATE KEY": - let parsed = try ASN1.PKCS8PrivateKey(asn1Encoded: Array(pem.derBytes)) - self = try .init(rawRepresentation: parsed.privateKey.privateKey) - default: - throw CryptoKitASN1Error.invalidPEMDocument - } - } -#endif - - /// Creates a ${DISPLAY_CURVE} private key for ${READABLE_FUNC} from a Distinguished Encoding - /// Rules (DER) encoded representation. - /// - /// - Parameters: - /// - derRepresentation: A DER-encoded representation of the key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public init(derRepresentation: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 { - let bytes = Array(derRepresentation) - - // We have to try to parse this twice because we have no information about what kind of key this is. - // We try with PKCS#8 first, and then fall back to SEC.1. - - do { - let key = try ASN1.PKCS8PrivateKey(asn1Encoded: bytes) - self = try .init(rawRepresentation: key.privateKey.privateKey) - } catch { - let key = try ASN1.SEC1PrivateKey(asn1Encoded: bytes) - self = try .init(rawRepresentation: key.privateKey) - } - } - - init(impl: NISTCurvePrivateKeyImpl<${CURVE}>) { - self.impl = impl - } - - /// The corresponding public key. - public var publicKey: ${CURVE}.${FUNC}.PublicKey { - return PublicKey(impl: impl.publicKey()) - } - - /// A data representation of the private key. - public var rawRepresentation: Data { impl.rawRepresentation } - - /// An ANSI x9.63 representation of the private key. - public var x963Representation: Data { impl.x963Representation } - - /// A Distinguished Encoding Rules (DER) encoded representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public var derRepresentation: Data { - let pkey = ASN1.PKCS8PrivateKey(algorithm: .ecdsa${CURVE}, privateKey: Array(self.rawRepresentation), publicKey: Array(self.publicKey.x963Representation)) - var serializer = ASN1.Serializer() - - // Serializing these keys can't throw - try! serializer.serialize(pkey) - return Data(serializer.serializedBytes) - } - -#if !hasFeature(Embedded) - /// A Privacy-Enhanced Mail (PEM) representation of the private key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif - public var pemRepresentation: String { - let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) - return pemDocument.pemString - } -#endif - } - } -} -% end -% end - -% for CURVE in NIST_CURVES: -// MARK: - ${CURVE} + DH -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif -extension ${CURVE}.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement { - /// Computes a shared secret with the provided public key from another party. - /// - /// - Parameters: - /// - publicKeyShare: The public key from another party to be combined with the private - /// key from this user to create the shared secret. - /// - Returns: The computed shared secret. - public func sharedSecretFromKeyAgreement(with publicKeyShare: ${CURVE}.KeyAgreement.PublicKey) throws(CryptoKitMetaError) -> SharedSecret { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare) - #else - return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare) - #endif - } -} -% end -#endif // Linux or !SwiftPM diff --git a/Sources/Crypto/Key Derivation/ANSIx963.swift b/Sources/Crypto/Key Derivation/ANSIx963.swift index 9c711172..84f2e68b 100644 --- a/Sources/Crypto/Key Derivation/ANSIx963.swift +++ b/Sources/Crypto/Key Derivation/ANSIx963.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif internal struct ANSIKDFx963: Sendable { public static func deriveKey(inputKeyMaterial: SymmetricKey, info: Info, outputByteCount: Int) -> SymmetricKey { diff --git a/Sources/Crypto/Key Derivation/HKDF.swift b/Sources/Crypto/Key Derivation/HKDF.swift index de7ac8ac..5b09d42d 100644 --- a/Sources/Crypto/Key Derivation/HKDF.swift +++ b/Sources/Crypto/Key Derivation/HKDF.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// A standards-based implementation of an HMAC-based Key Derivation Function /// (HKDF). @@ -41,11 +37,6 @@ public import Foundation /// material in the form of a hashed authentication code, then call /// ``expand(pseudoRandomKey:info:outputByteCount:)`` using that key material to /// generate a symmetric key of the length you specify. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif public struct HKDF: Sendable { /// Derives a symmetric encryption key from a main key or passcode using /// HKDF key derivation with information and salt you specify. @@ -57,11 +48,6 @@ public struct HKDF: Sendable { /// - info: The shared information to use for key derivation. /// - outputKey: An output span that will be populated with the derived /// symmetric key. -#if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif #if swift(<6.3) @_lifetime(outputKey: copy outputKey) #endif @@ -90,11 +76,6 @@ public struct HKDF: Sendable { /// - outputByteCount: The length in bytes of the resulting symmetric key. /// /// - Returns: The derived symmetric key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public static func deriveKey( inputKeyMaterial: SymmetricKey, salt: Salt, @@ -139,11 +120,6 @@ public struct HKDF: Sendable { /// - outputByteCount: The length in bytes of the resulting symmetric key. /// /// - Returns: The derived symmetric key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public static func deriveKey(inputKeyMaterial: SymmetricKey, info: Info, outputByteCount: Int) -> SymmetricKey { @@ -160,11 +136,6 @@ public struct HKDF: Sendable { /// - outputByteCount: The length in bytes of the resulting symmetric key. /// /// - Returns: The derived symmetric key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public static func deriveKey(inputKeyMaterial: SymmetricKey, salt: Salt, outputByteCount: Int) -> SymmetricKey { @@ -180,11 +151,6 @@ public struct HKDF: Sendable { /// - outputByteCount: The length in bytes of the resulting symmetric key. /// /// - Returns: The derived symmetric key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public static func deriveKey(inputKeyMaterial: SymmetricKey, outputByteCount: Int) -> SymmetricKey { return deriveKey(inputKeyMaterial: inputKeyMaterial, salt: [UInt8](), info: [UInt8](), outputByteCount: outputByteCount) @@ -204,11 +170,6 @@ public struct HKDF: Sendable { /// /// - Returns: A pseudorandom, cryptographically strong key in the form of a /// hashed authentication code. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public static func extract(inputKeyMaterial: SymmetricKey, salt: Salt?) -> HashedAuthenticationCode { let key: SymmetricKey if let salt { @@ -239,11 +200,6 @@ public struct HKDF: Sendable { /// /// - Returns: A pseudorandom, cryptographically strong key in the form of a /// hashed authentication code. -#if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif public static func extract(inputKeyMaterial: SymmetricKey, salt: RawSpan?) -> HashedAuthenticationCode { let key = if let salt { SymmetricKey(bytes: salt) @@ -267,11 +223,6 @@ public struct HKDF: Sendable { /// - outputByteCount: The length in bytes of the resulting symmetric key. /// /// - Returns: The derived symmetric key. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) - #endif public static func expand(pseudoRandomKey prk: PRK, info: Info?, outputByteCount: Int) -> SymmetricKey { SymmetricKey(capacity: outputByteCount) { output in prk.withUnsafeBytes { prkBuffer in @@ -307,11 +258,6 @@ public struct HKDF: Sendable { /// - outputByteCount: The length in bytes of the resulting symmetric key. /// /// - Returns: The derived symmetric key. -#if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif #if swift(<6.3) @_lifetime(output: copy output) #endif @@ -327,7 +273,6 @@ public struct HKDF: Sendable { hmac.update(bytes: info) } - // hmac.update(bytes: CollectionOfOne(i).span.bytes) var buf = i withUnsafeBytes(of: &buf) { rawBuffer in let span = RawSpan(_unsafeBytes: rawBuffer) @@ -341,4 +286,4 @@ public struct HKDF: Sendable { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Key Wrapping/AESWrap.swift b/Sources/Crypto/Key Wrapping/AESWrap.swift index 3117d92d..da99412a 100644 --- a/Sources/Crypto/Key Wrapping/AESWrap.swift +++ b/Sources/Crypto/Key Wrapping/AESWrap.swift @@ -11,32 +11,23 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -@available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *) -typealias AESWRAPImpl = CoreCryptoAESWRAPImpl -#else typealias AESWRAPImpl = BoringSSLAESWRAPImpl -#endif -@available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *) extension AES { /// An implementation of AES Key Wrapping in accordance with the IETF RFC /// 3394 specification. - @available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *) + @nonexhaustive public enum KeyWrap: Sendable { /// Wraps a key using the AES wrap algorithm. /// @@ -48,7 +39,6 @@ extension AES { /// - kek: The key encryption key. /// /// - Returns: The wrapped key. - @available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *) public static func wrap(_ keyToWrap: SymmetricKey, using kek: SymmetricKey) throws -> Data { return try AESWRAPImpl.wrap(key: kek, keyToWrap: keyToWrap) } @@ -64,11 +54,10 @@ extension AES { /// - kek: The key encryption key. /// /// - Returns: The unwrapped key. - @available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *) public static func unwrap(_ wrappedKey: WrappedKey, using kek: SymmetricKey) throws -> SymmetricKey { return try AESWRAPImpl.unwrap(key: kek, wrappedKey: wrappedKey) } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift b/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift index f493b865..5f41c5c0 100644 --- a/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift +++ b/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -146,4 +146,4 @@ extension SymmetricKey { } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift index 037de26f..c7d0b19a 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -123,4 +123,4 @@ extension Curve25519.Signing { } } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift index 955a820a..ff7d2e41 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -701,4 +701,4 @@ func _isCompactRepresentable( // The point is compact representable if y is less than or equal to newY. return y <= newY } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift index df7807cd..1c0c4b55 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -162,4 +162,4 @@ extension Curve25519.KeyAgreement { } } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/EC/Curve25519.swift b/Sources/Crypto/Keys/EC/Curve25519.swift index ae0e365c..df79153e 100644 --- a/Sources/Crypto/Keys/EC/Curve25519.swift +++ b/Sources/Crypto/Keys/EC/Curve25519.swift @@ -11,14 +11,11 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else /// An elliptic curve that enables X25519 key agreement and Ed25519 signatures. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum Curve25519: Sendable {} -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/EC/Ed25519Keys.swift b/Sources/Crypto/Keys/EC/Ed25519Keys.swift index 51e3b94d..ad1781b5 100644 --- a/Sources/Crypto/Keys/EC/Ed25519Keys.swift +++ b/Sources/Crypto/Keys/EC/Ed25519Keys.swift @@ -11,19 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif extension Curve25519.Signing { static var keyByteCount: Int { @@ -31,22 +27,13 @@ extension Curve25519.Signing { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Curve25519 { /// A mechanism used to create or verify a cryptographic signature using /// Ed25519. + @nonexhaustive public enum Signing: Sendable { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - typealias Curve25519PrivateKeyImpl = Curve25519.Signing.CoreCryptoCurve25519PrivateKeyImpl - typealias Curve25519PublicKeyImpl = Curve25519.Signing.CoreCryptoCurve25519PublicKeyImpl - #else typealias Curve25519PrivateKeyImpl = Curve25519.Signing.OpenSSLCurve25519PrivateKeyImpl typealias Curve25519PublicKeyImpl = Curve25519.Signing.OpenSSLCurve25519PublicKeyImpl - #endif /// A Curve25519 private key used to create cryptographic signatures. public struct PrivateKey: ECPrivateKey, Sendable { @@ -111,4 +98,4 @@ extension Curve25519 { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/EC/NISTCurvesKeys.swift b/Sources/Crypto/Keys/EC/NISTCurvesKeys.swift index 254274e1..6a4bdcc0 100644 --- a/Sources/Crypto/Keys/EC/NISTCurvesKeys.swift +++ b/Sources/Crypto/Keys/EC/NISTCurvesKeys.swift @@ -11,25 +11,17 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else + #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION -typealias SupportedCurveDetailsImpl = CorecryptoSupportedNISTCurve -#else typealias SupportedCurveDetailsImpl = OpenSSLSupportedNISTCurve -#endif protocol ECPublicKey { init (rawRepresentation: Bytes) throws(CryptoKitMetaError) @@ -56,26 +48,14 @@ protocol NISTECPrivateKey: ECPrivateKey where PK: NISTECPublicKey { } /// An elliptic curve that enables NIST P-256 signatures and key agreement. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum P256: Sendable { } /// An elliptic curve that enables NIST P-384 signatures and key agreement. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum P384: Sendable { } /// An elliptic curve that enables NIST P-521 signatures and key agreement. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum P521: Sendable { } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/EC/X25519Keys.swift b/Sources/Crypto/Keys/EC/X25519Keys.swift index 23e9fca4..c6eca27c 100644 --- a/Sources/Crypto/Keys/EC/X25519Keys.swift +++ b/Sources/Crypto/Keys/EC/X25519Keys.swift @@ -11,19 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif extension Curve25519.KeyAgreement { static var keyByteCount: Int { @@ -31,29 +27,15 @@ extension Curve25519.KeyAgreement { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Curve25519 { /// A mechanism used to create a shared secret between two users by /// performing X25519 key agreement. + @nonexhaustive public enum KeyAgreement: Sendable { -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - typealias Curve25519PrivateKeyImpl = Curve25519.KeyAgreement.CoreCryptoCurve25519PrivateKeyImpl - typealias Curve25519PublicKeyImpl = Curve25519.KeyAgreement.CoreCryptoCurve25519PublicKeyImpl - #else typealias Curve25519PrivateKeyImpl = Curve25519.KeyAgreement.OpenSSLCurve25519PrivateKeyImpl typealias Curve25519PublicKeyImpl = Curve25519.KeyAgreement.OpenSSLCurve25519PublicKeyImpl - #endif /// A Curve25519 public key used for key agreement. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif public struct PublicKey: ECPublicKey, Sendable { fileprivate var baseKey: Curve25519PublicKeyImpl @@ -93,11 +75,6 @@ extension Curve25519 { } /// A Curve25519 private key used for key agreement. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif public struct PrivateKey: DiffieHellmanKeyAgreement, Sendable { fileprivate var baseKey: Curve25519PrivateKeyImpl @@ -146,4 +123,4 @@ extension Curve25519 { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Keys/Symmetric/SymmetricKeys.swift b/Sources/Crypto/Keys/Symmetric/SymmetricKeys.swift index e06719ed..94d3fe0c 100644 --- a/Sources/Crypto/Keys/Symmetric/SymmetricKeys.swift +++ b/Sources/Crypto/Keys/Symmetric/SymmetricKeys.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif /// The sizes that a symmetric cryptographic key can take. /// @@ -33,11 +29,6 @@ public import Foundation /// standard key sizes, like ``bits128``, ``bits192``, or ``bits256``. When you /// need a key with a non-standard length, use the ``init(bitCount:)`` /// initializer to create a `SymmetricKeySize` instance with a custom bit count. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SymmetricKeySize: Sendable { /// The number of bits in the key. public let bitCount: Int @@ -78,11 +69,6 @@ public struct SymmetricKeySize: Sendable { /// symmetric key to compute a message authentication code like ``HMAC``, or to /// open and close a sealed box (``ChaChaPoly/SealedBox`` or /// ``AES/GCM/SealedBox``) using a cipher like ``ChaChaPoly`` or ``AES``. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct SymmetricKey: ContiguousBytes, Sendable { let sb: SecureBytes @@ -105,11 +91,6 @@ public struct SymmetricKey: ContiguousBytes, Sendable { #endif /// Access the raw bytes of the key. -#if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif public var bytes: RawSpan { sb.bytes } @@ -146,7 +127,6 @@ public struct SymmetricKey: ContiguousBytes, Sendable { /// /// - Parameters: /// - byte: The span of bytes from which to create the key. - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) public init(copyingWithZeroing bytes: inout MutableRawSpan) { self = bytes.withUnsafeBytes { SymmetricKey(data: $0) } bytes.withUnsafeMutableBytes { $0.zeroize() } @@ -188,11 +168,6 @@ public struct SymmetricKey: ContiguousBytes, Sendable { /// - callback: A callback that will be invoked to initialize the contents /// of the key. It must initialize the full set of size.bitCount / 8 bytes /// in the provided output span. -#if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public init(size: SymmetricKeySize, initializingWith callback: (inout OutputRawSpan) throws(E) -> Void) throws(E) { try self.init(capacity: Int(size.bitCount / 8), initializingWith: callback) } @@ -218,15 +193,10 @@ public struct SymmetricKey: ContiguousBytes, Sendable { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SymmetricKey: Equatable { public static func == (lhs: Self, rhs: Self) -> Bool { return safeCompare(lhs, rhs) } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Message Authentication Codes/HMAC/HMAC.swift b/Sources/Crypto/Message Authentication Codes/HMAC/HMAC.swift index dbffba18..27acd40a 100644 --- a/Sources/Crypto/Message Authentication Codes/HMAC/HMAC.swift +++ b/Sources/Crypto/Message Authentication Codes/HMAC/HMAC.swift @@ -11,24 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif -#else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION #else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif - /// A hash-based message authentication algorithm. /// @@ -43,11 +34,6 @@ public import Foundation /// need to encrypt the data as well as authenticate it, use a cipher like /// ``AES`` or ``ChaChaPoly`` to put the data into a sealed box (an instance of /// ``AES/GCM/SealedBox`` or ``ChaChaPoly/SealedBox``). -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else //CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct HMAC: MACAlgorithm, Sendable { /// An alias for the symmetric key type used to compute or verify a message /// authentication code. @@ -130,11 +116,6 @@ public struct HMAC: MACAlgorithm, Sendable { /// - key: The symmetric key used to secure the computation. /// /// - Returns: The message authentication code. -#if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif public static func authenticationCode(for data: RawSpan, using key: SymmetricKey) -> MAC { var authenticator = Self(key: key) authenticator.update(bytes: data) @@ -166,11 +147,6 @@ public struct HMAC: MACAlgorithm, Sendable { /// /// - Returns: A Boolean value that’s `true` if the message authentication /// code is valid for the specified block of data. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.2, macOS 10.13, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, visionOS 1.0, *) - #endif public static func isValidAuthenticationCode(_ authenticationCode: C, authenticating authenticatedData: D, using key: SymmetricKey) -> Bool { @@ -190,11 +166,6 @@ public struct HMAC: MACAlgorithm, Sendable { } } -#if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 14.0, macOS 10.13, watchOS 7.0, tvOS 14.0, macCatalyst 14.0, visionOS 1.0, *) -#endif public mutating func update(bytes: RawSpan) { innerHasher.update(bytes: bytes) } @@ -245,11 +216,6 @@ public struct HMAC: MACAlgorithm, Sendable { } /// A hash-based message authentication code. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif public struct HashedAuthenticationCode: MessageAuthenticationCode, Sendable { let digest: H.Digest @@ -283,4 +249,4 @@ public struct HashedAuthenticationCode: MessageAuthenticationCo } #endif } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Message Authentication Codes/MACFunctions.swift b/Sources/Crypto/Message Authentication Codes/MACFunctions.swift index 59cb6786..66ea3f6b 100644 --- a/Sources/Crypto/Message Authentication Codes/MACFunctions.swift +++ b/Sources/Crypto/Message Authentication Codes/MACFunctions.swift @@ -11,36 +11,19 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif protocol MACAlgorithm { associatedtype Key - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - #if CRYPTOKIT_STATIC_LIBRARY - associatedtype MAC: CryptoKit_Static.MessageAuthenticationCode - #else - associatedtype MAC: CryptoKit.MessageAuthenticationCode - #endif - #else associatedtype MAC: Crypto.MessageAuthenticationCode - #endif /// Initializes the MAC Algorithm /// @@ -58,11 +41,6 @@ protocol MACAlgorithm { func finalize() -> MAC } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension MACAlgorithm { /// Computes a Message Authentication Code. /// @@ -89,4 +67,4 @@ extension MACAlgorithm { return mac == Self.authenticationCode(bufferPointer: bufferPointer, using: key) } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Message Authentication Codes/MessageAuthenticationCode.swift b/Sources/Crypto/Message Authentication Codes/MessageAuthenticationCode.swift index afec75fc..a69baf20 100644 --- a/Sources/Crypto/Message Authentication Codes/MessageAuthenticationCode.swift +++ b/Sources/Crypto/Message Authentication Codes/MessageAuthenticationCode.swift @@ -11,39 +11,25 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif -#else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION #else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif #if hasFeature(Embedded) /// A type that represents a message authentication code. @preconcurrency -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) public protocol MessageAuthenticationCode: Hashable, ContiguousBytes, Sendable, Sequence where Element == UInt8 { /// The number of bytes in the message authentication code. var byteCount: Int { get } } #else /// A type that represents a message authentication code. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif @preconcurrency public protocol MessageAuthenticationCode: Hashable, ContiguousBytes, Sendable, CustomStringConvertible, Sequence where Element == UInt8 { /// The number of bytes in the message authentication code. @@ -51,11 +37,6 @@ public protocol MessageAuthenticationCode: Hashable, ContiguousBytes, Sendable, } #endif -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension MessageAuthenticationCode { /// Returns a Boolean value indicating whether two message authentication /// codes are equal. @@ -100,4 +81,4 @@ extension MessageAuthenticationCode { } #endif } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/PRF/AES.swift b/Sources/Crypto/PRF/AES.swift index 4c988efe..623ba98d 100644 --- a/Sources/Crypto/PRF/AES.swift +++ b/Sources/Crypto/PRF/AES.swift @@ -11,24 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else /// A container for Advanced Encryption Standard (AES) ciphers. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif +@nonexhaustive public enum AES: Sendable { static let blockSizeByteCount = 16 } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension AES { static func isValidKey(_ key: SymmetricKey) -> Bool { switch key.bitCount { @@ -43,4 +35,4 @@ extension AES { } } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift b/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift index d33110d3..d5e0ebdc 100644 --- a/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -119,4 +119,4 @@ class ECDSASignature { try body(self._baseSig) } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift b/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift index 0842eada..7a3c2731 100644 --- a/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -173,4 +173,4 @@ extension P521.Signing.PublicKey { return self.impl.key.isValidSignature(baseSignature, for: digest) } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift b/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift index 9c3e39c0..7ae85eee 100644 --- a/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -144,4 +144,4 @@ extension Curve25519.Signing.PrivateKey { return signature } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift b/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift index 05150b49..c4da5198 100644 --- a/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -642,4 +642,4 @@ enum MLDSA { static let seedByteCount = 32 } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift.gyb b/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift.gyb index 27aaa239..f8292c3e 100644 --- a/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift.gyb +++ b/Sources/Crypto/Signatures/BoringSSL/MLDSA_boring.swift.gyb @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -345,4 +345,4 @@ enum MLDSA { static let seedByteCount = 32 } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/BoringSSL/MLDSA_wrapper.swift b/Sources/Crypto/Signatures/BoringSSL/MLDSA_wrapper.swift index a02db507..de29a6cc 100644 --- a/Sources/Crypto/Signatures/BoringSSL/MLDSA_wrapper.swift +++ b/Sources/Crypto/Signatures/BoringSSL/MLDSA_wrapper.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -171,4 +171,4 @@ struct OpenSSLMLDSAPublicKeyImpl { } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/ECDSA.swift b/Sources/Crypto/Signatures/ECDSA.swift index 7c9670ab..d27e9783 100644 --- a/Sources/Crypto/Signatures/ECDSA.swift +++ b/Sources/Crypto/Signatures/ECDSA.swift @@ -11,20 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif // MARK: - Generated file, do NOT edit // any edits of this file WILL be overwritten and thus discarded @@ -37,7 +33,6 @@ protocol NISTECDSASignature { var rawRepresentation: Data { get } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) protocol NISTSigning { associatedtype PublicKey: NISTECPublicKey & DataValidator & DigestValidator associatedtype PrivateKey: NISTECPrivateKey & Signer @@ -45,7 +40,6 @@ protocol NISTSigning { } // MARK: - P256 + Signing -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P256.Signing { /// A P-256 elliptic curve digital signature algorithm (ECDSA) signature. @@ -146,11 +140,9 @@ extension P256.Signing { } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P256.Signing: NISTSigning {} // MARK: - P256 + PrivateKey -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P256.Signing.PrivateKey: DigestSigner { /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) /// signature of the digest you provide over the P-256 elliptic curve. @@ -161,15 +153,10 @@ extension P256.Signing.PrivateKey: DigestSigner { /// algorithm employs randomization to generate a different signature on /// every call, even for the same data and key. public func signature(for digest: D) throws(CryptoKitMetaError) -> P256.Signing.ECDSASignature { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSignature(for: digest) - #else return try self.openSSLSignature(for: digest) - #endif } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P256.Signing.PrivateKey: Signer { /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) /// signature of the data you provide over the P-256 elliptic curve, @@ -185,7 +172,6 @@ extension P256.Signing.PrivateKey: Signer { } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P256.Signing.PublicKey: DigestValidator { /// Verifies an elliptic curve digital signature algorithm (ECDSA) /// signature on a digest over the P-256 elliptic curve. @@ -196,15 +182,10 @@ extension P256.Signing.PublicKey: DigestValidator { /// - Returns: A Boolean value that’s `true` if the signature is valid for /// the given digest; otherwise, `false`. public func isValidSignature(_ signature: P256.Signing.ECDSASignature, for digest: D) -> Bool { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return self.coreCryptoIsValidSignature(signature, for: digest) - #else return self.openSSLIsValidSignature(signature, for: digest) - #endif } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P256.Signing.PublicKey: DataValidator { /// Verifies an elliptic curve digital signature algorithm (ECDSA) /// signature on a block of data over the P-256 elliptic curve. @@ -220,7 +201,6 @@ extension P256.Signing.PublicKey: DataValidator { } // MARK: - P384 + Signing -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P384.Signing { /// A P-384 elliptic curve digital signature algorithm (ECDSA) signature. @@ -321,11 +301,9 @@ extension P384.Signing { } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P384.Signing: NISTSigning {} // MARK: - P384 + PrivateKey -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P384.Signing.PrivateKey: DigestSigner { /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) /// signature of the digest you provide over the P-384 elliptic curve. @@ -336,15 +314,10 @@ extension P384.Signing.PrivateKey: DigestSigner { /// algorithm employs randomization to generate a different signature on /// every call, even for the same data and key. public func signature(for digest: D) throws(CryptoKitMetaError) -> P384.Signing.ECDSASignature { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSignature(for: digest) - #else return try self.openSSLSignature(for: digest) - #endif } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P384.Signing.PrivateKey: Signer { /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) /// signature of the data you provide over the P-384 elliptic curve, @@ -360,7 +333,6 @@ extension P384.Signing.PrivateKey: Signer { } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P384.Signing.PublicKey: DigestValidator { /// Verifies an elliptic curve digital signature algorithm (ECDSA) /// signature on a digest over the P-384 elliptic curve. @@ -371,15 +343,10 @@ extension P384.Signing.PublicKey: DigestValidator { /// - Returns: A Boolean value that’s `true` if the signature is valid for /// the given digest; otherwise, `false`. public func isValidSignature(_ signature: P384.Signing.ECDSASignature, for digest: D) -> Bool { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return self.coreCryptoIsValidSignature(signature, for: digest) - #else return self.openSSLIsValidSignature(signature, for: digest) - #endif } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P384.Signing.PublicKey: DataValidator { /// Verifies an elliptic curve digital signature algorithm (ECDSA) /// signature on a block of data over the P-384 elliptic curve. @@ -395,7 +362,6 @@ extension P384.Signing.PublicKey: DataValidator { } // MARK: - P521 + Signing -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P521.Signing { /// A P-521 elliptic curve digital signature algorithm (ECDSA) signature. @@ -496,11 +462,9 @@ extension P521.Signing { } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P521.Signing: NISTSigning {} // MARK: - P521 + PrivateKey -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P521.Signing.PrivateKey: DigestSigner { /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) /// signature of the digest you provide over the P-521 elliptic curve. @@ -511,15 +475,10 @@ extension P521.Signing.PrivateKey: DigestSigner { /// algorithm employs randomization to generate a different signature on /// every call, even for the same data and key. public func signature(for digest: D) throws(CryptoKitMetaError) -> P521.Signing.ECDSASignature { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSignature(for: digest) - #else return try self.openSSLSignature(for: digest) - #endif } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P521.Signing.PrivateKey: Signer { /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) /// signature of the data you provide over the P-521 elliptic curve, @@ -535,7 +494,6 @@ extension P521.Signing.PrivateKey: Signer { } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P521.Signing.PublicKey: DigestValidator { /// Verifies an elliptic curve digital signature algorithm (ECDSA) /// signature on a digest over the P-521 elliptic curve. @@ -546,15 +504,10 @@ extension P521.Signing.PublicKey: DigestValidator { /// - Returns: A Boolean value that’s `true` if the signature is valid for /// the given digest; otherwise, `false`. public func isValidSignature(_ signature: P521.Signing.ECDSASignature, for digest: D) -> Bool { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return self.coreCryptoIsValidSignature(signature, for: digest) - #else return self.openSSLIsValidSignature(signature, for: digest) - #endif } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension P521.Signing.PublicKey: DataValidator { /// Verifies an elliptic curve digital signature algorithm (ECDSA) /// signature on a block of data over the P-521 elliptic curve. @@ -570,4 +523,4 @@ extension P521.Signing.PublicKey: DataValidator { } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/ECDSA.swift.gyb b/Sources/Crypto/Signatures/ECDSA.swift.gyb deleted file mode 100644 index 6e789e90..00000000 --- a/Sources/Crypto/Signatures/ECDSA.swift.gyb +++ /dev/null @@ -1,234 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftCrypto open source project -// -// Copyright (c) 2019-2020 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 -// -//===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@_exported import CryptoKit -#else - -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else -#if canImport(FoundationEssentials) -public import FoundationEssentials -#else -public import Foundation -#endif -#endif - -// MARK: - Generated file, do NOT edit -// any edits of this file WILL be overwritten and thus discarded -// see section `gyb` in `README` for details. -%{ - NIST_CURVES_AND_HF = [{"curve": "P256", "hf": "SHA256"},{"curve": "P384", "hf": "SHA384"},{"curve": "P521", "hf": "SHA512"}] -}% - -protocol NISTECDSASignature { - init(rawRepresentation: D) throws(CryptoKitMetaError) - init(derRepresentation: D) throws(CryptoKitMetaError) - var derRepresentation: Data { get } - var rawRepresentation: Data { get } -} - -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -protocol NISTSigning { - associatedtype PublicKey: NISTECPublicKey & DataValidator & DigestValidator - associatedtype PrivateKey: NISTECPrivateKey & Signer - associatedtype ECDSASignature: NISTECDSASignature -} - -% for CURVE_AND_HF in NIST_CURVES_AND_HF: -%{ - CURVE = CURVE_AND_HF["curve"] - HF = CURVE_AND_HF["hf"] - DISPLAY_CURVE = CURVE[:1] + "-" + CURVE[1:] - DISPLAY_HF = HF[:3] + "-" + HF[3:] -}% -// MARK: - ${CURVE} + Signing -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -extension ${CURVE}.Signing { - - /// A ${DISPLAY_CURVE} elliptic curve digital signature algorithm (ECDSA) signature. - public struct ECDSASignature: ContiguousBytes, NISTECDSASignature, Sendable { - - /// A raw data representation of a ${DISPLAY_CURVE} digital signature. - public var rawRepresentation: Data - - /// Creates a ${DISPLAY_CURVE} digital signature from a raw representation. - /// - /// - Parameters: - /// - rawRepresentation: A raw representation of the signature as a - /// collection of contiguous bytes. - public init(rawRepresentation: D) throws(CryptoKitMetaError) { - guard rawRepresentation.count == 2 * ${CURVE}.coordinateByteCount else { - throw error(CryptoKitError.incorrectParameterSize) - } - - self.rawRepresentation = Data(rawRepresentation) - } - - internal init(_ dataRepresentation: Data) throws(CryptoKitMetaError) { - guard dataRepresentation.count == 2 * ${CURVE}.coordinateByteCount else { - throw error(CryptoKitError.incorrectParameterSize) - } - - self.rawRepresentation = dataRepresentation - } - - var composite: (r: Data, s: Data) { - let combined = rawRepresentation - assert(combined.count % 2 == 0) - let half = combined.count / 2 - return (combined.prefix(half), combined.suffix(half)) - } - - /// Creates a ${DISPLAY_CURVE} digital signature from a Distinguished Encoding - /// Rules (DER) encoded representation. - /// - /// - Parameters: - /// - derRepresentation: The DER-encoded representation of the - /// signature. - public init(derRepresentation: D) throws(CryptoKitMetaError) { - #if os(iOS) && (arch(arm) || arch(i386)) - fatalError("Unsupported architecture") - #else - let parsed = try ASN1.parse(Array(derRepresentation)) - let signature = try ASN1.ECDSASignature>(asn1Encoded: parsed) - - let coordinateByteCount = ${CURVE}.coordinateByteCount - - guard signature.r.count <= coordinateByteCount && signature.s.count <= coordinateByteCount else { - throw error(CryptoKitError.incorrectParameterSize) - } - - // r and s must be padded out to the coordinate byte count. - var raw = Data() - raw.reserveCapacity(2 * ${CURVE}.coordinateByteCount) - - raw.append(contentsOf: repeatElement(0, count: ${CURVE}.coordinateByteCount - signature.r.count)) - raw.append(contentsOf: signature.r) - raw.append(contentsOf: repeatElement(0, count: ${CURVE}.coordinateByteCount - signature.s.count)) - raw.append(contentsOf: signature.s) - - self.rawRepresentation = raw - #endif - } - - /// Invokes the given closure with a buffer pointer covering the raw - /// bytes of the signature. -#if hasFeature(Embedded) - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R { - try self.rawRepresentation.withUnsafeBytes(body) - } -#else - public func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { - try self.rawRepresentation.withUnsafeBytes(body) - } -#endif - - /// A Distinguished Encoding Rules (DER) encoded representation of a - /// ${DISPLAY_CURVE} digital signature. - public var derRepresentation: Data { - #if os(iOS) && (arch(arm) || arch(i386)) - fatalError("Unsupported architecture") - #else - let raw = rawRepresentation - let half = raw.count / 2 - let r = Array(raw.prefix(half))[...] - let s = Array(raw.suffix(half))[...] - - let sig = ASN1.ECDSASignature(r: r, s: s) - var serializer = ASN1.Serializer() - try! serializer.serialize(sig) - return Data(serializer.serializedBytes) - #endif - } - } -} - -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -extension ${CURVE}.Signing: NISTSigning {} - -// MARK: - ${CURVE} + PrivateKey -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -extension ${CURVE}.Signing.PrivateKey: DigestSigner { - /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) - /// signature of the digest you provide over the ${DISPLAY_CURVE} elliptic curve. - /// - /// - Parameters: - /// - digest: The digest of the data to sign. - /// - Returns: The signature corresponding to the digest. The signing - /// algorithm employs randomization to generate a different signature on - /// every call, even for the same data and key. - public func signature(for digest: D) throws(CryptoKitMetaError) -> ${CURVE}.Signing.ECDSASignature { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSignature(for: digest) - #else - return try self.openSSLSignature(for: digest) - #endif - } - } - -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -extension ${CURVE}.Signing.PrivateKey: Signer { - /// Generates an Elliptic Curve Digital Signature Algorithm (ECDSA) - /// signature of the data you provide over the ${DISPLAY_CURVE} elliptic curve, - /// using ${DISPLAY_HF} as the hash function. - /// - /// - Parameters: - /// - data: The data to sign. - /// - Returns: The signature corresponding to the data. The signing - /// algorithm employs randomization to generate a different signature on - /// every call, even for the same data and key. - public func signature(for data: D) throws(CryptoKitMetaError) -> ${CURVE}.Signing.ECDSASignature { - return try self.signature(for: ${HF}.hash(data: data)) - } -} - -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -extension ${CURVE}.Signing.PublicKey: DigestValidator { - /// Verifies an elliptic curve digital signature algorithm (ECDSA) - /// signature on a digest over the ${DISPLAY_CURVE} elliptic curve. - /// - /// - Parameters: - /// - signature: The signature to verify. - /// - digest: The signed digest. - /// - Returns: A Boolean value that’s `true` if the signature is valid for - /// the given digest; otherwise, `false`. - public func isValidSignature(_ signature: ${CURVE}.Signing.ECDSASignature, for digest: D) -> Bool { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return self.coreCryptoIsValidSignature(signature, for: digest) - #else - return self.openSSLIsValidSignature(signature, for: digest) - #endif - } -} - -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -extension ${CURVE}.Signing.PublicKey: DataValidator { - /// Verifies an elliptic curve digital signature algorithm (ECDSA) - /// signature on a block of data over the ${DISPLAY_CURVE} elliptic curve. - /// - /// - Parameters: - /// - signature: The signature to verify. - /// - data: The signed data. - /// - Returns: A Boolean value that’s `true` if the signature is valid for - /// the given data; otherwise, `false`. - public func isValidSignature(_ signature: ${CURVE}.Signing.ECDSASignature, for data: D) -> Bool { - return self.isValidSignature(signature, for: ${HF}.hash(data: data)) - } - } - -% end - -#endif // Linux or !SwiftPM diff --git a/Sources/Crypto/Signatures/Ed25519.swift b/Sources/Crypto/Signatures/Ed25519.swift index a4755b77..ba377bb2 100644 --- a/Sources/Crypto/Signatures/Ed25519.swift +++ b/Sources/Crypto/Signatures/Ed25519.swift @@ -11,40 +11,32 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -public import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) public import FoundationEssentials #else public import Foundation #endif -#endif -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) protocol DigestValidator { associatedtype Signature func isValidSignature(_ signature: Signature, for digest: D) -> Bool } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) protocol DataValidator { associatedtype Signature func isValidSignature(_ signature: Signature, for signedData: D) -> Bool } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension Curve25519.Signing { static var signatureByteCount: Int { return 64 } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension Curve25519.Signing.PublicKey: DataValidator { typealias Signature = Data @@ -57,15 +49,10 @@ extension Curve25519.Signing.PublicKey: DataValidator { /// - Returns: A Boolean value that’s `true` when the signature is valid for /// the given data. public func isValidSignature(_ signature: S, for data: D) -> Bool { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return self.coreCryptoIsValidSignature(signature, for: data) - #else return self.openSSLIsValidSignature(signature, for: data) - #endif } } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) extension Curve25519.Signing.PrivateKey: Signer { /// Generates an EdDSA signature over Curve25519. /// @@ -79,11 +66,7 @@ extension Curve25519.Signing.PrivateKey: Signer { /// different signature on every call, even for the same data and key, to /// guard against side-channel attacks. public func signature(for data: D) throws(CryptoKitMetaError) -> Data { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return try self.coreCryptoSignature(for: data) - #else return try self.openSSLSignature(for: data) - #endif } } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/MLDSA.swift b/Sources/Crypto/Signatures/MLDSA.swift index 009aea7d..26e9c1fd 100644 --- a/Sources/Crypto/Signatures/MLDSA.swift +++ b/Sources/Crypto/Signatures/MLDSA.swift @@ -11,7 +11,8 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else #if canImport(FoundationEssentials) @@ -20,21 +21,14 @@ public import FoundationEssentials public import Foundation #endif -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -typealias MLDSAPublicKeyImpl = CorecryptoMLDSAPublicKeyImpl -typealias MLDSAPrivateKeyImpl = CorecryptoMLDSAPrivateKeyImpl -#else typealias MLDSAPublicKeyImpl = OpenSSLMLDSAPublicKeyImpl typealias MLDSAPrivateKeyImpl = OpenSSLMLDSAPrivateKeyImpl -#endif /// The MLDSA65 Digital Signature Algorithm -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) +@nonexhaustive public enum MLDSA65: Sendable {} -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) extension MLDSA65 { /// The public key for MLDSA65. public struct PublicKey: Sendable { @@ -187,10 +181,9 @@ extension MLDSA65 { /// The MLDSA87 Digital Signature Algorithm -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) +@nonexhaustive public enum MLDSA87: Sendable {} -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) extension MLDSA87 { /// The public key for MLDSA87. public struct PublicKey: Sendable { @@ -342,4 +335,4 @@ extension MLDSA87 { } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Signatures/MLDSA.swift.gyb b/Sources/Crypto/Signatures/MLDSA.swift.gyb deleted file mode 100644 index 8fb0a46e..00000000 --- a/Sources/Crypto/Signatures/MLDSA.swift.gyb +++ /dev/null @@ -1,197 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the SwiftCrypto open source project -// -// Copyright (c) 2019-2020 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 -// -//===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@_exported import CryptoKit -#else -#if canImport(FoundationEssentials) -public import FoundationEssentials -#else -public import Foundation -#endif -%{ - MLDSA_VARIANTS = [{"name": "MLDSA65"}, {"name": "MLDSA87"}] -}% - -#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -typealias MLDSAPublicKeyImpl = CorecryptoMLDSAPublicKeyImpl -typealias MLDSAPrivateKeyImpl = CorecryptoMLDSAPrivateKeyImpl -#else -typealias MLDSAPublicKeyImpl = OpenSSLMLDSAPublicKeyImpl -typealias MLDSAPrivateKeyImpl = OpenSSLMLDSAPrivateKeyImpl -#endif - -% for MLDSA_VARIANT in MLDSA_VARIANTS: -%{ - NAME = MLDSA_VARIANT["name"] -}% - -/// The ${NAME} Digital Signature Algorithm -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -public enum ${NAME}: Sendable {} - -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) -extension ${NAME} { - /// The public key for ${NAME}. - public struct PublicKey: Sendable { - var impl: MLDSAPublicKeyImpl<${NAME}> - - internal init(_ impl: MLDSAPublicKeyImpl<${NAME}>) { - self.impl = impl - } - - /// Parses a public key from a serialized representation. - /// - /// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format. - /// - Returns: The deserialized public key. - public init(rawRepresentation: D) throws { - self.impl = try MLDSAPublicKeyImpl(rawRepresentation: rawRepresentation) - } - - /// A serialized representation of the public key. - /// - /// This property provides a representation of the public key in the FIPS 204 standard serialization format. - public var rawRepresentation: Data { - get { - return self.impl.rawRepresentation - } - } - - /// Verifies a ${NAME} signature. - /// - Parameters: - /// - signature: The ${NAME} signature to verify. - /// - data: The signed data. - /// - Returns: `true` if the signature is valid, `false` otherwise. - public func isValidSignature(_ signature: S, for data: D) -> Bool { - return self.impl.isValidSignature(signature, for: data) - } - - /// Verifies a ${NAME} signature, in a specific context. - /// - Parameters: - /// - signature: The ${NAME} signature to verify. - /// - data: The signed data. - /// - context: Context for the signature. - /// - Returns: `true` if the signature is valid in the specified context, `false` otherwise. - public func isValidSignature(_ signature: S, for data: D, context: C) -> Bool { - return self.impl.isValidSignature(signature, for: data, context: context) - } - } - - /// The private key for ${NAME}. - public struct PrivateKey: Signer, Sendable { - internal let impl: MLDSAPrivateKeyImpl<${NAME}> - - internal init(_ impl: MLDSAPrivateKeyImpl<${NAME}>) { - self.impl = impl - } - - /// Creates a random ${NAME} private key. - /// - /// This initializer is marked `throws` to support use in generic contexts, - /// but key generation itself doesn't produce errors. - /// - /// When you call this initializer directly on a concrete type, rather than - /// through a generic type parameter, you can safely call `try!` to create the key: - /// - /// ```swift - /// let privateKey = try! ${NAME}.PrivateKey() - /// ``` - public init() throws { - let impl = try MLDSAPrivateKeyImpl<${NAME}>() - self = PrivateKey(impl) - } - - /// Initializes a private key from the seed representation. - /// - /// - Parameter seedRepresentation: The seed representation of the private key. This parameter needs to be 32 bytes long. - /// - Parameter publicKey: The public key associated with the secret key. - /// - /// This initializer implements the `ML-DSA.KeyGen_internal` algorithm (Algorithm 16) of FIPS 204. - /// - /// If a public key is provided, a consistency check is performed between it and the derived public key. - public init(seedRepresentation: D, publicKey: ${NAME}.PublicKey?) throws { - var publicKeyRawRepresentation: Data? = nil - if publicKey != nil { - publicKeyRawRepresentation = publicKey!.rawRepresentation - } - self.impl = try MLDSAPrivateKeyImpl<${NAME}>(seedRepresentation: seedRepresentation, publicKeyRawRepresentation: publicKeyRawRepresentation) - } - - /// The seed representation of the private key. - /// - /// The seed representation is 32 bytes long, and is the parameter - /// for the `ML-DSA.KeyGen_internal` algorithm (Algorithm 16) of FIPS 204. - public var seedRepresentation: Data { - get { - return self.impl.seedRepresentation - } - } - - /// Generates a ${NAME} signature. - /// - Parameters: - /// - data: The data to sign. - /// - Returns: The ${NAME} signature. - /// This method throws if CryptoKit encounters an error producing the signature. - public func signature(for data: D) throws -> Data { - return try impl.signature(for: data) - } - - /// Generates a ${NAME} signature, with context. - /// - Parameters: - /// - data: The data to sign. - /// - context: Context for the signature. - /// - Returns: The ${NAME} signature. - /// This method throws if CryptoKit encounters an error producing the signature. - public func signature(for data: D, context: C) throws -> Data { - return try impl.signature(for: data, context: context) - } - - /// The associated public key. - public var publicKey: PublicKey { - get { - return PublicKey(self.impl.publicKey) - } - } - - /// Initializes a private key from an integrity-checked data representation. - /// - /// - Parameter integrityCheckedRepresentation: The integrity-checked data representation of the private key. - /// The parameter needs to be 64 bytes long, and contain the seed and a hash of the public key. - public init(integrityCheckedRepresentation: D) throws { - let seedSize = MLDSAPrivateKeyImpl<${NAME}>.seedSize - guard integrityCheckedRepresentation.count == seedSize + 32 else { - throw CryptoKitError.incorrectParameterSize - } - - let seed = Data(integrityCheckedRepresentation).subdata(in: 0..(seedRepresentation: seed, publicKeyHash: publicKeyHash) - } - - /// The integrity-checked data representation of the private key. - /// - /// This representation is 64 bytes long, and contains the seed and a hash of the public key. - public var integrityCheckedRepresentation: Data { - get { - return self.impl.integrityCheckedRepresentation - } - } - } -} - -% end - -#endif // Linux or !SwiftPM diff --git a/Sources/Crypto/Signatures/Signature.swift b/Sources/Crypto/Signatures/Signature.swift index 4c5bb97f..bfef886b 100644 --- a/Sources/Crypto/Signatures/Signature.swift +++ b/Sources/Crypto/Signatures/Signature.swift @@ -11,34 +11,27 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) protocol SignatureVerification { func verifySignature(signature: Data, data: Data) throws(CryptoKitMetaError) -> Bool } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) protocol DigestSigner { associatedtype Signature func signature(for digest: D) throws(CryptoKitMetaError) -> Signature } -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) protocol Signer { associatedtype Signature func signature(for data: D) throws(CryptoKitMetaError) -> Signature } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Util/ArraySpanHelpers.swift b/Sources/Crypto/Util/ArraySpanHelpers.swift index 5e3b3fcc..548fcb6d 100644 --- a/Sources/Crypto/Util/ArraySpanHelpers.swift +++ b/Sources/Crypto/Util/ArraySpanHelpers.swift @@ -11,11 +11,11 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) + extension InlineArray where Element == UInt8 { /// Copy of the bytes of the given raw span into this array. The span /// must have exactly count bytes in it. @@ -28,7 +28,6 @@ extension InlineArray where Element == UInt8 { } } } -#endif extension Array where Element == UInt8 { /// Copy of the bytes of the given raw span into this array. The span @@ -89,4 +88,5 @@ extension UnsafeMutableRawBufferPointer { } } } -#endif // Linux or !SwiftPM + +#endif diff --git a/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift b/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift index 78cd140a..b19b346f 100644 --- a/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift +++ b/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @_implementationOnly import CCryptoBoringSSL @@ -24,4 +24,4 @@ extension CryptoKitError { .underlyingCoreCryptoError(error: Int32(bitPattern: CCryptoBoringSSL_ERR_get_error())) } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Util/BoringSSL/InlineArray+withBytes_boring.swift b/Sources/Crypto/Util/BoringSSL/InlineArray+withBytes_boring.swift index afd9630d..ba5f0082 100644 --- a/Sources/Crypto/Util/BoringSSL/InlineArray+withBytes_boring.swift +++ b/Sources/Crypto/Util/BoringSSL/InlineArray+withBytes_boring.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else #if canImport(FoundationEssentials) diff --git a/Sources/Crypto/Util/BoringSSL/RNG_boring.swift b/Sources/Crypto/Util/BoringSSL/RNG_boring.swift index ee9b1cc5..3a4b7373 100644 --- a/Sources/Crypto/Util/BoringSSL/RNG_boring.swift +++ b/Sources/Crypto/Util/BoringSSL/RNG_boring.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else @@ -67,4 +67,4 @@ extension OutputRawSpan { } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Util/Data+ArraySpan.swift b/Sources/Crypto/Util/Data+ArraySpan.swift index 22f2e2ea..a154ed5c 100644 --- a/Sources/Crypto/Util/Data+ArraySpan.swift +++ b/Sources/Crypto/Util/Data+ArraySpan.swift @@ -11,19 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) @_exported import CryptoKit #else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else + #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif extension Data { /// Copy the raw bytes from the given span into a new Data instance. @@ -50,8 +46,6 @@ extension Data { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) extension InlineArray where Element == UInt8 { init(copying data: D) { self.init { outputSpan in @@ -63,24 +57,5 @@ extension InlineArray where Element == UInt8 { } } } -#endif -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -// Note: Should be provided by SwiftSystem's version of Data. We provide -extension Data { - var bytes: RawSpan { - get { - let buffer = withUnsafeBytes { $0 } - return _overrideLifetime(buffer.bytes, borrowing: self) - } - } - - var mutableBytes: MutableRawSpan { - mutating get { - let buffer = withUnsafeMutableBytes { $0 } - return _overrideLifetime(buffer.mutableBytes, mutating: &self) - } - } -} #endif -#endif // Linux or !SwiftPM diff --git a/Sources/Crypto/Util/PrettyBytes.swift b/Sources/Crypto/Util/PrettyBytes.swift index 4de60f02..97e8ba33 100644 --- a/Sources/Crypto/Util/PrettyBytes.swift +++ b/Sources/Crypto/Util/PrettyBytes.swift @@ -11,16 +11,12 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else + #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif enum ByteHexEncodingErrors: Error { case incorrectHexValue @@ -46,11 +42,6 @@ private func htoi(_ value: UInt8) throws -> UInt8 { } #if !hasFeature(Embedded) -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension DataProtocol { var hexString: String { let hexLen = self.count * 2 @@ -70,18 +61,12 @@ extension DataProtocol { } #endif // !hasFeature(Embedded) -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension RangeReplaceableCollection where Element == UInt8 { mutating func appendByte(_ byte: UInt64) { withUnsafeBytes(of: byte.littleEndian, { self.append(contentsOf: $0) }) } } -#if !CRYPTOKIT_NO_ACCESS_TO_FOUNDATION && !CRYPTOKIT_NO_IMPORT_FOUNDATION extension Data { init(hexString: String) throws { self.init() @@ -120,4 +105,3 @@ extension Array where Element == UInt8 { } } -#endif diff --git a/Sources/Crypto/Util/SafeCompare.swift b/Sources/Crypto/Util/SafeCompare.swift index fe1f2d58..8496f7b4 100644 --- a/Sources/Crypto/Util/SafeCompare.swift +++ b/Sources/Crypto/Util/SafeCompare.swift @@ -11,30 +11,18 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif -#else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION #else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif internal func safeCompare(_ lhs: LHS, _ rhs: RHS) -> Bool { - #if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - return coreCryptoSafeCompare(lhs, rhs) - #else return openSSLSafeCompare(lhs, rhs) - #endif } -#endif // Linux or !SwiftPM +#endif // canImport(CryptoKit) diff --git a/Sources/Crypto/Util/SecureBytes.swift b/Sources/Crypto/Util/SecureBytes.swift index 4b5f6ea2..6824c52d 100644 --- a/Sources/Crypto/Util/SecureBytes.swift +++ b/Sources/Crypto/Util/SecureBytes.swift @@ -11,32 +11,18 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -#if CRYPTOKIT_STATIC_LIBRARY -@_exported import CryptoKit_Static -#else + +#if canImport(CryptoKit) @_exported import CryptoKit -#endif -#else -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION #else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif - nonisolated(unsafe) private let emptyStorage:SecureBytes.Backing = SecureBytes.Backing.createEmpty() -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif struct SecureBytes: @unchecked Sendable { var backing: Backing @@ -97,11 +83,6 @@ struct SecureBytes: @unchecked Sendable { #endif } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes { mutating func append(_ bytes: RawSpan) { let requiredCapacity = self.count + bytes.byteCount @@ -133,11 +114,6 @@ extension SecureBytes { } // MARK: - Equatable conformance, constant-time -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: Equatable { public static func == (lhs: SecureBytes, rhs: SecureBytes) -> Bool { return safeCompare(lhs, rhs) @@ -145,11 +121,6 @@ extension SecureBytes: Equatable { } // MARK: - Collection conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: Collection { struct Index { fileprivate var offset: Int @@ -186,11 +157,6 @@ extension SecureBytes: Collection { } // MARK: - BidirectionalCollection conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: BidirectionalCollection { func index(before index: Index) -> Index { return index.advanced(by: -1) @@ -198,27 +164,12 @@ extension SecureBytes: BidirectionalCollection { } // MARK: - RandomAccessCollection conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: RandomAccessCollection { } // MARK: - MutableCollection conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: MutableCollection { } // MARK: - RangeReplaceableCollection conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: RangeReplaceableCollection { mutating func replaceSubrange(_ subrange: Range, with newElements: C) where C.Element == UInt8 { let requiredCapacity = self.backing.count - subrange.count + newElements.count @@ -259,11 +210,6 @@ extension SecureBytes: RangeReplaceableCollection { } // MARK: - ContiguousBytes conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: ContiguousBytes { /// Access the stored bytes. var bytes: RawSpan { @@ -318,11 +264,6 @@ extension SecureBytes: ContiguousBytes { } // MARK: - DataProtocol conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: DataProtocol { var regions: CollectionOfOne { return CollectionOfOne(self) @@ -330,37 +271,17 @@ extension SecureBytes: DataProtocol { } // MARK: - MutableDataProtocol conformance -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes: MutableDataProtocol { } // MARK: - Index conformances -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes.Index: Hashable { } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes.Index: Comparable { static func <(lhs: SecureBytes.Index, rhs: SecureBytes.Index) -> Bool { return lhs.offset < rhs.offset } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes.Index: Strideable { func advanced(by n: Int) -> SecureBytes.Index { return SecureBytes.Index(offset: self.offset + n) @@ -372,11 +293,6 @@ extension SecureBytes.Index: Strideable { } // MARK: - Heap allocated backing storage. -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes { #if !hasFeature(Embedded) internal struct BackingHeader { @@ -622,11 +538,6 @@ extension SecureBytes { #endif } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes.Backing { var allocatedCapacity: Int { #if os(OpenBSD) @@ -731,11 +642,6 @@ extension SecureBytes.Backing { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension SecureBytes.Backing: ContiguousBytes { func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws(E) -> T) throws(E) -> T { let count = self.count @@ -765,17 +671,11 @@ extension SecureBytes.Backing: ContiguousBytes { #if hasFeature(Embedded) /// Very unsafe in the sense that this points to uninitialized memory. Used only for implementations within this file. - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) func _withVeryUnsafeMutableBytes(_ body: (UnsafeMutableRawBufferPointer) throws(E) -> T) throws(E) -> T { return try body(storage) } #else /// Very unsafe in the sense that this points to uninitialized memory. Used only for implementations within this file. - #if !CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) - #else // CRYPTOKIT_STATIC_LIBRARY - @available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) - #endif func _withVeryUnsafeMutableBytes(_ body: (UnsafeMutableRawBufferPointer) throws(E) -> T) throws(E) -> T { let capacity = self.allocatedCapacity @@ -803,11 +703,6 @@ extension SecureBytes.Backing: ContiguousBytes { #endif } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension UInt32 { /// Returns the next power of two unless that would overflow, in which case UInt32.max (on 64-bit systems) or /// Int32.max (on 32-bit systems) is returned. The returned value is always safe to be cast to Int and passed @@ -841,22 +736,11 @@ extension UInt32 { } } -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension Data { /// A custom initializer for Data that attempts to share the same storage as the current SecureBytes instance. /// This is our best-effort attempt to expose the data in an auto-zeroing fashion. Any mutating function called on /// the constructed `Data` object will cause the bytes to be copied out: we can't avoid that. init(_ secureBytes: SecureBytes) { - #if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - self = secureBytes.withUnsafeBytes { - // We make a mutable copy of this pointer here because we know Data won't write through it. - return Data($0) - } - #else // We need to escape into unmanaged land here in order to keep the backing storage alive. let unmanagedBacking = Unmanaged.passRetained(secureBytes.backing) @@ -866,7 +750,6 @@ extension Data { // We make a mutable copy of this pointer here because we know Data won't write through it. return Data(bytesNoCopy: UnsafeMutableRawPointer(mutating: $0.baseAddress!), count: $0.count, deallocator: .custom { (_: UnsafeMutableRawPointer, _: Int) in unmanagedBacking.release() }) } - #endif } /// A custom initializer for Data that attempts to share the same storage as the current SecureBytes instance. @@ -879,15 +762,6 @@ extension Data { let baseOffset = secureByteSlice.startIndex.offset let endOffset = secureByteSlice.endIndex.offset - #if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION - self = base.withUnsafeBytes { - // Slice the base pointer down to just the range we want. - let slicedPointer = UnsafeRawBufferPointer(rebasing: $0[baseOffset.. $) diff --git a/Sources/CryptoExtras/ECToolbox/ECToolbox.swift b/Sources/CryptoExtras/ECToolbox/ECToolbox.swift index a1eb80e9..b6c9ddda 100644 --- a/Sources/CryptoExtras/ECToolbox/ECToolbox.swift +++ b/Sources/CryptoExtras/ECToolbox/ECToolbox.swift @@ -18,21 +18,12 @@ import FoundationEssentials import Foundation #endif -#if canImport(Darwin) && !CRYPTO_IN_SWIFTPM -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) -typealias SupportedCurveDetailsImpl = CorecryptoSupportedNISTCurve -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) -typealias GroupImpl = CoreCryptoGroup -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) -typealias HashToCurveImpl = CoreCryptoHashToCurve -#else @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) typealias SupportedCurveDetailsImpl = OpenSSLSupportedNISTCurve @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) typealias GroupImpl = OpenSSLGroup @available(macOS 10.15, iOS 13.2, tvOS 13.2, watchOS 6.1, macCatalyst 13.2, visionOS 1.2, *) typealias HashToCurveImpl = OpenSSLHashToCurve -#endif /// A prime-order group @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) diff --git a/Tests/CryptoTests/ASN1/ASN1Tests.swift b/Tests/CryptoTests/ASN1/ASN1Tests.swift index d94debc8..33272c4d 100644 --- a/Tests/CryptoTests/ASN1/ASN1Tests.swift +++ b/Tests/CryptoTests/ASN1/ASN1Tests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif class ASN1Tests: XCTestCase { func testSimpleASN1P256SPKI() throws { diff --git a/Tests/CryptoTests/ASN1/GeneralizedTimeTests.swift b/Tests/CryptoTests/ASN1/GeneralizedTimeTests.swift index 0878c36a..4abf820c 100644 --- a/Tests/CryptoTests/ASN1/GeneralizedTimeTests.swift +++ b/Tests/CryptoTests/ASN1/GeneralizedTimeTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif final class GeneralizedTimeTests: XCTestCase { private func assertRoundTrips(_ value: ASN1Object) throws { diff --git a/Tests/CryptoTests/Authenticated Encryption/AES-GCM-Runner.swift b/Tests/CryptoTests/Authenticated Encryption/AES-GCM-Runner.swift index 7bea074b..e860b71b 100644 --- a/Tests/CryptoTests/Authenticated Encryption/AES-GCM-Runner.swift +++ b/Tests/CryptoTests/Authenticated Encryption/AES-GCM-Runner.swift @@ -11,23 +11,12 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if canImport(FoundationEssentials) -import FoundationEssentials -#else -import Foundation -#endif import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -// Skip tests that use @available annotations on tests unless running on Linux. +#if canImport(CryptoKit) +// Skip tests that require @testable imports of CryptoKit. #else -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import Crypto -#elseif !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import CryptoKit -#else -import Crypto -#endif +@testable import Crypto struct AEADTestGroup: Codable { let ivSize: Int @@ -47,7 +36,6 @@ struct AESGCMTestVector: Codable { let result: String } -@available(iOS 17.4, macOS 14.4, watchOS 10.4, tvOS 17.4, macCatalyst 17.4, *) class AESGCMTests: XCTestCase { func testPropertiesStayTheSameAfterFailedOpening() throws { let message = Data("this is a message".utf8) @@ -101,7 +89,6 @@ class AESGCMTests: XCTestCase { XCTAssertEqual(recoveredPlaintextWithoutAAD.startIndex, 0) } - @available(macOS 27, iOS 27, tvOS 27, watchOS 27, visionOS 27, *) func testEncryptDecryptSpan() throws { let plaintext: [UInt8] = Array("Some Super Secret Message".utf8) @@ -296,4 +283,4 @@ class AESGCMTests: XCTestCase { } } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#endif diff --git a/Tests/CryptoTests/Authenticated Encryption/ChaChaPoly-Runner.swift b/Tests/CryptoTests/Authenticated Encryption/ChaChaPoly-Runner.swift index 22c0a403..c743faf5 100644 --- a/Tests/CryptoTests/Authenticated Encryption/ChaChaPoly-Runner.swift +++ b/Tests/CryptoTests/Authenticated Encryption/ChaChaPoly-Runner.swift @@ -11,25 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if canImport(FoundationEssentials) -import FoundationEssentials -#else -import Foundation -#endif import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -// Skip tests that use @available annotations on tests unless running on Linux. +#if canImport(CryptoKit) +// Skip tests that require @testable imports of CryptoKit. #else -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import Crypto -#elseif !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import CryptoKit -#else -import Crypto -#endif +@testable import Crypto -@available(iOS 17.4, macOS 14.4, watchOS 10.4, tvOS 17.4, macCatalyst 17.4, *) class ChaChaPolyTests: XCTestCase { func testIncorrectKeySize() throws { let plaintext: Data = "Some Super Secret Message".data(using: String.Encoding.utf8)! @@ -71,12 +59,9 @@ class ChaChaPolyTests: XCTestCase { XCTAssertEqual(recoveredPlaintext, plaintext) - // Some applications assume that the startIndex of the returned plaintext is zero. - // While that is an incorrect assumption, it was true of prior implementations, so XCTAssertEqual(recoveredPlaintext.startIndex, 0) } - @available(macOS 27, iOS 27, tvOS 27, watchOS 27, visionOS 27, *) func testEncryptDecryptSpans() throws { let plaintext: [UInt8] = Array("Some Super Secret Message".utf8) @@ -259,4 +244,5 @@ class ChaChaPolyTests: XCTestCase { } } } -#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API + +#endif diff --git a/Tests/CryptoTests/Digests/DigestsTests.swift b/Tests/CryptoTests/Digests/DigestsTests.swift index eaeaae47..f8b5cfd4 100644 --- a/Tests/CryptoTests/Digests/DigestsTests.swift +++ b/Tests/CryptoTests/Digests/DigestsTests.swift @@ -11,12 +11,11 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) import Crypto -#elseif !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import CryptoKit #else import Crypto #endif @@ -27,7 +26,7 @@ enum TestError: Error { func nullTestVectorForAlgorithm(hashFunction: H.Type) throws -> String { #if !CRYPTOKIT_IN_SEP -#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#if canImport(CryptoKit, _version: 324.0.4) if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) { switch H.self { case is SHA3_256.Type: return "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a" @@ -37,7 +36,7 @@ func nullTestVectorForAlgorithm(hashFunction: H.Type) throws -> break } } -#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#endif // canImport(CryptoKit, _version: 324.0.4) #endif // !CRYPTOKIT_IN_SEP switch H.self { @@ -53,7 +52,7 @@ func nullTestVectorForAlgorithm(hashFunction: H.Type) throws -> func testVectorForAlgorithm(hashFunction: H.Type) throws -> String { #if !CRYPTOKIT_IN_SEP -#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#if canImport(CryptoKit, _version: 324.0.4) if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) { switch H.self { case is SHA3_256.Type: return "916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18" @@ -63,7 +62,7 @@ func testVectorForAlgorithm(hashFunction: H.Type) throws -> Str break } } -#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#endif // canImport(CryptoKit, _version: 324.0.4) #endif // !CRYPTOKIT_IN_SEP switch H.self { @@ -118,13 +117,13 @@ class DigestsTests: XCTestCase { try orFail { try testHashFunction(hf: SHA384.self) } try orFail { try testHashFunction(hf: SHA512.self) } #if !CRYPTOKIT_IN_SEP -#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#if canImport(CryptoKit, _version: 324.0.4) if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) { try orFail { try testHashFunction(hf: SHA3_256.self) } try orFail { try testHashFunction(hf: SHA3_384.self) } try orFail { try testHashFunction(hf: SHA3_512.self) } } -#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#endif // canImport(CryptoKit, _version: 324.0.4) #endif // !CRYPTOKIT_IN_SEP } @@ -149,34 +148,32 @@ class DigestsTests: XCTestCase { try orFail { try testHashFunctionImplementsCoW(hf: SHA384.self) } try orFail { try testHashFunctionImplementsCoW(hf: SHA512.self) } #if !CRYPTOKIT_IN_SEP -#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#if canImport(CryptoKit, _version: 324.0.4) if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) { try orFail { try testHashFunctionImplementsCoW(hf: SHA3_256.self) } try orFail { try testHashFunctionImplementsCoW(hf: SHA3_384.self) } try orFail { try testHashFunctionImplementsCoW(hf: SHA3_512.self) } } -#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#endif // canImport(CryptoKit, _version: 324.0.4) #endif // !CRYPTOKIT_IN_SEP } func testBlockSizes() { - if #available(iOS 13.2, macOS 10.15, watchOS 6.1, tvOS 13.2, macCatalyst 13.2, *) { - XCTAssertEqual(Insecure.MD5.blockByteCount, 64) - XCTAssertEqual(Insecure.SHA1.blockByteCount, 64) - XCTAssertEqual(SHA256.blockByteCount, 64) - - XCTAssertEqual(SHA384.blockByteCount, 128) - XCTAssertEqual(SHA512.blockByteCount, 128) - } + XCTAssertEqual(Insecure.MD5.blockByteCount, 64) + XCTAssertEqual(Insecure.SHA1.blockByteCount, 64) + XCTAssertEqual(SHA256.blockByteCount, 64) + + XCTAssertEqual(SHA384.blockByteCount, 128) + XCTAssertEqual(SHA512.blockByteCount, 128) #if !CRYPTOKIT_IN_SEP -#if !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#if canImport(CryptoKit, _version: 324.0.4) if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, visionOS 2.0, *) { XCTAssertEqual(SHA3_256.blockByteCount, 136) XCTAssertEqual(SHA3_384.blockByteCount, 104) XCTAssertEqual(SHA3_512.blockByteCount, 72) } -#endif // !CRYPTO_IN_SWIFTPM || canImport(CryptoKit, _version: 324.0.4) +#endif // canImport(CryptoKit, _version: 324.0.4) #endif // !CRYPTOKIT_IN_SEP } } diff --git a/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift b/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift index 1a3716f3..839bfc2b 100644 --- a/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift +++ b/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift @@ -13,21 +13,16 @@ //===----------------------------------------------------------------------===// import XCTest +#if canImport(CryptoKit) +// Skip tests that require @testable imports of CryptoKit. +#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif - -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -// Skip tests that require @testable imports of CryptoKit. -#else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @_implementationOnly import CCryptoBoringSSL @testable import Crypto -#endif // This module implements "just enough" ASN.1. Specifically, we implement exactly enough ASN.1 DER parsing to handle // taking the Wycheproof input of EC public keys and to turn them into the x963 representation of the key. This is not @@ -342,4 +337,4 @@ extension UInt { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift b/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift index d1d82af0..28177f54 100644 --- a/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift +++ b/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift @@ -13,21 +13,16 @@ //===----------------------------------------------------------------------===// import XCTest +#if canImport(CryptoKit) +// Skip tests that require @testable imports of CryptoKit. +#else #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif - -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -// Skip tests that require @testable imports of CryptoKit. -#else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @_implementationOnly import CCryptoBoringSSL @testable import Crypto -#endif extension NISTECDHTests { func testGroupOpenSSL< @@ -185,4 +180,4 @@ extension NISTECPublicKey { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/ECDH/X25519-Runner.swift b/Tests/CryptoTests/ECDH/X25519-Runner.swift index 41deb870..9f5c55f3 100644 --- a/Tests/CryptoTests/ECDH/X25519-Runner.swift +++ b/Tests/CryptoTests/ECDH/X25519-Runner.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + #if canImport(FoundationEssentials) import FoundationEssentials #else @@ -18,14 +19,10 @@ import Foundation #endif import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif struct ECDHTestGroup: Codable { let curve: String @@ -206,4 +203,4 @@ class X25519Tests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/ECDH/secpECDH_Runner.swift b/Tests/CryptoTests/ECDH/secpECDH_Runner.swift index e03efa1d..e8d07ade 100644 --- a/Tests/CryptoTests/ECDH/secpECDH_Runner.swift +++ b/Tests/CryptoTests/ECDH/secpECDH_Runner.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + #if canImport(FoundationEssentials) import FoundationEssentials #else @@ -18,14 +19,10 @@ import Foundation #endif import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif enum ECDHTestErrors: Error { case PublicKeyFailure @@ -113,19 +110,11 @@ class NISTECDHTests: XCTestCase { } func testGroup(group: ECDHTestGroup, privateKeys: PrivKey.Type, onCurve curve: Curve.Type) { - #if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API - self.testGroupCC(group: group, privateKeys: privateKeys, onCurve: curve) - #else self.testGroupOpenSSL(group: group, privateKeys: privateKeys, onCurve: curve) - #endif } func testGroupPoint(group: ECDHTestGroup, privateKeys: PrivKey.Type, onCurve curve: Curve.Type) { - #if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API - self.testGroupPointCC(group: group, privateKeys: privateKeys, onCurve: curve) - #else self.testGroupPointOpenSSL(group: group, privateKeys: privateKeys, onCurve: curve) - #endif } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Encodings/BoringSSL/DERTests_boring.swift b/Tests/CryptoTests/Encodings/BoringSSL/DERTests_boring.swift index f0a8896c..8320cd8d 100644 --- a/Tests/CryptoTests/Encodings/BoringSSL/DERTests_boring.swift +++ b/Tests/CryptoTests/Encodings/BoringSSL/DERTests_boring.swift @@ -13,14 +13,10 @@ //===----------------------------------------------------------------------===// import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif extension DERTests { func openSSLCoordinateSizeForCurve(_: Curve.Type) -> Int { @@ -28,4 +24,4 @@ extension DERTests { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Encodings/DERTests.swift b/Tests/CryptoTests/Encodings/DERTests.swift index 38bcfbca..88e025ba 100644 --- a/Tests/CryptoTests/Encodings/DERTests.swift +++ b/Tests/CryptoTests/Encodings/DERTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif class DERTests: XCTestCase { func testEncodeDecodeECDSASignature() throws { @@ -41,15 +38,11 @@ class DERTests: XCTestCase { } func coordinateSizeForCurve(_ curve: Curve.Type) -> Int { - #if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API - return self.coreCryptoCoordinateSizeForCurve(curve) - #else return self.openSSLCoordinateSizeForCurve(curve) - #endif } func randomBytes(count: Int) -> [UInt8] { - #if canImport(Darwin) || os(Linux) || os(Android) || os(Windows) + #if os(Linux) || os(Android) || os(Windows) var rng = SystemRandomNumberGenerator() return (0.. HPKE.AEAD? { return aeadValues.first } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/HPKE/HPKETests.swift b/Tests/CryptoTests/HPKE/HPKETests.swift index c187c6ea..c78da04e 100644 --- a/Tests/CryptoTests/HPKE/HPKETests.swift +++ b/Tests/CryptoTests/HPKE/HPKETests.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + #if canImport(FoundationEssentials) import FoundationEssentials #else @@ -18,17 +19,10 @@ import Foundation #endif import XCTest - -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -@testable import CryptoKit -@testable import CryptoKit -#else @testable import Crypto -#endif class HPKETests: XCTestCase { func testCases() throws { @@ -70,7 +64,6 @@ class HPKETests: XCTestCase { } } - @available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) func testHPKECiphersuite(_ c: HPKE.Ciphersuite, withKeys: SK.Type) throws { let skR = try SK.PublicKey.EphemeralPrivateKey() let info = Data("Some Test Data".utf8) @@ -166,6 +159,17 @@ class HPKETests: XCTestCase { XCTAssertEqual(cc25519.aead.nonceByteCount, 12) XCTAssertEqual(cc25519.aead.tagByteCount, 16) } + + func testHPKEKEMInterface() throws { + if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, *) { + let c = HPKE.Ciphersuite.XWingMLKEM768X25519_SHA256_AES_GCM_256 + let skR = try XWingMLKEM768X25519.PrivateKey.generate() + let info = Data("Some Test Data".utf8) + var sender = try HPKE.Sender(recipientKey: skR.publicKey, ciphersuite: c, info: info) + var recipient = try HPKE.Recipient(privateKey: skR, ciphersuite: c, info: info, encapsulatedKey: sender.encapsulatedKey) + XCTAssertNoThrow(try testSenderRecipient(sender: &sender, recipient: &recipient)) + } + } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/KEM/Boring/XWingTests_boring.swift b/Tests/CryptoTests/KEM/Boring/XWingTests_boring.swift index ea3890f7..5876c6a6 100644 --- a/Tests/CryptoTests/KEM/Boring/XWingTests_boring.swift +++ b/Tests/CryptoTests/KEM/Boring/XWingTests_boring.swift @@ -12,14 +12,10 @@ // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif extension XWingMLKEM768X25519.PrivateKey { static func generateWithRng(rngState: SequenceDrbg) throws -> Self { @@ -49,4 +45,4 @@ extension XWingMLKEM768X25519.PublicKey { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/KEM/MLKEMKeyGenTests.swift b/Tests/CryptoTests/KEM/MLKEMKeyGenTests.swift index 932b2949..7441798e 100644 --- a/Tests/CryptoTests/KEM/MLKEMKeyGenTests.swift +++ b/Tests/CryptoTests/KEM/MLKEMKeyGenTests.swift @@ -11,24 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) final class MLKEMKeyGenTests: XCTestCase { func processKeyGenKATFile(filename: String) throws -> [MLKEMKeyGenKAT] { - #if CRYPTO_IN_SWIFTPM let bundle = Bundle.module - #else - let bundle = Bundle(for: type(of: self)) - #endif let fileURL = bundle.url(forResource: filename, withExtension: "json") let json = try Data(contentsOf: fileURL!) let stringInput = String(data: json, encoding: .ascii)! @@ -99,4 +91,4 @@ struct MLKEMKeyGenKAT { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/KEM/MLKEMTests.swift b/Tests/CryptoTests/KEM/MLKEMTests.swift index b499bce0..ca7479f7 100644 --- a/Tests/CryptoTests/KEM/MLKEMTests.swift +++ b/Tests/CryptoTests/KEM/MLKEMTests.swift @@ -11,17 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) final class MLKEMTests: XCTestCase { func testMLKEM768() throws { let privateKey = try MLKEM768.PrivateKey.generate() @@ -57,7 +53,6 @@ final class MLKEMTests: XCTestCase { XCTAssert(er.sharedSecret == ss) } - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) func testOneTimeKeys() throws { let privateKey = try MLKEM1024.OneTimePrivateKey.generate() let publicKey = privateKey.publicKey @@ -74,11 +69,7 @@ final class MLKEMTests: XCTestCase { } func processKATFile(filename: String) throws -> [MLKEMKAT] { - #if CRYPTO_IN_SWIFTPM let bundle = Bundle.module - #else - let bundle = Bundle(for: type(of: self)) - #endif let fileURL = bundle.url(forResource: filename, withExtension: "json") let json = try Data(contentsOf: fileURL!) let stringInput = String(data: json, encoding: .ascii)! @@ -87,65 +78,13 @@ final class MLKEMTests: XCTestCase { } func test768KAT() throws { - #if CRYPTO_IN_SWIFTPM // No support for encapsulateWithSeed in BoringSSL. throw XCTSkip() - #else - let katTests = try processKATFile(filename:"MLKEM768KAT") - for katTest in katTests { - let rndGen = try Drbg(katTest.rngSeed) - - var keyGenSeed = Data(count: 64) - try keyGenSeed.withUnsafeMutableBytes { buffer in - try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen) - } - var encapSeed = Data(count: 32) - try encapSeed.withUnsafeMutableBytes { buffer in - try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen) - } - - let privateKey = try MLKEM768.PrivateKey.generateWithSeed(keyGenSeed) // 2 * 32 bytes - XCTAssert(privateKey.publicKey.rawRepresentation == katTest.pk) - - let encapsulatedKey = try privateKey.publicKey.encapsulateWithSeed(encapSeed: encapSeed) // 32 bytes - XCTAssert(encapsulatedKey.encapsulated == katTest.ek) - XCTAssert(encapsulatedKey.sharedSecret == katTest.k) - - let retrievedSharedSecret = try privateKey.decapsulate(encapsulatedKey.encapsulated) - XCTAssert(retrievedSharedSecret.dataRepresentation == katTest.k) - } - #endif } func test1024KAT() throws { - #if CRYPTO_IN_SWIFTPM // No support for encapsulateWithSeed in BoringSSL. throw XCTSkip() - #else - let katTests = try processKATFile(filename:"MLKEM1024KAT") - for katTest in katTests { - let rndGen = try Drbg(katTest.rngSeed) - - var keyGenSeed = Data(count: 64) - try keyGenSeed.withUnsafeMutableBytes { buffer in - try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen) - } - var encapSeed = Data(count: 32) - try encapSeed.withUnsafeMutableBytes { buffer in - try buffer.initializeWithRandomBytes(count: buffer.count, rngState: rndGen) - } - - let privateKey = try MLKEM1024.PrivateKey.generateWithSeed(keyGenSeed) - XCTAssert(privateKey.publicKey.rawRepresentation == katTest.pk) - - let encapsulatedKey = try privateKey.publicKey.encapsulateWithSeed(encapSeed: encapSeed) - XCTAssert(encapsulatedKey.encapsulated == katTest.ek) - XCTAssert(encapsulatedKey.sharedSecret == katTest.k) - - let retrievedSharedSecret = try privateKey.decapsulate(encapsulatedKey.encapsulated) - XCTAssert(retrievedSharedSecret.dataRepresentation == katTest.k) - } - #endif } } @@ -174,4 +113,4 @@ struct MLKEMKAT { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/KEM/XWingTests.swift b/Tests/CryptoTests/KEM/XWingTests.swift index 2e54d373..4df48b1c 100644 --- a/Tests/CryptoTests/KEM/XWingTests.swift +++ b/Tests/CryptoTests/KEM/XWingTests.swift @@ -11,18 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKitPrivate -@testable import CryptoKit -#else @testable import Crypto -#endif -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) final class XWingTests: XCTestCase { func testKEM() throws { let privateKey = try XWingMLKEM768X25519.PrivateKey.generate() @@ -40,7 +35,6 @@ final class XWingTests: XCTestCase { XCTAssert(er.sharedSecret == ss) } - @available(iOS 27.0, macOS 27.0, watchOS 27.0, tvOS 27.0, macCatalyst 27.0, visionOS 27.0, *) func testOneTimeKeys() throws { let privateKey = try XWingMLKEM768X25519.OneTimePrivateKey.generate() let publicKey = privateKey.publicKey @@ -56,11 +50,7 @@ final class XWingTests: XCTestCase { } func processKATFile(filename: String) throws -> [XWingKAT] { - #if CRYPTO_IN_SWIFTPM let bundle = Bundle.module - #else - let bundle = Bundle(for: type(of: self)) - #endif let fileURL = bundle.url(forResource: filename, withExtension: "json") let json = try Data(contentsOf: fileURL!) let stringInput = String(data: json, encoding: .ascii)! @@ -131,4 +121,4 @@ struct XWingKAT { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Key Derivation/HKDFTests.swift b/Tests/CryptoTests/Key Derivation/HKDFTests.swift index a93b9506..09063c93 100644 --- a/Tests/CryptoTests/Key Derivation/HKDFTests.swift +++ b/Tests/CryptoTests/Key Derivation/HKDFTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif // Test Vectors are coming from https://tools.ietf.org/html/rfc5869 class HKDFTests: XCTestCase { @@ -144,4 +141,4 @@ class HKDFTests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Key Derivation/SharedSecretTests.swift b/Tests/CryptoTests/Key Derivation/SharedSecretTests.swift index 910fade7..8dbc9755 100644 --- a/Tests/CryptoTests/Key Derivation/SharedSecretTests.swift +++ b/Tests/CryptoTests/Key Derivation/SharedSecretTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif class SharedSecretTests: XCTestCase { func testEqualityWithDataProtocol() throws { @@ -34,4 +31,4 @@ class SharedSecretTests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Key Derivation/X963KDFTests.swift b/Tests/CryptoTests/Key Derivation/X963KDFTests.swift index 3a335cb3..67076188 100644 --- a/Tests/CryptoTests/Key Derivation/X963KDFTests.swift +++ b/Tests/CryptoTests/Key Derivation/X963KDFTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif // Test Vectors are coming from ANSI X9.63-2001. class X963KDFTests: XCTestCase { @@ -91,4 +88,4 @@ class X963KDFTests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Key Wrapping/KeyWrapping.swift b/Tests/CryptoTests/Key Wrapping/KeyWrapping.swift index 8bf166c8..079ca98a 100644 --- a/Tests/CryptoTests/Key Wrapping/KeyWrapping.swift +++ b/Tests/CryptoTests/Key Wrapping/KeyWrapping.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif class KeyWrappingTests: XCTestCase { func testAESWrapTestVectors() throws { @@ -95,4 +92,4 @@ class KeyWrappingTests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/MAC/HMACTests.swift b/Tests/CryptoTests/MAC/HMACTests.swift index df36bb11..5343266c 100644 --- a/Tests/CryptoTests/MAC/HMACTests.swift +++ b/Tests/CryptoTests/MAC/HMACTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif // Test Vectors are coming from https://tools.ietf.org/html/rfc4231 class HMACTests: XCTestCase { @@ -233,4 +230,4 @@ class HMACTests: XCTestCase { XCTAssertFalse(mac == DispatchData.empty) } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift b/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift index 99aaf3ce..04cbfe6e 100644 --- a/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift +++ b/Tests/CryptoTests/SecureBytes/SecureBytesTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif final class SecureBytesTests: XCTestCase { func testBasicSoundness() { @@ -177,7 +174,6 @@ final class SecureBytesTests: XCTestCase { XCTAssertThrowsError(try testThrowingInitialization(), error: CryptoKitError.incorrectKeySize) } - @available(macOS 26, iOS 26, tvOS 26, watchOS 26, visionOS 26, *) func testAppendingDataPerformsACoW() { var base = SecureBytes(repeating: 0, count: 10) let copy = base @@ -200,7 +196,6 @@ final class SecureBytesTests: XCTestCase { XCTAssertEqual(Array(copy), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) } - @available(macOS 26, iOS 26, tvOS 26, watchOS 26, visionOS 26, *) func testDataCausesCoWs() { var base = SecureBytes(repeating: 0, count: 10) let copy = Data(base) @@ -212,7 +207,6 @@ final class SecureBytesTests: XCTestCase { XCTAssertEqual(copy.count, 10) } - @available(macOS 26, iOS 26, tvOS 26, watchOS 26, visionOS 26, *) func testDataFromSlice() { var base = SecureBytes(0..<10) let copy = Data(base.prefix(5)) @@ -225,4 +219,4 @@ final class SecureBytesTests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Signatures/ECDSA/ECDSASignatureTests.swift b/Tests/CryptoTests/Signatures/ECDSA/ECDSASignatureTests.swift index fc72e4c5..4f8d1cda 100644 --- a/Tests/CryptoTests/Signatures/ECDSA/ECDSASignatureTests.swift +++ b/Tests/CryptoTests/Signatures/ECDSA/ECDSASignatureTests.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + #if canImport(FoundationEssentials) import FoundationEssentials #else @@ -18,14 +19,10 @@ import Foundation #endif import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif struct ECDSATestGroup: Codable { let tests: [SignatureTestVector] @@ -390,4 +387,4 @@ class SignatureTests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Signatures/ECDSA/RawECDSASignaturesTests.swift b/Tests/CryptoTests/Signatures/ECDSA/RawECDSASignaturesTests.swift index a085be4e..1947978f 100644 --- a/Tests/CryptoTests/Signatures/ECDSA/RawECDSASignaturesTests.swift +++ b/Tests/CryptoTests/Signatures/ECDSA/RawECDSASignaturesTests.swift @@ -11,16 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif typealias TestVector = (publicKey: Data, privateKey: Data, rs: rAndS) typealias rAndS = (r: Data, s: Data) @@ -86,4 +83,4 @@ class RawECDSASignaturesTests: XCTestCase { try orFail { try testForCurve(curve: P521.Signing.self) } } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Signatures/EdDSA/Ed25519-Runner.swift b/Tests/CryptoTests/Signatures/EdDSA/Ed25519-Runner.swift index c025e04c..64c36cbe 100644 --- a/Tests/CryptoTests/Signatures/EdDSA/Ed25519-Runner.swift +++ b/Tests/CryptoTests/Signatures/EdDSA/Ed25519-Runner.swift @@ -11,15 +11,14 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import Crypto -#elseif !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import CryptoKit +import XCTest +#if canImport(CryptoKit) +// Skip tests that require @testable imports of CryptoKit. #else -import Crypto -#endif +@testable import Crypto struct Ed25519TestGroup: Codable { let tests: [Ed25519TestVector] @@ -57,9 +56,7 @@ class Ed25519Tests: XCTestCase { let signatureOnContiguous = try orFail { try privateKey.signature(for: someContiguousData) } let signatureOnDiscontiguous = try orFail { try privateKey.signature(for: someDiscontiguousData) } - #if !canImport(Darwin) XCTAssertEqual(signatureOnContiguous, signatureOnDiscontiguous) - #endif // This tests the 4 combinations. let (contiguousSignature, discontiguousSignature) = Array(signatureOnContiguous).asDataProtocols() @@ -140,3 +137,5 @@ class Ed25519Tests: XCTestCase { } } } + +#endif diff --git a/Tests/CryptoTests/Signatures/MLDSA/MLDSAKeyGenTests.swift b/Tests/CryptoTests/Signatures/MLDSA/MLDSAKeyGenTests.swift index aa7dd912..a14aa4bd 100644 --- a/Tests/CryptoTests/Signatures/MLDSA/MLDSAKeyGenTests.swift +++ b/Tests/CryptoTests/Signatures/MLDSA/MLDSAKeyGenTests.swift @@ -11,24 +11,16 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) final class MLDSAKeyGenTests: XCTestCase { func processKeyGenKATFile(filename: String) throws -> [MLDSAKeyGenKAT] { - #if CRYPTO_IN_SWIFTPM let bundle = Bundle.module - #else - let bundle = Bundle(for: type(of: self)) - #endif let fileURL = bundle.url(forResource: filename, withExtension: "json") let json = try Data(contentsOf: fileURL!) let stringInput = String(data: json, encoding: .ascii)! @@ -98,4 +90,4 @@ struct MLDSAKeyGenKAT { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Signatures/MLDSA/MLDSATests.swift b/Tests/CryptoTests/Signatures/MLDSA/MLDSATests.swift index 4ad07eec..c6a3bd52 100644 --- a/Tests/CryptoTests/Signatures/MLDSA/MLDSATests.swift +++ b/Tests/CryptoTests/Signatures/MLDSA/MLDSATests.swift @@ -11,17 +11,13 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) // Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -@testable import CryptoKit -#else @testable import Crypto -#endif -@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *) final class MLDSATests: XCTestCase { func testMLDSA65() throws { let privateKey = try MLDSA65.PrivateKey() @@ -82,4 +78,4 @@ final class MLDSATests: XCTestCase { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/SymmetricKeyTests/SymmetricKeyTests.swift b/Tests/CryptoTests/SymmetricKeyTests/SymmetricKeyTests.swift index 1cba792d..6db318e5 100644 --- a/Tests/CryptoTests/SymmetricKeyTests/SymmetricKeyTests.swift +++ b/Tests/CryptoTests/SymmetricKeyTests/SymmetricKeyTests.swift @@ -11,20 +11,15 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest - -#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -// Skip tests that use @available annotations on tests unless running on Linux. +#if canImport(CryptoKit) +// Skip tests that require @testable imports of CryptoKit. #else -#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API -import CryptoKit -#else -import Crypto -#endif +@testable import Crypto class SymmetricKeyTests: XCTestCase { - @available(macOS 27, iOS 27, watchOS 27, tvOS 27, visionOS 27, *) func testBytesZeroing() throws { var myData: [UInt8] = [1, 2, 3, 4, 5, 6, 7, 8] @@ -52,4 +47,4 @@ extension RawSpan { } } } -#endif // CRYPTO_IN_SWIFTPM +#endif diff --git a/Tests/CryptoTests/Utils/Boring/CTRDRBG.swift b/Tests/CryptoTests/Utils/Boring/CTRDRBG.swift index aff625d8..8d7cfee1 100644 --- a/Tests/CryptoTests/Utils/Boring/CTRDRBG.swift +++ b/Tests/CryptoTests/Utils/Boring/CTRDRBG.swift @@ -11,7 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) +@_exported import CryptoKit +#else #if canImport(FoundationEssentials) import FoundationEssentials #else @@ -73,4 +75,4 @@ extension SymmetricKey { } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Utils/Boring/SequenceDRBG.swift b/Tests/CryptoTests/Utils/Boring/SequenceDRBG.swift index 89c32f62..98e4df9d 100644 --- a/Tests/CryptoTests/Utils/Boring/SequenceDRBG.swift +++ b/Tests/CryptoTests/Utils/Boring/SequenceDRBG.swift @@ -11,7 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTO_IN_SWIFTPM && CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#if canImport(CryptoKit) +@_exported import CryptoKit +#else #if canImport(FoundationEssentials) import FoundationEssentials #else @@ -31,4 +33,4 @@ final class SequenceDrbg { var detRngPtr: Self { self } } -#endif // CRYPTO_IN_SWIFTPM +#endif // canImport(CryptoKit) diff --git a/Tests/CryptoTests/Utils/PrettyBytes.swift b/Tests/CryptoTests/Utils/PrettyBytes.swift index 4de60f02..97e8ba33 100644 --- a/Tests/CryptoTests/Utils/PrettyBytes.swift +++ b/Tests/CryptoTests/Utils/PrettyBytes.swift @@ -11,16 +11,12 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION -import SwiftSystem -#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION -#else + #if canImport(FoundationEssentials) import FoundationEssentials #else import Foundation #endif -#endif enum ByteHexEncodingErrors: Error { case incorrectHexValue @@ -46,11 +42,6 @@ private func htoi(_ value: UInt8) throws -> UInt8 { } #if !hasFeature(Embedded) -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension DataProtocol { var hexString: String { let hexLen = self.count * 2 @@ -70,18 +61,12 @@ extension DataProtocol { } #endif // !hasFeature(Embedded) -#if !CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *) -#else // CRYPTOKIT_STATIC_LIBRARY -@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *) -#endif extension RangeReplaceableCollection where Element == UInt8 { mutating func appendByte(_ byte: UInt64) { withUnsafeBytes(of: byte.littleEndian, { self.append(contentsOf: $0) }) } } -#if !CRYPTOKIT_NO_ACCESS_TO_FOUNDATION && !CRYPTOKIT_NO_IMPORT_FOUNDATION extension Data { init(hexString: String) throws { self.init() @@ -120,4 +105,3 @@ extension Array where Element == UInt8 { } } -#endif diff --git a/Tests/CryptoTests/Utils/RFCVector.swift b/Tests/CryptoTests/Utils/RFCVector.swift index 363a083e..6bbe4aaf 100644 --- a/Tests/CryptoTests/Utils/RFCVector.swift +++ b/Tests/CryptoTests/Utils/RFCVector.swift @@ -11,7 +11,12 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + +#if canImport(FoundationEssentials) +import FoundationEssentials +#else import Foundation +#endif /// This is a not-very-performant decoder of the RFC formatted test vectors. /// @@ -25,10 +30,6 @@ struct RFCVectorDecoder { private var index: Int? init(bundleType: AnyObject, fileName: String) throws { - #if !CRYPTO_IN_SWIFTPM - let bundle = Bundle(for: type(of: bundleType)) - let fileURL = bundle.url(forResource: fileName, withExtension: "txt") - #else var fileURL: URL? = URL(fileURLWithPath: "\(#filePath)") for _ in 0..<3 { fileURL!.deleteLastPathComponent() @@ -45,7 +46,6 @@ struct RFCVectorDecoder { fileURL! = fileURL!.appendingPathComponent("Test Vectors", isDirectory: true) fileURL! = fileURL!.appendingPathComponent("\(fileName).txt", isDirectory: false) #endif - #endif let rfcVectorData = try Data(contentsOf: fileURL!) self.rfcVectorData = String(decoding: rfcVectorData, as: Unicode.UTF8.self) @@ -284,14 +284,14 @@ extension ArraySlice where Element == Substring { // or begin with whitespace (these are used in some cases and we don't want them). This is a brute // force comparison, but it's test code, don't worry about it. let elements: [(String, String)] = self[.. 0 && + return (string.trimmingWhitespace.count > 0 && !string.hasPrefix("#") && !string.hasPrefix("[") && !string.first!.isWhitespace) }.map { string in let split = string.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false) assert(split.count == 2) - return (String(split.first!.trimmingCharacters(in: .whitespaces)), String(split.last!).trimmingCharacters(in: .whitespaces)) + return (String(split.first!.trimmingWhitespace), String(split.last!).trimmingWhitespace) } // Slice off the section we've parsed. @@ -306,3 +306,16 @@ extension ArraySlice where Element == Substring { } } } + +fileprivate extension StringProtocol { + /// Returns the string with leading and trailing whitespace removed. + var trimmingWhitespace: String { self.trimming { $0.isWhitespace } } + + /// Returns a new string by removing leading and trailing characters + /// that satisfy the given predicate. + func trimming(while predicate: (Character) -> Bool) -> String { + guard let start = self.firstIndex(where: { !predicate($0) }) else { return "" } + let end = self.lastIndex(where: { !predicate($0) })! + return String(self[start...end]) + } +} diff --git a/Tests/CryptoTests/Utils/SplitData.swift b/Tests/CryptoTests/Utils/SplitData.swift index 37283d85..d4e72f5e 100644 --- a/Tests/CryptoTests/Utils/SplitData.swift +++ b/Tests/CryptoTests/Utils/SplitData.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + #if canImport(FoundationEssentials) import FoundationEssentials #else diff --git a/Tests/CryptoTests/Utils/Wycheproof.swift b/Tests/CryptoTests/Utils/Wycheproof.swift index fc381339..a4bb46cd 100644 --- a/Tests/CryptoTests/Utils/Wycheproof.swift +++ b/Tests/CryptoTests/Utils/Wycheproof.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest struct WycheproofTest: Codable { @@ -21,10 +22,6 @@ struct WycheproofTest: Codable { extension XCTestCase { func wycheproofTest(bundleType: AnyObject, jsonName: String, file: StaticString = #filePath, line: UInt = #line, testFunction: (T) throws -> Void) throws { - #if !CRYPTO_IN_SWIFTPM - let bundle = Bundle(for: type(of: bundleType)) - let fileURL = bundle.url(forResource: jsonName, withExtension: "json") - #else var fileURL = URL(fileURLWithPath: "\(#filePath)") for _ in 0..<3 { fileURL.deleteLastPathComponent() @@ -41,7 +38,6 @@ extension XCTestCase { fileURL = fileURL.appendingPathComponent("Test Vectors", isDirectory: true) fileURL = fileURL.appendingPathComponent("\(jsonName).json", isDirectory: false) #endif - #endif let data = try orFail(file: file, line: line) { try Data(contentsOf: unwrap(fileURL, file: file, line: line)) } diff --git a/Tests/CryptoTests/Utils/XCTestUtils.swift b/Tests/CryptoTests/Utils/XCTestUtils.swift index a7a09ef1..79922111 100644 --- a/Tests/CryptoTests/Utils/XCTestUtils.swift +++ b/Tests/CryptoTests/Utils/XCTestUtils.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// + import XCTest // Xcode 11.4 catches errors thrown during tests and reports them on the