mirror of
https://github.com/apple/swift-crypto
synced 2026-11-23 04:34:21 +00:00
Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da9d28d69e | ||
|
|
f79a3ffbc0 | ||
|
|
fa2832e44b | ||
|
|
315a799ccc | ||
|
|
3ed653865a | ||
|
|
47d3869a72 | ||
|
|
c3d9fc8938 | ||
|
|
1ca42a1885 |
No files matched your search
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"name": "swift-crypto",
|
||||
"image": "swift:latest",
|
||||
"features": {
|
||||
"ghcr.io/devcontainers/features/common-utils:2": {
|
||||
"installZsh": "false",
|
||||
"username": "vscode",
|
||||
"upgradePackages": "false"
|
||||
},
|
||||
"ghcr.io/devcontainers/features/git:1": {
|
||||
"version": "latest"
|
||||
}
|
||||
},
|
||||
"capAdd": ["CAP_SYS_PTRACE"],
|
||||
"securityOpt": ["seccomp=unconfined"],
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"settings": {
|
||||
"swift.path": "/usr/bin",
|
||||
"lldb.library": "/usr/lib/liblldb.so"
|
||||
},
|
||||
"extensions": [
|
||||
"swiftlang.swift-vscode"
|
||||
]
|
||||
}
|
||||
},
|
||||
"remoteUser": "vscode"
|
||||
}
|
||||
@@ -55,4 +55,3 @@ jobs:
|
||||
with:
|
||||
runner_pool: nightly
|
||||
build_scheme: swift-crypto-Package
|
||||
swift_6_1_enabled: false
|
||||
@@ -67,7 +67,6 @@ jobs:
|
||||
{ "name": "CCryptoBoringSSL", "type": "assembly", "exceptions": [ "*/AES/*.swift" ] }
|
||||
]
|
||||
}
|
||||
image: swift:6.2
|
||||
|
||||
macos-tests:
|
||||
name: macOS tests
|
||||
@@ -75,7 +74,6 @@ jobs:
|
||||
with:
|
||||
runner_pool: general
|
||||
build_scheme: swift-crypto-Package
|
||||
swift_6_1_enabled: false
|
||||
|
||||
static-sdk:
|
||||
name: Static SDK
|
||||
|
||||
@@ -65,8 +65,6 @@ Sources/Crypto/Signatures/ECDSA.swift
|
||||
Sources/Crypto/Signatures/Ed25519.swift
|
||||
Sources/Crypto/Signatures/MLDSA.swift
|
||||
Sources/Crypto/Signatures/Signature.swift
|
||||
Sources/Crypto/Util/ArraySpanHelpers.swift
|
||||
Sources/Crypto/Util/Data+ArraySpan.swift
|
||||
Sources/Crypto/Util/PrettyBytes.swift
|
||||
Sources/Crypto/Util/SafeCompare.swift
|
||||
Sources/Crypto/Util/SecureBytes.swift
|
||||
@@ -138,7 +136,6 @@ Tests/CryptoTests/Signatures/ECDSA/RawECDSASignaturesTests.swift
|
||||
Tests/CryptoTests/Signatures/EdDSA/Ed25519-Runner.swift
|
||||
Tests/CryptoTests/Signatures/MLDSA/MLDSAKeyGenTests.swift
|
||||
Tests/CryptoTests/Signatures/MLDSA/MLDSATests.swift
|
||||
Tests/CryptoTests/SymmetricKeyTests/SymmetricKeyTests.swift
|
||||
Tests/CryptoTests/Utils/PrettyBytes.swift
|
||||
Tests/CryptoTests/Utils/RFCVector.swift
|
||||
Tests/CryptoTests/Utils/SplitData.swift
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:6.2
|
||||
// swift-tools-version:6.1
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
|
||||
@@ -37,7 +37,6 @@ set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
|
||||
|
||||
set(SWIFT_CRYPTO_COMPILE_OPTIONS
|
||||
-package-name com.apple.swift-crypto
|
||||
-enable-experimental-feature Lifetimes
|
||||
)
|
||||
|
||||
option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
|
||||
|
||||
+29
-9
@@ -5,6 +5,18 @@ your contribution to Apple and the community, and agree by submitting the patch
|
||||
that your contributions are licensed under the Apache 2.0 license (see
|
||||
`LICENSE.txt`).
|
||||
|
||||
## AI tools
|
||||
|
||||
Human discourse is an essential part of open source development. To encourage
|
||||
productive collaboration between contributors and maintainers, please refrain
|
||||
from using AI tools for the following:
|
||||
|
||||
- Issues: titles, bodies, and comments.
|
||||
- Pull requests: titles, descriptions, and comments.
|
||||
|
||||
Contributors who feel more comfortable writing in another language may use
|
||||
automated translation tools, but must include their original content in their
|
||||
preferred language.
|
||||
|
||||
## How to submit a bug report
|
||||
|
||||
@@ -44,19 +56,27 @@ $ uname -a
|
||||
Linux beefy.machine 4.4.0-101-generic #124-Ubuntu SMP Fri Nov 10 18:29:59 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
|
||||
```
|
||||
|
||||
## Writing a Patch
|
||||
## Contributing a pull request
|
||||
|
||||
A good swift-crypto patch is:
|
||||
> [!IMPORTANT]
|
||||
> To ensure productive use of contributor and maintainer time and resources,
|
||||
> contributors must first agree their proposed change with a maintainer in an
|
||||
> issue. Pull requests opened without prior discussion may be closed without
|
||||
> review.
|
||||
|
||||
1. Concise, and contains as few changes as needed to achieve the end result.
|
||||
2. Tested, ensuring that any tests provided failed before the patch and pass after it.
|
||||
3. Documented, adding API documentation as needed to cover new functions and properties.
|
||||
4. Accompanied by a great commit message, using our commit message template.
|
||||
If there is no issue already tracking the problem or feature request, please
|
||||
file a new one using your own voice (see "AI tools" usage above).
|
||||
|
||||
Once an issue is assigned to you, follow these steps:
|
||||
|
||||
## How to contribute your work
|
||||
|
||||
Please open a pull request at https://github.com/apple/swift-crypto. Make sure the CI passes, and then wait for code review.
|
||||
- Prepare your change, keeping in mind that a good patch is:
|
||||
- Concise, and contains as few changes as needed to achieve the end result.
|
||||
- Tested, ensuring that any tests provided failed before the patch and pass
|
||||
after it.
|
||||
- Documented, adding API documentation as needed to cover new functions and
|
||||
properties.
|
||||
- Accompanied by a great commit message.
|
||||
- Open a pull request and wait for code review by the maintainers.
|
||||
|
||||
### How to update CMakeLists files
|
||||
|
||||
|
||||
+2
-5
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:6.2
|
||||
// swift-tools-version:6.1
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
@@ -44,7 +44,6 @@ if development || isFreeBSD {
|
||||
swiftSettings = [
|
||||
.define("CRYPTO_IN_SWIFTPM"),
|
||||
.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"),
|
||||
.enableExperimentalFeature("Lifetimes"),
|
||||
]
|
||||
dependencies = [
|
||||
"CCryptoBoringSSL",
|
||||
@@ -64,7 +63,6 @@ if development || isFreeBSD {
|
||||
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)),
|
||||
@@ -211,8 +209,7 @@ let package = Package(
|
||||
exclude: privacyManifestExclude + [
|
||||
"CMakeLists.txt"
|
||||
],
|
||||
resources: privacyManifestResource,
|
||||
swiftSettings: swiftSettings
|
||||
resources: privacyManifestResource
|
||||
),
|
||||
.executableTarget(name: "crypto-shasum", dependencies: ["Crypto"]),
|
||||
.testTarget(
|
||||
|
||||
@@ -31,10 +31,11 @@ When building Swift Crypto for use on an Apple platform where CryptoKit is alrea
|
||||
When building Swift Crypto for use on Linux or Windows, 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.
|
||||
3. The backing implementation of this common API, which calls into BoringSSL.
|
||||
2. A vendored copy of XKCP's libXKCP.
|
||||
3. The common API of Swift Crypto and CryptoKit.
|
||||
4. The backing implementation of this common API, which calls into BoringSSL or XKCP.
|
||||
|
||||
The API code, and some cryptographic primitives which are directly implemented in Swift, are exactly the same for both Apple CryptoKit and Swift Crypto. The backing BoringSSL-based implementation is unique to Swift Crypto. In addition, there is another product, `CryptoExtras`, which provides additional functionality that is not offered in CryptoKit, which contains cryptographic APIS predominantly useful in the server ecosystem. **Note**: if you depend on CryptoExtras you'll bundle the BoringSSL implementation of the library in your application, no matter the platform.
|
||||
The API code, and some cryptographic primitives which are directly implemented in Swift, are exactly the same for both Apple CryptoKit and Swift Crypto. The BoringSSL/XKCP backing implementation is unique to Swift Crypto. In addition, there is another product, `CryptoExtras`, which provides additional functionality that is not offered in CryptoKit, which contains cryptographic APIs predominantly useful in the server ecosystem. **Note**: if you depend on CryptoExtras you'll bundle the BoringSSL/XKCP implementation of the library in your application, no matter the platform.
|
||||
|
||||
## Evolution
|
||||
|
||||
@@ -48,11 +49,11 @@ Note that Swift Crypto does not intend to support all possible cryptographic pri
|
||||
|
||||
### Code Organisation
|
||||
|
||||
Files in this repository are divided into two groups, based on whether they have a name that ends in `_boring` or are in a `BoringSSL` directory, or if they are not.
|
||||
Files in this repository are divided into two groups, based on whether they have a name that ends in `_boring`/`_xkcp` or are in a `BoringSSL`/`XKCP` directory, or if they are not.
|
||||
|
||||
Files that meet the above criteria are specific to the Swift Crypto implementation. Changes to these files can be made fairly easily, so long as they meet the criteria below. If your file needs to `import CCryptoBoringSSL` or access a BoringSSL API, it needs to be marked this way.
|
||||
Files that meet the above criteria are specific to the Swift Crypto implementation. Changes to these files can be made fairly easily, so long as they meet the criteria below. If your file needs to `import CCryptoBoringSSL` or `import CXKCP` or access a BoringSSL or XKCP API, it needs to be marked this way.
|
||||
|
||||
Files that do not have the `_boring` suffix are part of the public API of CryptoKit. Changing these requires passing a higher bar, as any change in these files must be accompanied by a change in CryptoKit itself.
|
||||
Files that do not have the `_boring` or `_xkcp` suffix are part of the public API of CryptoKit. Changing these requires passing a higher bar, as any change in these files must be accompanied by a change in CryptoKit itself.
|
||||
|
||||
## Contributing
|
||||
|
||||
@@ -146,3 +147,7 @@ To do so, please use the following dependency in your `Package.swift`:
|
||||
|
||||
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.
|
||||
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
This project contains source code from the BoringSSL and XKCP projects.
|
||||
@@ -14,20 +14,16 @@
|
||||
#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
|
||||
#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 AESGCMImpl = CoreCryptoGCMImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@@ -36,23 +32,15 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension AES {
|
||||
/// The Advanced Encryption Standard (AES) Galois Counter Mode (GCM) cipher
|
||||
/// suite.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
/// additional data.
|
||||
@@ -83,46 +71,6 @@ extension AES {
|
||||
return try AESGCMImpl.seal(key: key, message: message, nonce: nonce, authenticatedData: Data?.none)
|
||||
}
|
||||
|
||||
/// Secures the given plaintext message with encryption and an optional authentication tag.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - message: The plaintext data to seal, which will be updated in place.
|
||||
/// - key: A cryptographic key used to seal the message.
|
||||
/// - 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
|
||||
public static func seal(
|
||||
inPlace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: Nonce,
|
||||
authenticating authenticatedData: RawSpan? = nil,
|
||||
tag: inout OutputRawSpan
|
||||
) throws(CryptoKitMetaError) {
|
||||
return try AESGCMImpl.seal(
|
||||
key: key, message: &message, nonce: nonce.bytes,
|
||||
authenticatedData: authenticatedData, tag: &tag
|
||||
)
|
||||
}
|
||||
|
||||
// Note: historical version of the above, which should be removed once
|
||||
// the above is API and clients move over to it.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
internal static func seal(
|
||||
inplace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: RawSpan,
|
||||
authenticating authenticatedData: RawSpan? = nil,
|
||||
tag: inout OutputRawSpan
|
||||
) throws(CryptoKitMetaError) {
|
||||
return try AESGCMImpl.seal(key: key, message: &message, nonce: nonce, authenticatedData: authenticatedData, tag: &tag)
|
||||
}
|
||||
|
||||
/// Decrypts the message and verifies the authenticity of both the
|
||||
/// encrypted message and additional data.
|
||||
///
|
||||
@@ -151,67 +99,10 @@ extension AES {
|
||||
public static func open(_ sealedBox: SealedBox, using key: SymmetricKey) throws(CryptoKitMetaError) -> Data {
|
||||
return try AESGCMImpl.open(key: key, sealedBox: sealedBox, authenticatedData: Data?.none)
|
||||
}
|
||||
|
||||
/// Decrypts the message and verifies its authenticity.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - message: The message, which will be decrypted in place.
|
||||
/// - key: The cryptographic key that was used to seal the message.
|
||||
/// - nonce: The nonce used to encrypt the message.
|
||||
/// - tag : The 16-byte authentication tag.
|
||||
/// - 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
|
||||
public static func open(
|
||||
inPlace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: Nonce,
|
||||
authenticating authenticatedData: RawSpan? = nil,
|
||||
tag: RawSpan
|
||||
) throws(CryptoKitMetaError) {
|
||||
try AESGCMImpl
|
||||
.open(
|
||||
key: key,
|
||||
message: &message,
|
||||
nonce: nonce.bytes,
|
||||
authenticatedData: authenticatedData,
|
||||
tag: tag
|
||||
)
|
||||
}
|
||||
|
||||
// Note: historical version of the above, which should be removed once
|
||||
// the above is API and clients move over to it.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
internal static func open(
|
||||
inplace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: RawSpan,
|
||||
tag: RawSpan,
|
||||
authenticating authenticatedData: RawSpan? = nil
|
||||
) throws(CryptoKitMetaError) {
|
||||
try AESGCMImpl
|
||||
.open(
|
||||
key: key,
|
||||
message: &message,
|
||||
nonce: nonce,
|
||||
authenticatedData: authenticatedData,
|
||||
tag: tag
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension AES.GCM {
|
||||
/// A secure container for your data that you can access using a cipher.
|
||||
///
|
||||
@@ -227,11 +118,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SealedBox: AEADSealedBox, Sendable {
|
||||
private let combinedRepresentation: Data
|
||||
private let nonceByteCount: Int
|
||||
@@ -314,10 +201,10 @@ extension AES.GCM {
|
||||
throw error(CryptoKitError.incorrectParameterSize)
|
||||
}
|
||||
|
||||
let nonceByteCount = nonce.storage.count
|
||||
let nonceByteCount = nonce.bytes.count
|
||||
var combinedRepresentation = Data()
|
||||
combinedRepresentation.reserveCapacity(nonceByteCount + ciphertext.count + tag.count)
|
||||
combinedRepresentation.append(contentsOf: nonce.storage)
|
||||
combinedRepresentation.append(contentsOf: nonce.bytes)
|
||||
combinedRepresentation.append(contentsOf: ciphertext)
|
||||
combinedRepresentation.append(contentsOf: tag)
|
||||
|
||||
|
||||
@@ -35,16 +35,17 @@ enum OpenSSLAESGCMImpl {
|
||||
|
||||
let aead = try Self._backingAEAD(key: key)
|
||||
|
||||
let combined: Data
|
||||
let ciphertext: Data
|
||||
let tag: Data
|
||||
if let ad = authenticatedData {
|
||||
combined = try aead.seal(
|
||||
(ciphertext, tag) = try aead.seal(
|
||||
message: message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
authenticatedData: ad
|
||||
)
|
||||
} else {
|
||||
combined = try aead.seal(
|
||||
(ciphertext, tag) = try aead.seal(
|
||||
message: message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
@@ -52,39 +53,7 @@ enum OpenSSLAESGCMImpl {
|
||||
)
|
||||
}
|
||||
|
||||
return AES.GCM.SealedBox(combined: combined, nonceByteCount: nonce.count)
|
||||
}
|
||||
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
@inlinable
|
||||
static func seal(
|
||||
key: SymmetricKey,
|
||||
message: inout MutableRawSpan,
|
||||
nonce: RawSpan,
|
||||
authenticatedData: RawSpan?,
|
||||
tag: inout OutputRawSpan
|
||||
) throws {
|
||||
let aead = try Self._backingAEAD(key: key)
|
||||
|
||||
if let ad = authenticatedData {
|
||||
try aead.seal(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
authenticatedData: ad,
|
||||
tag: &tag
|
||||
)
|
||||
} else {
|
||||
try aead.seal(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
authenticatedData: RawSpan(),
|
||||
tag: &tag
|
||||
)
|
||||
}
|
||||
return try AES.GCM.SealedBox(nonce: nonce, ciphertext: ciphertext, tag: tag)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -114,38 +83,6 @@ enum OpenSSLAESGCMImpl {
|
||||
}
|
||||
}
|
||||
|
||||
/// Open a given message in place.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
@inlinable
|
||||
static func open(
|
||||
key: SymmetricKey,
|
||||
message: inout MutableRawSpan,
|
||||
nonce: RawSpan,
|
||||
authenticatedData: RawSpan?,
|
||||
tag: RawSpan
|
||||
) throws {
|
||||
let aead = try Self._backingAEAD(key: key)
|
||||
if let authenticatedData {
|
||||
return try aead.open(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
tag: tag,
|
||||
authenticatedData: authenticatedData
|
||||
)
|
||||
} else {
|
||||
return try aead.open(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
tag: tag,
|
||||
authenticatedData: RawSpan()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
static func _backingAEAD(key: SymmetricKey) throws -> BoringSSLAEAD {
|
||||
switch key.bitCount {
|
||||
|
||||
@@ -31,7 +31,7 @@ extension BoringSSLAEAD {
|
||||
key: SymmetricKey,
|
||||
nonce: Nonce,
|
||||
authenticatedData: AuthenticatedData
|
||||
) throws -> Data {
|
||||
) throws -> (ciphertext: Data, tag: Data) {
|
||||
do {
|
||||
let context = try AEADContext(cipher: self, key: key)
|
||||
return try context.seal(
|
||||
@@ -44,30 +44,6 @@ extension BoringSSLAEAD {
|
||||
}
|
||||
}
|
||||
|
||||
/// Seal a given message in place
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
func seal(
|
||||
message: inout MutableRawSpan,
|
||||
key: SymmetricKey,
|
||||
nonce: RawSpan,
|
||||
authenticatedData: RawSpan,
|
||||
tag: inout OutputRawSpan
|
||||
) throws {
|
||||
do {
|
||||
let context = try AEADContext(cipher: self, key: key)
|
||||
return try context.seal(
|
||||
message: &message,
|
||||
nonce: nonce,
|
||||
authenticatedData: authenticatedData,
|
||||
tag: &tag,
|
||||
)
|
||||
} catch CryptoBoringWrapperError.underlyingCoreCryptoError(let errorCode) {
|
||||
throw CryptoKitError.underlyingCoreCryptoError(error: errorCode)
|
||||
}
|
||||
}
|
||||
|
||||
/// Open a given message.
|
||||
func open<Nonce: ContiguousBytes, AuthenticatedData: DataProtocol>(
|
||||
ciphertext: Data,
|
||||
@@ -88,30 +64,6 @@ extension BoringSSLAEAD {
|
||||
throw CryptoKitError.underlyingCoreCryptoError(error: errorCode)
|
||||
}
|
||||
}
|
||||
|
||||
/// Open a given message in place.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public func open(
|
||||
message: inout MutableRawSpan,
|
||||
key: SymmetricKey,
|
||||
nonce: RawSpan,
|
||||
tag: RawSpan,
|
||||
authenticatedData: RawSpan
|
||||
) throws {
|
||||
do {
|
||||
let context = try AEADContext(cipher: self, key: key)
|
||||
return try context.open(
|
||||
message: &message,
|
||||
nonce: nonce,
|
||||
tag: tag,
|
||||
authenticatedData: authenticatedData
|
||||
)
|
||||
} catch CryptoBoringWrapperError.underlyingCoreCryptoError(let errorCode) {
|
||||
throw CryptoKitError.underlyingCoreCryptoError(error: errorCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
@@ -127,16 +79,17 @@ enum OpenSSLChaChaPolyImpl {
|
||||
}
|
||||
let nonce = nonce ?? ChaChaPoly.Nonce()
|
||||
|
||||
let combined: Data
|
||||
let ciphertext: Data
|
||||
let tag: Data
|
||||
if let ad = authenticatedData {
|
||||
combined = try BoringSSLAEAD.chacha20.seal(
|
||||
(ciphertext, tag) = try BoringSSLAEAD.chacha20.seal(
|
||||
message: message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
authenticatedData: ad
|
||||
)
|
||||
} else {
|
||||
combined = try BoringSSLAEAD.chacha20.seal(
|
||||
(ciphertext, tag) = try BoringSSLAEAD.chacha20.seal(
|
||||
message: message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
@@ -144,40 +97,7 @@ enum OpenSSLChaChaPolyImpl {
|
||||
)
|
||||
}
|
||||
|
||||
return ChaChaPoly.SealedBox(combined: combined, nonceByteCount: nonce.count)
|
||||
}
|
||||
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
static func encrypt(
|
||||
key: SymmetricKey,
|
||||
inPlace message: inout MutableRawSpan,
|
||||
nonce: RawSpan,
|
||||
authenticatedData: RawSpan?,
|
||||
tag: inout OutputRawSpan
|
||||
) throws {
|
||||
guard key.bitCount == ChaChaPoly.keyBitsCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
|
||||
if let ad = authenticatedData {
|
||||
try BoringSSLAEAD.chacha20.seal(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
authenticatedData: ad,
|
||||
tag: &tag
|
||||
)
|
||||
} else {
|
||||
try BoringSSLAEAD.chacha20.seal(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
authenticatedData: RawSpan(),
|
||||
tag: &tag
|
||||
)
|
||||
}
|
||||
return try ChaChaPoly.SealedBox(nonce: nonce, ciphertext: ciphertext, tag: tag)
|
||||
}
|
||||
|
||||
static func decrypt<AD: DataProtocol>(
|
||||
@@ -207,39 +127,5 @@ enum OpenSSLChaChaPolyImpl {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
static func decrypt(
|
||||
key: SymmetricKey,
|
||||
inPlace message: inout MutableRawSpan,
|
||||
nonce: RawSpan,
|
||||
tag: RawSpan,
|
||||
authenticatedData: RawSpan?
|
||||
) throws {
|
||||
guard key.bitCount == ChaChaPoly.keyBitsCount else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
|
||||
if let authenticatedData {
|
||||
try BoringSSLAEAD.chacha20.open(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
tag: tag,
|
||||
authenticatedData: authenticatedData
|
||||
)
|
||||
} else {
|
||||
try BoringSSLAEAD.chacha20.open(
|
||||
message: &message,
|
||||
key: key,
|
||||
nonce: nonce,
|
||||
tag: tag,
|
||||
authenticatedData: RawSpan()
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@@ -14,20 +14,16 @@
|
||||
#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
|
||||
#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 ChaChaPolyImpl = CoreCryptoChaChaPolyImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@@ -38,11 +34,7 @@ public import Foundation
|
||||
|
||||
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum ChaChaPoly: Cipher, Sendable {
|
||||
static let tagByteCount = 16
|
||||
static let keyBitsCount = 256
|
||||
@@ -78,44 +70,6 @@ public enum ChaChaPoly: Cipher, Sendable {
|
||||
return try ChaChaPolyImpl.encrypt(key: key, message: message, nonce: nonce, authenticatedData: Data?.none)
|
||||
}
|
||||
|
||||
/// Secures the given plaintext message in place with encryption and an
|
||||
/// authentication tag.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - message: The plaintext data to seal.
|
||||
/// - key: A cryptographic key used to seal the message.
|
||||
/// - 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
|
||||
public static func seal(
|
||||
inPlace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: Nonce,
|
||||
authenticating authenticatedData: RawSpan? = nil,
|
||||
tag: inout OutputRawSpan
|
||||
) throws(CryptoKitMetaError) {
|
||||
try ChaChaPolyImpl.encrypt(key: key, inPlace: &message, nonce: nonce.bytes, authenticatedData: authenticatedData, tag: &tag)
|
||||
}
|
||||
|
||||
// Note: historical version of the above, which should be removed once
|
||||
// the above is API and clients move over to it.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
internal static func seal(
|
||||
inplace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: RawSpan,
|
||||
authenticating authenticatedData: RawSpan? = nil,
|
||||
tag: inout OutputRawSpan
|
||||
) throws(CryptoKitMetaError) {
|
||||
try ChaChaPolyImpl.encrypt(key: key, inPlace: &message, nonce: nonce, authenticatedData: authenticatedData, tag: &tag)
|
||||
}
|
||||
|
||||
/// Decrypts the message and verifies the authenticity of both the encrypted
|
||||
/// message and additional data.
|
||||
///
|
||||
@@ -145,53 +99,9 @@ public enum ChaChaPoly: Cipher, Sendable {
|
||||
(_ sealedBox: SealedBox, using key: SymmetricKey) throws -> Data {
|
||||
return try ChaChaPolyImpl.decrypt(key: key, ciphertext: sealedBox, authenticatedData: Data?.none)
|
||||
}
|
||||
|
||||
/// Decrypts the message and verifies the authenticity of both the encrypted
|
||||
/// message and additional data.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - message: The message, which will be decrypted in place.
|
||||
/// - key: The cryptographic key that was used to seal the message.
|
||||
/// - nonce: The nonce used to encrypt the message.
|
||||
/// - tag : The 16-byte authentication tag.
|
||||
/// - 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
|
||||
public static func open(
|
||||
inPlace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: Nonce,
|
||||
authenticating authenticatedData: RawSpan? = nil,
|
||||
tag: RawSpan
|
||||
) throws(CryptoKitMetaError) {
|
||||
try ChaChaPolyImpl.decrypt(key: key, inPlace: &message, nonce: nonce.bytes, tag: tag, authenticatedData: authenticatedData)
|
||||
}
|
||||
|
||||
// Note: historical version of the above, which should be removed once
|
||||
// the above is API and clients move over to it.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public static func open(
|
||||
inplace message: inout MutableRawSpan,
|
||||
using key: SymmetricKey,
|
||||
nonce: RawSpan,
|
||||
tag: RawSpan,
|
||||
authenticating authenticatedData: RawSpan? = nil
|
||||
) throws(CryptoKitMetaError) {
|
||||
try ChaChaPolyImpl.decrypt(key: key, inPlace: &message, nonce: nonce, tag: tag, authenticatedData: authenticatedData)
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ChaChaPoly {
|
||||
/// A secure container for your data that you access using a cipher.
|
||||
///
|
||||
@@ -208,11 +118,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SealedBox: AEADSealedBox, Sendable {
|
||||
/// A combined element composed of the tag, the nonce, and the
|
||||
/// ciphertext.
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -27,6 +26,7 @@ import Foundation
|
||||
#endif
|
||||
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol AEADSealedBox: Sendable {
|
||||
associatedtype Nonce: Sequence
|
||||
/// The authentication tag
|
||||
@@ -46,11 +46,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol Cipher {
|
||||
associatedtype Key
|
||||
associatedtype SealedBox: AEADSealedBox
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -32,67 +31,29 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct Nonce: ContiguousBytes, Sequence, Sendable {
|
||||
typealias Storage = ContiguousArray<UInt8>
|
||||
|
||||
let storage: Storage
|
||||
let bytes: Data
|
||||
|
||||
/// 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:
|
||||
/// - data: A data representation of the nonce.
|
||||
/// The initializer throws an error if the data has a length smaller than 12 bytes.
|
||||
internal init(data: RawSpan) throws(CryptoKitMetaError) {
|
||||
if data.byteCount < AES.GCM.defaultNonceByteCount {
|
||||
throw error(CryptoKitError.incorrectParameterSize)
|
||||
var data = Data(repeating: 0, count: AES.GCM.defaultNonceByteCount)
|
||||
data.withUnsafeMutableBytes {
|
||||
assert($0.count == AES.GCM.defaultNonceByteCount)
|
||||
$0.initializeWithRandomBytes(count: AES.GCM.defaultNonceByteCount)
|
||||
}
|
||||
|
||||
self.storage = Storage(copying: data)
|
||||
self.bytes = 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:
|
||||
/// - 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)
|
||||
}
|
||||
|
||||
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
|
||||
@@ -106,9 +67,9 @@ extension AES.GCM {
|
||||
throw error(CryptoKitError.incorrectParameterSize)
|
||||
}
|
||||
|
||||
self.storage = Storage(data)
|
||||
self.bytes = Data(data)
|
||||
}
|
||||
|
||||
|
||||
/// Calls the given closure with a pointer to the underlying bytes of the array’s
|
||||
/// contiguous storage.
|
||||
///
|
||||
@@ -130,104 +91,39 @@ extension AES.GCM {
|
||||
}
|
||||
#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 {
|
||||
var data = Storage(repeating: 0, count: AES.GCM.defaultNonceByteCount)
|
||||
assert(data.count == AES.GCM.defaultNonceByteCount)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct Nonce: ContiguousBytes, Sequence, Sendable {
|
||||
typealias Storage = [12 of UInt8]
|
||||
|
||||
let storage: Storage
|
||||
let bytes: Data
|
||||
|
||||
/// 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:
|
||||
/// - data: A 12-byte data representation of the nonce.
|
||||
/// The initializer throws an error if the data isn't 12 bytes long.
|
||||
internal init(data: RawSpan) throws(CryptoKitMetaError) {
|
||||
if data.byteCount != ChaChaPoly.nonceByteCount {
|
||||
throw error(CryptoKitError.incorrectParameterSize)
|
||||
var data = Data(repeating: 0, count: ChaChaPoly.nonceByteCount)
|
||||
data.withUnsafeMutableBytes {
|
||||
assert($0.count == ChaChaPoly.nonceByteCount)
|
||||
$0.initializeWithRandomBytes(count: ChaChaPoly.nonceByteCount)
|
||||
}
|
||||
|
||||
self.storage = Storage(copying: data)
|
||||
self.bytes = 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:
|
||||
/// - 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)
|
||||
}
|
||||
|
||||
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
|
||||
@@ -241,9 +137,9 @@ extension ChaChaPoly {
|
||||
throw error(CryptoKitError.incorrectParameterSize)
|
||||
}
|
||||
|
||||
self.storage = Storage(copying: data)
|
||||
self.bytes = Data(data)
|
||||
}
|
||||
|
||||
|
||||
/// Calls the given closure with a pointer to the underlying bytes of the array’s
|
||||
/// contiguous storage.
|
||||
///
|
||||
@@ -265,38 +161,12 @@ extension ChaChaPoly {
|
||||
}
|
||||
#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 {
|
||||
var data = Storage(repeating: 0)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -29,12 +28,12 @@ public import Foundation
|
||||
// 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}]
|
||||
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."}]
|
||||
|
||||
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})
|
||||
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."})
|
||||
}%
|
||||
|
||||
|
||||
@@ -44,78 +43,30 @@ 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
|
||||
let bytes: Data
|
||||
|
||||
/// 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)
|
||||
var data = Data(repeating: 0, count: ${nonceSize})
|
||||
data.withUnsafeMutableBytes {
|
||||
assert($0.count == ${nonceSize})
|
||||
$0.initializeWithRandomBytes(count: ${nonceSize})
|
||||
}
|
||||
|
||||
self.storage = Storage(copying: data)
|
||||
self.bytes = 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
|
||||
@@ -128,13 +79,9 @@ ${dataDescription}
|
||||
throw error(CryptoKitError.incorrectParameterSize)
|
||||
}
|
||||
|
||||
% if fixedLength is None:
|
||||
self.storage = Storage(data)
|
||||
% else:
|
||||
self.storage = Storage(copying: data)
|
||||
% end
|
||||
self.bytes = Data(data)
|
||||
}
|
||||
|
||||
|
||||
/// Calls the given closure with a pointer to the underlying bytes of the array’s
|
||||
/// contiguous storage.
|
||||
///
|
||||
@@ -156,43 +103,12 @@ ${dataDescription}
|
||||
}
|
||||
#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
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -113,15 +112,18 @@ import Foundation
|
||||
// generally: that is, we don't hard-code special knowledge of these formats as part of the parsing process. Instead we have written a
|
||||
// parser that can divide the world of ASN.1 into parseable chunks, and then we try to decode specific formats from those chunks. This
|
||||
// allows us to extend things in the future without too much pain.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal enum ASN1 { }
|
||||
|
||||
// MARK: - Parser Node
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// An `ASN1ParserNode` is a representation of a parsed ASN.1 TLV section. An `ASN1ParserNode` may be primitive, or may be composed of other `ASN1ParserNode`s.
|
||||
/// In our representation, we keep track of this by storing a node "depth", which allows rapid forward and backward scans to hop over sections
|
||||
/// we're uninterested in.
|
||||
///
|
||||
/// This type is not exposed to users of the API: it is only used internally for implementation of the user-level API.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
fileprivate struct ASN1ParserNode {
|
||||
/// The identifier.
|
||||
var identifier: ASN1Identifier
|
||||
@@ -134,9 +136,11 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1ParserNode: Hashable { }
|
||||
|
||||
#if !hasFeature(Embedded)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1ParserNode: CustomStringConvertible {
|
||||
var description: String {
|
||||
return "ASN1.ASN1ParserNode(identifier: \(self.identifier), depth: \(self.depth), dataBytes: \(self.dataBytes?.count ?? 0))"
|
||||
@@ -145,6 +149,7 @@ extension ASN1.ASN1ParserNode: CustomStringConvertible {
|
||||
#endif
|
||||
|
||||
// MARK: - Sequence, SequenceOf, and Set
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// Parse the node as an ASN.1 sequence.
|
||||
internal static func sequence<T>(_ node: ASN1Node, identifier: ASN1.ASN1Identifier, _ builder: (inout ASN1.ASN1NodeCollection.Iterator) throws(CryptoKitMetaError) -> T) throws(CryptoKitMetaError) -> T {
|
||||
@@ -193,6 +198,7 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
// MARK: - Optional explicitly tagged
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// Parses an optional explicitly tagged element. Throws on a tag mismatch, returns nil if the element simply isn't there.
|
||||
///
|
||||
@@ -230,6 +236,7 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
// MARK: - DEFAULT
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// Parses a value that is encoded with a DEFAULT. Such a value is optional, and if absent will
|
||||
/// be replaced with its default.
|
||||
@@ -278,8 +285,10 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
// MARK: - Parsing
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// A parsed representation of ASN.1.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
fileprivate struct ASN1ParseResult {
|
||||
private static let maximumNodeDepth = 10
|
||||
|
||||
@@ -343,8 +352,10 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1ParseResult: Hashable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
static func parse(_ data: [UInt8]) throws(CryptoKitMetaError) -> ASN1Node {
|
||||
return try parse(data[...])
|
||||
@@ -373,11 +384,13 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
// MARK: - ASN1NodeCollection
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// Represents a collection of ASN.1 nodes contained in a constructed ASN.1 node.
|
||||
///
|
||||
/// Constructed ASN.1 nodes are made up of multiple child nodes. This object represents the collection of those child nodes.
|
||||
/// It allows us to lazily construct the child nodes, potentially skipping over them when we don't care about them.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal struct ASN1NodeCollection {
|
||||
private var nodes: ArraySlice<ASN1ParserNode>
|
||||
|
||||
@@ -395,7 +408,9 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1NodeCollection: Sequence {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct Iterator: IteratorProtocol {
|
||||
private var nodes: ArraySlice<ASN1.ASN1ParserNode>
|
||||
private var depth: Int
|
||||
@@ -429,6 +444,7 @@ extension ASN1.ASN1NodeCollection: Sequence {
|
||||
}
|
||||
|
||||
// MARK: - ASN1Node
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// An `ASN1Node` is a single entry in the ASN.1 representation of a data structure.
|
||||
///
|
||||
@@ -438,6 +454,7 @@ extension ASN1 {
|
||||
///
|
||||
/// In this way, ASN.1 objects tend to form a "tree", where each object is represented by a single top-level constructed
|
||||
/// node that contains other objects and primitives, eventually reaching the bottom which is made up of primitive objects.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal struct ASN1Node {
|
||||
internal var identifier: ASN1Identifier
|
||||
|
||||
@@ -446,8 +463,10 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
// MARK: - ASN1Node.Content
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1Node {
|
||||
/// The content of a single ASN1Node.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum Content {
|
||||
case constructed(ASN1.ASN1NodeCollection)
|
||||
case primitive(ArraySlice<UInt8>)
|
||||
@@ -455,7 +474,9 @@ extension ASN1.ASN1Node {
|
||||
}
|
||||
|
||||
// MARK: - Serializing
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct Serializer {
|
||||
var serializedBytes: [UInt8]
|
||||
|
||||
@@ -565,10 +586,12 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal protocol ASN1Parseable {
|
||||
init(asn1Encoded: ASN1.ASN1Node) throws(CryptoKitMetaError)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1Parseable {
|
||||
internal init(asn1Encoded sequenceNodeIterator: inout ASN1.ASN1NodeCollection.Iterator) throws(CryptoKitMetaError) {
|
||||
guard let node = sequenceNodeIterator.next() else {
|
||||
@@ -587,11 +610,13 @@ extension ASN1Parseable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal protocol ASN1Serializable {
|
||||
func serialize(into coder: inout ASN1.Serializer) throws(CryptoKitMetaError)
|
||||
}
|
||||
|
||||
/// Covers ASN.1 types that may be implicitly tagged. Not all nodes can be!
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal protocol ASN1ImplicitlyTaggable: ASN1Parseable, ASN1Serializable {
|
||||
/// The tag that the first node will use "by default" if the grammar omits
|
||||
/// any more specific tag definition.
|
||||
@@ -602,6 +627,7 @@ internal protocol ASN1ImplicitlyTaggable: ASN1Parseable, ASN1Serializable {
|
||||
func serialize(into coder: inout ASN1.Serializer, withIdentifier identifier: ASN1.ASN1Identifier) throws(CryptoKitMetaError)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1ImplicitlyTaggable {
|
||||
internal init(asn1Encoded sequenceNodeIterator: inout ASN1.ASN1NodeCollection.Iterator,
|
||||
withIdentifier identifier: ASN1.ASN1Identifier = Self.defaultIdentifier) throws(CryptoKitMetaError) {
|
||||
@@ -629,6 +655,7 @@ extension ASN1ImplicitlyTaggable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ArraySlice where Element == UInt8 {
|
||||
fileprivate mutating func readASN1Length() throws(CryptoKitMetaError) -> UInt? {
|
||||
guard let firstByte = self.popFirst() else {
|
||||
@@ -677,6 +704,7 @@ extension ArraySlice where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension FixedWidthInteger {
|
||||
internal init<Bytes: Collection>(bigEndianBytes bytes: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 {
|
||||
guard bytes.count <= (Self.bitWidth / 8) else {
|
||||
@@ -694,6 +722,7 @@ extension FixedWidthInteger {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Array where Element == UInt8 {
|
||||
fileprivate mutating func writeIdentifier(_ identifier: ASN1.ASN1Identifier) {
|
||||
self.append(identifier.baseTag)
|
||||
@@ -719,6 +748,7 @@ extension Array where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Int {
|
||||
fileprivate var bytesNeededToEncode: Int {
|
||||
// ASN.1 lengths are in two forms. If we can store the length in 7 bits, we should:
|
||||
@@ -736,6 +766,7 @@ extension Int {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension FixedWidthInteger {
|
||||
// Bytes needed to store a given integer.
|
||||
internal var neededBytes: Int {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,6 +25,7 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// An ASN1 ANY represents...well, anything.
|
||||
///
|
||||
@@ -34,6 +34,7 @@ extension ASN1 {
|
||||
///
|
||||
/// The only things we allow users to do with ASN.1 ANYs is to try to decode them as something else,
|
||||
/// to create them from something else, or to serialize them.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1Any: ASN1Parseable, ASN1Serializable, Hashable {
|
||||
fileprivate var serializedBytes: ArraySlice<UInt8>
|
||||
|
||||
@@ -65,12 +66,14 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1Parseable {
|
||||
init(asn1Any: ASN1.ASN1Any) throws(CryptoKitMetaError) {
|
||||
try self.init(asn1Encoded: asn1Any.serializedBytes)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1ImplicitlyTaggable {
|
||||
init(asn1Any: ASN1.ASN1Any, withIdentifier identifier: ASN1.ASN1Identifier) throws(CryptoKitMetaError) {
|
||||
try self.init(asn1Encoded: asn1Any.serializedBytes, withIdentifier: identifier)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,8 +25,10 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// A bitstring is a representation of...well...some bits.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1BitString: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.primitiveBitString
|
||||
@@ -66,8 +67,10 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1BitString: Hashable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1BitString: ContiguousBytes {
|
||||
#if hasFeature(Embedded)
|
||||
func withUnsafeBytes<R, E: Error>(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,6 +25,7 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Bool: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.boolean
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,9 +25,11 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
/// tag number as well as the properties of the identifier in the stream.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal struct ASN1Identifier {
|
||||
/// The base tag. In a general ASN.1 implementation we'd need an arbitrary precision integer here as the tag number can be arbitrarily large, but
|
||||
/// we don't need the full generality here.
|
||||
@@ -121,6 +122,7 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1Identifier {
|
||||
internal static let objectIdentifier = try! ASN1.ASN1Identifier(rawIdentifier: 0x06)
|
||||
internal static let primitiveBitString = try! ASN1.ASN1Identifier(rawIdentifier: 0x03)
|
||||
@@ -145,9 +147,11 @@ extension ASN1.ASN1Identifier {
|
||||
internal static let generalizedTime = try! ASN1.ASN1Identifier(rawIdentifier: 0x18)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1Identifier: Hashable { }
|
||||
|
||||
#if !hasFeature(Embedded)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1Identifier: CustomStringConvertible {
|
||||
var description: String {
|
||||
return "ASN1Identifier(\(self.baseTag))"
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -32,6 +31,7 @@ import Foundation
|
||||
/// This is not a very good solution for a fully-fledged ASN.1 library: we'd rather have a better numerics
|
||||
/// protocol that could both initialize from and serialize to either bytes or words. However, no such
|
||||
/// protocol exists today.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol ASN1IntegerRepresentable: ASN1ImplicitlyTaggable {
|
||||
associatedtype IntegerBytes: RandomAccessCollection where IntegerBytes.Element == UInt8
|
||||
|
||||
@@ -44,6 +44,7 @@ protocol ASN1IntegerRepresentable: ASN1ImplicitlyTaggable {
|
||||
func withBigEndianIntegerBytes<ReturnType>(_ body: (IntegerBytes) throws(CryptoKitMetaError) -> ReturnType) throws(CryptoKitMetaError) -> ReturnType
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1IntegerRepresentable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.integer
|
||||
@@ -112,6 +113,7 @@ extension ASN1IntegerRepresentable {
|
||||
}
|
||||
|
||||
// MARK: - Auto-conformance for FixedWidthInteger with fixed width magnitude.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1IntegerRepresentable where Self: FixedWidthInteger {
|
||||
init(asn1IntegerBytes bytes: ArraySlice<UInt8>) throws(CryptoKitMetaError) {
|
||||
// Defer to the FixedWidthInteger constructor.
|
||||
@@ -132,6 +134,7 @@ extension ASN1IntegerRepresentable where Self: FixedWidthInteger {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct IntegerBytesCollection<Integer: FixedWidthInteger> {
|
||||
private var integer: Integer
|
||||
|
||||
@@ -140,7 +143,9 @@ struct IntegerBytesCollection<Integer: FixedWidthInteger> {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension IntegerBytesCollection: RandomAccessCollection {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct Index {
|
||||
fileprivate var byteNumber: Int
|
||||
|
||||
@@ -174,8 +179,10 @@ extension IntegerBytesCollection: RandomAccessCollection {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension IntegerBytesCollection.Index: Equatable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension IntegerBytesCollection.Index: Comparable {
|
||||
// Comparable here is backwards to the original ordering.
|
||||
static func <(lhs: Self, rhs: Self) -> Bool {
|
||||
@@ -195,6 +202,7 @@ extension IntegerBytesCollection.Index: Comparable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension IntegerBytesCollection.Index: Strideable {
|
||||
func advanced(by n: Int) -> IntegerBytesCollection<Integer>.Index {
|
||||
return IntegerBytesCollection.Index(byteNumber: self.byteNumber - n)
|
||||
@@ -206,26 +214,37 @@ extension IntegerBytesCollection.Index: Strideable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Int8: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt8: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Int16: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt16: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Int32: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt32: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Int64: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt64: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Int: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt: ASN1IntegerRepresentable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension RandomAccessCollection where Element == UInt8 {
|
||||
fileprivate func trimLeadingExcessBytes() -> SubSequence {
|
||||
var slice = self[...]
|
||||
@@ -273,6 +292,7 @@ extension RandomAccessCollection where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt8 {
|
||||
fileprivate var topBitSet: Bool {
|
||||
return (self & 0x80) != 0
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,8 +25,10 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// An ASN1 NULL represents nothing.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1Null: ASN1ImplicitlyTaggable, Hashable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.null
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,8 +25,10 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// An octet string is a representation of a string of octets.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1OctetString: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.primitiveOctetString
|
||||
@@ -59,8 +60,10 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1OctetString: Hashable { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1OctetString: ContiguousBytes {
|
||||
#if hasFeature(Embedded)
|
||||
func withUnsafeBytes<R, E: Error>(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,6 +25,7 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
private func contents(of string: StaticString) -> ArraySlice<UInt8> {
|
||||
if string.hasPointerRepresentation {
|
||||
return ArraySlice(
|
||||
@@ -41,9 +41,11 @@ private func contents(of string: StaticString) -> ArraySlice<UInt8> {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// A UTF8String is roughly what it sounds like. We note that all the string types are encoded as implicitly tagged
|
||||
/// octet strings, and so for now we just piggyback on the decoder and encoder for that type.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1UTF8String: ASN1ImplicitlyTaggable, Hashable, ContiguousBytes {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.primitiveUTF8String
|
||||
@@ -87,6 +89,7 @@ extension ASN1 {
|
||||
|
||||
/// We note that all the string types are encoded as implicitly tagged
|
||||
/// octet strings, and so for now we just piggyback on the decoder and encoder for that type.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1TeletexString: ASN1ImplicitlyTaggable, Hashable, ContiguousBytes {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.primitiveTeletexString
|
||||
@@ -130,6 +133,7 @@ extension ASN1 {
|
||||
|
||||
/// We note that all the string types are encoded as implicitly tagged
|
||||
/// octet strings, and so for now we just piggyback on the decoder and encoder for that type.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1PrintableString: ASN1ImplicitlyTaggable, Hashable, ContiguousBytes {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.primitivePrintableString
|
||||
@@ -173,6 +177,7 @@ extension ASN1 {
|
||||
|
||||
/// We note that all the string types are encoded as implicitly tagged
|
||||
/// octet strings, and so for now we just piggyback on the decoder and encoder for that type.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1UniversalString: ASN1ImplicitlyTaggable, Hashable, ContiguousBytes {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.primitiveUniversalString
|
||||
@@ -216,6 +221,7 @@ extension ASN1 {
|
||||
|
||||
/// We note that all the string types are encoded as implicitly tagged
|
||||
/// octet strings, and so for now we just piggyback on the decoder and encoder for that type.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1BMPString: ASN1ImplicitlyTaggable, Hashable, ContiguousBytes {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.primitiveBMPString
|
||||
@@ -259,30 +265,35 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
#if !hasFeature(Embedded)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1TeletexString: ExpressibleByStringLiteral {
|
||||
public init(stringLiteral value: StringLiteralType) {
|
||||
self.bytes = ArraySlice(value.utf8)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1UTF8String: ExpressibleByStringLiteral {
|
||||
public init(stringLiteral value: StringLiteralType) {
|
||||
self.bytes = ArraySlice(value.utf8)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1PrintableString: ExpressibleByStringLiteral {
|
||||
public init(stringLiteral value: StringLiteralType) {
|
||||
self.bytes = ArraySlice(value.utf8)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1UniversalString: ExpressibleByStringLiteral {
|
||||
public init(stringLiteral value: StringLiteralType) {
|
||||
self.bytes = ArraySlice(value.utf8)
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1BMPString: ExpressibleByStringLiteral {
|
||||
public init(stringLiteral value: StringLiteralType) {
|
||||
self.bytes = ArraySlice(value.utf8)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -27,12 +26,16 @@ import Foundation
|
||||
#endif
|
||||
|
||||
// For temporary purposes we pretend that ArraySlice is our "bigint" type. We don't really need anything else.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ArraySlice: ASN1Serializable where Element == UInt8 { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ArraySlice: ASN1Parseable where Element == UInt8 { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ArraySlice: ASN1ImplicitlyTaggable where Element == UInt8 { }
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ArraySlice: ASN1IntegerRepresentable where Element == UInt8 {
|
||||
// We only use unsigned "bigint"s
|
||||
static var isSigned: Bool {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,7 +25,9 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct GeneralizedTime: ASN1ImplicitlyTaggable, Hashable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.generalizedTime
|
||||
@@ -179,6 +180,7 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.GeneralizedTime {
|
||||
fileprivate static func parseDateBytes(_ bytes: ArraySlice<UInt8>) throws(CryptoKitMetaError) -> ASN1.GeneralizedTime {
|
||||
var bytes = bytes
|
||||
@@ -261,6 +263,7 @@ extension ASN1.GeneralizedTime {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ArraySlice where Element == UInt8 {
|
||||
fileprivate mutating func readFourDigitDecimalInteger() -> Int? {
|
||||
guard let first = self.readTwoDigitDecimalInteger(),
|
||||
@@ -326,6 +329,7 @@ extension ArraySlice where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Array where Element == UInt8 {
|
||||
fileprivate mutating func append(_ generalizedTime: ASN1.GeneralizedTime) {
|
||||
self.appendFourDigitDecimal(generalizedTime.year)
|
||||
@@ -397,6 +401,7 @@ extension Array where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Int {
|
||||
fileprivate init?(fromDecimalASCII ascii: UInt8) {
|
||||
let asciiZero = UInt8(48 /* "0" */)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -26,12 +25,14 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// An Object Identifier is a representation of some kind of object: really any kind of object.
|
||||
///
|
||||
/// It represents a node in an OID hierarchy, and is usually represented as an ordered sequence of numbers.
|
||||
///
|
||||
/// We mostly don't care about the semantics of the thing, we just care about being able to store and compare them.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ASN1ObjectIdentifier: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.objectIdentifier
|
||||
@@ -141,15 +142,19 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1ObjectIdentifier: Hashable {}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1ObjectIdentifier: ExpressibleByArrayLiteral {
|
||||
init(arrayLiteral elements: UInt...) {
|
||||
self.oidComponents = elements
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.ASN1ObjectIdentifier {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum NamedCurves {
|
||||
static let secp256r1: ASN1.ASN1ObjectIdentifier = [1, 2, 840, 10_045, 3, 1, 7]
|
||||
|
||||
@@ -158,16 +163,19 @@ extension ASN1.ASN1ObjectIdentifier {
|
||||
static let secp521r1: ASN1.ASN1ObjectIdentifier = [1, 3, 132, 0, 35]
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum HashFunctions {
|
||||
static let sha256: ASN1.ASN1ObjectIdentifier = [2, 16, 840, 1, 101, 3, 4, 2, 1]
|
||||
static let sha384: ASN1.ASN1ObjectIdentifier = [2, 16, 840, 1, 101, 3, 4, 2, 2]
|
||||
static let sha512: ASN1.ASN1ObjectIdentifier = [2, 16, 840, 1, 101, 3, 4, 2, 3]
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum AlgorithmIdentifier {
|
||||
static let idEcPublicKey: ASN1.ASN1ObjectIdentifier = [1, 2, 840, 10_045, 2, 1]
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum NameAttributes {
|
||||
static let name: ASN1.ASN1ObjectIdentifier = [2, 5, 4, 41]
|
||||
static let surname: ASN1.ASN1ObjectIdentifier = [2, 5, 4, 4]
|
||||
@@ -190,6 +198,7 @@ extension ASN1.ASN1ObjectIdentifier {
|
||||
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ArraySlice where Element == UInt8 {
|
||||
mutating fileprivate func readOIDSubidentifier() throws(CryptoKitMetaError) -> UInt {
|
||||
// In principle OID subidentifiers can be too large to fit into a UInt. We are choosing to not care about that
|
||||
@@ -206,6 +215,7 @@ extension ArraySlice where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt {
|
||||
fileprivate init<Bytes: Collection>(sevenBitBigEndianBytes bytes: Bytes) throws(CryptoKitMetaError) where Bytes.Element == UInt8 {
|
||||
// We need to know how many bytes we _need_ to store this "int".
|
||||
|
||||
@@ -12,16 +12,11 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -30,6 +25,7 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// An ECDSA signature is laid out as follows:
|
||||
///
|
||||
@@ -39,6 +35,7 @@ extension ASN1 {
|
||||
/// }
|
||||
///
|
||||
/// This type is generic because our different backends want to use different bignum representations.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct ECDSASignature<IntegerType: ASN1IntegerRepresentable>: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.sequence
|
||||
|
||||
@@ -12,16 +12,11 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -30,8 +25,10 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
/// A PEM document is some data, and a discriminator type that is used to advertise the content.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct PEMDocument {
|
||||
private static let lineLength = 64
|
||||
|
||||
@@ -100,6 +97,7 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Substring {
|
||||
fileprivate var pemStartDiscriminator: String? {
|
||||
return self.pemDiscriminator(expectedPrefix: "-----BEGIN ", expectedSuffix: "-----")
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -25,6 +24,7 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
// A PKCS#8 private key is one of two formats, depending on the version:
|
||||
//
|
||||
@@ -48,6 +48,7 @@ extension ASN1 {
|
||||
//
|
||||
// The private key octet string contains (surprise!) a SEC1-encoded private key! So we recursively invoke the
|
||||
// ASN.1 parser and go again.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct PKCS8PrivateKey: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
return .sequence
|
||||
|
||||
@@ -12,16 +12,11 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -30,6 +25,7 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
// For private keys, SEC 1 uses:
|
||||
//
|
||||
@@ -39,6 +35,7 @@ extension ASN1 {
|
||||
// parameters [0] EXPLICIT ECDomainParameters OPTIONAL,
|
||||
// publicKey [1] EXPLICIT BIT STRING OPTIONAL
|
||||
// }
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct SEC1PrivateKey: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
return .sequence
|
||||
|
||||
@@ -12,16 +12,11 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -30,7 +25,9 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct SubjectPublicKeyInfo: ASN1ImplicitlyTaggable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.sequence
|
||||
@@ -73,6 +70,7 @@ extension ASN1 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct RFC5480AlgorithmIdentifier: ASN1ImplicitlyTaggable, Hashable {
|
||||
static var defaultIdentifier: ASN1.ASN1Identifier {
|
||||
.sequence
|
||||
@@ -123,6 +121,7 @@ extension ASN1 {
|
||||
}
|
||||
|
||||
// MARK: Algorithm Identifier Statics
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension ASN1.RFC5480AlgorithmIdentifier {
|
||||
static let ecdsaP256 = ASN1.RFC5480AlgorithmIdentifier(algorithm: .AlgorithmIdentifier.idEcPublicKey,
|
||||
parameters: try! .init(erasing: ASN1.ASN1ObjectIdentifier.NamedCurves.secp256r1))
|
||||
|
||||
@@ -97,15 +97,12 @@ add_library(Crypto
|
||||
"Signatures/Ed25519.swift"
|
||||
"Signatures/MLDSA.swift"
|
||||
"Signatures/Signature.swift"
|
||||
"Util/ArraySpanHelpers.swift"
|
||||
"Util/BoringSSL/CryptoKitErrors_boring.swift"
|
||||
"Util/BoringSSL/InlineArray+withBytes_boring.swift"
|
||||
"Util/BoringSSL/Optional+withUnsafeBytes_boring.swift"
|
||||
"Util/BoringSSL/RNG_boring.swift"
|
||||
"Util/BoringSSL/SafeCompare_boring.swift"
|
||||
"Util/BoringSSL/Zeroization_boring.swift"
|
||||
"Util/BoringSSL/_CryptoModuleAnchor.swift"
|
||||
"Util/Data+ArraySpan.swift"
|
||||
"Util/PrettyBytes.swift"
|
||||
"Util/SafeCompare.swift"
|
||||
"Util/SecureBytes.swift"
|
||||
|
||||
@@ -12,18 +12,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum CryptoKitError: Error {
|
||||
/// The key size is incorrect.
|
||||
case incorrectKeySize
|
||||
@@ -35,29 +27,18 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum CryptoKitASN1Error: Equatable, Error, Hashable {
|
||||
/// The ASN.1 tag for this field is invalid or unsupported.
|
||||
case invalidFieldIdentifier
|
||||
@@ -86,6 +67,7 @@ public enum CryptoKitASN1Error: Equatable, Error, Hashable {
|
||||
case invalidPEMDocument
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum RSAPSSSPKIErrors: Error {
|
||||
case invalidPSSOID
|
||||
case missingParameters
|
||||
@@ -97,35 +79,45 @@ enum RSAPSSSPKIErrors: Error {
|
||||
}
|
||||
|
||||
#if hasFeature(Embedded)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct RSAPSSSPKIError: Error {
|
||||
internal var error: RSAPSSSPKIErrors
|
||||
}
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct RSAPSSSPKIError: Error {
|
||||
internal var error: RSAPSSSPKIErrors
|
||||
}
|
||||
#endif
|
||||
|
||||
#if hasFeature(Embedded)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum CryptoKitMetaError: Error {
|
||||
case cryptoKitError(underlyingError: CryptoKitError)
|
||||
case asn1Error(underlyingError: CryptoKitASN1Error)
|
||||
case rsapssspkiError(underlyingError: RSAPSSSPKIError)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func error(_ error: CryptoKitError) -> CryptoKitMetaError {
|
||||
.cryptoKitError(underlyingError: error)
|
||||
}
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func error(_ error: CryptoKitASN1Error) -> CryptoKitMetaError {
|
||||
.asn1Error(underlyingError: error)
|
||||
}
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func error(_ error: RSAPSSSPKIErrors) -> CryptoKitMetaError {
|
||||
.rsapssspkiError(underlyingError: RSAPSSSPKIError(error: error))
|
||||
}
|
||||
#else /* !hasFeature(Embedded) */
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public typealias CryptoKitMetaError = any Error
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func error(_ error: CryptoKitError) -> CryptoKitError { error }
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func error(_ error: CryptoKitASN1Error) -> CryptoKitASN1Error { error }
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func error(_ error: RSAPSSSPKIErrors) -> RSAPSSSPKIErrors { error }
|
||||
#endif
|
||||
|
||||
|
||||
@@ -202,7 +202,7 @@ private final class DigestContext<H: BoringSSLBackedHashFunction> {
|
||||
preconditionFailure("Unable to finalize digest state")
|
||||
}
|
||||
// We force unwrap here because if the digest size is wrong it's an internal error.
|
||||
return H.Digest(copying: digestPointer.bytes)!
|
||||
return H.Digest(bufferPointer: UnsafeRawBufferPointer(digestPointer))!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,16 +12,11 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_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
|
||||
@@ -32,58 +27,44 @@ public import Foundation
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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)
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol Digest: Hashable, Sendable, ContiguousBytes, CustomStringConvertible, Sequence where Element == UInt8 {
|
||||
/// The number of bytes in the digest.
|
||||
static var byteCount: Int { get }
|
||||
}
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol DigestPrivate: Digest {
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ())
|
||||
init?(bufferPointer: UnsafeRawBufferPointer)
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension DigestPrivate {
|
||||
@inlinable
|
||||
init?(copying bytes: RawSpan) {
|
||||
self.init() {
|
||||
$0.append(contentsOf: bytes)
|
||||
init?(bytes: [UInt8]) {
|
||||
let some = bytes.withUnsafeBytes { bufferPointer in
|
||||
return Self(bufferPointer: bufferPointer)
|
||||
}
|
||||
|
||||
if some != nil {
|
||||
self = some!
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Digest {
|
||||
public func makeIterator() -> Array<UInt8>.Iterator {
|
||||
self.withUnsafeBytes({ (buffPtr) in
|
||||
@@ -93,11 +74,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Digest {
|
||||
/// Determines whether two digests are equal.
|
||||
///
|
||||
@@ -130,7 +107,7 @@ extension Digest {
|
||||
}
|
||||
|
||||
#if !hasFeature(Embedded)
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Digest {
|
||||
public var description: String {
|
||||
return "\(Self.self): \(Array(self).hexString)"
|
||||
|
||||
@@ -22,24 +22,19 @@
|
||||
|
||||
// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SHA256Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64, UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (UInt64(0), UInt64(0), 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 == 32 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 32 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -110,24 +105,19 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SHA384Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), 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 == 48 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 48 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -200,24 +190,19 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SHA512Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), 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 == 64 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 64 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -289,32 +274,23 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SHA1Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (UInt64(0), 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 == 20 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 20 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -381,32 +357,23 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct MD5Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (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 == 16 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 16 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -478,20 +445,19 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SHA3_256Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64, UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (UInt64(0), UInt64(0), 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 == 32 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 32 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -560,20 +526,19 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SHA3_384Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), 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 == 48 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 48 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -644,20 +609,19 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SHA3_512Digest: DigestPrivate {
|
||||
let bytes: (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64, UInt64)
|
||||
|
||||
init?(initializingWith body: (inout OutputRawSpan) -> ()) {
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), 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 == 64 else {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == 64 else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0), UInt64(0))
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
|
||||
@@ -35,11 +35,6 @@ else:
|
||||
%{
|
||||
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
|
||||
@@ -56,24 +51,18 @@ else:
|
||||
${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 {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == ${byteCount} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (${(wordsCount-1)*"UInt64(0), "+"UInt64(0)"})
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
@@ -165,20 +154,18 @@ digests_and_length = [
|
||||
}%
|
||||
// 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 {
|
||||
init?(bufferPointer: UnsafeRawBufferPointer) {
|
||||
guard bufferPointer.count == ${byteCount} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bytes = (${(wordsCount-1)*"UInt64(0), "+"UInt64(0)"})
|
||||
withUnsafeMutableBytes(of: &bytes) { targetPtr in
|
||||
targetPtr.copyMemory(from: bufferPointer)
|
||||
}
|
||||
self.bytes = bytes
|
||||
}
|
||||
|
||||
|
||||
@@ -14,27 +14,20 @@
|
||||
#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
|
||||
#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 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias DigestImplSHA3 = CoreCryptoDigestImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias DigestImpl = OpenSSLDigestImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@@ -61,27 +54,14 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_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
|
||||
@@ -130,20 +110,7 @@ public protocol HashFunction: Sendable {
|
||||
func finalize() -> Digest
|
||||
}
|
||||
|
||||
extension HashFunction {
|
||||
@inlinable
|
||||
public mutating func update(bytes: RawSpan) {
|
||||
bytes.withUnsafeBytes {
|
||||
self.update(bufferPointer: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HashFunction {
|
||||
/// Computes a digest of the buffer.
|
||||
///
|
||||
@@ -156,18 +123,6 @@ extension HashFunction {
|
||||
hasher.update(bufferPointer: bufferPointer)
|
||||
return hasher.finalize()
|
||||
}
|
||||
|
||||
/// Computes a digest of a span of bytes.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - bytes: The bytes to be hashed.
|
||||
/// - Returns: The computed digest.
|
||||
@inlinable
|
||||
public static func hash(bytes: RawSpan) -> Digest {
|
||||
var hasher = Self()
|
||||
hasher.update(bytes: bytes)
|
||||
return hasher.finalize()
|
||||
}
|
||||
|
||||
/// Computes the digest of the bytes in the given data instance and
|
||||
/// returns the computed digest.
|
||||
|
||||
@@ -26,11 +26,7 @@
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public typealias SHA2_256 = SHA256
|
||||
|
||||
/// An implementation of Secure Hashing Algorithm 2 (SHA-2) hashing with a
|
||||
@@ -45,18 +41,9 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +113,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public typealias SHA2_384 = SHA384
|
||||
|
||||
/// An implementation of Secure Hashing Algorithm 2 (SHA-2) hashing with a
|
||||
@@ -145,18 +128,9 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +201,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public typealias SHA2_512 = SHA512
|
||||
|
||||
/// An implementation of Secure Hashing Algorithm 2 (SHA-2) hashing with a
|
||||
@@ -246,18 +216,9 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
/// 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,7 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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 +168,7 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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
|
||||
|
||||
@@ -149,7 +149,7 @@ private final class XKCPDigestContext<H: XKCPBackedHashFunction> {
|
||||
preconditionFailure("Unable to finalize digest state")
|
||||
}
|
||||
// We force unwrap here because if the digest size is wrong it's an internal error.
|
||||
return H.Digest(copying: digestPointer.bytes)!
|
||||
return H.Digest(bufferPointer: UnsafeRawBufferPointer(digestPointer))!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,14 +25,11 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HPKE {
|
||||
/// The authenticated encryption with associated data (AEAD) algorithms to use in HPKE.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@@ -46,7 +43,8 @@ extension HPKE {
|
||||
case exportOnly
|
||||
|
||||
/// Return the AEAD algorithm identifier as defined in section 7.3 of [RFC 9180](https://www.ietf.org/rfc/rfc9180.pdf).
|
||||
internal var value: UInt16 {
|
||||
@_spi(HPKEAlgID)
|
||||
public var value: UInt16 {
|
||||
switch self {
|
||||
case .AES_GCM_128: return 0x0001
|
||||
case .AES_GCM_256: return 0x0002
|
||||
@@ -60,7 +58,8 @@ extension HPKE {
|
||||
}
|
||||
|
||||
/// Return the AEAD key size in bytes
|
||||
internal var keyByteCount: Int {
|
||||
@_spi(HPKEAlgID)
|
||||
public var keyByteCount: Int {
|
||||
switch self {
|
||||
case .AES_GCM_128:
|
||||
return 16
|
||||
@@ -74,7 +73,8 @@ extension HPKE {
|
||||
}
|
||||
|
||||
/// Return the AEAD nonce size in bytes
|
||||
internal var nonceByteCount: Int {
|
||||
@_spi(HPKEAlgID)
|
||||
public var nonceByteCount: Int {
|
||||
switch self {
|
||||
case .AES_GCM_128, .AES_GCM_256, .chaChaPoly:
|
||||
return 12
|
||||
@@ -84,7 +84,8 @@ extension HPKE {
|
||||
}
|
||||
|
||||
/// Return the AEAD tag size in bytes
|
||||
internal var tagByteCount: Int {
|
||||
@_spi(HPKEAlgID)
|
||||
public var tagByteCount: Int {
|
||||
switch self {
|
||||
case .AES_GCM_128, .AES_GCM_256, .chaChaPoly:
|
||||
return 16
|
||||
@@ -97,7 +98,8 @@ extension HPKE {
|
||||
return I2OSP(value: Int(self.value), outputByteCount: 2)
|
||||
}
|
||||
|
||||
internal func seal<D: DataProtocol, AD: DataProtocol>(_ message: D, authenticating aad: AD, nonce: Data, using key: SymmetricKey) throws -> Data {
|
||||
@_spi(MLS)
|
||||
public func seal<D: DataProtocol, AD: DataProtocol>(_ message: D, authenticating aad: AD, nonce: Data, using key: SymmetricKey) throws -> Data {
|
||||
switch self {
|
||||
case .chaChaPoly:
|
||||
return try ChaChaPoly.seal(message, using: key, nonce: ChaChaPoly.Nonce(data: nonce), authenticating: aad).combined.dropFirst(nonce.count)
|
||||
@@ -106,7 +108,8 @@ extension HPKE {
|
||||
}
|
||||
}
|
||||
|
||||
internal func open<C: DataProtocol, AD: DataProtocol>(_ ct: C, nonce: Data, authenticating aad: AD, using key: SymmetricKey) throws -> Data {
|
||||
@_spi(MLS)
|
||||
public func open<C: DataProtocol, AD: DataProtocol>(_ ct: C, nonce: Data, authenticating aad: AD, using key: SymmetricKey) throws -> Data {
|
||||
guard ct.count >= self.tagByteCount else {
|
||||
throw HPKE.Errors.expectedPSK
|
||||
}
|
||||
|
||||
@@ -25,12 +25,8 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HPKE {
|
||||
/// Cipher suites to use in hybrid public key encryption (HPKE).
|
||||
///
|
||||
@@ -38,6 +34,7 @@ extension HPKE {
|
||||
/// and decrypting messages, the key derivation function (KDF) for deriving the shared key, and the key encapsulation
|
||||
/// mechanism (KEM) for sharing the symmetric key. The sender and recipient of encrypted messages need to use the
|
||||
/// same cipher suite.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct Ciphersuite: Sendable {
|
||||
/// A cipher suite for HPKE that uses NIST P-256 elliptic curve key agreement, 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.
|
||||
@@ -53,7 +50,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)
|
||||
|
||||
@@ -12,11 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
@@ -29,13 +25,10 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HPKE {
|
||||
/// The key derivation functions to use in HPKE.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@@ -45,7 +38,8 @@ extension HPKE {
|
||||
case HKDF_SHA512
|
||||
|
||||
/// Return the KDF algorithm identifier as defined in section 7.2 of [RFC 9180](https://www.ietf.org/rfc/rfc9180.pdf).
|
||||
internal var value: UInt16 {
|
||||
@_spi(HPKEAlgID)
|
||||
public var value: UInt16 {
|
||||
switch self {
|
||||
case .HKDF_SHA256: return 0x0001
|
||||
case .HKDF_SHA384: return 0x0002
|
||||
@@ -79,7 +73,8 @@ extension HPKE {
|
||||
/// - ikm: The initial key material the derivation function uses to derive a key.
|
||||
///
|
||||
/// - Returns: A pseudorandom, cryptographically strong key in the form of a hashed authentication code.
|
||||
internal func extract<S: DataProtocol>(salt: S, ikm: SymmetricKey) -> SymmetricKey {
|
||||
@_spi(MLS)
|
||||
public func extract<S: DataProtocol>(salt: S, ikm: SymmetricKey) -> SymmetricKey {
|
||||
switch self {
|
||||
case .HKDF_SHA256:
|
||||
return SymmetricKey(data: HKDF<SHA256>.extract(inputKeyMaterial: ikm, salt: salt))
|
||||
@@ -101,7 +96,8 @@ extension HPKE {
|
||||
/// - outputByteCount: The length in bytes of the resulting symmetric key.
|
||||
///
|
||||
/// - Returns: The derived symmetric key.
|
||||
internal func expand(prk: SymmetricKey, info: Data, outputByteCount: Int) -> SymmetricKey {
|
||||
@_spi(MLS)
|
||||
public func expand(prk: SymmetricKey, info: Data, outputByteCount: Int) -> SymmetricKey {
|
||||
switch self {
|
||||
case .HKDF_SHA256:
|
||||
return SymmetricKey(data: HKDF<SHA256>.expand(pseudoRandomKey: prk, info: info, outputByteCount: outputByteCount))
|
||||
|
||||
@@ -26,27 +26,20 @@ import Foundation
|
||||
#endif
|
||||
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HPKE {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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)
|
||||
|
||||
@@ -26,34 +26,32 @@ import Foundation
|
||||
#endif
|
||||
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
private let protocolLabel = Data("HPKE-v1".utf8)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
private let eaePRKLabel = Data("eae_prk".utf8)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
private let sharedSecretLabel = Data("shared_secret".utf8)
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Data {
|
||||
internal init(unsafeFromContiguousBytes cb: ContiguousBytes) {
|
||||
self = cb.withUnsafeBytes { return Data($0) }
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func ExtractAndExpand(zz: ContiguousBytes, kemContext: Data, suiteID: Data, kem: HPKE.KEM, kdf: HPKE.KDF) -> SymmetricKey {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
let eaePrk = LabeledExtract(salt: Data(), label: eaePRKLabel, ikm: zz, suiteID: suiteID, kdf: kdf)
|
||||
|
||||
return LabeledExpand(prk: eaePrk, label: sharedSecretLabel,
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func LabeledExtract(salt: Data?, label: Data, ikm: ContiguousBytes?, suiteID: Data, kdf: HPKE.KDF) -> SymmetricKey {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
var labeled_ikm = protocolLabel
|
||||
labeled_ikm.append(suiteID)
|
||||
labeled_ikm.append(label)
|
||||
@@ -61,12 +59,9 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func LabeledExpand<Info: DataProtocol>(prk: SymmetricKey, label: Data, info: Info, outputByteCount: UInt16, suiteID: Data, kdf: HPKE.KDF) -> SymmetricKey {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
var labeled_info = I2OSP(value: Int(outputByteCount), outputByteCount: 2)
|
||||
labeled_info.append(protocolLabel)
|
||||
labeled_info.append(suiteID)
|
||||
@@ -75,20 +70,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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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))
|
||||
}
|
||||
|
||||
@@ -39,18 +39,22 @@ import Foundation
|
||||
#endif
|
||||
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
let requiredBytes = Int(ceil(log2(Double(max(value, 1) + 1)) / 8))
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
let requiredBytes = Int((log2_bridge(Double(max(value, 1) + 1)) / 8).rounded(.up))
|
||||
#endif
|
||||
|
||||
precondition(outputByteCount >= requiredBytes)
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
var data = Data(repeating: 0, count: outputByteCount)
|
||||
|
||||
for i in (outputByteCount - requiredBytes)...(outputByteCount - 1) {
|
||||
|
||||
@@ -12,11 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
@@ -30,12 +26,8 @@ public import Foundation
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol HPKEPublicKeySerialization: Sendable {
|
||||
/// Creates a public key from an encoded representation.
|
||||
///
|
||||
@@ -53,94 +45,59 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol HPKEDiffieHellmanPublicKey: HPKEPublicKeySerialization, Sendable where EphemeralPrivateKey.PublicKey == Self {
|
||||
/// The type of the ephemeral private key.
|
||||
associatedtype EphemeralPrivateKey: HPKEDiffieHellmanPrivateKeyGeneration
|
||||
}
|
||||
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol HPKEKEMPublicKey: KEMPublicKey, HPKEPublicKeySerialization where EphemeralPrivateKey.PublicKey == Self {
|
||||
/// The type of the ephemeral private key.
|
||||
associatedtype EphemeralPrivateKey: HPKEKEMPrivateKeyGeneration
|
||||
}
|
||||
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol HPKEDiffieHellmanPrivateKeyGeneration: HPKEDiffieHellmanPrivateKey, Sendable {
|
||||
/// Creates a private key generator.
|
||||
init()
|
||||
}
|
||||
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol HPKEKEMPrivateKeyGeneration: HPKEKEMPrivateKey, Sendable {
|
||||
/// Creates a private key generator.
|
||||
init() throws
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@@ -167,11 +124,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct PrivateKey<DHSK: HPKEDiffieHellmanPrivateKey>: KEMPrivateKey {
|
||||
let kem: HPKE.KEM
|
||||
let key: DHSK
|
||||
|
||||
@@ -25,19 +25,12 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Curve25519.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey {
|
||||
/// The type of the ephemeral private key associated with this public key.
|
||||
public typealias EphemeralPrivateKey = Curve25519.KeyAgreement.PrivateKey
|
||||
|
||||
@@ -12,11 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
@@ -29,11 +25,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +33,8 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +74,8 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +83,8 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +124,8 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +133,8 @@ 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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P521.KeyAgreement.PublicKey: HPKEDiffieHellmanPublicKey {
|
||||
/// The type of the ephemeral private key associated with this public key.
|
||||
public typealias EphemeralPrivateKey = P521.KeyAgreement.PrivateKey
|
||||
|
||||
@@ -24,27 +24,17 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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]
|
||||
if #available(iOS 19.0, macOS 16.0, watchOS 12.0, tvOS 19.0, macCatalyst 19.0, *) {
|
||||
cases.append(KEM.XWingMLKEM768X25519)
|
||||
}
|
||||
cases.append(KEM.XWingMLKEM768X25519)
|
||||
return cases
|
||||
}
|
||||
|
||||
@@ -62,11 +52,11 @@ 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).
|
||||
internal var value: UInt16 {
|
||||
@_spi(HPKEAlgID)
|
||||
public var value: UInt16 {
|
||||
switch self {
|
||||
case .P256_HKDF_SHA256: return 0x0010
|
||||
case .P384_HKDF_SHA384: return 0x0011
|
||||
@@ -101,7 +91,8 @@ extension HPKE {
|
||||
}
|
||||
|
||||
/// Return the size of the encapsulation in bytes
|
||||
internal var nEnc: UInt16 {
|
||||
@_spi(HPKEAlgID)
|
||||
public var nEnc: UInt16 {
|
||||
switch self {
|
||||
case .P256_HKDF_SHA256: return 65
|
||||
case .P384_HKDF_SHA384: return 97
|
||||
|
||||
@@ -25,13 +25,10 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HPKE {
|
||||
/// Hybrid public key encryption (HPKE) errors that CryptoKit uses.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Errors: Error {
|
||||
/// The parameters for initializing an HPKE sender or receiver are inconsistent.
|
||||
case inconsistentParameters
|
||||
|
||||
@@ -12,11 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
@@ -57,18 +53,10 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +70,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct Sender: Sendable {
|
||||
private var context: Context
|
||||
/// The encapsulated symmetric key that the recipient uses to decrypt messages.
|
||||
@@ -141,7 +125,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 +225,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct Recipient: Sendable {
|
||||
|
||||
private var context: Context
|
||||
@@ -295,7 +274,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)
|
||||
}
|
||||
|
||||
@@ -25,12 +25,9 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HPKE {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct Context: Sendable {
|
||||
var keySchedule: KeySchedule
|
||||
var encapsulated: Data
|
||||
@@ -44,7 +41,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 +73,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
|
||||
|
||||
@@ -25,12 +25,9 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HPKE {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal struct KeySchedule: Sendable {
|
||||
fileprivate static let pksIDHashLabel = Data("psk_id_hash".utf8)
|
||||
fileprivate static let infoHashLabel = Data("info_hash".utf8)
|
||||
|
||||
@@ -15,18 +15,10 @@
|
||||
@_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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal enum Mode: CaseIterable {
|
||||
case base
|
||||
case psk
|
||||
|
||||
@@ -12,21 +12,13 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Insecure: Sendable {}
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -14,11 +14,7 @@
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@_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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Insecure {
|
||||
/// An implementation of SHA1 hashing.
|
||||
///
|
||||
@@ -36,14 +32,10 @@ extension Insecure {
|
||||
/// secure, but is provided for backward compatibility with older services
|
||||
/// that require it. For new services, prefer one of the secure hashes, like
|
||||
/// ``SHA512``.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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.
|
||||
@@ -121,14 +113,10 @@ extension Insecure {
|
||||
/// secure, but is provided for backward compatibility with older services
|
||||
/// that require it. For new services, prefer one of the secure hashes, like
|
||||
/// ``SHA512``.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
|
||||
@@ -29,7 +29,6 @@ import Foundation
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM768 {
|
||||
/// A ML-KEM-768 private key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPrivateKey: @unchecked Sendable, KEMPrivateKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -176,7 +175,6 @@ extension MLKEM768 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM768 {
|
||||
/// A ML-KEM-768 public key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPublicKey: @unchecked Sendable, KEMPublicKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -298,7 +296,6 @@ extension MLKEM768 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM1024 {
|
||||
/// A ML-KEM-1024 private key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPrivateKey: @unchecked Sendable, KEMPrivateKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -445,7 +442,6 @@ extension MLKEM1024 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM1024 {
|
||||
/// A ML-KEM-1024 public key.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct InternalPublicKey: @unchecked Sendable, KEMPublicKey {
|
||||
private var backing: Backing
|
||||
|
||||
@@ -564,7 +560,6 @@ extension MLKEM1024 {
|
||||
private static let ciphertextByteCount = Int(MLKEM1024_CIPHERTEXT_BYTES)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum MLKEM {
|
||||
/// The size of the seed in bytes.
|
||||
static let seedByteCount = 64
|
||||
|
||||
@@ -127,7 +127,6 @@ struct OpenSSLXWingPrivateKeyImpl: Sendable {
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension OpenSSLXWingPrivateKeyImpl {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
final class Backing: @unchecked Sendable {
|
||||
private var privateKey: XWING_private_key
|
||||
|
||||
|
||||
@@ -25,13 +25,10 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension KEM {
|
||||
/// Errors that CryptoKit throws when it encounters problems in key encapsulation mechanism (KEM) operations.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Errors: Error {
|
||||
/// The public key CryptoKit receives when it initializes a key encapsulation operation doesn't match the expected value.
|
||||
case publicKeyMismatchDuringInitialization
|
||||
|
||||
@@ -12,11 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
@@ -32,13 +28,10 @@ public import Foundation
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum KEM: Sendable {
|
||||
/// The result of a key encapsulation operation.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct EncapsulationResult: Sendable {
|
||||
/// The shared secret.
|
||||
public let sharedSecret: SymmetricKey
|
||||
@@ -54,12 +47,8 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol KEMPublicKey: Sendable {
|
||||
/// Generates and encapsulates a shared secret.
|
||||
///
|
||||
@@ -70,12 +59,8 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol KEMPrivateKey: Sendable {
|
||||
associatedtype PublicKey: KEMPublicKey
|
||||
|
||||
@@ -94,26 +79,4 @@ public protocol KEMPrivateKey: Sendable {
|
||||
/// The associated public key.
|
||||
var publicKey: PublicKey { get }
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
/// Generates a new random private key.
|
||||
/// - Returns: The generated private key.
|
||||
///
|
||||
/// Give the ``publicKey`` to another person so that they can encapsulate
|
||||
/// shared secrets that you recover by calling ``decapsulate(_:)``.
|
||||
static func generate() throws -> Self
|
||||
|
||||
/// Recovers a shared secret from an encapsulated representation.
|
||||
/// - Parameter encapsulated: The encapsulated shared secret that someone created using this key's ``publicKey``.
|
||||
/// - Returns: The decapsulated shared secret.
|
||||
consuming func decapsulate(_ encapsulated: Data) throws -> SymmetricKey
|
||||
|
||||
/// The associated public key.
|
||||
var publicKey: PublicKey { get }
|
||||
}
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -21,22 +21,26 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLKEMPublicKeyImpl = CoreCryptoMLKEMPublicKeyImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLKEMPrivateKeyImpl = CoreCryptoMLKEMPrivateKeyImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLKEMPublicKeyImpl = OpenSSLMLKEMPublicKeyImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum MLKEM768: Sendable {}
|
||||
|
||||
@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM768 {
|
||||
/// A public key you use to encapsulate shared secrets with the Module-Lattice key encapsulation mechanism.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: KEMPublicKey, Sendable {
|
||||
var impl: MLKEMPublicKeyImpl<MLKEM768>
|
||||
|
||||
@@ -66,6 +70,7 @@ extension MLKEM768 {
|
||||
}
|
||||
|
||||
/// A private key you use to decapsulate shared secrets with the Module-Lattice key encapsulation mechanism.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: KEMPrivateKey {
|
||||
internal let impl: MLKEMPrivateKeyImpl<MLKEM768>
|
||||
|
||||
@@ -111,7 +116,7 @@ extension MLKEM768 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Decapsulate a shared secret.
|
||||
/// Decapsulated a shared secret.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - encapsulated: An encapsulated shared secret, that you get by calling ``MLKEM768/PublicKey/encapsulate()`` on the corresponding public key.
|
||||
@@ -123,7 +128,7 @@ extension MLKEM768 {
|
||||
/// The corresponding public key.
|
||||
public var publicKey: MLKEM768.PublicKey {
|
||||
get {
|
||||
self.impl.publicKey
|
||||
try self.impl.publicKey
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +141,7 @@ extension MLKEM768 {
|
||||
}
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<MLKEMPrivateKeyImpl<MLKEM768>.seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: MLKEMPrivateKeyImpl<MLKEM768>.seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(copying: publicKeyHashData.bytes)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLKEMPrivateKeyImpl<MLKEM768>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
@@ -150,56 +155,17 @@ 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,
|
||||
/// but this private key can only be used to decapsulate a shared secret once.
|
||||
public struct OneTimePrivateKey: KEMOneTimePrivateKey, ~Copyable {
|
||||
internal let impl: MLKEMPrivateKeyImpl<MLKEM768>
|
||||
|
||||
internal init(_ impl: MLKEMPrivateKeyImpl<MLKEM768>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Generates a new, random one-time-use private key.
|
||||
public static func generate() throws -> MLKEM768.OneTimePrivateKey {
|
||||
let impl = try MLKEMPrivateKeyImpl<MLKEM768>.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 ``MLKEM768/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: MLKEM768.PublicKey {
|
||||
get {
|
||||
self.impl.publicKey
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum MLKEM1024: Sendable {}
|
||||
|
||||
@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLKEM1024 {
|
||||
/// A public key you use to encapsulate shared secrets with the Module-Lattice key encapsulation mechanism.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: KEMPublicKey, Sendable {
|
||||
var impl: MLKEMPublicKeyImpl<MLKEM1024>
|
||||
|
||||
@@ -229,6 +195,7 @@ extension MLKEM1024 {
|
||||
}
|
||||
|
||||
/// A private key you use to decapsulate shared secrets with the Module-Lattice key encapsulation mechanism.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: KEMPrivateKey {
|
||||
internal let impl: MLKEMPrivateKeyImpl<MLKEM1024>
|
||||
|
||||
@@ -274,7 +241,7 @@ extension MLKEM1024 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Decapsulate a shared secret.
|
||||
/// Decapsulated a shared secret.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - encapsulated: An encapsulated shared secret, that you get by calling ``MLKEM1024/PublicKey/encapsulate()`` on the corresponding public key.
|
||||
@@ -286,7 +253,7 @@ extension MLKEM1024 {
|
||||
/// The corresponding public key.
|
||||
public var publicKey: MLKEM1024.PublicKey {
|
||||
get {
|
||||
self.impl.publicKey
|
||||
try self.impl.publicKey
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,7 +266,7 @@ extension MLKEM1024 {
|
||||
}
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<MLKEMPrivateKeyImpl<MLKEM1024>.seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: MLKEMPrivateKeyImpl<MLKEM1024>.seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(copying: publicKeyHashData.bytes)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLKEMPrivateKeyImpl<MLKEM1024>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
@@ -313,46 +280,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,
|
||||
/// but this private key can only be used to decapsulate a shared secret once.
|
||||
public struct OneTimePrivateKey: KEMOneTimePrivateKey, ~Copyable {
|
||||
internal let impl: MLKEMPrivateKeyImpl<MLKEM1024>
|
||||
|
||||
internal init(_ impl: MLKEMPrivateKeyImpl<MLKEM1024>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Generates a new, random one-time-use private key.
|
||||
public static func generate() throws -> MLKEM1024.OneTimePrivateKey {
|
||||
let impl = try MLKEMPrivateKeyImpl<MLKEM1024>.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 ``MLKEM1024/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: MLKEM1024.PublicKey {
|
||||
get {
|
||||
self.impl.publicKey
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ public import Foundation
|
||||
}%
|
||||
|
||||
#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
|
||||
@@ -39,10 +38,8 @@ typealias MLKEMPrivateKeyImpl = OpenSSLMLKEMPrivateKeyImpl
|
||||
}%
|
||||
|
||||
/// 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 {
|
||||
@@ -119,7 +116,7 @@ extension ${NAME} {
|
||||
}
|
||||
}
|
||||
|
||||
/// Decapsulate a shared secret.
|
||||
/// Decapsulated a shared secret.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - encapsulated: An encapsulated shared secret, that you get by calling ``${NAME}/PublicKey/encapsulate()`` on the corresponding public key.
|
||||
@@ -131,7 +128,7 @@ extension ${NAME} {
|
||||
/// The corresponding public key.
|
||||
public var publicKey: ${NAME}.PublicKey {
|
||||
get {
|
||||
self.impl.publicKey
|
||||
try self.impl.publicKey
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +141,7 @@ extension ${NAME} {
|
||||
}
|
||||
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)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLKEMPrivateKeyImpl<${NAME}>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
@@ -158,46 +155,6 @@ extension ${NAME} {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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
|
||||
|
||||
@@ -15,23 +15,11 @@
|
||||
@_exported import CryptoKit
|
||||
#else
|
||||
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_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, *)
|
||||
@@ -47,19 +35,12 @@ typealias XWingPrivateKeyImpl = OpenSSLXWingPrivateKeyImpl
|
||||
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension XWingMLKEM768X25519 {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: KEMPublicKey {
|
||||
var impl: XWingPublicKeyImpl
|
||||
|
||||
@@ -82,6 +63,7 @@ extension XWingMLKEM768X25519 {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: KEMPrivateKey {
|
||||
private var impl: XWingPrivateKeyImpl
|
||||
|
||||
@@ -119,48 +101,9 @@ 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,
|
||||
/// but this private key can only be used to decapsulate a shared secret once.
|
||||
public struct OneTimePrivateKey: KEMOneTimePrivateKey, ~Copyable {
|
||||
private var impl: XWingPrivateKeyImpl
|
||||
|
||||
internal init(impl: XWingPrivateKeyImpl) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Generates a new, random one-time-use private key.
|
||||
public static func generate() throws -> XWingMLKEM768X25519.OneTimePrivateKey {
|
||||
let impl = try XWingPrivateKeyImpl.generate()
|
||||
return OneTimePrivateKey(impl: impl)
|
||||
}
|
||||
|
||||
/// Decapsulate a shared secret.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - encapsulated: An encapsulated shared secret, that you get by calling ``XWingMLKEM768X25519/PublicKey/encapsulate()`` on the corresponding public key.
|
||||
/// - Returns: The shared secret.
|
||||
public consuming func decapsulate(_ encapsulated: Data) throws -> SymmetricKey {
|
||||
return try impl.decapsulate(encapsulated)
|
||||
}
|
||||
|
||||
/// The corresponding public key.
|
||||
public var publicKey: XWingMLKEM768X25519.PublicKey {
|
||||
get {
|
||||
XWingMLKEM768X25519.PublicKey(impl: self.impl.publicKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension XWingMLKEM768X25519.PrivateKey: HPKEKEMPrivateKeyGeneration {
|
||||
public init() throws {
|
||||
self = try Self.generate()
|
||||
@@ -178,23 +121,13 @@ extension XWingMLKEM768X25519.PrivateKey: HPKEKEMPrivateKeyGeneration {
|
||||
public init<D: DataProtocol>(integrityCheckedRepresentation: D) throws {
|
||||
let seed = integrityCheckedRepresentation.dropLast(32) // sizeof(SHA3-256 digest)
|
||||
let publicKeyHashBytes = integrityCheckedRepresentation.dropFirst(32)
|
||||
let publicKeyHash = SHA3_256Digest { output in
|
||||
for region in publicKeyHashBytes.regions {
|
||||
region.withUnsafeBytes {
|
||||
output.append(contentsOf: $0.bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashBytes))
|
||||
|
||||
self = try XWingMLKEM768X25519.PrivateKey.init(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension XWingMLKEM768X25519.PublicKey: HPKEKEMPublicKey {
|
||||
/// The type of the ephemeral private key associated with this public key.
|
||||
public typealias EphemeralPrivateKey = XWingMLKEM768X25519.PrivateKey
|
||||
@@ -236,5 +169,4 @@ extension XWingMLKEM768X25519.PublicKey: HPKEKEMPublicKey {
|
||||
/// The type of the ephemeral private key associated with this public key.
|
||||
public typealias HPKEEphemeralPrivateKey = XWingMLKEM768X25519.PrivateKey
|
||||
}
|
||||
#endif
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -27,12 +26,8 @@ public import Foundation
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol DiffieHellmanKeyAgreement: Sendable {
|
||||
/// The public key share type to perform the DH Key Agreement
|
||||
associatedtype PublicKey: Sendable
|
||||
@@ -63,18 +58,10 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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 +119,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SharedSecret: Hashable {
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
ss.withUnsafeBytes { hasher.combine(bytes: $0) }
|
||||
@@ -144,11 +127,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SharedSecret: Equatable {
|
||||
public static func == (lhs: Self, rhs: Self) -> Bool {
|
||||
return safeCompare(lhs, rhs)
|
||||
@@ -174,11 +153,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SharedSecret: CustomStringConvertible {
|
||||
public var description: String {
|
||||
return "\(Self.self): \(ss.hexString)"
|
||||
@@ -186,11 +161,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension HashFunction {
|
||||
// A wrapper function to keep the unsafe code in one place.
|
||||
mutating func update(_ secret: SharedSecret) {
|
||||
|
||||
@@ -14,27 +14,20 @@
|
||||
#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
|
||||
#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 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias NISTCurvePrivateKeyImpl = CoreCryptoNISTCurvePrivateKeyImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias NISTCurvePublicKeyImpl = OpenSSLNISTCurvePublicKeyImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@@ -48,18 +41,16 @@ public import Foundation
|
||||
// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P256 {
|
||||
|
||||
/// A mechanism used to create or verify a cryptographic signature using
|
||||
/// the NIST P-256 elliptic curve digital signature algorithm (ECDSA).
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Signing: Sendable {
|
||||
|
||||
/// A P-256 public key used to verify cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: NISTECPublicKey, Sendable {
|
||||
var impl: NISTCurvePublicKeyImpl<P256>
|
||||
|
||||
@@ -97,11 +88,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 +98,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 +112,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 +132,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 +146,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
|
||||
@@ -193,6 +154,7 @@ extension P256 {
|
||||
}
|
||||
|
||||
/// A P-256 private key used to create cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: NISTECPrivateKey, Sendable {
|
||||
let impl: NISTCurvePrivateKeyImpl<P256>
|
||||
|
||||
@@ -227,26 +189,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 +216,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 +247,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 +258,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,19 +267,17 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum KeyAgreement: Sendable {
|
||||
|
||||
/// A P-256 public key used for key agreement.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: NISTECPublicKey, Sendable {
|
||||
var impl: NISTCurvePublicKeyImpl<P256>
|
||||
|
||||
@@ -384,11 +315,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 +325,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 +339,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 +359,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 +373,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
|
||||
@@ -480,6 +381,7 @@ extension P256 {
|
||||
}
|
||||
|
||||
/// A P-256 private key used for key agreement.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: NISTECPrivateKey, Sendable {
|
||||
let impl: NISTCurvePrivateKeyImpl<P256>
|
||||
|
||||
@@ -514,26 +416,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 +443,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 +474,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 +485,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,18 +494,16 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P384 {
|
||||
|
||||
/// A mechanism used to create or verify a cryptographic signature using
|
||||
/// the NIST P-384 elliptic curve digital signature algorithm (ECDSA).
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Signing: Sendable {
|
||||
|
||||
/// A P-384 public key used to verify cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: NISTECPublicKey, Sendable {
|
||||
var impl: NISTCurvePublicKeyImpl<P384>
|
||||
|
||||
@@ -670,11 +541,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 +551,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 +565,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 +585,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 +599,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
|
||||
@@ -766,6 +607,7 @@ extension P384 {
|
||||
}
|
||||
|
||||
/// A P-384 private key used to create cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: NISTECPrivateKey, Sendable {
|
||||
let impl: NISTCurvePrivateKeyImpl<P384>
|
||||
|
||||
@@ -800,26 +642,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 +669,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 +700,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 +711,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,19 +720,17 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum KeyAgreement: Sendable {
|
||||
|
||||
/// A P-384 public key used for key agreement.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: NISTECPublicKey, Sendable {
|
||||
var impl: NISTCurvePublicKeyImpl<P384>
|
||||
|
||||
@@ -957,11 +768,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 +778,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 +792,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 +812,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 +826,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
|
||||
@@ -1053,6 +834,7 @@ extension P384 {
|
||||
}
|
||||
|
||||
/// A P-384 private key used for key agreement.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: NISTECPrivateKey, Sendable {
|
||||
let impl: NISTCurvePrivateKeyImpl<P384>
|
||||
|
||||
@@ -1087,26 +869,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 +896,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 +927,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 +938,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,18 +947,16 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P521 {
|
||||
|
||||
/// A mechanism used to create or verify a cryptographic signature using
|
||||
/// the NIST P-521 elliptic curve digital signature algorithm (ECDSA).
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Signing: Sendable {
|
||||
|
||||
/// A P-521 public key used to verify cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: NISTECPublicKey, Sendable {
|
||||
var impl: NISTCurvePublicKeyImpl<P521>
|
||||
|
||||
@@ -1243,11 +994,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 +1004,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 +1018,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 +1038,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 +1052,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
|
||||
@@ -1339,6 +1060,7 @@ extension P521 {
|
||||
}
|
||||
|
||||
/// A P-521 private key used to create cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: NISTECPrivateKey, Sendable {
|
||||
let impl: NISTCurvePrivateKeyImpl<P521>
|
||||
|
||||
@@ -1373,26 +1095,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 +1122,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 +1153,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 +1164,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,19 +1173,17 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum KeyAgreement: Sendable {
|
||||
|
||||
/// A P-521 public key used for key agreement.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: NISTECPublicKey, Sendable {
|
||||
var impl: NISTCurvePublicKeyImpl<P521>
|
||||
|
||||
@@ -1530,11 +1221,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 +1231,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 +1245,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 +1265,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 +1279,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
|
||||
@@ -1626,6 +1287,7 @@ extension P521 {
|
||||
}
|
||||
|
||||
/// A P-521 private key used for key agreement.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: NISTECPrivateKey, Sendable {
|
||||
let impl: NISTCurvePrivateKeyImpl<P521>
|
||||
|
||||
@@ -1660,26 +1322,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 +1349,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 +1380,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 +1391,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 +1401,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P256.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement {
|
||||
/// Computes a shared secret with the provided public key from another party.
|
||||
///
|
||||
@@ -1781,7 +1410,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
#else
|
||||
return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
@@ -1789,11 +1418,7 @@ extension P256.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement {
|
||||
}
|
||||
}
|
||||
// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P384.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement {
|
||||
/// Computes a shared secret with the provided public key from another party.
|
||||
///
|
||||
@@ -1802,7 +1427,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
#else
|
||||
return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
@@ -1810,11 +1435,7 @@ extension P384.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement {
|
||||
}
|
||||
}
|
||||
// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P521.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement {
|
||||
/// Computes a shared secret with the provided public key from another party.
|
||||
///
|
||||
@@ -1823,7 +1444,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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
#else
|
||||
return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
|
||||
@@ -14,18 +14,8 @@
|
||||
#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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
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
|
||||
@@ -34,7 +24,6 @@ typealias NISTCurvePrivateKeyImpl = OpenSSLNISTCurvePrivateKeyImpl
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -55,11 +44,6 @@ public import Foundation
|
||||
%{ 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":
|
||||
@@ -116,11 +100,6 @@ extension ${CURVE} {
|
||||
/// - 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)
|
||||
}
|
||||
@@ -131,11 +110,6 @@ extension ${CURVE} {
|
||||
///
|
||||
/// - 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 {
|
||||
@@ -150,11 +124,6 @@ extension ${CURVE} {
|
||||
///
|
||||
/// - 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)
|
||||
@@ -175,19 +144,9 @@ extension ${CURVE} {
|
||||
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()
|
||||
@@ -199,11 +158,6 @@ extension ${CURVE} {
|
||||
|
||||
#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
|
||||
@@ -250,26 +204,12 @@ extension ${CURVE} {
|
||||
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)
|
||||
|
||||
@@ -291,11 +231,6 @@ extension ${CURVE} {
|
||||
///
|
||||
/// - 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)
|
||||
|
||||
@@ -327,11 +262,6 @@ extension ${CURVE} {
|
||||
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()
|
||||
@@ -343,11 +273,6 @@ extension ${CURVE} {
|
||||
|
||||
#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
|
||||
@@ -361,11 +286,6 @@ extension ${CURVE} {
|
||||
|
||||
% 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.
|
||||
///
|
||||
@@ -374,7 +294,7 @@ extension ${CURVE}.KeyAgreement.PrivateKey: DiffieHellmanKeyAgreement {
|
||||
/// 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
#else
|
||||
return try self.openSSLSharedSecretFromKeyAgreement(with: publicKeyShare)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -26,7 +25,9 @@ public import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
internal struct ANSIKDFx963<H: HashFunction>: Sendable {
|
||||
@_spi(ANSIKDF)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct ANSIKDFx963<H: HashFunction>: Sendable {
|
||||
public static func deriveKey<Info: DataProtocol>(inputKeyMaterial: SymmetricKey, info: Info, outputByteCount: Int) -> SymmetricKey {
|
||||
|
||||
guard UInt64(outputByteCount) < (UInt64(H.Digest.byteCount) * UInt64(UInt32.max)) else {
|
||||
@@ -43,11 +44,7 @@ internal struct ANSIKDFx963<H: HashFunction>: Sendable {
|
||||
// 1. Compute: Ki = Hash(Z || Counter || [SharedInfo]).
|
||||
var hasher = H()
|
||||
inputKeyMaterial.withUnsafeBytes { ikmBytes in
|
||||
#if CRYPTOKIT_URBP_LACKS_CONFORMANCE
|
||||
hasher.update(bufferPointer: ikmBytes)
|
||||
#else
|
||||
hasher.update(data: ikmBytes)
|
||||
#endif
|
||||
}
|
||||
hasher.update(counter.bigEndian)
|
||||
hasher.update(data: info)
|
||||
@@ -59,7 +56,7 @@ internal struct ANSIKDFx963<H: HashFunction>: Sendable {
|
||||
// Append the bytes of the digest. We don't want to append more than the remaining number of bytes.
|
||||
let bytesToAppend = min(remainingBytes, H.Digest.byteCount)
|
||||
digest.withUnsafeBytes { digestPtr in
|
||||
key.append(digestPtr.bytes.extracting(first: bytesToAppend))
|
||||
key.append(digestPtr.prefix(bytesToAppend))
|
||||
}
|
||||
remainingBytes -= bytesToAppend
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -41,44 +40,8 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - inputKeyMaterial: The main key or passcode the derivation function
|
||||
/// uses to derive a key.
|
||||
/// - salt: The salt to use for key derivation.
|
||||
/// - 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
|
||||
public static func deriveKey(inputKeyMaterial: SymmetricKey,
|
||||
salt: RawSpan? = nil,
|
||||
info: RawSpan? = nil,
|
||||
output outputKey: inout OutputRawSpan) {
|
||||
let code = extract(inputKeyMaterial: inputKeyMaterial, salt: salt)
|
||||
code.withUnsafeBytes { codeBytes in
|
||||
expand(
|
||||
pseudoRandomKey: codeBytes.bytes,
|
||||
info: info,
|
||||
into: &outputKey
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Derives a symmetric encryption key from a main key or passcode using
|
||||
/// HKDF key derivation with information and salt you specify.
|
||||
///
|
||||
@@ -90,43 +53,11 @@ 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,
|
||||
info: Info,
|
||||
outputByteCount: Int
|
||||
) -> SymmetricKey {
|
||||
// Fast path: Both salt and info have a single contiguous region, so
|
||||
// go directly to the span.
|
||||
if salt.regions.count == 1 && info.regions.count == 1 {
|
||||
return salt.regions.first!.withUnsafeBytes { saltBytes in
|
||||
info.regions.first!.withUnsafeBytes { infoBytes in
|
||||
SymmetricKey(size: SymmetricKeySize(bitCount: outputByteCount * 8)) { output in
|
||||
deriveKey(
|
||||
inputKeyMaterial: inputKeyMaterial,
|
||||
salt: saltBytes.bytes,
|
||||
info: infoBytes.bytes,
|
||||
output: &output
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Turn salt and info into contiguous regions, then recurse.
|
||||
let contiguousSalt = Array(salt)
|
||||
let contiguousInfo = Array(info)
|
||||
return deriveKey(
|
||||
inputKeyMaterial: inputKeyMaterial,
|
||||
salt: contiguousSalt,
|
||||
info: contiguousInfo,
|
||||
outputByteCount: outputByteCount
|
||||
)
|
||||
public static func deriveKey<Salt: DataProtocol, Info: DataProtocol>(inputKeyMaterial: SymmetricKey,
|
||||
salt: Salt,
|
||||
info: Info,
|
||||
outputByteCount: Int) -> SymmetricKey {
|
||||
return expand(pseudoRandomKey: extract(inputKeyMaterial: inputKeyMaterial, salt: salt), info: info, outputByteCount: outputByteCount)
|
||||
}
|
||||
|
||||
/// Derives a symmetric encryption key from a main key or passcode using
|
||||
@@ -139,11 +70,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 +86,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 +101,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 +120,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 {
|
||||
@@ -222,36 +133,9 @@ public struct HKDF<H: HashFunction>: Sendable {
|
||||
key = SymmetricKey(data: [UInt8]())
|
||||
}
|
||||
|
||||
return HMAC<H>.authenticationCode(for: inputKeyMaterial.bytes, using: key)
|
||||
}
|
||||
|
||||
/// Creates cryptographically strong key material from a main key or
|
||||
/// passcode that you specify.
|
||||
///
|
||||
/// Generate a derived symmetric key from the cryptographically strong key
|
||||
/// material this function creates by calling
|
||||
/// ``expand(pseudoRandomKey:info:outputByteCount:)``.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - inputKeyMaterial: The main key or passcode the derivation function
|
||||
/// uses to derive a key.
|
||||
/// - salt: The salt to use for key derivation.
|
||||
///
|
||||
/// - 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)
|
||||
} else {
|
||||
SymmetricKey(data: SecureBytes())
|
||||
return inputKeyMaterial.withUnsafeBytes { ikmBytes in
|
||||
return HMAC<H>.authenticationCode(for: ikmBytes, using: key)
|
||||
}
|
||||
|
||||
return HMAC<H>.authenticationCode(for: inputKeyMaterial.bytes, using: key)
|
||||
}
|
||||
|
||||
/// Expands cryptographically strong key material into a derived symmetric
|
||||
@@ -267,78 +151,28 @@ 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
|
||||
guard let info else {
|
||||
self.expand(pseudoRandomKey: prkBuffer.bytes, info: nil, into: &output)
|
||||
return
|
||||
}
|
||||
|
||||
if info.regions.count == 1 {
|
||||
info.regions.first!.withUnsafeBytes { infoBuffer in
|
||||
self.expand(pseudoRandomKey: prkBuffer.bytes, info: infoBuffer.bytes, into: &output)
|
||||
}
|
||||
} else {
|
||||
let contiguous = ContiguousArray(info)
|
||||
self.expand(pseudoRandomKey: prkBuffer.bytes, info: contiguous.span.bytes, into: &output)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Expands cryptographically strong key material into a derived symmetric
|
||||
/// key.
|
||||
///
|
||||
/// Generate cryptographically strong key material to use with this function
|
||||
/// by calling ``extract(inputKeyMaterial:salt:)``.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - prk: A pseudorandom, cryptographically strong key generated from the
|
||||
/// ``extract(inputKeyMaterial:salt:)`` function.
|
||||
/// - info: The shared information to use for key derivation.
|
||||
/// - 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
|
||||
public static func expand(pseudoRandomKey prk: RawSpan, info: RawSpan?, into output: inout OutputRawSpan) {
|
||||
let iterations: UInt8 = UInt8((Double(output.freeCapacity) / Double(H.Digest.byteCount)).rounded(.up))
|
||||
|
||||
let iterations: UInt8 = UInt8((Double(outputByteCount) / Double(H.Digest.byteCount)).rounded(.up))
|
||||
|
||||
let key = SymmetricKey(bytes: prk)
|
||||
var lastIterationBytes = 0
|
||||
var output = SecureBytes()
|
||||
let key = SymmetricKey(data: prk)
|
||||
var TMinusOne = SecureBytes()
|
||||
for i in 1...iterations {
|
||||
var hmac = HMAC<H>(key: key)
|
||||
hmac.update(bytes: output.bytes.extracting(last: lastIterationBytes))
|
||||
hmac.update(data: TMinusOne)
|
||||
if let info {
|
||||
hmac.update(bytes: info)
|
||||
hmac.update(data: info)
|
||||
}
|
||||
|
||||
// hmac.update(bytes: CollectionOfOne(i).span.bytes)
|
||||
var buf = i
|
||||
withUnsafeBytes(of: &buf) { rawBuffer in
|
||||
let span = RawSpan(_unsafeBytes: rawBuffer)
|
||||
hmac.update(bytes: span)
|
||||
}
|
||||
hmac.finalize().withUnsafeBytes {
|
||||
let bytesToAppend = $0.bytes.extracting(first: output.freeCapacity)
|
||||
output.append(contentsOf: bytesToAppend)
|
||||
lastIterationBytes = bytesToAppend.byteCount
|
||||
|
||||
withUnsafeBytes(of: i) { counter in
|
||||
hmac.update(bufferPointer: counter)
|
||||
}
|
||||
TMinusOne = SecureBytes(bytes: hmac.finalize())
|
||||
output.append(TMinusOne)
|
||||
}
|
||||
|
||||
return SymmetricKey(data: output.prefix(outputByteCount))
|
||||
}
|
||||
}
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -26,17 +26,18 @@ public import Foundation
|
||||
#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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias AESWRAPImpl = CoreCryptoAESWRAPImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias AESWRAPImpl = BoringSSLAESWRAPImpl
|
||||
#endif
|
||||
|
||||
@available(iOS 15.0, macOS 12.0, watchOS 8.0, tvOS 15.0, macCatalyst 15.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum KeyWrap: Sendable {
|
||||
/// Wraps a key using the AES wrap algorithm.
|
||||
///
|
||||
@@ -48,7 +49,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,7 +64,6 @@ 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)
|
||||
}
|
||||
|
||||
@@ -74,10 +74,6 @@ struct OpenSSLNISTCurvePrivateKeyImpl<Curve: OpenSSLSupportedNISTCurve>: Sendabl
|
||||
self.key = try BoringSSLECPrivateKeyWrapper(rawRepresentation: data)
|
||||
}
|
||||
|
||||
init(seed: Data, compactRepresentable: Bool) throws {
|
||||
fatalError("unimplemented on this platform")
|
||||
}
|
||||
|
||||
func publicKey() -> OpenSSLNISTCurvePublicKeyImpl<Curve> {
|
||||
OpenSSLNISTCurvePublicKeyImpl(wrapping: self.key.publicKey)
|
||||
}
|
||||
|
||||
@@ -15,10 +15,6 @@
|
||||
@_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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Curve25519: Sendable {}
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -25,22 +24,20 @@ public import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Curve25519.Signing {
|
||||
static var keyByteCount: Int {
|
||||
return 32
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Curve25519 {
|
||||
/// A mechanism used to create or verify a cryptographic signature using
|
||||
/// Ed25519.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum Signing: Sendable {
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
typealias Curve25519PrivateKeyImpl = Curve25519.Signing.CoreCryptoCurve25519PrivateKeyImpl
|
||||
typealias Curve25519PublicKeyImpl = Curve25519.Signing.CoreCryptoCurve25519PublicKeyImpl
|
||||
#else
|
||||
@@ -49,6 +46,7 @@ extension Curve25519 {
|
||||
#endif
|
||||
|
||||
/// A Curve25519 private key used to create cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: ECPrivateKey, Sendable {
|
||||
private var baseKey: Curve25519.Signing.Curve25519PrivateKeyImpl
|
||||
|
||||
@@ -84,6 +82,7 @@ extension Curve25519 {
|
||||
}
|
||||
|
||||
/// A Curve25519 public key used to verify cryptographic signatures.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: Sendable {
|
||||
private var baseKey: Curve25519.Signing.Curve25519PublicKeyImpl
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -25,57 +24,51 @@ import Foundation
|
||||
#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 (!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 SupportedCurveDetailsImpl = CorecryptoSupportedNISTCurve
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias SupportedCurveDetailsImpl = OpenSSLSupportedNISTCurve
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol ECPublicKey {
|
||||
init <Bytes: ContiguousBytes>(rawRepresentation: Bytes) throws(CryptoKitMetaError)
|
||||
init <Bytes: ContiguousBytes>(rawRepresentation: Bytes) throws
|
||||
var rawRepresentation: Data { get }
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol ECPrivateKey {
|
||||
associatedtype PK
|
||||
var publicKey: PK { get }
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol NISTECPublicKey: ECPublicKey {
|
||||
init<Bytes: ContiguousBytes>(compactRepresentation: Bytes) throws(CryptoKitMetaError)
|
||||
init<Bytes: ContiguousBytes>(compressedRepresentation: Bytes) throws(CryptoKitMetaError)
|
||||
init<Bytes: ContiguousBytes>(x963Representation: Bytes) throws(CryptoKitMetaError)
|
||||
init<Bytes: ContiguousBytes>(compactRepresentation: Bytes) throws
|
||||
init<Bytes: ContiguousBytes>(compressedRepresentation: Bytes) throws
|
||||
init<Bytes: ContiguousBytes>(x963Representation: Bytes) throws
|
||||
|
||||
var compactRepresentation: Data? { get }
|
||||
var x963Representation: Data { get }
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol NISTECPrivateKey: ECPrivateKey where PK: NISTECPublicKey {
|
||||
init <Bytes: ContiguousBytes>(rawRepresentation: Bytes) throws(CryptoKitMetaError)
|
||||
init <Bytes: ContiguousBytes>(rawRepresentation: Bytes) throws
|
||||
var rawRepresentation: Data { get }
|
||||
}
|
||||
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum P521: Sendable { }
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -25,22 +24,20 @@ public import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Curve25519.KeyAgreement {
|
||||
static var keyByteCount: Int {
|
||||
return 32
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Curve25519 {
|
||||
/// A mechanism used to create a shared secret between two users by
|
||||
/// performing X25519 key agreement.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum KeyAgreement: Sendable {
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
typealias Curve25519PrivateKeyImpl = Curve25519.KeyAgreement.CoreCryptoCurve25519PrivateKeyImpl
|
||||
typealias Curve25519PublicKeyImpl = Curve25519.KeyAgreement.CoreCryptoCurve25519PublicKeyImpl
|
||||
#else
|
||||
@@ -49,11 +46,7 @@ extension Curve25519 {
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: ECPublicKey, Sendable {
|
||||
fileprivate var baseKey: Curve25519PublicKeyImpl
|
||||
|
||||
@@ -93,11 +86,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: DiffieHellmanKeyAgreement, Sendable {
|
||||
fileprivate var baseKey: Curve25519PrivateKeyImpl
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -33,11 +32,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SymmetricKeySize: Sendable {
|
||||
/// The number of bits in the key.
|
||||
public let bitCount: Int
|
||||
@@ -78,11 +73,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct SymmetricKey: ContiguousBytes, Sendable {
|
||||
let sb: SecureBytes
|
||||
|
||||
@@ -104,16 +95,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
|
||||
}
|
||||
|
||||
/// Creates a key from the given data.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -122,36 +103,6 @@ public struct SymmetricKey: ContiguousBytes, Sendable {
|
||||
self.init(key: SecureBytes(bytes: data))
|
||||
}
|
||||
|
||||
/// Creates a key from the given data.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - bytes: The span of bytes from which to create the key.
|
||||
///
|
||||
/// Note: historical version of init(copying:) below, SPI only.
|
||||
@inlinable
|
||||
internal init(bytes: RawSpan) {
|
||||
self = bytes.withUnsafeBytes { SymmetricKey(data: $0) }
|
||||
}
|
||||
|
||||
/// Creates a key from the given data.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - bytes: The span of bytes from which to create the key.
|
||||
@inlinable
|
||||
public init(copying bytes: RawSpan) {
|
||||
self = bytes.withUnsafeBytes { SymmetricKey(data: $0) }
|
||||
}
|
||||
|
||||
/// Creates a key from the given data, zeroing out the bytes afterward.
|
||||
///
|
||||
/// - 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() }
|
||||
}
|
||||
|
||||
/// Generates a new random key of the given size.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -173,30 +124,6 @@ public struct SymmetricKey: ContiguousBytes, Sendable {
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Create a symmetric key with a closure that will initialize the memory.
|
||||
internal init<E: Error>(capacity: Int, initializingWith callback: (inout OutputRawSpan) throws(E) -> Void) throws(E) {
|
||||
self.init(key: try SecureBytes(capacity: capacity, initializingWith: callback))
|
||||
}
|
||||
|
||||
/// Creates a new key of the given size where the key contents are initialized via a callback.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - size: The size of the key to generate. You can use one of the standard
|
||||
/// sizes, like ``SymmetricKeySize/bits256``, or you can create a key of
|
||||
/// custom length by initializing a ``SymmetricKeySize`` instance with a
|
||||
/// non-standard value.
|
||||
/// - 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)
|
||||
}
|
||||
|
||||
// Fast-path alias for cases whe know we have a SecureBytes object.
|
||||
internal init(data: SecureBytes) {
|
||||
self.init(key: data)
|
||||
@@ -218,11 +145,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SymmetricKey: Equatable {
|
||||
public static func == (lhs: Self, rhs: Self) -> Bool {
|
||||
return safeCompare(lhs, rhs)
|
||||
|
||||
@@ -12,24 +12,31 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
#if os(Windows)
|
||||
import ucrt
|
||||
#elseif canImport(Darwin)
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import Musl
|
||||
#elseif canImport(Android)
|
||||
import Android
|
||||
#elseif canImport(WASILibc)
|
||||
import WASILibc
|
||||
#endif
|
||||
public import FoundationEssentials
|
||||
#else
|
||||
public import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/// A hash-based message authentication algorithm.
|
||||
///
|
||||
/// Use hash-based message authentication to create a code with a value that’s
|
||||
@@ -43,11 +50,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct HMAC<H: HashFunction>: MACAlgorithm, Sendable {
|
||||
/// An alias for the symmetric key type used to compute or verify a message
|
||||
/// authentication code.
|
||||
@@ -79,32 +82,39 @@ public struct HMAC<H: HashFunction>: MACAlgorithm, Sendable {
|
||||
#if os(iOS) && (arch(arm) || arch(i386))
|
||||
fatalError("Unsupported architecture")
|
||||
#else
|
||||
var innerKey = SecureBytes(capacity: H.blockByteCount) { keyOutput in
|
||||
if key.byteCount <= keyOutput.freeCapacity {
|
||||
keyOutput.append(contentsOf: key.bytes)
|
||||
} else if key.byteCount > H.blockByteCount {
|
||||
let hash = H.hash(bytes: key.bytes)
|
||||
hash.withUnsafeBytes {
|
||||
keyOutput.append(contentsOf: $0.bytes)
|
||||
}
|
||||
var K: SymmetricKey
|
||||
if key.byteCount == H.blockByteCount {
|
||||
K = key
|
||||
} else if key.byteCount > H.blockByteCount {
|
||||
var array = Array(repeating: UInt8(0), count: H.blockByteCount)
|
||||
|
||||
K = key.withUnsafeBytes { (keyBytes) in
|
||||
let hash = H.hash(bufferPointer: keyBytes)
|
||||
|
||||
return SymmetricKey(data: hash.withUnsafeBytes({ (hashBytes) in
|
||||
memcpy(&array, hashBytes.baseAddress!, hashBytes.count)
|
||||
return array
|
||||
}))
|
||||
}
|
||||
keyOutput.append(repeating: 0, count: keyOutput.freeCapacity, as: UInt8.self)
|
||||
} else {
|
||||
var keyArray = Array(repeating: UInt8(0), count: H.blockByteCount)
|
||||
key.withUnsafeBytes { keyArray.replaceSubrange(0..<$0.count, with: $0) }
|
||||
K = SymmetricKey(data: keyArray)
|
||||
}
|
||||
var outerKey = innerKey
|
||||
|
||||
self.innerHasher = H()
|
||||
innerKey.withUnsafeMutableBytes {
|
||||
for i in 0 ..< $0.count {
|
||||
$0[i] ^= 0x36
|
||||
}
|
||||
let innerKey = K.withUnsafeBytes {
|
||||
return $0.map({ (keyByte) in
|
||||
keyByte ^ 0x36
|
||||
})
|
||||
}
|
||||
innerHasher.update(bytes: innerKey.bytes)
|
||||
innerHasher.update(data: innerKey)
|
||||
|
||||
self.outerHasher = H()
|
||||
outerKey.withUnsafeMutableBytes {
|
||||
for i in 0 ..< $0.count {
|
||||
$0[i] ^= 0x5c
|
||||
}
|
||||
let outerKey = K.withUnsafeBytes {
|
||||
return $0.map({ (keyByte) in
|
||||
keyByte ^ 0x5c
|
||||
})
|
||||
}
|
||||
outerHasher.update(data: outerKey)
|
||||
#endif
|
||||
@@ -123,24 +133,6 @@ public struct HMAC<H: HashFunction>: MACAlgorithm, Sendable {
|
||||
return authenticator.finalize()
|
||||
}
|
||||
|
||||
/// Computes a message authentication code for the given data.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - data: The data for which to compute the authentication code.
|
||||
/// - 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)
|
||||
return authenticator.finalize()
|
||||
}
|
||||
|
||||
/// Returns a Boolean value indicating whether the given message
|
||||
/// authentication code is valid for a block of data.
|
||||
///
|
||||
@@ -166,11 +158,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,15 +177,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)
|
||||
}
|
||||
|
||||
/// Finalizes the message authentication computation and returns the
|
||||
/// computed code.
|
||||
///
|
||||
@@ -233,23 +211,10 @@ public struct HMAC<H: HashFunction>: MACAlgorithm, Sendable {
|
||||
let computedMac = authenticator.finalize()
|
||||
return safeCompare(authenticationCodeBytes, computedMac)
|
||||
}
|
||||
|
||||
private static func isValidAuthenticationCode<C: ContiguousBytes>(authenticationCodeBytes: C,
|
||||
authenticatedData: UnsafeRawBufferPointer,
|
||||
key: SymmetricKey) -> Bool {
|
||||
var authenticator = Self(key: key)
|
||||
authenticator.update(bufferPointer: authenticatedData)
|
||||
let computedMac = authenticator.finalize()
|
||||
return safeCompare(authenticationCodeBytes, computedMac)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct HashedAuthenticationCode<H: HashFunction>: MessageAuthenticationCode, Sendable {
|
||||
let digest: H.Digest
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -25,19 +24,11 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
associatedtype MAC: CryptoKit.MessageAuthenticationCode
|
||||
#endif
|
||||
#else
|
||||
associatedtype MAC: Crypto.MessageAuthenticationCode
|
||||
#endif
|
||||
@@ -58,11 +49,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MACAlgorithm {
|
||||
/// Computes a Message Authentication Code.
|
||||
///
|
||||
|
||||
@@ -12,15 +12,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_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
|
||||
@@ -32,30 +27,22 @@ public import Foundation
|
||||
#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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public protocol MessageAuthenticationCode: Hashable, ContiguousBytes, Sendable, CustomStringConvertible, Sequence where Element == UInt8 {
|
||||
/// The number of bytes in the message authentication code.
|
||||
var byteCount: Int { get }
|
||||
}
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MessageAuthenticationCode {
|
||||
/// Returns a Boolean value indicating whether two message authentication
|
||||
/// codes are equal.
|
||||
|
||||
@@ -15,20 +15,12 @@
|
||||
@_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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension AES {
|
||||
static func isValidKey(_ key: SymmetricKey) -> Bool {
|
||||
switch key.bitCount {
|
||||
|
||||
@@ -14,22 +14,20 @@
|
||||
#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 !CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
#else
|
||||
public import Foundation
|
||||
#endif
|
||||
#else
|
||||
public import SwiftSystem
|
||||
#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.
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol NISTECDSASignature {
|
||||
init<D: DataProtocol>(rawRepresentation: D) throws(CryptoKitMetaError)
|
||||
init<D: DataProtocol>(derRepresentation: D) throws(CryptoKitMetaError)
|
||||
@@ -37,7 +35,7 @@ protocol NISTECDSASignature {
|
||||
var rawRepresentation: Data { get }
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol NISTSigning {
|
||||
associatedtype PublicKey: NISTECPublicKey & DataValidator & DigestValidator
|
||||
associatedtype PrivateKey: NISTECPrivateKey & Signer
|
||||
@@ -45,10 +43,11 @@ protocol NISTSigning {
|
||||
}
|
||||
|
||||
// MARK: - P256 + Signing
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P256.Signing {
|
||||
|
||||
/// A P-256 elliptic curve digital signature algorithm (ECDSA) signature.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct ECDSASignature: ContiguousBytes, NISTECDSASignature, Sendable {
|
||||
|
||||
/// A raw data representation of a P-256 digital signature.
|
||||
@@ -146,11 +145,11 @@ extension P256.Signing {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P256.Signing: NISTSigning {}
|
||||
|
||||
// MARK: - P256 + PrivateKey
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,7 +160,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSignature(for: digest)
|
||||
#else
|
||||
return try self.openSSLSignature(for: digest)
|
||||
@@ -169,7 +168,7 @@ extension P256.Signing.PrivateKey: DigestSigner {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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 +184,7 @@ extension P256.Signing.PrivateKey: Signer {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,7 +195,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return self.coreCryptoIsValidSignature(signature, for: digest)
|
||||
#else
|
||||
return self.openSSLIsValidSignature(signature, for: digest)
|
||||
@@ -204,7 +203,7 @@ extension P256.Signing.PublicKey: DigestValidator {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,10 +219,11 @@ extension P256.Signing.PublicKey: DataValidator {
|
||||
}
|
||||
|
||||
// MARK: - P384 + Signing
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P384.Signing {
|
||||
|
||||
/// A P-384 elliptic curve digital signature algorithm (ECDSA) signature.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct ECDSASignature: ContiguousBytes, NISTECDSASignature, Sendable {
|
||||
|
||||
/// A raw data representation of a P-384 digital signature.
|
||||
@@ -321,11 +321,11 @@ extension P384.Signing {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P384.Signing: NISTSigning {}
|
||||
|
||||
// MARK: - P384 + PrivateKey
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,7 +336,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSignature(for: digest)
|
||||
#else
|
||||
return try self.openSSLSignature(for: digest)
|
||||
@@ -344,7 +344,7 @@ extension P384.Signing.PrivateKey: DigestSigner {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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 +360,7 @@ extension P384.Signing.PrivateKey: Signer {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,7 +371,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return self.coreCryptoIsValidSignature(signature, for: digest)
|
||||
#else
|
||||
return self.openSSLIsValidSignature(signature, for: digest)
|
||||
@@ -379,7 +379,7 @@ extension P384.Signing.PublicKey: DigestValidator {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,10 +395,11 @@ extension P384.Signing.PublicKey: DataValidator {
|
||||
}
|
||||
|
||||
// MARK: - P521 + Signing
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P521.Signing {
|
||||
|
||||
/// A P-521 elliptic curve digital signature algorithm (ECDSA) signature.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct ECDSASignature: ContiguousBytes, NISTECDSASignature, Sendable {
|
||||
|
||||
/// A raw data representation of a P-521 digital signature.
|
||||
@@ -496,11 +497,11 @@ extension P521.Signing {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension P521.Signing: NISTSigning {}
|
||||
|
||||
// MARK: - P521 + PrivateKey
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,7 +512,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSignature(for: digest)
|
||||
#else
|
||||
return try self.openSSLSignature(for: digest)
|
||||
@@ -519,7 +520,7 @@ extension P521.Signing.PrivateKey: DigestSigner {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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 +536,7 @@ extension P521.Signing.PrivateKey: Signer {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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,7 +547,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return self.coreCryptoIsValidSignature(signature, for: digest)
|
||||
#else
|
||||
return self.openSSLIsValidSignature(signature, for: digest)
|
||||
@@ -554,7 +555,7 @@ extension P521.Signing.PublicKey: DigestValidator {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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.
|
||||
|
||||
@@ -14,18 +14,15 @@
|
||||
#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 !CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
#else
|
||||
public import Foundation
|
||||
#endif
|
||||
#else
|
||||
public import SwiftSystem
|
||||
#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.
|
||||
@@ -40,7 +37,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
|
||||
@@ -55,7 +51,6 @@ protocol NISTSigning {
|
||||
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.
|
||||
@@ -156,11 +151,9 @@ extension ${CURVE}.Signing {
|
||||
}
|
||||
}
|
||||
|
||||
@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.
|
||||
@@ -171,7 +164,7 @@ extension ${CURVE}.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) -> ${CURVE}.Signing.ECDSASignature {
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSignature(for: digest)
|
||||
#else
|
||||
return try self.openSSLSignature(for: digest)
|
||||
@@ -179,7 +172,6 @@ extension ${CURVE}.Signing.PrivateKey: DigestSigner {
|
||||
}
|
||||
}
|
||||
|
||||
@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,
|
||||
@@ -195,7 +187,6 @@ extension ${CURVE}.Signing.PrivateKey: Signer {
|
||||
}
|
||||
}
|
||||
|
||||
@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.
|
||||
@@ -206,7 +197,7 @@ extension ${CURVE}.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: ${CURVE}.Signing.ECDSASignature, for digest: D) -> Bool {
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return self.coreCryptoIsValidSignature(signature, for: digest)
|
||||
#else
|
||||
return self.openSSLIsValidSignature(signature, for: digest)
|
||||
@@ -214,7 +205,6 @@ extension ${CURVE}.Signing.PublicKey: DigestValidator {
|
||||
}
|
||||
}
|
||||
|
||||
@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.
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
public import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
public import FoundationEssentials
|
||||
@@ -25,26 +24,26 @@ public import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Curve25519.Signing.PublicKey: DataValidator {
|
||||
typealias Signature = Data
|
||||
|
||||
@@ -57,7 +56,7 @@ 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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return self.coreCryptoIsValidSignature(signature, for: data)
|
||||
#else
|
||||
return self.openSSLIsValidSignature(signature, for: data)
|
||||
@@ -65,7 +64,7 @@ extension Curve25519.Signing.PublicKey: DataValidator {
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Curve25519.Signing.PrivateKey: Signer {
|
||||
/// Generates an EdDSA signature over Curve25519.
|
||||
///
|
||||
@@ -79,7 +78,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
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return try self.coreCryptoSignature(for: data)
|
||||
#else
|
||||
return try self.openSSLSignature(for: data)
|
||||
|
||||
@@ -21,29 +21,33 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLDSAPublicKeyImpl = CorecryptoMLDSAPublicKeyImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLDSAPrivateKeyImpl = CorecryptoMLDSAPrivateKeyImpl
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
typealias MLDSAPublicKeyImpl = OpenSSLMLDSAPublicKeyImpl
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum MLDSA65: Sendable {}
|
||||
|
||||
@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLDSA65 {
|
||||
/// The public key for MLDSA65.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: Sendable {
|
||||
var impl: MLDSAPublicKeyImpl<MLDSA65>
|
||||
|
||||
internal init(_ impl: MLDSAPublicKeyImpl<MLDSA65>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
@@ -51,7 +55,7 @@ extension MLDSA65 {
|
||||
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.
|
||||
@@ -82,24 +86,15 @@ extension MLDSA65 {
|
||||
}
|
||||
|
||||
/// The private key for MLDSA65.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: Signer, Sendable {
|
||||
internal let impl: MLDSAPrivateKeyImpl<MLDSA65>
|
||||
|
||||
|
||||
internal init(_ impl: MLDSAPrivateKeyImpl<MLDSA65>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Creates a random MLDSA65 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! MLDSA65.PrivateKey()
|
||||
/// ```
|
||||
|
||||
/// Initializes a new random private key.
|
||||
public init() throws {
|
||||
let impl = try MLDSAPrivateKeyImpl<MLDSA65>()
|
||||
self = PrivateKey(impl)
|
||||
@@ -120,7 +115,7 @@ extension MLDSA65 {
|
||||
}
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA65>(seedRepresentation: seedRepresentation, publicKeyRawRepresentation: publicKeyRawRepresentation)
|
||||
}
|
||||
|
||||
|
||||
/// The seed representation of the private key.
|
||||
///
|
||||
/// The seed representation is 32 bytes long, and is the parameter
|
||||
@@ -149,11 +144,11 @@ extension MLDSA65 {
|
||||
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)
|
||||
PublicKey(impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,7 +164,7 @@ extension MLDSA65 {
|
||||
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(copying: publicKeyHashData.bytes)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA65>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
@@ -187,19 +182,20 @@ 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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public enum MLDSA87: Sendable {}
|
||||
|
||||
@available(iOS 26.0, macOS 26.0, watchOS 26.0, tvOS 26.0, macCatalyst 26.0, visionOS 26.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MLDSA87 {
|
||||
/// The public key for MLDSA87.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PublicKey: Sendable {
|
||||
var impl: MLDSAPublicKeyImpl<MLDSA87>
|
||||
|
||||
internal init(_ impl: MLDSAPublicKeyImpl<MLDSA87>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
|
||||
/// Parses a public key from a serialized representation.
|
||||
///
|
||||
/// - Parameter rawRepresentation: The public key, in the FIPS 204 standard serialization format.
|
||||
@@ -207,7 +203,7 @@ extension MLDSA87 {
|
||||
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.
|
||||
@@ -238,24 +234,15 @@ extension MLDSA87 {
|
||||
}
|
||||
|
||||
/// The private key for MLDSA87.
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
public struct PrivateKey: Signer, Sendable {
|
||||
internal let impl: MLDSAPrivateKeyImpl<MLDSA87>
|
||||
|
||||
|
||||
internal init(_ impl: MLDSAPrivateKeyImpl<MLDSA87>) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
/// Creates a random MLDSA87 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! MLDSA87.PrivateKey()
|
||||
/// ```
|
||||
|
||||
/// Initializes a new random private key.
|
||||
public init() throws {
|
||||
let impl = try MLDSAPrivateKeyImpl<MLDSA87>()
|
||||
self = PrivateKey(impl)
|
||||
@@ -276,7 +263,7 @@ extension MLDSA87 {
|
||||
}
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA87>(seedRepresentation: seedRepresentation, publicKeyRawRepresentation: publicKeyRawRepresentation)
|
||||
}
|
||||
|
||||
|
||||
/// The seed representation of the private key.
|
||||
///
|
||||
/// The seed representation is 32 bytes long, and is the parameter
|
||||
@@ -305,11 +292,11 @@ extension MLDSA87 {
|
||||
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)
|
||||
PublicKey(impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,7 +312,7 @@ extension MLDSA87 {
|
||||
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(copying: publicKeyHashData.bytes)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLDSAPrivateKeyImpl<MLDSA87>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ public import Foundation
|
||||
}%
|
||||
|
||||
#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
|
||||
@@ -38,19 +37,13 @@ typealias MLDSAPrivateKeyImpl = OpenSSLMLDSAPrivateKeyImpl
|
||||
}%
|
||||
|
||||
/// 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.
|
||||
@@ -58,7 +51,7 @@ extension ${NAME} {
|
||||
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.
|
||||
@@ -91,22 +84,12 @@ extension ${NAME} {
|
||||
/// 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()
|
||||
/// ```
|
||||
|
||||
/// Initializes a new random private key.
|
||||
public init() throws {
|
||||
let impl = try MLDSAPrivateKeyImpl<${NAME}>()
|
||||
self = PrivateKey(impl)
|
||||
@@ -127,7 +110,7 @@ extension ${NAME} {
|
||||
}
|
||||
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
|
||||
@@ -156,11 +139,11 @@ extension ${NAME} {
|
||||
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)
|
||||
PublicKey(impl.publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +159,7 @@ extension ${NAME} {
|
||||
|
||||
let seed = Data(integrityCheckedRepresentation).subdata(in: 0..<seedSize)
|
||||
let publicKeyHashData = Data(integrityCheckedRepresentation).subdata(in: seedSize..<integrityCheckedRepresentation.count)
|
||||
let publicKeyHash = SHA3_256Digest(copying: publicKeyHashData.bytes)
|
||||
let publicKeyHash = SHA3_256Digest(bytes: [UInt8](publicKeyHashData))
|
||||
|
||||
self.impl = try MLDSAPrivateKeyImpl<${NAME}>(seedRepresentation: seed, publicKeyHash: publicKeyHash)
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -25,18 +24,18 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.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, *)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol Signer {
|
||||
associatedtype Signature
|
||||
func signature<D: DataProtocol>(for data: D) throws(CryptoKitMetaError) -> Signature
|
||||
|
||||
@@ -1,92 +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_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.
|
||||
init(copying bytes: RawSpan) {
|
||||
precondition(count == bytes.byteCount)
|
||||
self.init { outputSpan in
|
||||
for i in 0..<count {
|
||||
outputSpan.append(bytes.unsafeLoad(fromByteOffset: i, as: UInt8.self))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
extension Array where Element == UInt8 {
|
||||
/// Copy of the bytes of the given raw span into this array. The span
|
||||
/// must have exactly count bytes in it.
|
||||
init(copying bytes: RawSpan) {
|
||||
self.init(unsafeUninitializedCapacity: bytes.byteCount) { outputBuffer, initializedCount in
|
||||
bytes.withUnsafeBytes { inputBuffer in
|
||||
UnsafeMutableRawBufferPointer(outputBuffer).copyMemory(from: inputBuffer)
|
||||
}
|
||||
initializedCount = bytes.byteCount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ContiguousArray where Element == UInt8 {
|
||||
/// Copy of the bytes of the given raw span into this array. The span
|
||||
/// must have exactly count bytes in it.
|
||||
init(copying bytes: RawSpan) {
|
||||
self.init(unsafeUninitializedCapacity: bytes.byteCount) { outputBuffer, initializedCount in
|
||||
bytes.withUnsafeBytes { inputBuffer in
|
||||
UnsafeMutableRawBufferPointer(outputBuffer).copyMemory(from: inputBuffer)
|
||||
}
|
||||
initializedCount = bytes.byteCount
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension OutputRawSpan {
|
||||
/// Append the contents of the given raw span to this output span.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func append(contentsOf bytes: RawSpan) {
|
||||
for i in 0..<bytes.byteCount {
|
||||
append(bytes.unsafeLoad(fromByteOffset: i, as: UInt8.self))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension OutputSpan<UInt8> {
|
||||
/// Append the contents of the given raw span to this output span.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func append(contentsOf bytes: RawSpan) {
|
||||
for i in 0..<bytes.byteCount {
|
||||
append(bytes.unsafeLoad(fromByteOffset: i, as: UInt8.self))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeMutableRawBufferPointer {
|
||||
/// Copy the bytes from the source span into this beginning of this buffer.
|
||||
func copyBytes(from source: RawSpan) {
|
||||
precondition(source.byteCount <= self.count)
|
||||
for index in 0..<source.byteCount {
|
||||
self[index] = source.unsafeLoad(fromByteOffset: index, as: UInt8.self)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // Linux or !SwiftPM
|
||||
@@ -1,35 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
//
|
||||
// Copyright (c) 2026 Apple Inc. and the SwiftCrypto project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@_exported import CryptoKit
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
#else
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
// This doesn't seem to be present on Linux.
|
||||
extension InlineArray where Element == UInt8 {
|
||||
func withUnsafeBytes<R, E>(_ body: (UnsafeRawBufferPointer) throws(E) -> R) throws(E) -> R {
|
||||
try self.span.withUnsafeBytes(body)
|
||||
}
|
||||
|
||||
func withBytes<R, E>(_ body: (RawSpan) throws(E) -> R) throws(E) -> R where R: ~Copyable {
|
||||
try body(self.span.bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -18,7 +18,6 @@ import FoundationEssentials
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Optional where Wrapped: DataProtocol {
|
||||
func withUnsafeBytes<ReturnValue>(_ body: (UnsafeRawBufferPointer) throws -> ReturnValue) rethrows -> ReturnValue {
|
||||
if let self {
|
||||
|
||||
@@ -43,28 +43,4 @@ extension UnsafeMutableRawBufferPointer {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension MutableRawSpan {
|
||||
#if swift(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func initializeWithRandomBytes(count: Int) {
|
||||
self.withUnsafeMutableBytes { $0.initializeWithRandomBytes(count: count) }
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension OutputRawSpan {
|
||||
#if swift(<6.3)
|
||||
@_lifetime(self: copy self)
|
||||
#endif
|
||||
mutating func appendingRandomBytes(count: Int) {
|
||||
self.withUnsafeMutableBytes { buffer, initializedCount in
|
||||
UnsafeMutableRawBufferPointer(rebasing: buffer[initializedCount..<initializedCount + count])
|
||||
.initializeWithRandomBytes(count: count)
|
||||
initializedCount += count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@@ -22,5 +22,4 @@
|
||||
// See: https://github.com/apple/swift-crypto/issues/435
|
||||
|
||||
@usableFromInline
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func _cryptoModuleAnchor() {}
|
||||
@@ -1,86 +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
|
||||
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.
|
||||
init(copying bytes: RawSpan) {
|
||||
if bytes.byteCount == 0 {
|
||||
self = Data()
|
||||
} else {
|
||||
self = bytes.withUnsafeBytes { buffer in
|
||||
Data(
|
||||
UnsafeBufferPointer<UInt8>(
|
||||
start: buffer.baseAddress!.assumingMemoryBound(to: UInt8.self),
|
||||
count: buffer.count
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Append the contents of the given span to this Data instance.
|
||||
mutating func append(contentsOf bytes: RawSpan) {
|
||||
bytes.withUnsafeBytes { buffer in
|
||||
self.append(contentsOf: buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
for region in data.regions {
|
||||
region.withUnsafeBytes { bytes in
|
||||
outputSpan.append(contentsOf: bytes.bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#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
|
||||
@@ -13,7 +13,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -22,18 +21,23 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
enum ByteHexEncodingErrors: Error {
|
||||
case incorrectHexValue
|
||||
case incorrectString
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
let charA = UInt8(97 /* "a" */)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
let char0 = UInt8(48 /* "0" */)
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
private func itoh(_ value: UInt8) -> UInt8 {
|
||||
return (value > 9) ? (charA + value - 10) : (char0 + value)
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
private func htoi(_ value: UInt8) throws -> UInt8 {
|
||||
switch value {
|
||||
case char0...char0 + 9:
|
||||
@@ -46,11 +50,7 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension DataProtocol {
|
||||
var hexString: String {
|
||||
let hexLen = self.count * 2
|
||||
@@ -70,18 +70,15 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
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
|
||||
#if !CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Data {
|
||||
init(hexString: String) throws {
|
||||
self.init()
|
||||
@@ -101,6 +98,7 @@ extension Data {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Array where Element == UInt8 {
|
||||
init(hexString: String) throws {
|
||||
self.init()
|
||||
|
||||
@@ -12,15 +12,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -29,8 +24,9 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal func safeCompare<LHS: ContiguousBytes, RHS: ContiguousBytes>(_ lhs: LHS, _ rhs: RHS) -> Bool {
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#if (!CRYPTO_IN_SWIFTPM_FORCE_BUILD_API) || CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
return coreCryptoSafeCompare(lhs, rhs)
|
||||
#else
|
||||
return openSSLSafeCompare(lhs, rhs)
|
||||
|
||||
@@ -12,15 +12,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
#elseif CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#else
|
||||
#if canImport(FoundationEssentials)
|
||||
import FoundationEssentials
|
||||
@@ -29,14 +24,10 @@ import Foundation
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
nonisolated(unsafe) private let emptyStorage:SecureBytes.Backing = SecureBytes.Backing.createEmpty()
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct SecureBytes: @unchecked Sendable {
|
||||
var backing: Backing
|
||||
|
||||
@@ -52,24 +43,11 @@ struct SecureBytes: @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
init(bytes: RawSpan) {
|
||||
init<D: ContiguousBytes>(bytes: D) {
|
||||
self.backing = Backing.create(bytes: bytes)
|
||||
}
|
||||
|
||||
init<D: ContiguousBytes>(bytes: D) {
|
||||
self = bytes.withUnsafeBytes{ buffer in
|
||||
SecureBytes(bytes: buffer.bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a SecureBytes object with a closure that will initialize the memory.
|
||||
init<E: Error>(capacity: Int, initializingWith callback: (inout OutputRawSpan) throws(E) -> Void) throws(E) {
|
||||
self.backing = try Backing.create(capacity: capacity, initializingWith: callback)
|
||||
}
|
||||
|
||||
/// Allows initializing a SecureBytes object with a closure that will initialize the memory.
|
||||
///
|
||||
/// Note: Please use the safe counterpart, `init(capacity:initializingWith:)`.
|
||||
#if hasFeature(Embedded)
|
||||
init<E: Error>(unsafeUninitializedCapacity: Int, initializingWith callback: (inout UnsafeMutableRawBufferPointer, inout Int) throws(E) -> Void) throws(E) {
|
||||
self.backing = Backing.create(capacity: unsafeUninitializedCapacity)
|
||||
@@ -97,27 +75,17 @@ struct SecureBytes: @unchecked Sendable {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes {
|
||||
mutating func append(_ bytes: RawSpan) {
|
||||
let requiredCapacity = self.count + bytes.byteCount
|
||||
mutating func append<C: Collection>(_ data: C) where C.Element == UInt8 {
|
||||
let requiredCapacity = self.count + data.count
|
||||
let backingCapacity = self.backing.allocatedCapacity
|
||||
if !isKnownUniquelyReferenced(&self.backing) || requiredCapacity > backingCapacity {
|
||||
let newBacking = Backing.create(capacity: requiredCapacity)
|
||||
newBacking._appendBytes(self.backing, inRange: 0..<self.count)
|
||||
self.backing = newBacking
|
||||
}
|
||||
self.backing._appendBytes(bytes)
|
||||
}
|
||||
|
||||
mutating func append<D: ContiguousBytes>(_ bytes: D) {
|
||||
bytes.withUnsafeBytes { bytesBuffer in
|
||||
self.append(bytesBuffer.bytes)
|
||||
}
|
||||
self.backing._appendBytes(data)
|
||||
}
|
||||
|
||||
mutating func reserveCapacity(_ n: Int) {
|
||||
@@ -133,11 +101,7 @@ extension SecureBytes {
|
||||
}
|
||||
|
||||
// MARK: - Equatable conformance, constant-time
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: Equatable {
|
||||
public static func == (lhs: SecureBytes, rhs: SecureBytes) -> Bool {
|
||||
return safeCompare(lhs, rhs)
|
||||
@@ -145,12 +109,9 @@ extension SecureBytes: Equatable {
|
||||
}
|
||||
|
||||
// MARK: - Collection conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: Collection {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
struct Index {
|
||||
fileprivate var offset: Int
|
||||
|
||||
@@ -186,11 +147,7 @@ extension SecureBytes: Collection {
|
||||
}
|
||||
|
||||
// MARK: - BidirectionalCollection conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: BidirectionalCollection {
|
||||
func index(before index: Index) -> Index {
|
||||
return index.advanced(by: -1)
|
||||
@@ -198,27 +155,15 @@ extension SecureBytes: BidirectionalCollection {
|
||||
}
|
||||
|
||||
// MARK: - RandomAccessCollection conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: RandomAccessCollection { }
|
||||
|
||||
// MARK: - MutableCollection conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: MutableCollection { }
|
||||
|
||||
// MARK: - RangeReplaceableCollection conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: RangeReplaceableCollection {
|
||||
mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C) where C.Element == UInt8 {
|
||||
let requiredCapacity = self.backing.count - subrange.count + newElements.count
|
||||
@@ -247,7 +192,7 @@ extension SecureBytes: RangeReplaceableCollection {
|
||||
// The default implementation of this from RangeReplaceableCollection can't take advantage of `ContiguousBytes`, so we override it here
|
||||
mutating func append(contentsOf newElements: some Sequence<UInt8>) {
|
||||
let done:Void? = newElements.withContiguousStorageIfAvailable {
|
||||
self.append($0.span.bytes)
|
||||
replaceSubrange(endIndex..<endIndex, with: $0)
|
||||
}
|
||||
|
||||
if done == nil {
|
||||
@@ -259,34 +204,17 @@ extension SecureBytes: RangeReplaceableCollection {
|
||||
}
|
||||
|
||||
// MARK: - ContiguousBytes conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: ContiguousBytes {
|
||||
/// Access the stored bytes.
|
||||
var bytes: RawSpan {
|
||||
get {
|
||||
_overrideLifetime(self.backing.bytes, borrowing: self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Provide mutating access to the stored bytes.
|
||||
var mutableBytes: MutableRawSpan {
|
||||
@_lifetime(&self)
|
||||
mutating _read {
|
||||
if !isKnownUniquelyReferenced(&self.backing) {
|
||||
self.backing = Backing.create(copying: self.backing)
|
||||
}
|
||||
|
||||
yield backing.mutableBytes
|
||||
}
|
||||
}
|
||||
|
||||
#if hasFeature(Embedded)
|
||||
func withUnsafeBytes<T, E: Error>(_ body: (UnsafeRawBufferPointer) throws(E) -> T) throws(E) -> T {
|
||||
return try self.backing.withUnsafeBytes(body)
|
||||
}
|
||||
#else
|
||||
func withUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
|
||||
return try self.backing.withUnsafeBytes(body)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if hasFeature(Embedded)
|
||||
mutating func withUnsafeMutableBytes<T, E: Error>(_ body: (UnsafeMutableRawBufferPointer) throws(E) -> T) throws(E) -> T {
|
||||
@@ -318,11 +246,7 @@ extension SecureBytes: ContiguousBytes {
|
||||
}
|
||||
|
||||
// MARK: - DataProtocol conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: DataProtocol {
|
||||
var regions: CollectionOfOne<SecureBytes> {
|
||||
return CollectionOfOne(self)
|
||||
@@ -330,37 +254,21 @@ extension SecureBytes: DataProtocol {
|
||||
}
|
||||
|
||||
// MARK: - MutableDataProtocol conformance
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes: MutableDataProtocol { }
|
||||
|
||||
// MARK: - Index conformances
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes.Index: Hashable { }
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes.Index: Comparable {
|
||||
static func <(lhs: SecureBytes.Index, rhs: SecureBytes.Index) -> Bool {
|
||||
return lhs.offset < rhs.offset
|
||||
}
|
||||
}
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes.Index: Strideable {
|
||||
func advanced(by n: Int) -> SecureBytes.Index {
|
||||
return SecureBytes.Index(offset: self.offset + n)
|
||||
@@ -372,19 +280,17 @@ extension SecureBytes.Index: Strideable {
|
||||
}
|
||||
|
||||
// MARK: - Heap allocated backing storage.
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes {
|
||||
#if !hasFeature(Embedded)
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal struct BackingHeader {
|
||||
internal var count: Int
|
||||
|
||||
internal var capacity: Int
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal class Backing: ManagedBuffer<BackingHeader, UInt8> {
|
||||
|
||||
class func createEmpty() -> Backing {
|
||||
@@ -396,46 +302,29 @@ extension SecureBytes {
|
||||
return Backing.create(minimumCapacity: capacity, makingHeaderWith: { _ in BackingHeader(count: 0, capacity: capacity) }) as! Backing
|
||||
}
|
||||
|
||||
/// Create a new backing buffer with the given capacity, initializing its contents by calling the `body` closure
|
||||
/// with an output span. Clients should initialize the contents of the buffer via that output span.
|
||||
class func create<E>(capacity: Int, initializingWith body: (inout OutputRawSpan) throws(E) -> Void) throws(E) -> Backing {
|
||||
let backing = create(capacity: capacity)
|
||||
try backing._withVeryUnsafeMutableBytes { (overallocatedTargetPtr) throws(E) in
|
||||
// As Array does, we only produce an output span that covers the requested capacity,
|
||||
// not the over-allocated capacity.
|
||||
let targetPtr = overallocatedTargetPtr[0..<capacity]
|
||||
|
||||
var output = OutputRawSpan(
|
||||
buffer: targetPtr,
|
||||
initializedCount: 0
|
||||
)
|
||||
|
||||
try body(&output)
|
||||
backing.count = output.finalize(for: targetPtr)
|
||||
}
|
||||
return backing
|
||||
}
|
||||
|
||||
class func create(copying original: Backing) -> Backing {
|
||||
return Backing.create(bytes: original)
|
||||
}
|
||||
|
||||
final class func create(bytes: RawSpan) -> Backing {
|
||||
let backing = create(capacity: bytes.byteCount) { output in
|
||||
output.append(contentsOf: bytes)
|
||||
}
|
||||
precondition(backing.count <= backing.allocatedCapacity)
|
||||
return backing
|
||||
}
|
||||
|
||||
final class func create<D: ContiguousBytes>(bytes: D) -> Backing {
|
||||
return bytes.withUnsafeBytes { create(bytes: $0.bytes) }
|
||||
return bytes.withUnsafeBytes { bytesPtr in
|
||||
let backing = Backing.create(capacity: bytesPtr.count)
|
||||
backing._withVeryUnsafeMutableBytes { targetPtr in
|
||||
targetPtr.copyMemory(from: bytesPtr)
|
||||
}
|
||||
backing.count = bytesPtr.count
|
||||
precondition(backing.count <= backing.allocatedCapacity)
|
||||
return backing
|
||||
}
|
||||
}
|
||||
|
||||
class func create(randomBytes: Int) -> Backing {
|
||||
let backing = Backing.create(capacity: randomBytes) { output in
|
||||
output.appendingRandomBytes(count: randomBytes)
|
||||
let backing = Backing.create(capacity: randomBytes)
|
||||
backing._withVeryUnsafeMutableBytes { targetPtr in
|
||||
assert(targetPtr.count >= randomBytes)
|
||||
targetPtr.initializeWithRandomBytes(count: randomBytes)
|
||||
}
|
||||
backing.count = randomBytes
|
||||
return backing
|
||||
}
|
||||
|
||||
@@ -467,47 +356,9 @@ extension SecureBytes {
|
||||
return self.withUnsafeMutablePointerToElements { ($0 + offset).pointee = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
/// Access the bytes in the buffer. Note that this is only safe to use
|
||||
/// when you can ensure that no other code has mutating access to the
|
||||
/// buffer.
|
||||
@unsafe
|
||||
var bytes: RawSpan {
|
||||
_read {
|
||||
let buffer = self.withUnsafeMutablePointerToElements { pointer in
|
||||
UnsafeMutableBufferPointer(start: pointer, count: self.count)
|
||||
}
|
||||
|
||||
yield buffer.span.bytes
|
||||
}
|
||||
}
|
||||
|
||||
/// Provide mutating access to the bytes in the buffer. Note that this
|
||||
/// is only safe to use when you can ensure that no other code has
|
||||
/// access to the buffer.
|
||||
@unsafe
|
||||
var mutableBytes: MutableRawSpan {
|
||||
_read {
|
||||
let buffer = self.withUnsafeMutablePointerToElements { pointer in
|
||||
UnsafeMutableBufferPointer(start: pointer, count: self.count)
|
||||
}
|
||||
|
||||
var span = buffer.mutableSpan
|
||||
yield span.mutableBytes
|
||||
}
|
||||
|
||||
_modify {
|
||||
let buffer = self.withUnsafeMutablePointerToElements { pointer in
|
||||
UnsafeMutableBufferPointer(start: pointer, count: self.count)
|
||||
}
|
||||
|
||||
var span = buffer.mutableSpan
|
||||
var rawSpan = span.mutableBytes
|
||||
yield &rawSpan
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
internal class Backing {
|
||||
private var storage: UnsafeMutableRawBufferPointer
|
||||
|
||||
@@ -516,27 +367,6 @@ extension SecureBytes {
|
||||
storage.count
|
||||
}
|
||||
|
||||
var bytes: RawSpan {
|
||||
@_lifetime(self)
|
||||
_read {
|
||||
yield storage.bytes.extracting(first: count)
|
||||
}
|
||||
}
|
||||
|
||||
var mutableBytes: MutableRawSpan {
|
||||
_read {
|
||||
var mutableStorage = storage.mutableBytes
|
||||
let mutableBytes = mutableStorage._mutatingExtracting(first: count)
|
||||
yield mutableBytes
|
||||
}
|
||||
|
||||
_modify {
|
||||
var mutableStorage = storage.mutableBytes
|
||||
var mutableBytes = mutableStorage._mutatingExtracting(first: count)
|
||||
yield &mutableBytes
|
||||
}
|
||||
}
|
||||
|
||||
private init(storage: UnsafeMutableRawBufferPointer, count: Int) {
|
||||
self.storage = storage
|
||||
self.count = count
|
||||
@@ -552,43 +382,21 @@ extension SecureBytes {
|
||||
return Backing.init(storage: buffer, count: 0)
|
||||
}
|
||||
|
||||
/// Create a new backing buffer with the given capacity, initializing its contents by calling the `body` closure
|
||||
/// with an output span. Clients should initialize the contents of the buffer via that output span.
|
||||
final class func create<E>(capacity: Int, initializingWith body: (inout OutputRawSpan) throws(E) -> Void) throws(E) -> Backing {
|
||||
let backing = create(capacity: capacity)
|
||||
try backing._withVeryUnsafeMutableBytes { (overallocatedTargetPtr) throws(E) in
|
||||
// As Array does, we only produce an output span that covers the requested capacity,
|
||||
// not the over-allocated capacity.
|
||||
let targetPtr = overallocatedTargetPtr[0..<capacity]
|
||||
|
||||
var output = OutputRawSpan(
|
||||
buffer: targetPtr,
|
||||
initializedCount: 0
|
||||
)
|
||||
|
||||
try body(&output)
|
||||
backing.count = output.finalize(for: targetPtr)
|
||||
}
|
||||
return backing
|
||||
}
|
||||
|
||||
class func create(copying original: Backing) -> Backing {
|
||||
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: original.capacity, alignment: Int(CC_MAX_ALIGNMENT))
|
||||
buffer.copyBytes(from: original.storage)
|
||||
return Backing.init(storage: buffer, count: original.count)
|
||||
}
|
||||
|
||||
final class func create(bytes: RawSpan) -> Backing {
|
||||
let backing = create(capacity: bytes.byteCount) { output in
|
||||
output.append(contentsOf: bytes)
|
||||
}
|
||||
precondition(backing.count <= backing.allocatedCapacity)
|
||||
return backing
|
||||
}
|
||||
|
||||
final class func create<D: ContiguousBytes>(bytes: D) -> Backing {
|
||||
return bytes.withUnsafeBytes { bytesPtr in
|
||||
create(bytes: bytesPtr.bytes)
|
||||
let backing = Backing.create(capacity: bytesPtr.count)
|
||||
backing._withVeryUnsafeMutableBytes { targetPtr in
|
||||
targetPtr.copyMemory(from: bytesPtr)
|
||||
}
|
||||
backing.count = bytesPtr.count
|
||||
precondition(backing.count <= backing.capacity)
|
||||
return backing
|
||||
}
|
||||
}
|
||||
|
||||
@@ -604,8 +412,7 @@ extension SecureBytes {
|
||||
|
||||
deinit {
|
||||
// We always clear the whole capacity, even if we don't think we used it all.
|
||||
memset_s(storage.baseAddress!, storage.count, 0, storage.count)
|
||||
storage.deallocate()
|
||||
memset_s(storage.baseAddress, storage.count, 0, storage.count)
|
||||
}
|
||||
|
||||
subscript(offset offset: Int) -> UInt8 {
|
||||
@@ -622,11 +429,7 @@ extension SecureBytes {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes.Backing {
|
||||
var allocatedCapacity: Int {
|
||||
#if os(OpenBSD)
|
||||
@@ -678,24 +481,23 @@ extension SecureBytes.Backing {
|
||||
self.count += byteCount
|
||||
}
|
||||
|
||||
/// Appends the bytes of a collection to this storage, crashing if there is not enough room.
|
||||
fileprivate func _appendBytes(_ bytes: RawSpan) {
|
||||
let byteCount = bytes.byteCount
|
||||
|
||||
precondition(self.allocatedCapacity - self.count - byteCount >= 0, "Insufficient space for byte copying, must have reallocated!")
|
||||
|
||||
let lowerOffset = self.count
|
||||
self._withVeryUnsafeMutableBytes { bytesPtr in
|
||||
let innerPtrSlice = UnsafeMutableRawBufferPointer(rebasing: bytesPtr[lowerOffset...])
|
||||
innerPtrSlice.copyBytes(from: bytes)
|
||||
}
|
||||
self.count += byteCount
|
||||
}
|
||||
|
||||
/// Appends the bytes of a slice of another backing buffer to this storage, crashing if there
|
||||
/// is not enough room.
|
||||
fileprivate func _appendBytes(_ backing: SecureBytes.Backing, inRange range: Range<Int>) {
|
||||
self._appendBytes(backing.bytes.extracting(range))
|
||||
precondition(range.lowerBound >= 0)
|
||||
precondition(range.upperBound <= backing.allocatedCapacity)
|
||||
precondition(self.allocatedCapacity - self.count - range.count >= 0, "Insufficient space for byte copying, must have reallocated!")
|
||||
|
||||
backing.withUnsafeBytes { backingPtr in
|
||||
let ptrSlice = UnsafeRawBufferPointer(rebasing: backingPtr[range])
|
||||
|
||||
let lowerOffset = self.count
|
||||
self._withVeryUnsafeMutableBytes { bytesPtr in
|
||||
let innerPtrSlice = UnsafeMutableRawBufferPointer(rebasing: bytesPtr[lowerOffset...])
|
||||
innerPtrSlice.copyMemory(from: ptrSlice)
|
||||
}
|
||||
self.count += ptrSlice.count
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the range of bytes identified by the slice by the delta, crashing if the move would
|
||||
@@ -731,55 +533,50 @@ extension SecureBytes.Backing {
|
||||
}
|
||||
}
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension SecureBytes.Backing: ContiguousBytes {
|
||||
#if hasFeature(Embedded)
|
||||
func withUnsafeBytes<T, E: Error>(_ body: (UnsafeRawBufferPointer) throws(E) -> T) throws(E) -> T {
|
||||
let count = self.count
|
||||
|
||||
#if hasFeature(Embedded)
|
||||
return try storage.withUnsafeBytes { elementsPtr throws(E) in
|
||||
return try body(UnsafeRawBufferPointer(start: elementsPtr.baseAddress, count: count))
|
||||
}
|
||||
}
|
||||
#else
|
||||
return try self.withUnsafeMutablePointerToElements { elementsPtr throws(E) in
|
||||
func withUnsafeBytes<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T {
|
||||
let count = self.count
|
||||
return try self.withUnsafeMutablePointerToElements { elementsPtr in
|
||||
return try body(UnsafeRawBufferPointer(start: elementsPtr, count: count))
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if hasFeature(Embedded)
|
||||
|
||||
func withUnsafeMutableBytes<T, E: Error>(_ body: (UnsafeMutableRawBufferPointer) throws(E) -> T) throws(E) -> T {
|
||||
#if hasFeature(Embedded)
|
||||
return try body(UnsafeMutableRawBufferPointer(rebasing: storage[..<count]))
|
||||
#else
|
||||
return try body(storage)
|
||||
}
|
||||
#else
|
||||
func withUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
|
||||
let count = self.count
|
||||
|
||||
return try self.withUnsafeMutablePointerToElements { elementsPtr throws(E) in
|
||||
return try self.withUnsafeMutablePointerToElements { elementsPtr in
|
||||
return try body(UnsafeMutableRawBufferPointer(start: elementsPtr, count: count))
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if hasFeature(Embedded)
|
||||
/// Very unsafe in the sense that this points to uninitialized memory. Used only for implementations within this file.
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
func _withVeryUnsafeMutableBytes<T, E: Error>(_ body: (UnsafeMutableRawBufferPointer) throws(E) -> T) throws(E) -> T {
|
||||
return try body(storage)
|
||||
}
|
||||
#else
|
||||
/// Very unsafe in the sense that this points to uninitialized memory. Used only for implementations within this file.
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
func _withVeryUnsafeMutableBytes<T, E: Error>(_ body: (UnsafeMutableRawBufferPointer) throws(E) -> T) throws(E) -> T {
|
||||
func _withVeryUnsafeMutableBytes<T>(_ body: (UnsafeMutableRawBufferPointer) throws -> T) rethrows -> T {
|
||||
let capacity = self.allocatedCapacity
|
||||
|
||||
return try self.withUnsafeMutablePointerToElements { (elementsPtr) throws(E) in
|
||||
return try self.withUnsafeMutablePointerToElements { elementsPtr in
|
||||
return try body(UnsafeMutableRawBufferPointer(start: elementsPtr, count: capacity))
|
||||
}
|
||||
}
|
||||
@@ -803,11 +600,7 @@ extension SecureBytes.Backing: ContiguousBytes {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UInt32 {
|
||||
/// Returns the next power of two unless that would overflow, in which case UInt32.max (on 64-bit systems) or
|
||||
/// Int32.max (on 32-bit systems) is returned. The returned value is always safe to be cast to Int and passed
|
||||
@@ -841,17 +634,13 @@ extension UInt32 {
|
||||
}
|
||||
}
|
||||
|
||||
#if !CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, *)
|
||||
#else // CRYPTOKIT_STATIC_LIBRARY
|
||||
@available(iOS 13.0, macOS 10.13, watchOS 6.0, tvOS 13.0, macCatalyst 13.0, visionOS 1.0, *)
|
||||
#endif
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Data {
|
||||
/// A custom initializer for Data that attempts to share the same storage as the current SecureBytes instance.
|
||||
/// This is our best-effort attempt to expose the data in an auto-zeroing fashion. Any mutating function called on
|
||||
/// the constructed `Data` object will cause the bytes to be copied out: we can't avoid that.
|
||||
init(_ secureBytes: SecureBytes) {
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
self = secureBytes.withUnsafeBytes {
|
||||
// We make a mutable copy of this pointer here because we know Data won't write through it.
|
||||
return Data($0)
|
||||
@@ -879,7 +668,7 @@ extension Data {
|
||||
let baseOffset = secureByteSlice.startIndex.offset
|
||||
let endOffset = secureByteSlice.endIndex.offset
|
||||
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION || CRYPTOKIT_NO_IMPORT_FOUNDATION
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
self = base.withUnsafeBytes {
|
||||
// Slice the base pointer down to just the range we want.
|
||||
let slicedPointer = UnsafeRawBufferPointer(rebasing: $0[baseOffset..<endOffset])
|
||||
|
||||
@@ -12,11 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
#if CRYPTOKIT_STATIC_LIBRARY
|
||||
@_exported import CryptoKit_Static
|
||||
#else
|
||||
@_exported import CryptoKit
|
||||
#endif
|
||||
#else
|
||||
#if CRYPTOKIT_NO_ACCESS_TO_FOUNDATION
|
||||
import SwiftSystem
|
||||
@@ -28,20 +24,12 @@ 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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
protocol Zeroization {
|
||||
mutating func zeroize()
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UnsafeMutablePointer: Zeroization {
|
||||
/// Zeroizes the pointee
|
||||
func zeroize() {
|
||||
@@ -50,17 +38,14 @@ extension UnsafeMutablePointer: Zeroization {
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension UnsafeMutableRawBufferPointer: Zeroization {
|
||||
func zeroize() {
|
||||
memset_s(self.baseAddress!, self.count, 0, self.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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Array: Zeroization where Element == UInt8 {
|
||||
/// Zeroizes the array
|
||||
mutating func zeroize() {
|
||||
@@ -68,11 +53,7 @@ extension Array: Zeroization where Element == UInt8 {
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
extension Data: Zeroization {
|
||||
internal mutating func zeroize() {
|
||||
_ = self.withUnsafeMutableBytes {
|
||||
|
||||
@@ -84,7 +84,9 @@ extension BoringSSLAEAD.AEADContext {
|
||||
message: Plaintext,
|
||||
nonce: Nonce,
|
||||
authenticatedData: AuthenticatedData
|
||||
) throws -> Data {
|
||||
) throws -> (
|
||||
ciphertext: Data, tag: Data
|
||||
) {
|
||||
// Seal is a somewhat awkward function. As it returns a Data, we are going to need to initialize a Data large enough to write into. Data does not provide us an
|
||||
// initializer that gives us access to its uninitialized memory, so the cost of creating this Data is the cost of allocating the data + the cost of initializing
|
||||
// it. For smaller plaintexts this isn't too big a deal, but for larger ones the initialization cost can really get hairy.
|
||||
@@ -134,107 +136,72 @@ extension BoringSSLAEAD.AEADContext {
|
||||
message: Plaintext,
|
||||
nonce: Nonce,
|
||||
authenticatedData: AuthenticatedData
|
||||
) throws -> Data {
|
||||
) throws -> (
|
||||
ciphertext: Data, tag: Data
|
||||
) {
|
||||
try message.withUnsafeBytes { messagePointer in
|
||||
try nonce.withUnsafeBytes { noncePointer in
|
||||
try authenticatedData.withUnsafeBytes { authenticatedDataPointer in
|
||||
try self._sealContiguous(
|
||||
plaintext: messagePointer.bytes,
|
||||
nonce: noncePointer.bytes,
|
||||
authenticatedData: authenticatedDataPointer.bytes
|
||||
plaintext: messagePointer,
|
||||
noncePointer: noncePointer,
|
||||
authenticatedData: authenticatedDataPointer
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lowest level seal operation that calls into BoringSSL directly and
|
||||
/// operates on already-allocated memory.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(tag: copy tag)
|
||||
#endif
|
||||
public func seal(
|
||||
message: inout MutableRawSpan,
|
||||
nonce: RawSpan,
|
||||
authenticatedData: RawSpan,
|
||||
tag: inout OutputRawSpan
|
||||
) throws {
|
||||
let tagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.aead)
|
||||
precondition(tag.freeCapacity >= tagByteCount)
|
||||
var actualTagSize = tagByteCount
|
||||
|
||||
let rc = withUnsafeMutablePointer(to: &self.context) { contextPointer in
|
||||
message.withUnsafeMutableBytes { messageBuffer in
|
||||
tag.withUnsafeMutableBytes { tagBuffer, tagInitializedCount in
|
||||
defer {
|
||||
tagInitializedCount += actualTagSize
|
||||
}
|
||||
|
||||
return authenticatedData.withUnsafeBytes { authenticatedDataBuffer in
|
||||
nonce.withUnsafeBytes { nonceBuffer in
|
||||
CCryptoBoringSSLShims_EVP_AEAD_CTX_seal_scatter(
|
||||
contextPointer,
|
||||
messageBuffer.baseAddress,
|
||||
tagBuffer.baseAddress! + tagInitializedCount,
|
||||
&actualTagSize,
|
||||
tagBuffer.count - tagInitializedCount,
|
||||
nonceBuffer.baseAddress,
|
||||
nonceBuffer.count,
|
||||
messageBuffer.baseAddress,
|
||||
messageBuffer.count,
|
||||
nil,
|
||||
0,
|
||||
authenticatedDataBuffer.baseAddress,
|
||||
authenticatedDataBuffer.count
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
guard rc == 1 else {
|
||||
throw CryptoBoringWrapperError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
|
||||
/// The unsafe base call: not inlinable so that it can touch private variables.
|
||||
@usableFromInline
|
||||
func _sealContiguous(
|
||||
plaintext: RawSpan,
|
||||
nonce: RawSpan,
|
||||
authenticatedData: RawSpan
|
||||
) throws -> Data {
|
||||
plaintext: UnsafeRawBufferPointer,
|
||||
noncePointer: UnsafeRawBufferPointer,
|
||||
authenticatedData: UnsafeRawBufferPointer
|
||||
) throws -> (ciphertext: Data, tag: Data) {
|
||||
let tagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.aead)
|
||||
|
||||
// Form the combined represention of a sealed box with nonce + plaintext + tag.
|
||||
var combined = Data()
|
||||
combined.reserveCapacity(nonce.byteCount + plaintext.byteCount + tagByteCount)
|
||||
combined.append(contentsOf: nonce)
|
||||
combined.append(contentsOf: plaintext)
|
||||
combined.append(Data(count: tagByteCount))
|
||||
// We use malloc here because we are going to call free later. We force unwrap to trigger crashes if the allocation
|
||||
// fails.
|
||||
let outputBuffer = UnsafeMutableRawBufferPointer(
|
||||
start: malloc(plaintext.count)!,
|
||||
count: plaintext.count
|
||||
)
|
||||
let tagBuffer = UnsafeMutableRawBufferPointer(start: malloc(tagByteCount)!, count: tagByteCount)
|
||||
var actualTagSize = tagBuffer.count
|
||||
|
||||
try combined.withUnsafeMutableBytes { (combinedBuffer: UnsafeMutableRawBufferPointer) in
|
||||
let messageRange = nonce.byteCount..<(nonce.byteCount + plaintext.byteCount)
|
||||
let messageBuffer = UnsafeMutableRawBufferPointer(rebasing: combinedBuffer[messageRange])
|
||||
var messageSpan = messageBuffer.mutableBytes
|
||||
|
||||
let tagBuffer = UnsafeMutableRawBufferPointer(
|
||||
rebasing: combinedBuffer[(nonce.byteCount + plaintext.byteCount)...]
|
||||
let rc = withUnsafeMutablePointer(to: &self.context) { contextPointer in
|
||||
CCryptoBoringSSLShims_EVP_AEAD_CTX_seal_scatter(
|
||||
contextPointer,
|
||||
outputBuffer.baseAddress,
|
||||
tagBuffer.baseAddress,
|
||||
&actualTagSize,
|
||||
tagBuffer.count,
|
||||
noncePointer.baseAddress,
|
||||
noncePointer.count,
|
||||
plaintext.baseAddress,
|
||||
plaintext.count,
|
||||
nil,
|
||||
0,
|
||||
authenticatedData.baseAddress,
|
||||
authenticatedData.count
|
||||
)
|
||||
var tagSpan = OutputRawSpan(buffer: tagBuffer, initializedCount: 0)
|
||||
try seal(
|
||||
message: &messageSpan,
|
||||
nonce: nonce,
|
||||
authenticatedData: authenticatedData,
|
||||
tag: &tagSpan
|
||||
)
|
||||
let tagBytesWritten = tagSpan.finalize(for: tagBuffer)
|
||||
assert(tagBytesWritten == tagByteCount)
|
||||
}
|
||||
|
||||
return combined
|
||||
guard rc == 1 else {
|
||||
// Ooops, error. Free the memory we allocated before we throw.
|
||||
free(outputBuffer.baseAddress)
|
||||
free(tagBuffer.baseAddress)
|
||||
throw CryptoBoringWrapperError.internalBoringSSLError()
|
||||
}
|
||||
|
||||
let output = Data(
|
||||
bytesNoCopy: outputBuffer.baseAddress!,
|
||||
count: outputBuffer.count,
|
||||
deallocator: .free
|
||||
)
|
||||
let tag = Data(bytesNoCopy: tagBuffer.baseAddress!, count: actualTagSize, deallocator: .free)
|
||||
return (ciphertext: output, tag: tag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,10 +255,10 @@ extension BoringSSLAEAD.AEADContext {
|
||||
try tag.withUnsafeBytes { tagBytes in
|
||||
try authenticatedData.withUnsafeBytes { authenticatedDataBytes in
|
||||
try self._openContiguous(
|
||||
ciphertext: ciphertextPointer.bytes,
|
||||
nonceBytes: nonceBytes.bytes,
|
||||
tagBytes: tagBytes.bytes,
|
||||
authenticatedData: authenticatedDataBytes.bytes
|
||||
ciphertext: ciphertextPointer,
|
||||
nonceBytes: nonceBytes,
|
||||
tagBytes: tagBytes,
|
||||
authenticatedData: authenticatedDataBytes
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -299,63 +266,47 @@ extension BoringSSLAEAD.AEADContext {
|
||||
}
|
||||
}
|
||||
|
||||
/// Lowest level call into BoringSSL that decrypts in place.
|
||||
#if swift(<6.3)
|
||||
@_lifetime(message: copy message)
|
||||
#endif
|
||||
public func open(
|
||||
message: inout MutableRawSpan,
|
||||
nonce: RawSpan,
|
||||
tag: RawSpan,
|
||||
authenticatedData: RawSpan
|
||||
) throws {
|
||||
let rc = withUnsafePointer(to: &self.context) { contextPointer in
|
||||
message.withUnsafeMutableBytes { messageBuffer in
|
||||
nonce.withUnsafeBytes { nonceBuffer in
|
||||
tag.withUnsafeBytes { tagBuffer in
|
||||
authenticatedData.withUnsafeBytes { adBuffer in
|
||||
CCryptoBoringSSLShims_EVP_AEAD_CTX_open_gather(
|
||||
contextPointer,
|
||||
messageBuffer.baseAddress,
|
||||
nonceBuffer.baseAddress,
|
||||
nonceBuffer.count,
|
||||
messageBuffer.baseAddress,
|
||||
messageBuffer.count,
|
||||
tagBuffer.baseAddress,
|
||||
tagBuffer.count,
|
||||
adBuffer.baseAddress,
|
||||
adBuffer.count
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
guard rc == 1 else {
|
||||
throw CryptoBoringWrapperError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
|
||||
/// The unsafe base call: not inlinable so that it can touch private variables.
|
||||
@usableFromInline
|
||||
func _openContiguous(
|
||||
ciphertext: RawSpan,
|
||||
nonceBytes: RawSpan,
|
||||
tagBytes: RawSpan,
|
||||
authenticatedData: RawSpan
|
||||
ciphertext: UnsafeRawBufferPointer,
|
||||
nonceBytes: UnsafeRawBufferPointer,
|
||||
tagBytes: UnsafeRawBufferPointer,
|
||||
authenticatedData: UnsafeRawBufferPointer
|
||||
) throws -> Data {
|
||||
var output = Data(copying: ciphertext)
|
||||
if #available(visionOS 1.1, *) {
|
||||
var outputSpan = output.mutableBytes
|
||||
try open(message: &outputSpan, nonce: nonceBytes, tag: tagBytes, authenticatedData: authenticatedData)
|
||||
} else {
|
||||
// For some reason `Data.mutableBytes` is not available on visionOS 1.0 so we'll bounce through wUSMB.
|
||||
try output.withUnsafeMutableBytes { bytes in
|
||||
var outputSpan = bytes.mutableBytes
|
||||
try open(message: &outputSpan, nonce: nonceBytes, tag: tagBytes, authenticatedData: authenticatedData)
|
||||
}
|
||||
// We use malloc here because we are going to call free later. We force unwrap to trigger crashes if the allocation
|
||||
// fails.
|
||||
let outputBuffer = UnsafeMutableRawBufferPointer(
|
||||
start: malloc(ciphertext.count)!,
|
||||
count: ciphertext.count
|
||||
)
|
||||
|
||||
let rc = withUnsafePointer(to: &self.context) { contextPointer in
|
||||
CCryptoBoringSSLShims_EVP_AEAD_CTX_open_gather(
|
||||
contextPointer,
|
||||
outputBuffer.baseAddress,
|
||||
nonceBytes.baseAddress,
|
||||
nonceBytes.count,
|
||||
ciphertext.baseAddress,
|
||||
ciphertext.count,
|
||||
tagBytes.baseAddress,
|
||||
tagBytes.count,
|
||||
authenticatedData.baseAddress,
|
||||
authenticatedData.count
|
||||
)
|
||||
}
|
||||
|
||||
guard rc == 1 else {
|
||||
// Ooops, error. Free the memory we allocated before we throw.
|
||||
free(outputBuffer.baseAddress)
|
||||
throw CryptoBoringWrapperError.internalBoringSSLError()
|
||||
}
|
||||
|
||||
let output = Data(
|
||||
bytesNoCopy: outputBuffer.baseAddress!,
|
||||
count: outputBuffer.count,
|
||||
deallocator: .free
|
||||
)
|
||||
return output
|
||||
}
|
||||
|
||||
|
||||
Loaded 100 of 142 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user