[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
This commit is contained in:
Si Beaumont authored and GitHub committed 2026-06-23 15:21:37 +01:00
1 parent 0a26a9e309
commit 8ee9962f37
143 files changed
+623 -3927

No files matched your search

+22 -54
View File
@@ -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,
+11 -7
View File
@@ -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.
+4 -35
View File
@@ -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)
@@ -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)
@@ -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)
@@ -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)
+2 -11
View File
@@ -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
+3 -29
View File
@@ -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<UInt8>
@@ -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)
-199
View File
@@ -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<UInt8>
% 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<D: DataProtocol>(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<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.bytes.withUnsafeBytes(body)
}
#else
public func withUnsafeBytes<R, E: Error>(_ 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<UInt8>.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
+3 -7
View File
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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<UInt8> {
if string.hasPointerRepresentation {
@@ -291,4 +287,4 @@ extension ASN1.ASN1BMPString: ExpressibleByStringLiteral {
#endif
#endif // Linux or !SwiftPM
#endif // canImport(CryptoKit)
@@ -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)
@@ -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)
@@ -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)
+3 -11
View File
@@ -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)
+3 -11
View File
@@ -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)
+3 -7
View File
@@ -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)
+3 -11
View File
@@ -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)
+3 -11
View File
@@ -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)
-5
View File
@@ -115,11 +115,6 @@ add_library(Crypto
target_compile_definitions(Crypto PRIVATE
"$<$<COMPILE_LANGUAGE:Swift>:CRYPTO_IN_SWIFTPM>")
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
target_compile_definitions(Crypto PRIVATE
"$<$<COMPILE_LANGUAGE:Swift>:CRYPTO_IN_SWIFTPM_FORCE_BUILD_API>")
endif()
target_include_directories(Crypto PRIVATE
$<TARGET_PROPERTY:CCryptoBoringSSL,INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:CCryptoBoringSSLShims,INCLUDE_DIRECTORIES>
+6 -24
View File
@@ -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 }
@@ -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<H: BoringSSLBackedHashFunction> {
withUnsafeMutablePointer(to: &self.context) { $0.zeroize() }
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#endif // canImport(CryptoKit)
+4 -43
View File
@@ -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<UInt8>.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)
+3 -40
View File
@@ -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)
-250
View File
@@ -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<R>(_ 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<R, E: Error>(_ 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<UInt8> {
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
/// <doc://com.apple.documentation/documentation/swift/hashable> 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<R>(_ 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<R, E: Error>(_ 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<UInt8> {
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
/// <doc://com.apple.documentation/documentation/swift/hashable> 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
+3 -46
View File
@@ -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)
@@ -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)
@@ -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)
@@ -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<H: XKCPBackedHashFunction> {
withUnsafeMutablePointer(to: &self.context) { $0.zeroize() }
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#endif // canImport(CryptoKit)
@@ -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)
@@ -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)
+4 -15
View File
@@ -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)
@@ -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)
@@ -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<Info: DataProtocol>(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<Info: DataProtocol>(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)
@@ -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)
@@ -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<DHPK: HPKEDiffieHellmanPublicKey>: 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<DHSK: HPKEDiffieHellmanPrivateKey>: KEMPrivateKey {
let kem: HPKE.KEM
let key: DHSK
@@ -232,4 +168,4 @@ extension HPKE {
}
}
#endif // Linux or !SwiftPM
#endif // canImport(CryptoKit)
@@ -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)
@@ -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)
@@ -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)
+4 -11
View File
@@ -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)
+4 -32
View File
@@ -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<PK: HPKEKEMPublicKey>(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<SK: HPKEKEMPrivateKey>(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)
@@ -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<PublicKey: HPKEKEMPublicKey>(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<PrivateKey: HPKEKEMPrivateKey>(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)
@@ -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)
+3 -13
View File
@@ -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)
+4 -11
View File
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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<Parameters: BoringSSLBackedMLKEMParameters>: B
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#endif // canImport(CryptoKit)
@@ -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)
+4 -11
View File
@@ -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)
+4 -26
View File
@@ -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)
+5 -14
View File
@@ -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)
-205
View File
@@ -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<D: DataProtocol>(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<D: DataProtocol>(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<D: DataProtocol>(_ 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<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
guard integrityCheckedRepresentation.count == MLKEMPrivateKeyImpl<${NAME}>.seedSize + 32 else {
throw KEM.Errors.invalidSeed
}
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<MLKEMPrivateKeyImpl<${NAME}>.seedSize)
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: MLKEMPrivateKeyImpl<${NAME}>.seedSize..<integrityCheckedRepresentation.count)
let publicKeyHash = SHA3_256Digest(copying: publicKeyHashData.bytes)
self.impl = try MLKEMPrivateKeyImpl<${NAME}>(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<D: DataProtocol>(_ 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
+5 -41
View File
@@ -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)
@@ -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)
+3 -41
View File
@@ -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<SS:ContiguousBytes>(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)
+9 -433
View File
@@ -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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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<Bytes: RandomAccessCollection>(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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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<Bytes: RandomAccessCollection>(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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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<Bytes: RandomAccessCollection>(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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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<Bytes: RandomAccessCollection>(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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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<Bytes: RandomAccessCollection>(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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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<Bytes: RandomAccessCollection>(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)
-385
View File
@@ -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<D: ContiguousBytes>(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<Bytes: ContiguousBytes>(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<Bytes: ContiguousBytes>(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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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<Bytes: ContiguousBytes>(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<Bytes: ContiguousBytes>(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<Bytes: RandomAccessCollection>(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
+2 -6
View File
@@ -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<H: HashFunction>: Sendable {
public static func deriveKey<Info: DataProtocol>(inputKeyMaterial: SymmetricKey, info: Info, outputByteCount: Int) -> SymmetricKey {
+3 -58
View File
@@ -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<H: HashFunction>: 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<H: HashFunction>: 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<H: HashFunction>: 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<Salt: DataProtocol, Info: DataProtocol>(
inputKeyMaterial: SymmetricKey,
salt: Salt,
@@ -139,11 +120,6 @@ public struct HKDF<H: HashFunction>: 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<Info: DataProtocol>(inputKeyMaterial: SymmetricKey,
info: Info,
outputByteCount: Int) -> SymmetricKey {
@@ -160,11 +136,6 @@ public struct HKDF<H: HashFunction>: 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<Salt: DataProtocol>(inputKeyMaterial: SymmetricKey,
salt: Salt,
outputByteCount: Int) -> SymmetricKey {
@@ -180,11 +151,6 @@ public struct HKDF<H: HashFunction>: 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<H: HashFunction>: 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<Salt: DataProtocol>(inputKeyMaterial: SymmetricKey, salt: Salt?) -> HashedAuthenticationCode<H> {
let key: SymmetricKey
if let salt {
@@ -239,11 +200,6 @@ public struct HKDF<H: HashFunction>: 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<H> {
let key = if let salt {
SymmetricKey(bytes: salt)
@@ -267,11 +223,6 @@ public struct HKDF<H: HashFunction>: 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<PRK: ContiguousBytes, Info: DataProtocol>(pseudoRandomKey prk: PRK, info: Info?, outputByteCount: Int) -> SymmetricKey {
SymmetricKey(capacity: outputByteCount) { output in
prk.withUnsafeBytes { prkBuffer in
@@ -307,11 +258,6 @@ public struct HKDF<H: HashFunction>: 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<H: HashFunction>: 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<H: HashFunction>: Sendable {
}
}
}
#endif // Linux or !SwiftPM
#endif // canImport(CryptoKit)
+4 -15
View File
@@ -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: DataProtocol>(_ wrappedKey: WrappedKey, using kek: SymmetricKey) throws -> SymmetricKey {
return try AESWRAPImpl.unwrap(key: kek, wrappedKey: wrappedKey)
}
}
}
#endif // Linux or !SwiftPM
#endif // canImport(CryptoKit)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
+4 -7
View File
@@ -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)
+4 -17
View File
@@ -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)
+6 -26
View File
@@ -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 <Bytes: ContiguousBytes>(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)
+4 -27
View File
@@ -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)
@@ -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<E: Error>(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)
@@ -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<H: HashFunction>: 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<H: HashFunction>: 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<H: HashFunction>: 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<C: ContiguousBytes, D: DataProtocol>(_ authenticationCode: C,
authenticating authenticatedData: D,
using key: SymmetricKey) -> Bool {
@@ -190,11 +166,6 @@ public struct HMAC<H: HashFunction>: 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<H: HashFunction>: 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<H: HashFunction>: MessageAuthenticationCode, Sendable {
let digest: H.Digest
@@ -283,4 +249,4 @@ public struct HashedAuthenticationCode<H: HashFunction>: MessageAuthenticationCo
}
#endif
}
#endif // Linux or !SwiftPM
#endif // canImport(CryptoKit)
@@ -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)
@@ -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)
+4 -12
View File
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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<Parameters: BoringSSLBackedMLDSAParameters> {
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#endif // canImport(CryptoKit)
+3 -50
View File
@@ -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<D: Digest>(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<D: Digest>(_ 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<D: Digest>(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<D: Digest>(_ 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<D: Digest>(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<D: Digest>(_ 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)
-234
View File
@@ -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<D: DataProtocol>(rawRepresentation: D) throws(CryptoKitMetaError)
init<D: DataProtocol>(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<D: DataProtocol>(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<D: DataProtocol>(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<ArraySlice<UInt8>>(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<R, E: Error>(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R {
try self.rawRepresentation.withUnsafeBytes(body)
}
#else
public func withUnsafeBytes<R>(_ 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<D: Digest>(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<D: DataProtocol>(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<D: Digest>(_ 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<D: DataProtocol>(_ signature: ${CURVE}.Signing.ECDSASignature, for data: D) -> Bool {
return self.isValidSignature(signature, for: ${HF}.hash(data: data))
}
}
% end
#endif // Linux or !SwiftPM
+3 -20
View File
@@ -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<D: Digest>(_ 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<D: DataProtocol>(_ 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<S: DataProtocol, D: DataProtocol>(_ 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<D: DataProtocol>(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)
+5 -12
View File
@@ -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)
-197
View File
@@ -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<D: DataProtocol>(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<S: DataProtocol, D: DataProtocol>(_ 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<S: DataProtocol, D: DataProtocol, C: DataProtocol>(_ 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<D: DataProtocol>(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<D: DataProtocol>(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<D: DataProtocol, C: DataProtocol>(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<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
let seedSize = MLDSAPrivateKeyImpl<${NAME}>.seedSize
guard integrityCheckedRepresentation.count == seedSize + 32 else {
throw CryptoKitError.incorrectParameterSize
}
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<seedSize)
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: seedSize..<integrityCheckedRepresentation.count)
let publicKeyHash = SHA3_256Digest(copying: publicKeyHashData.bytes)
self.impl = try MLDSAPrivateKeyImpl<${NAME}>(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
+3 -10
View File
@@ -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<D: Digest>(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<D: DataProtocol>(for data: D) throws(CryptoKitMetaError) -> Signature
}
#endif // Linux or !SwiftPM
#endif // canImport(CryptoKit)
+5 -5
View File
@@ -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
@@ -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)
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#if canImport(CryptoKit)
@_exported import CryptoKit
#else
#if canImport(FoundationEssentials)
@@ -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)
+2 -27
View File
@@ -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<D: DataProtocol>(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
+1 -17
View File
@@ -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
Loaded 100 of 143 files, more files were not shown because too many files have changed in this diff. Show more