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.47k stars 166 forks source link

ChaCha20 CTR Encryption #169

Closed btoms20 closed 1 year ago

btoms20 commented 1 year ago

Added a method to interact with BoringSSL's CCryptoBoringSSL_CRYPTO_chacha_20()

Checklist

Motivation:

Having the ability to explicitly set the Counter in the ChaCha20 Cipher allows us to generate 'Header Protection Masks' as described in RFC 9001 - Using TLS to Secure QUIC and issue #168

Modifications:

Added a single encryption method under the Insecure.ChaCha20CTR enum that lets the user of this library perform single block encryptions with an explicitly defined Counter and Nonce / IV.

The ChaCha20CTR enum described above is defined in the _CryptoExtras product.

Result:

After including both Crypto and _CryptoExtras in your project you'll have access to the ChaCha20CTR enum and the associated encryption method that enables one off encryption operations.

import Crypto
import _CryptoExtras

Insecure.ChaCha20CTR.encrypt(
   message: DataProtocol,
   using: SymmetricKey,
   counter: Insecure.ChaCha20CTR.Counter,
   nonce: Insecure.ChaCha20CTR.Nonce
) throws -> Data { ... }

See the included ChaCha20CTRTests.swift file for usage examples.

Lukasa commented 1 year ago

@swift-server-bot test this please

Lukasa commented 1 year ago

@swift-server-bot test this please

Lukasa commented 1 year ago

Excellent, one quick fixup: can you run scripts/update_cmakelists.sh and commit the changes?

Lukasa commented 1 year ago

@swift-server-bot test this please

btoms20 commented 1 year ago

Awesome! Thanks for guiding me through this PR, I really appreciate all the comments / help. Sorry for taking up so much of your time! 😅