mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
### 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 │ ╘══════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛ ```