apple / swift-crypto

Open-source implementation of a substantial portion of the API of Apple CryptoKit suitable for use on Linux platforms.
https://apple.github.io/swift-crypto
Apache License 2.0
1.43k stars 151 forks source link

Use BoringSSL implementation for `_CryptoExtras.AES._CTR` #229

Closed simonjbeaumont closed 2 months ago

simonjbeaumont commented 2 months ago

Motivation:

_CryptoExtras provides AES CTR mode with a Swift implementation, which operates one block at a time. For encrypting larger plaintexts this can become a bottleneck. BoringSSL provides an implementation which makes use of hardware capabilities if present for parallel block processing.

Modifications:

Result:

The runtime performance is improved by around 93% for encryption of larger plaintexts, e.g. 4k:

----------------------------------------------------------------------------------------------------------------------------
AES._CTR.encrypt 4k metrics
----------------------------------------------------------------------------------------------------------------------------

╒══════════════════════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│         Time (total CPU) (ns) *          │      p0 │     p25 │     p50 │     p75 │     p90 │     p99 │    p100 │ Samples │
╞══════════════════════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│                  alpha                   │      14 │      14 │      14 │      14 │      14 │      15 │      15 │     213 │
├──────────────────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│               Current_run                │       1 │       1 │       1 │       1 │       1 │       1 │       1 │    3560 │
├──────────────────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│                    Δ                     │     -13 │     -13 │     -13 │     -13 │     -13 │     -14 │     -14 │    3347 │
├──────────────────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│              Improvement %               │      93 │      93 │      93 │      93 │      93 │      93 │      93 │    3347 │
╘══════════════════════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛