BT

InfoQ Homepage News Swift Crypto Brings Apple CryptoKit API to Server-Side Swift

Swift Crypto Brings Apple CryptoKit API to Server-Side Swift

Bookmarks

Swift Crypto is a new open-source library for Swift that aims to provide a common API for cryptographic operations on all supported platforms. On macOS, Swift Crypto leverage Apple's CryptoKit framework, while BoringSSL is used for all other platforms.

Swift Crypto is a new Swift package that brings the fantastic APIs of Apple CryptoKit to the wider Swift community. This will allow Swift developers, regardless of the platform on which they deploy their applications, to access these APIs for a common set of cryptographic operations.

Besides correctness, one of Swift Crypto main goals is being easy to use and safeguarding developers from common pitfalls they may incur into when using cryptographic functions. For example, this is how you would encrypt some data using AES GCM (PDF):

func encrypt(input: [UInt8]) throws -> Data {
    let key = SymmetricKey(size: .bits256)
    let sealedBox = try AES.GCM.seal(input, using: key)
    return sealedBox.combined!
}

This syntax follows closely CryptoKit. As Apple engineer Cory Benfield remarks, this code is not only extremely easy, it also ensures correct use of a random nonce and ciphertext authentication. This is vital for AES GCM, since using the same nonce allows an attacker to easily forge a ciphertext. According to Benfield, other cryptographic libraries do not provide the same level of safeguards against incorrect usage.

A direct consequence of its goal of being easy to use in a correct way, Swift Crypto will not aim to include a large set of cryptographic primitives, since this would only make it harder for developers to choose the right primitive. Additionally, when deciding on any addition to the library, a key criteria will be whether an equivalent, safer, or more widely deployed primitive is already present.

Another major goal of Swift Crypto is ensuring it provides the same results on all supported platform. This is no trivial task, since, as mentioned, Swift Crypto uses two different underlying implementations of cryptographic primitives depending of the host platform. To this aim, Swift Crypto includes a shared test suite to ensure its BoringSSL-based implementation provides the same results as the CryptoKit-based implementation. As Benfield notes, this has required some major effort, even including a completely new implementation of some algorithms, and will continue to do so in the future.

It is important to note that while Swift Crypto adopts CryptoKit API, it will only implement it partially. Specifically, it will not include any primitives requiring specialized hardware only available on Apple platforms, such as Apple's Secure Enclave.

Swift Crypto requires Swift 5.1 and can be installed using the Swift Package Manager.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.