Minimum deployment versions in package manifests can be problematic for
libraries, because changing them or adding them to an existing package
that doesn't already use them forces SemVer breaking majors.
Furthermore, depending on a new library that specifies minimum
deployment targets would force the adopting library to also have to add
these platforms to its manifest (which would in turn force a major).
This PR removes the platforms stanza from the package manifest and
replaces them with availability guards.
This will allow more packages to use swift-crypto, without forcing them
to mint new majors to support the appropriate platforms.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
This change prepares swift-crypto for use on Windows with CMake and also
the Package.swift for use in the Swift toolchain CI system.
### Checklist
- [X] I've run tests to see all new and existing tests pass
- [X] I've followed the code style of the rest of the project
- [X] I've read the [Contribution Guidelines](CONTRIBUTING.md)
- [X] I've updated the documentation if necessary
#### If you've made changes to `gyb` files
- [X] I've run `.script/generate_boilerplate_files_with_gyb` and
included updated generated files in a commit of this pull request
### Motivation:
Upgrading swift-crypto from an older version requires that Windows works
properly with CMake, and also SwiftPM.
### Modifications:
Modify the CMakeLists.txt files and the generator script so that it
conditionally adds assembly files for non-Windows platforms. Prepare the
Package.swift file for use in Swift CI pipelines, by consulting an
environment to either use the absolute URL, or a prepared version of the
swift-asn1 dependency.
### Result:
After this change it should be possible to build with the newest
swift-crypto in the Swift CI system.
Enable MemberImportVisibility check on all targets. Use a standard
string header and footer to bracket the new block for ease of updating
in the future with scripts.
Improve DocC landing pages by mimicking Apple's CryptoKit docs
### Motivation:
Right now the DocC docs group all APIs by protocols, structures and
enums.
By adding a topics section in the landing page we can group them by type
("Ciphers", "Public key cryptography", etc.) exactly in the way Apple
does in [CryptoKit
docs](https://developer.apple.com/documentation/cryptokit/).
### Modifications:
- Fix DocC compilation errors in `_CryptoExtras`
- Add topics section in `_CryptoExtras` landing page
- Add topics section in `Crypto` landing page
### Result:
APIs will be grouped by type.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Fix FreeBSD support
### Checklist
- [x] I've run tests to see all new and existing tests pass
- [x] I've followed the code style of the rest of the project
- [x] I've read the [Contribution Guidelines](CONTRIBUTING.md)
- [x] I've updated the documentation if necessary
### Motivation:
To add FreeBSD support to Swift, we must ensure that swift crypto builds
on FreeBSD since it's a dependency of swiftpm and several essential
packages.
### Modifications:
- `Package.swift` - Ideally FreeBSD should use `.when(platforms:)` to
set the corresponding swift settings and dependencies. However,
`Platform.freebsd` is not yet available on swiftpm and therefore use
guard the settings behind a `isFreeBSD` boolean flag. This is chosen
because it minimize the diff and maintains the maximum readability.
- `cmake/modules/SwiftSupport.cmake` - add `amd64` arch support, maps to
`x86_64`.
- `**/CMakeLists.txt` - Use the Linux assembly files for FreeBSD. Both
Linux and FreeBSD uses SysV ABI, and there're no syscalls involved
(after all, they're supposed to be "pure") so there are no ABI
compatibility problem. After all, all the tests are passing on FreeBSD
with this PR.
### Result:
All the tests (via `swift test`) passed on x86_64 FreeBSD 14.1-p6, and
passed on aarch64 macOS.
Test log available here:
[swift-test-result.txt](https://github.com/user-attachments/files/18147920/swift-test-result.txt)
Adds some additional documentation. I don't know how far you want me to
go with documenting everything so let me know. This should at least get
the current APIs rendered on SPI. As an aside I'm not a huge fan of
linking out to the CryptoKit docs since not everything is available
here, all the code is already documented here and most of the tutorials
and articles on developer.apple.com for CryptoKit refer to Apple
Platform specific stuff, like interacting with the keychain, that is not
applicable here.
Resolves#273
### Checklist
- [x] I've run tests to see all new and existing tests pass
- [x] I've followed the code style of the rest of the project
- [x] I've read the [Contribution Guidelines](CONTRIBUTING.md)
- [x] I've updated the documentation if necessary
#### If you've made changes to `gyb` files
- [x] I've run `.script/generate_boilerplate_files_with_gyb` and
included updated generated files in a commit of this pull request
### Motivation:
Currently Swift Crypto has no documentation which is not great
### Modifications:
Adds additional DocC scaffolding to take advantage of the DocC comments
in Crypto and CryptoExtras. Adds an SPI config file for Swift Package
Index
### Result:
We will have some documentation
## Motivation
As part of the Github Actions CI migration we held off enabling the
format check and moving to the same formatting rules as Swift NIO, but
we'd like to do that now.
## Modifications
- Add a script to generate (and update) the `.swiftformatignore` file
since this repo only wants to format a specific set of files matching a
pattern.
- Generate and commit the `.swiftformatignore` file.
- Run the fomatter and commit the results.
- Additional commits to satisfy the remaining linter rules that weren't
auto-fixed by swift-format.
- Enable the format check in CI.
## Result
- Code that's not shared is formatted with a style consistent with the
other repos this team maintain.
- CI is now checking code format again.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Rick Newton-Rogers <rnro@apple.com>
* Replaced potentially unexpected accesses to coordinate slices with consistent ranges no matter if the raw representation is a root or a slice of data
* Updated uses of `.suffix(from:)` with `.dropFirst()` to prevent issues with potential non-0 start indexes
* wrapper: Remove _boring suffix from files in CryptoBoringWrapper
* wrapper: Move EC types and random bytes into CryptoBoringWrapper
* tests: Remove unused conditional compilation conditions in wrapper tests
This adds a _createFromNumbers API which creates an RSA key generating p and q primes based on n, e and d
Motivation:
There might be cases where private RSA keys have to be generated based on their raw elements (modulus n, exponent e and private exponent d) without p and q. These are random primes which are integral parts of the key but can be generated based on the former numbers, and are not always included when representing RSA keys, for example these parameters are not required in JWKs
Modifications:
This adds a static _createFromNumbers method which creates a new RSA key given its raw modulus, exponent and private exponent, generating the needed p and q primes without having to provide them as parameters. The algorithm to generate primes is based on a hybrid approach of the Miller Rabin primality test and the proof of Fact 1 in Dan Boneh's Twenty Years of Attacks on the RSA Cryptosystem paper. To write this algorithm, some additions had to be made to the ArbitraryPrecisionInteger
Result:
Private RSA keys can now be generated without providing p and q
It is expected behavior of the swift-build build system to copy links as-is, so presenting these as symlinks results in broken symlinks being present in the produced resource bundles, which won't work properly.
On Windows, it even causes the build entirely to fail due to a related bug: https://github.com/apple/swift-foundation/issues/845
Convert these to flat files instead.
* Remove array allocation when computing digest
Instead of creating a temporary array, the digest bytes are stored in a temporary stack-allocated buffer.
This reduces the number of heap allocations needed to hash a message from 7 to 6.
* Store digest context inline
This reduces the number of heap allocations needed to hash a message from 6 to 2.
Before:
1. the DigestContext object is created
2. the EVP_MD_CTX struct is allocated by EVP_MD_CTX_new
3. md_data is allocated when initializing the context
After:
1. the DigestContext object is created
In both cases, the number is doubled because COW happens during finalize().
* Use temporary variable to finalize context
This reduces the number of heap allocations needed to hash a message from 2 to 1.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
* Move ArbitraryPrecisionInteger_boring.swift to CryptoBoringWrapper
* Remove conditional compilation guards for ArbitraryPrecisionInteger
* Throw CryptoBoringWrapperError.internalBoringSSLError instead of CryptoKitError.internalBoringSSLError
* Make most operations public and add raw pointer APIs
* Move ArbitraryPrecisionIntegerTests to new CryptoBoringWrapperTests target
* rename some files
* Remove @_implementationOnly from imports and raw pointer APIs
* Also move over FiniteFieldArithmeticContext to CryptoBoringWrapper
* Add FiniteFieldArithmeticContext.inverse
* Remove conditional compilation guards for FFAC
* Update CMakeLists.txt for moved files
* Revert "Add FiniteFieldArithmeticContext.inverse"
This reverts commit e02700e07c.
* Fix CMakeLists.txt
* Make most public symbols @inlinable
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation
This patch brings us up-to-date with CryptoKit's final 2023 API form.
Modifications
Slight tweak to exporter secrets for HPKE to match final spec.
New representation for SecureBytes when empty.
Result
We're ready for the future
Motivation
WWDC has arrived! 🎉 As part of the celebration, let's
bring Crypto up to speed with the new CryptoKit API surface.
Modifications
Substantial new docstrings
HPKE support
Result
WWDC 2023 support.
Needed to expose SecureEncalve through SwiftCrypto when building for
Swift-Certificates. CRYPTO_IN_SWIFTPM is set all of the time in
package.swift, while CRYPTO_IN_SWIFTPM_FORCE_BUILD_API is only set in
development. Lets enable the one that's on all of the time, and remove
the one that isn't so that we get our declarations.
# Motivation
BoringSSL exposes AES-GCM-SIV algorithms which are in general useful to have and provide a nonce-misuse resistant mode of AES-GCM. Since, `CryptoKit` is not exposing AES-GCM-SIV we need to add this to `_CryptoExtras`
# Modification
Exposes `AES-GCM-SIV` through `_CryptoExtras`.
# Result
We can now use `AES-GCM-SIV` through `_CryptoExtras`.
* Make BoringSSL wrapper match CryptoKit behaviour when working with x963 representation
* Revert Package.swift changes
* Fix Signing tests and add test cases for invalid key lengths
* Bring over API files from macOS Ventura
* Implement the new API surface
This patch implements the new API surface, and removes anything that's
no longer compatible with the expected API surface from CryptoKit
Without the explicit typing, the build fails with recent toolchain
builds expecting the type to conform to an incorrect type. This
repairs the build.
Co-authored-by: Cory Benfield <lukasa@apple.com>
When contentLength is greater than 8 bits, i.e., greater than UInt8.max, the original implementation crashes.
To work around this, use UInt8.init(truncatingIfNeeded:) to truncate only the lower 8 bits.
Motivation:
When using only the CryptoKit API on Apple platforms we have always
thunked through to the CryptoKit interface and implementation. However,
we didn't do a thorough job of preventing the BoringSSL target from
getting compiled and linked.
We can do a better job now, which will save compile times and binary
sizes in many cases.
Modifications:
- Change Package.swift to express a target specific dependency in most
cases.
- Preserve a development mode which overrides that target specific
dependency.
- Add the missing compile guards.
Results:
Smaller binaries and faster compiles on Apple platforms.
Motivation:
CryptoKit supports the compressed key format: we should as well.
Modifications:
Extend the constructors of public keys to tolerate the compressed
representation.
Result:
Users holding compressed keys can use them.
Motivation:
The various Apple SoS projects are currently dropping Swift 5.1 support.
Swift Crypto should do the same. While we're here, let's add CI for 5.4,
5.5 nightlies, and main nightlies.
Modifications:
- Remove 5.1 dockerfile.
- Stop claiming we support 5.1.
- Add 5.4, 5.5, and main dockerfiles.
- Updated Swift Format to 5.2.
- Associated formatting changes.
Result:
Better test matrix and support matrix.
When building swift-crypto from source for a toolchain build, it is
convenient to build it without building up a toolchain image. This
additional knob allows for building swift-crypto with the build
artifacts of previous builds, allowing building up a toolchain image
while chaining multiple builds together.
Co-authored-by: tomer doron <tomer@apple.com>
Resolves [issue#51](https://github.com/apple/swift-crypto/issues/51).
In Swift 5.3, a warning is viewed on Linux because `memset_s` implemented in this module returns `Void`.
This commit let the function return the same type with Darwin.
Motivation:
At WWDC 2020 Apple updated the CryptoKit APIs. This PR brings those new
APIs to Swift Crypto. This brings Swift Crypto up-to-date with the
latest CryptoKit API support, including PEM/DER key support and HKDF API
extensions.
Modifications:
- Bring forward the CryptoKit API files
- Update to have API match the WWDC seed.
Result:
Users will have access to the seed APIs.
Motiviation:
One `let` was hiding as a `var` and a few `#file` parameters have been
forwarded to (now) `#filePath` ones.
Modification:
- make the `var` a `let`
- silence the `#file`/`#filePath` warning by adding `(#file)` because we
can't forward them properly right now
(https://bugs.swift.org/browse/SR-12934). That's okay because even on
the Swift master branch, `#file` == `#filePath`.
Result:
No warnings in newer Swift compilers.
* 1) Adding gyb to scripts
2) Adding warning in gyb files about gyb overwritting manual edits to gyb generated swift files
3) Updated README with gyb instructions
4) Adding script for running all gyb generation
5) Adding CI check for discrepancies in gyb generated files
* Updating Dockerfile