mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
main
9
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
ab0beab369 |
Zero temporary buffers that hold key material and reduce more allocations (#493)
### Motivation: AES-KeyWrap unwrap, ML-KEM decapsulation, PBKDF2, and scrypt each write the key they return into a temporary buffer, copy it into a `SymmetricKey`, and free the buffer without zeroing it. The `SymmetricKey` stores its copy in `SecureBytes`, which zeroes the copy before freeing it. Nothing zeroes the original. AES-KeyWrap and ML-KEM can write the key directly into the `SymmetricKey`, as X-Wing already does. That removes both the buffer and the copy. PBKDF2 and scrypt can't, because they are in CryptoExtras and `SymmetricKey(unsafeUninitializedCapacity:initializingWith:)` is internal to Crypto. They keep their buffer and zero it after the copy. Moving that buffer onto the stack made them twice as slow. ### Modifications: - Add benchmarks for AES-KeyWrap, ML-KEM, PBKDF2, and scrypt - Unwrap AES-KeyWrap keys straight into the `SymmetricKey` - Decapsulate ML-KEM shared secrets straight into the `SymmetricKey`. Regenerating `MLKEM_boring.swift` from its `.gyb` also drops five `@available` attributes that were only ever in the generated file. - Zero the PBKDF2 and scrypt derived key buffers ### Result: - After the call returns, no temporary buffer still contains the key. - `AES.KeyWrap.unwrap` makes 2 allocations instead of 3 and is about 6% faster. - `MLKEM768.PrivateKey.decapsulate` makes 2 allocations instead of 4. - `KDF.Insecure.PBKDF2.deriveKey` and `KDF.Scrypt.deriveKey` make the same number of allocations and take the same time as before. ### Benchmarks: The tables come from `swift package benchmark --metric mallocCountTotal --metric wallClock`. ``` aes-keywrap-unwrap (before) ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 3 │ 3 │ 3 │ 3 │ 3 │ 3 │ 3 │ 5 │ ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (wall clock) (ns) * │ 597 │ 601 │ 614 │ 621 │ 630 │ 630 │ 630 │ 5 │ ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ aes-keywrap-unwrap (after) ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 2 │ 2 │ 2 │ 2 │ 2 │ 2 │ 2 │ 5 │ ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (wall clock) (ns) * │ 563 │ 564 │ 578 │ 578 │ 581 │ 581 │ 581 │ 5 │ ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ mlkem768-decapsulate (before) ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 4 │ 4 │ 4 │ 4 │ 4 │ 4 │ 4 │ 10 │ ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (wall clock) (μs) * │ 48 │ 49 │ 50 │ 51 │ 51 │ 51 │ 51 │ 10 │ ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ mlkem768-decapsulate (after) ╒══════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 2 │ 2 │ 2 │ 2 │ 2 │ 2 │ 2 │ 10 │ ├──────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (wall clock) (μs) * │ 46 │ 48 │ 49 │ 51 │ 51 │ 51 │ 51 │ 10 │ ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ``` |
||
|
|
47df2f1f6d |
Use a scoped zeroing allocation for the HMAC pad (#490)
### Motivation: Currently HMAC heap allocates twice per construction for temporary storage. `HMAC.init(key:)` builds the inner and outer pads using two `SecureBytes` allocations. This is a bottleneck for both HMAC and HKDF, which constructs an HMAC per output block. Both pads never escape the initializer, so neither needs to be on the heap at all. A scoped temporary allocation that zeroes after use does the same job with no allocations. ### Modifications: - Fix the Benchmarks package build (missing `Sendable` annotation) - Add benchmarks for HMAC and HKDF - Add scoped zeroing temporary allocation helpers - Use a scoped zeroing allocation for the HMAC pad ### Result: - `HMAC<SHA256>.authenticationCode`: was 5 allocations, now 3, ~20% faster. - `HKDF<SHA256>.deriveKey`, 1 KiB output: was 170 allocations, now 104, ~30% faster. ### Benchmarks: ``` hmac-sha256-authenticate (before) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 5 │ 5 │ 5 │ 5 │ 5 │ 5 │ 5 │ 5 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (ns) * │ 1170 │ 1251 │ 1251 │ 1271 │ 1340 │ 1340 │ 1340 │ 5 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ hmac-sha256-authenticate (after) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 3 │ 3 │ 3 │ 3 │ 3 │ 3 │ 3 │ 5 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (ns) * │ 870 │ 910 │ 970 │ 1030 │ 1030 │ 1030 │ 1030 │ 5 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ hkdf-sha256-derive (before) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 170 │ 170 │ 170 │ 170 │ 170 │ 170 │ 170 │ 10 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (μs) * │ 20 │ 20 │ 30 │ 30 │ 30 │ 30 │ 30 │ 10 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ hkdf-sha256-derive (after) ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ Malloc (total) * │ 104 │ 104 │ 104 │ 104 │ 104 │ 104 │ 104 │ 10 │ ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Time (total CPU) (μs) * │ 10 │ 20 │ 20 │ 20 │ 20 │ 20 │ 20 │ 10 │ ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ``` ### Credits: This PR is a re-work of the original contribution by @ptoffy. The merge-commit will show him as a co-author. --------- Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com> |
||
|
|
a9d1d5ab89 |
Merge the 5.x branch for the Swift Crypto 5.0 release (#470)
## Motivation Xcode 27 is now available[^1] so it's time to merge in our 5.x branch to main and release 5.0. ## Modifications See "Release Notes" section below for overview of changes and links to the PRs that landed on the 5.x branch during the beta period. ## Result The main branch will be ready to tag a 5.0 release. [^1]: https://developer.apple.com/download/all/?q=Xcode%2027 --- ## Release Notes Swift Crypto 5.0 aligns the cross-platform API surface with CryptoKit as shipped in Xcode 27 (CryptoKit-383.2.1), and takes the opportunity to streamline the package for both adopters and maintainers. Most users will be able to depend on the 1.0.0, 2.0.0, 3.0.0, 4.0.0, or 5.0.0 series of releases. To do so, please use the following dependency in your `Package.swift`: ```swift .package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "6.0.0"), ``` ### Requires Swift 6.2 The package now declares 6.2 as the minimum Swift version in its package manifest. ### New span-based API CryptoKit's new non-allocating, span-based entry points are now available cross-platform: - `SymmetricKey` gains `bytes: RawSpan`, `init(copying: RawSpan)`, `init(copyingWithZeroing: inout MutableRawSpan)`, and `init(size:initializingWith:)` which initializes the key through an `inout OutputRawSpan`. - `HashFunction` gains `update(bytes: RawSpan)` and `hash(bytes: RawSpan)`, and `Digest` gains `init?(copying: RawSpan)`. - `HMAC` gains `authenticationCode(for: RawSpan, using:)`. - `HKDF` gains `deriveKey(inputKeyMaterial:salt:info:output:)` writing into an `inout OutputRawSpan`, along with span-based `extract` and `expand`. The existing `DataProtocol` overload now has a fast path for contiguous salt and info. - `AES.GCM` and `ChaChaPoly` gain in-place `seal(inPlace:using:nonce:authenticating:tag:)` and `open(inPlace:using:nonce:authenticating:tag:)` over `MutableRawSpan`, and their `Nonce` types gain `bytes: RawSpan` and `init(copying: RawSpan)`. ### Single-use KEM keys A new `KEMOneTimePrivateKey` protocol, refining `~Copyable` and `Sendable`, provides a `consuming func decapsulate(_:)` so a decapsulation key cannot be reused. Conforming types are `MLKEM768.OneTimePrivateKey`, `MLKEM1024.OneTimePrivateKey`, and `XWingMLKEM768X25519.OneTimePrivateKey`. ### Extensible enums Historically Swift Crypto needed a new major version each time CryptoKit added a case to a non-frozen enum, because Swift Crypto is not built with library evolution. All public non-frozen enums are now marked `@nonexhaustive` (SE-0487), which should remove that reason for future major bumps. Adopters who do not switch over these enums should not need to widen their version constraints again. ### Other changes - Symbols that were previously marked `@_spi(...) public` have been made internal; they were never intended to be available. - `@available` annotations have been removed from `Crypto`; they are unused in all supported compilation modes. - Some internal reference types are now marked `final` - Some unused code, e.g. `ECDSASignature.derBytes`, is removed. ### Build and packaging - The `CCryptoBoringSSLShims` target is removed, along with all `bindMemory(to:)` calls. - The `.gyb` templates for nonces, digests, ECDH, ECDSA, MLDSA, and MLKEM are removed and the generated Swift is checked in. The BoringSSL backend templates for MLDSA and MLKEM remain. - Conditional compilation is simplified to `#if canImport(CryptoKit)`; branches that could never be taken are gone. - Developing the BoringSSL backend on macOS is no longer supported. Use the Dev Container configuration now shipped in the repository, or build on the target platform. - Warnings are silenced for vendored C sources: `-Wshorten-64-to-32` for BoringSSL and `macro-redefined` for XKCP. CI builds with `-warnings-as-errors`. ### Commits The changes in this release were reviewed and merged to the 5.x branch during the beta period: - |
||
|
|
476538ccb8 |
Drop Swift 6.0 (#434)
Motivation: Swift 6.0 is no longer supported, we should bump the tools version and remove it from our CI. Modifications: * Bump the Swift tools version to Swift 6.1 * Remove Swift 6.0 jobs where appropriate in main.yml, pull_request.yml Result: Code reflects our support window. |
||
|
+6 |
431013afaf |
Update to WWDC25 release (#417)
This change updates Crypto to WWDC25 equivalent code. Each of these changes were individually merged to the WWDC25 branch. This includes: - #359 - #363 - #365 - #367 - #368 - #397 - #403 - #410 - #407 - #401 - #415 --------- Co-authored-by: YourMJK <37852512+YourMJK@users.noreply.github.com> Co-authored-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Rick Newton-Rogers <rnro@apple.com> Co-authored-by: Raphael <josephnoir@users.noreply.github.com> Co-authored-by: Evan Wilde <ewilde@apple.com> Co-authored-by: Tim Condon <0xTim@users.noreply.github.com> Co-authored-by: Gus Cairo <gus.cairo@apple.com> Co-authored-by: George Barnett <gbarnett@apple.com> Co-authored-by: Fabrice de Gans <Steelskin@users.noreply.github.com> Co-authored-by: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Co-authored-by: Jesse L. Zamora <xtremekforever@gmail.com> Co-authored-by: aryan-25 <aryan_shah@apple.com> Co-authored-by: Gwynne Raskind <gwynne@vapor.codes> Co-authored-by: 3405691582 <dsk@google.com> Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com> |
||
|
|
64a4de00fb |
Add some benchmarks for EC key agreement (#390)
We should have some benchmarks for EC key agreement. |
||
|
|
93a2ebfad7 |
Add API for ARC(P-256) and deprecate ARC(P-384) (#374)
## Motivation The ARC IETF draft has been updated to now support P256 and drop support for P384. ## Modifications - Update the internal ARC implementation to accommodate multiple curves - Add API surface for P256 - Deprecate API for P384 - Add tests for P256 with test vectors from the draft - Move over the end-to-end API tests to P256 - Add benchmarks for P256 ## Result - New API `P256._ARCV1`, which provides an implementation of ARC(P-256). - Deprecate `P384._ARCV1`, which provides an implementation of ARC(P-256). [^1]: https://chris-wood.github.io/draft-arc/draft-yun-cfrg-arc.html#name-arcv1-p256 --------- Co-authored-by: Cory Benfield <lukasa@apple.com> |
||
|
|
5bd06ee1d2 |
Drop Swift 5.9 (#353)
Motivation: Swift 5.9 is no longer supported, we should bump the tools version and remove it from our CI. Modifications: * Bump the Swift tools version to Swift 5.10 * Remove Swift 5.9 jobs where appropriate in main.yml, pull_request.yml Result: Code reflects our support window. |
||
|
|
6db5a75258 |
perf: Add benchmarks and reduce allocations by ~50% for ARC and VOPRF (#346)
### Motivation We've recently added some EC operations internally to support some schemes, including ARC and VOPRF. Some of this code is quite allocation-heavy, which could be avoided with some minimally invasive changes. ### Modifications - Add package benchmarks for ARC and VOPRF. - Make use of the `consuming` keyword in some targeted places to reduce allocations in arithmetic- and append-chains. - Use a Thread-/Task-local FiniteFieldArithmeticContext for EC operations, and thread this through as a `BN_CTX` to BoringSSL functions when available. - Replace EC static computed properties with stored properties for things that are statically knowable about the EC group. ### Result This has resulted in a 50% reduction in allocations for ARC issuance and VOPRF evaluation, and a 34% reduction in ARC verification. ```none SwiftCryptoBenchmarks ============================================================================================================================ ---------------------------------------------------------------------------------------------------------------------------- arc-issue-p384 metrics ---------------------------------------------------------------------------------------------------------------------------- ╒══════════════════════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Malloc (total) * │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ alpha │ 945 │ 945 │ 945 │ 945 │ 945 │ 945 │ 945 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ beta │ 464 │ 464 │ 464 │ 464 │ 464 │ 464 │ 464 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Δ │ -481 │ -481 │ -481 │ -481 │ -481 │ -481 │ -481 │ 0 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Improvement % │ 51 │ 51 │ 51 │ 51 │ 51 │ 51 │ 51 │ 0 │ ╘══════════════════════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ---------------------------------------------------------------------------------------------------------------------------- arc-verify-p384 metrics ---------------------------------------------------------------------------------------------------------------------------- ╒══════════════════════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Malloc (total) * │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ alpha │ 410 │ 410 │ 410 │ 415 │ 415 │ 415 │ 415 │ 10 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ beta │ 271 │ 271 │ 271 │ 275 │ 275 │ 275 │ 275 │ 10 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Δ │ -139 │ -139 │ -139 │ -140 │ -140 │ -140 │ -140 │ 0 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Improvement % │ 34 │ 34 │ 34 │ 34 │ 34 │ 34 │ 34 │ 0 │ ╘══════════════════════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ---------------------------------------------------------------------------------------------------------------------------- voprf-evaluate-p384 metrics ---------------------------------------------------------------------------------------------------------------------------- ╒══════════════════════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕ │ Malloc (total) * │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │ ╞══════════════════════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡ │ alpha │ 331 │ 331 │ 331 │ 331 │ 331 │ 331 │ 331 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ beta │ 168 │ 168 │ 168 │ 168 │ 168 │ 168 │ 168 │ 3 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Δ │ -163 │ -163 │ -163 │ -163 │ -163 │ -163 │ -163 │ 0 │ ├──────────────────────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤ │ Improvement % │ 49 │ 49 │ 49 │ 49 │ 49 │ 49 │ 49 │ 0 │ ╘══════════════════════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ``` --------- Co-authored-by: Cory Benfield <lukasa@apple.com> |