How apple/swift-crypto works
Project Type and Purpose
• This is an open‐source Swift library (“Swift Crypto”) that re‐implements Apple’s CryptoKit API for cross‐platform use (particularly Linux).
• It exposes a unified public API (Crypto module) that at runtime either re‐exports the platform’s CryptoKit (on Apple platforms) or calls into a vendored BoringSSL (libcrypto) implementation (on Linux).
File‐Structure Highlights & Architectural Patterns
• Top‐level manifests & build scripts
• Package.swift (SwiftPM)
• CMakeLists.txt + cmake/modules (for building CCryptoBoringSSL)
• scripts/ (gyb code‐generation, vendoring BoringSSL, etc.)
• Sources/
• CCryptoBoringSSL/… (C++/C sources & headers for a vendored BoringSSL)
• Crypto/… (pure Swift API surfaces and Swift‐only primitive implementations)
• CryptoBoringWrapper/… (Swift wrappers around BoringSSL shims)
• _CryptoExtras/… (additional algorithms not in core CryptoKit)
• Conditional compilation pattern (“apple vs. linux mode”)
• Swift code is shared (API + primitives)
• Platform‐specific code marked _boring or in BoringSSL directories
• Tests/ & Benchmarks/ target each layer (Swift API, wrapper, BoringSSL integration).
Key Components
a. Public API Layer
• Crypto Swift module (mirrors CryptoKit)
• Optional staging & server namespaces
b. Implementation Layer
• On Apple: direct re‐export to system CryptoKit framework
• On Linux: Swift wrappers → CCryptoBoringSSL module → BoringSSL C code
c. Build System
• SwiftPM driving the Swift build
• CMake driving the C build of CCryptoBoringSSL
• gyb templates → generated Swift sources
d. External Dependencies & Tools
• Apple CryptoKit (on Apple)
• BoringSSL vendored sources
• Swift 5.7+ compiler, Swift Package Manager
• GyB code generator
• GitHub Actions workflows for CI
Relationships & Interactions
• Build-time
• SwiftPM invokes CMake to build CCryptoBoringSSL → static .a library + modulemap
• gyb scripts generate Swift files from .gyb templates
• SwiftPM compiles Swift modules, linking with CCryptoBoringSSL on Linux, or linking CocoaCryptoKit on Apple
• Runtime
• Application code → import Crypto → public API
• Public API calls into one of two backends:
– Apple platform: CryptoKit dynamic framework
– Linux: Swift wrapper calls into BoringSSL via CCryptoBoringSSL bridging
Architectural Patterns & Principles
• Layered architecture: Public API / abstraction → platform backends → native/third‐party crypto engine
• Separation of concerns: Swift‐only API code vs. C‐based implementation
• Conditional compilation & module naming (_boring, BoringSSL directories) to isolate platform‐specific code
• Metaprogramming for boilerplate (gyb) to keep API surface in sync with CryptoKit