Swift Crypto Documentation

repository·main·Indexed 23 days ago

https://github.com/apple/swift-crypto

An open-source implementation of the Apple CryptoKit API for Linux and ARM64 Windows platforms. It provides safe, modern cryptographic primitives for cross-platform or server applications, including the core Crypto library and CryptoExtras for server-centric APIs. The library leverages BoringSSL and libXKCP on non-Apple platforms and acts as a transparent wrapper for native CryptoKit on Apple platforms.

Tokens
1.5K
Snippets
2
Records
12
Agent score
74%

What's inside Swift Crypto

  1. Overview of Swift Crypto libraries

    main

    Swift Crypto provides a Swift library for common cryptographic operations, built on top of BoringSSL. It is distributed as two main libraries:

    • Crypto: An open-source implementation of a substantial portion of the Apple CryptoKit API. It is designed for use on Linux platforms and enables cross-platform or server applications to use CryptoKit-style APIs.
    • CryptoExtras: A collection of additional cryptographic primitives and utilities not included in CryptoKit but useful in server environments.
  2. Use CryptoExtras for additional cryptographic primitives

    main
    CryptoExtras is a collection of cryptographic APIs and utilities designed for server environments. It provides primitives that are not included in the core CryptoKit or the main Crypto library. Use CryptoExtras when you need access to specific ciphers, public key cryptography, or key derivation functions that fall outside the standard CryptoKit scope.
  3. How Staging and Server namespaces work

    main

    Swift Crypto aims to stay in lockstep with Apple's CryptoKit. To manage APIs that are not yet in the main CryptoKit release, the library uses two specific namespaces:

    • Staging namespace: A temporary home for APIs expected to be added to Apple's CryptoKit in the future. Once an API becomes generally available in CryptoKit, it is deprecated in Staging and moved to the main Swift Crypto namespace.
    • Server namespace: A permanent home for APIs that are useful for server-side use cases but do not meet the criteria for inclusion in the general CryptoKit library. APIs in this namespace are not expected to move.
  4. Understand Swift Crypto's platform-specific implementations

    main

    Swift Crypto behaves differently depending on the target platform:

    1. Apple Platforms (macOS, iOS, etc.): Swift Crypto acts as a transparent wrapper. It compiles its API surface down to nothing and re-exports the native CryptoKit API. All work is delegated to the OS-level CryptoKit implementation.
    2. Linux and Windows: Swift Crypto builds a full implementation. It includes a vendored copy of BoringSSL's libcrypto, a vendored copy of XKCP's libXKCP, and the common API surface that calls into these libraries.

    Note on CryptoExtras: If you depend on CryptoExtras (which provides server-centric cryptographic APIs not found in CryptoKit), you will bundle the BoringSSL/XKCP implementation in your application regardless of the platform.

  5. Install Swift Crypto via Swift Package Manager

    main

    Swift Crypto is available as a Swift Package Manager package. To use it, add the package dependency to your Package.swift using a version range that covers your required version up to the next major version. It is recommended to allow a wide range of versions (e.g., 1.x through 4.x) as they are largely API compatible.

    To use the library in your code, add Crypto to your target's dependencies and use import Crypto.

    // swift-crypto 1.x, 2.x, 3.x, and 4.x are almost API compatible, so most clients
    // should allow any of them
    .package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "5.0.0"),
  6. Force Swift Crypto implementation on macOS for development

    main
    By default, Swift Crypto on macOS defers to the system's CryptoKit. If you are developing Swift Crypto itself on macOS and need to force it to build its own open-source implementation instead of delegating to the OS, set development to true in your Package.swift.
  7. Available cryptographic primitives in Swift Crypto

    main

    Swift Crypto provides several categories of cryptographic operations. The following primitives are available:

    Cryptographically secure hashes

    • HashFunction
    • SHA512
    • SHA384
    • SHA256

    Message authentication codes

    • HMAC
    • SymmetricKey
    • SymmetricKeySize

    Ciphers

    • AES
    • ChaChaPoly

    Public key cryptography

    • Curve25519
    • P521
    • P384
    • P256
    • SharedSecret
    • HPKE

    Key derivation functions

    • HKDF

    Legacy algorithms

    • Insecure
  8. Explore CryptoExtras topics

    main

    The CryptoExtras library is organized into several functional areas:

    • Ciphers: Includes AES implementations (CryptoExtras/Crypto/AES).
    • Public key cryptography: Includes RSA support (_RSA).
    • Key derivation functions: Includes KDF implementations (KDF).
    • Legacy algorithms: Provides access to insecure or legacy algorithms (CryptoExtras/Crypto/Insecure).
  9. Swift Crypto minimum Swift version compatibility

    main

    The minimum supported Swift version depends on the version of Swift Crypto you are using:

    Swift Crypto VersionMinimum Swift Version
    2.0.0 ..< 2.1.05.2
    2.1.0 ..< 2.2.05.4
    2.2.0 ..< 2.4.25.5
    2.4.2 ..< 3.1.05.6
    3.1.0 ..< 3.3.05.7
    3.3.0 ..< 3.8.05.8
    3.9.0 ..< 3.13.05.9
    3.13.0 ..< 4.0.05.10
    4.0.0 ...6.0
  10. Use the crypto-shasum CLI to print SHA checksums

    main
    The crypto-shasum tool calculates and prints SHA checksums for files or standard input. By default, it uses the SHA-256 algorithm. You can specify a different algorithm using the -a or --algorithm flag, or provide specific files to hash. If no files are provided, or if - is used as a filename, it reads from standard input.