Files
swift-crypto/Tests/CryptoTests/Encodings/DERTests.swift
T
michael-yuji bce1725222 FreeBSD support (#315)
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)
2024-12-16 12:26:11 +00:00

61 lines
2.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019-2020 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
// Skip tests that require @testable imports of CryptoKit.
#else
#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@testable import CryptoKit
#else
@testable import Crypto
#endif
class DERTests: XCTestCase {
func testEncodeDecodeECDSASignature() throws {
let pointSize = self.coordinateSizeForCurve(P256.self)
let r = self.randomBytes(count: pointSize)
let s = self.randomBytes(count: pointSize)
let signature = try orFail { try P256.Signing.ECDSASignature(rawRepresentation: (r + s)) }
XCTAssertEqual(Data(r + s), signature.rawRepresentation)
let der = try orFail { try P256.Signing.ECDSASignature(derRepresentation: signature.derRepresentation) }
XCTAssertEqual(der.rawRepresentation, signature.rawRepresentation)
XCTAssertEqual(der.derRepresentation, signature.derRepresentation)
XCTAssertEqual(der.rawRepresentation.count, 64)
}
func coordinateSizeForCurve<Curve: SupportedCurveDetailsImpl>(_ curve: Curve.Type) -> Int {
#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
return self.coreCryptoCoordinateSizeForCurve(curve)
#else
return self.openSSLCoordinateSizeForCurve(curve)
#endif
}
func randomBytes(count: Int) -> [UInt8] {
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows) || os(FreeBSD)
var rng = SystemRandomNumberGenerator()
return (0..<count).map { _ in rng.next() }
#else
fatalError("No secure random number generator on this platform.")
#endif
}
}
#endif // CRYPTO_IN_SWIFTPM