diff --git a/Package.swift b/Package.swift index 665ffe02..1a0bcb67 100644 --- a/Package.swift +++ b/Package.swift @@ -127,7 +127,7 @@ let package = Package( resources: [ .copy("PrivacyInfo.xcprivacy"), ], - swiftSettings: swiftSettings + swiftSettings: swiftSettings + [.define("MODULE_IS_CRYPTO")] ), .target( name: "_CryptoExtras", @@ -168,7 +168,6 @@ let package = Package( swiftSettings: swiftSettings ), .testTarget(name: "_CryptoExtrasTests", dependencies: ["_CryptoExtras"]), - .testTarget(name: "CryptoBoringWrapperTests", dependencies: ["CryptoBoringWrapper"]), ], cxxLanguageStandard: .cxx11 ) diff --git a/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift b/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift index cd7458fe..c85ac49e 100644 --- a/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift +++ b/Sources/Crypto/AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift @@ -14,8 +14,8 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CryptoBoringWrapper import Foundation enum OpenSSLAESGCMImpl { diff --git a/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift b/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift index f9c2ec0c..c4698aa1 100644 --- a/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift +++ b/Sources/Crypto/AEADs/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift @@ -14,9 +14,9 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CCryptoBoringSSLShims -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims +@_implementationOnly import CryptoBoringWrapper import Foundation extension BoringSSLAEAD { diff --git a/Sources/Crypto/CMakeLists.txt b/Sources/Crypto/CMakeLists.txt index aab7d236..da0fb6ec 100644 --- a/Sources/Crypto/CMakeLists.txt +++ b/Sources/Crypto/CMakeLists.txt @@ -89,6 +89,8 @@ add_library(Crypto "Util/BoringSSL/CryptoKitErrors_boring.swift" "Util/BoringSSL/RNG_boring.swift" "Util/BoringSSL/SafeCompare_boring.swift" + "Util/BoringSSL/Shared/ArbitraryPrecisionInteger_boring.swift" + "Util/BoringSSL/Shared/FiniteFieldArithmeticContext_boring.swift" "Util/BoringSSL/Zeroization_boring.swift" "Util/PrettyBytes.swift" "Util/SafeCompare.swift" diff --git a/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift b/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift index f50a6bdb..20a7a2c7 100644 --- a/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift +++ b/Sources/Crypto/Digests/BoringSSL/Digest_boring.swift @@ -14,7 +14,7 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL protocol HashFunctionImplementationDetails: HashFunction where Digest: DigestPrivate {} diff --git a/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift b/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift index c6b98fbf..98bf29ae 100644 --- a/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift +++ b/Sources/Crypto/Key Agreement/BoringSSL/ECDH_boring.swift @@ -14,7 +14,7 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL extension P256.KeyAgreement.PrivateKey { internal func openSSLSharedSecretFromKeyAgreement(with publicKeyShare: P256.KeyAgreement.PublicKey) throws -> SharedSecret { diff --git a/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift b/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift index f11d5fbf..bb96bf87 100644 --- a/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift +++ b/Sources/Crypto/Key Wrapping/BoringSSL/AESWrap_boring.swift @@ -14,7 +14,7 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL import Foundation enum BoringSSLAESWRAPImpl { diff --git a/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift index 02e73ade..17a4233b 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/Ed25519_boring.swift @@ -14,8 +14,8 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CCryptoBoringSSLShims +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation // For signing and verifying, we use BoringSSL's Ed25519, not the X25519 stuff. diff --git a/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift index ab1ca540..84283ddf 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift @@ -14,8 +14,7 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL /// A wrapper around BoringSSL's EC_POINT with some lifetime management. @usableFromInline diff --git a/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurve_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurve_boring.swift index ba7dfda4..3bfc6f2a 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurve_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/EllipticCurve_boring.swift @@ -14,8 +14,7 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL /// A wrapper around BoringSSL's EC_GROUP object that handles reference counting and /// liveness. diff --git a/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift index 2aa72453..d7439b99 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift @@ -14,9 +14,8 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CCryptoBoringSSLShims -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation @usableFromInline diff --git a/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift b/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift index 6eb62475..51cbeced 100644 --- a/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift +++ b/Sources/Crypto/Keys/EC/BoringSSL/X25519Keys_boring.swift @@ -14,8 +14,8 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CCryptoBoringSSLShims +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation extension Curve25519.KeyAgreement { diff --git a/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift b/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift index 311761cb..87a9a31c 100644 --- a/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/ECDSASignature_boring.swift @@ -14,9 +14,8 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CCryptoBoringSSLShims -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation /// A wrapper around BoringSSL's ECDSA_SIG with some lifetime management. diff --git a/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift b/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift index 49d48c60..7b0a79bf 100644 --- a/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/ECDSA_boring.swift @@ -14,7 +14,7 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL import Foundation extension Data { diff --git a/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift b/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift index ff25f612..5fd65e44 100644 --- a/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift +++ b/Sources/Crypto/Signatures/BoringSSL/EdDSA_boring.swift @@ -14,8 +14,8 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL -import CCryptoBoringSSLShims +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation extension Curve25519.Signing.PublicKey { diff --git a/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift b/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift index f0003b1b..62d3910b 100644 --- a/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift +++ b/Sources/Crypto/Util/BoringSSL/CryptoKitErrors_boring.swift @@ -14,7 +14,7 @@ #if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @_exported import CryptoKit #else -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL extension CryptoKitError { /// A helper function that packs the value of `ERR_get_error` into the internal error field. diff --git a/Sources/CryptoBoringWrapper/ArbitraryPrecisionInteger.swift b/Sources/Crypto/Util/BoringSSL/Shared/ArbitraryPrecisionInteger_boring.swift similarity index 77% rename from Sources/CryptoBoringWrapper/ArbitraryPrecisionInteger.swift rename to Sources/Crypto/Util/BoringSSL/Shared/ArbitraryPrecisionInteger_boring.swift index a5590629..73476e64 100644 --- a/Sources/CryptoBoringWrapper/ArbitraryPrecisionInteger.swift +++ b/Sources/Crypto/Util/BoringSSL/Shared/ArbitraryPrecisionInteger_boring.swift @@ -11,32 +11,38 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL -import CCryptoBoringSSLShims +#if MODULE_IS_CRYPTO && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +@_exported import CryptoKit +#else +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation +#if !MODULE_IS_CRYPTO +import enum Crypto.CryptoKitError +#endif /// A wrapper around the OpenSSL BIGNUM object that is appropriately lifetime managed, /// and that provides better Swift types for this object. -public struct ArbitraryPrecisionInteger { - /* private but @usableFromInline */ @usableFromInline var _backing: BackingStorage +@usableFromInline +struct ArbitraryPrecisionInteger { + private var _backing: BackingStorage - @inlinable - public init() { + @usableFromInline + init() { self._backing = BackingStorage() } - @inlinable - public init(copying original: UnsafePointer) throws { + init(copying original: UnsafePointer) throws { self._backing = try BackingStorage(copying: original) } - @inlinable - public init(_ original: ArbitraryPrecisionInteger) throws { + @usableFromInline + init(_ original: ArbitraryPrecisionInteger) throws { self._backing = try BackingStorage(copying: original._backing) } - @inlinable - public init(integerLiteral value: Int64) { + @usableFromInline + init(integerLiteral value: Int64) { self._backing = BackingStorage(value) } } @@ -44,36 +50,31 @@ public struct ArbitraryPrecisionInteger { // MARK: - BackingStorage extension ArbitraryPrecisionInteger { - @usableFromInline final class BackingStorage { - /* private but @usableFromInline */ @usableFromInline var _backing: BIGNUM + private var _backing: BIGNUM - @usableFromInline init() { self._backing = BIGNUM() CCryptoBoringSSL_BN_init(&self._backing) } - @usableFromInline init(copying original: UnsafePointer) throws { self._backing = BIGNUM() guard CCryptoBoringSSL_BN_copy(&self._backing, original) != nil else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } } - @usableFromInline init(copying original: BackingStorage) throws { self._backing = BIGNUM() try original.withUnsafeMutableBignumPointer { bnPtr in guard CCryptoBoringSSL_BN_copy(&self._backing, bnPtr) != nil else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } } } - @usableFromInline init(_ value: Int64) { self._backing = BIGNUM() let rc = CCryptoBoringSSL_BN_set_u64(&self._backing, value.magnitude) @@ -84,7 +85,6 @@ extension ArbitraryPrecisionInteger { } } - @inlinable deinit { CCryptoBoringSSL_BN_clear_free(&self._backing) } @@ -94,22 +94,21 @@ extension ArbitraryPrecisionInteger { // MARK: - Extra initializers extension ArbitraryPrecisionInteger { - @inlinable - public init(bytes: Bytes) throws { + @usableFromInline + init(bytes: Bytes) throws { self._backing = try BackingStorage(bytes: bytes) } /// Create an `ArbitraryPrecisionInteger` from a hex string. /// /// - Parameter hexString: Hex byte string (big-endian, no `0x` prefix, may start with `-` for a negative number). - @inlinable - public init(hexString: String) throws { + @usableFromInline + init(hexString: String) throws { self._backing = try BackingStorage(hexString: hexString) } } extension ArbitraryPrecisionInteger.BackingStorage { - @inlinable convenience init(bytes: Bytes) throws { self.init() @@ -117,7 +116,7 @@ extension ArbitraryPrecisionInteger.BackingStorage { CCryptoBoringSSLShims_BN_bin2bn(bytesPointer.baseAddress, bytesPointer.count, &self._backing) } guard rc != nil else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } } @@ -131,7 +130,7 @@ extension ArbitraryPrecisionInteger.BackingStorage { try withUnsafeMutablePointer(to: &backingPtr) { backingPtrPtr in /// `BN_hex2bin` returns the number of bytes of `in` processed or zero on error. guard CCryptoBoringSSL_BN_hex2bn(backingPtrPtr, hexStringPtr) == hexString.count else { - throw CryptoBoringWrapperError.incorrectParameterSize + throw CryptoKitError.incorrectParameterSize } } } @@ -139,18 +138,14 @@ extension ArbitraryPrecisionInteger.BackingStorage { } } - - // MARK: - Pointer helpers extension ArbitraryPrecisionInteger { - @inlinable - public func withUnsafeBignumPointer(_ body: (UnsafePointer) throws -> T) rethrows -> T { + func withUnsafeBignumPointer(_ body: (UnsafePointer) throws -> T) rethrows -> T { try self._backing.withUnsafeBignumPointer(body) } - @inlinable - public mutating func withUnsafeMutableBignumPointer(_ body: (UnsafeMutablePointer) throws -> T) rethrows -> T { + mutating func withUnsafeMutableBignumPointer(_ body: (UnsafeMutablePointer) throws -> T) rethrows -> T { if !isKnownUniquelyReferenced(&self._backing) { // Failing to CoW is a fatal error here. self._backing = try! BackingStorage(copying: self._backing) @@ -161,12 +156,10 @@ extension ArbitraryPrecisionInteger { } extension ArbitraryPrecisionInteger.BackingStorage { - @usableFromInline func withUnsafeBignumPointer(_ body: (UnsafePointer) throws -> T) rethrows -> T { try body(&self._backing) } - @usableFromInline func withUnsafeMutableBignumPointer(_ body: (UnsafeMutablePointer) throws -> T) rethrows -> T { try body(&self._backing) } @@ -190,8 +183,8 @@ extension ArbitraryPrecisionInteger { } } - @inlinable - public func squared() -> ArbitraryPrecisionInteger { + @usableFromInline + func squared() -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() let rc = result.withUnsafeMutableBignumPointer { resultPtr in self.withUnsafeBignumPointer { selfPtr in @@ -204,8 +197,8 @@ extension ArbitraryPrecisionInteger { return result } - @inlinable - public func positiveSquareRoot() throws -> ArbitraryPrecisionInteger { + @usableFromInline + func positiveSquareRoot() throws -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() let rc = result.withUnsafeMutableBignumPointer { resultPtr in self.withUnsafeBignumPointer { selfPtr in @@ -216,20 +209,20 @@ extension ArbitraryPrecisionInteger { } guard rc == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return result } - @inlinable - public var byteCount: Int { + @usableFromInline + var byteCount: Int { self._backing.withUnsafeBignumPointer { Int(CCryptoBoringSSL_BN_num_bytes($0)) } } /// Some functions require a BN_CTX parameter: this obtains one with a scoped lifetime. - /* private but @usableFromInline */ @usableFromInline static func withUnsafeBN_CTX(_ body: (OpaquePointer) throws -> T) rethrows -> T { + private static func withUnsafeBN_CTX(_ body: (OpaquePointer) throws -> T) rethrows -> T { // We force unwrap here because this call can only fail if the allocator is broken, and if // the allocator fails we don't have long to live anyway. let bnCtx = CCryptoBoringSSL_BN_CTX_new()! @@ -245,7 +238,7 @@ extension ArbitraryPrecisionInteger { extension ArbitraryPrecisionInteger: Equatable { @inlinable - public static func == (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { + static func == (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { self._compare(lhs: lhs, rhs: rhs) == 0 } } @@ -254,22 +247,22 @@ extension ArbitraryPrecisionInteger: Equatable { extension ArbitraryPrecisionInteger: Comparable { @inlinable - public static func < (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { + static func < (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { self._compare(lhs: lhs, rhs: rhs) < 0 } @inlinable - public static func <= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { + static func <= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { self._compare(lhs: lhs, rhs: rhs) <= 0 } @inlinable - public static func > (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { + static func > (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { self._compare(lhs: lhs, rhs: rhs) > 0 } @inlinable - public static func >= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { + static func >= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool { self._compare(lhs: lhs, rhs: rhs) >= 0 } } @@ -282,12 +275,12 @@ extension ArbitraryPrecisionInteger: ExpressibleByIntegerLiteral {} extension ArbitraryPrecisionInteger: AdditiveArithmetic { @inlinable - public static var zero: ArbitraryPrecisionInteger { + static var zero: ArbitraryPrecisionInteger { 0 } - @inlinable - public static func + (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger { + @usableFromInline + static func + (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() let rc = result.withUnsafeMutableBignumPointer { resultPtr in @@ -302,8 +295,8 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic { return result } - @inlinable - public static func += (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) { + @usableFromInline + static func += (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) { let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in rhs.withUnsafeBignumPointer { rhsPtr in CCryptoBoringSSL_BN_add(lhsPtr, lhsPtr, rhsPtr) @@ -312,8 +305,8 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic { precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger") } - @inlinable - public static func - (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger { + @usableFromInline + static func - (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() let rc = result.withUnsafeMutableBignumPointer { resultPtr in @@ -328,8 +321,8 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic { return result } - @inlinable - public static func -= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) { + @usableFromInline + static func -= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) { let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in rhs.withUnsafeBignumPointer { rhsPtr in CCryptoBoringSSL_BN_sub(lhsPtr, lhsPtr, rhsPtr) @@ -342,10 +335,11 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic { // MARK: - Numeric extension ArbitraryPrecisionInteger: Numeric { - public typealias Magnitude = Self + @usableFromInline + typealias Magnitude = Self - @inlinable - public var magnitude: Magnitude { + @usableFromInline + var magnitude: Magnitude { if self._positive { return self } @@ -359,8 +353,8 @@ extension ArbitraryPrecisionInteger: Numeric { return copy } - @inlinable - public static func * (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger { + @usableFromInline + static func * (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() let rc = result.withUnsafeMutableBignumPointer { resultPtr in @@ -377,8 +371,8 @@ extension ArbitraryPrecisionInteger: Numeric { return result } - @inlinable - public static func *= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) { + @usableFromInline + static func *= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) { let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in rhs.withUnsafeBignumPointer { rhsPtr in ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in @@ -390,7 +384,7 @@ extension ArbitraryPrecisionInteger: Numeric { } @inlinable - public init?(exactly integer: T) { + init?(exactly integer: T) { fatalError("Not currently implemented") } } @@ -398,8 +392,8 @@ extension ArbitraryPrecisionInteger: Numeric { // MARK: - SignedNumeric extension ArbitraryPrecisionInteger: SignedNumeric { - @inlinable - public mutating func negate() { + @usableFromInline + mutating func negate() { let signBit: CInt = self._positive ? 1 : 0 self.withUnsafeMutableBignumPointer { @@ -411,8 +405,8 @@ extension ArbitraryPrecisionInteger: SignedNumeric { // MARK: - Other arithmetic operations extension ArbitraryPrecisionInteger { - @inlinable - public static func gcd(_ a: ArbitraryPrecisionInteger, _ b: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + static func gcd(_ a: ArbitraryPrecisionInteger, _ b: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() guard result.withUnsafeMutableBignumPointer({ resultPtr in @@ -424,19 +418,19 @@ extension ArbitraryPrecisionInteger { } } }) == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return result } - @inlinable - public func isCoprime(with other: ArbitraryPrecisionInteger) throws -> Bool { + @usableFromInline + func isCoprime(with other: ArbitraryPrecisionInteger) throws -> Bool { try Self.gcd(self, other) == 1 } - @inlinable - public static func random(inclusiveMin: UInt, exclusiveMax: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + static func random(inclusiveMin: UInt, exclusiveMax: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() guard result.withUnsafeMutableBignumPointer({ resultPtr in @@ -444,7 +438,7 @@ extension ArbitraryPrecisionInteger { CCryptoBoringSSL_BN_rand_range_ex(resultPtr, BN_ULONG(inclusiveMin), exclusiveMaxPtr) } }) == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return result @@ -455,12 +449,12 @@ extension ArbitraryPrecisionInteger { extension Data { /// Serializes an ArbitraryPrecisionInteger padded out to a certain minimum size. - @inlinable - public mutating func append(bytesOf integer: ArbitraryPrecisionInteger, paddedToSize paddingSize: Int) throws { + @usableFromInline + mutating func append(bytesOf integer: ArbitraryPrecisionInteger, paddedToSize paddingSize: Int) throws { let byteCount = integer.byteCount guard paddingSize >= byteCount else { - throw CryptoBoringWrapperError.incorrectParameterSize + throw CryptoKitError.incorrectParameterSize } // To extend the data we need to write some zeroes into it. @@ -479,8 +473,8 @@ extension Data { assert(written == byteCount) } - @inlinable - public init(bytesOf integer: ArbitraryPrecisionInteger, paddedToSize paddingSize: Int) throws { + @usableFromInline + init(bytesOf integer: ArbitraryPrecisionInteger, paddedToSize paddingSize: Int) throws { self.init(capacity: paddingSize) try self.append(bytesOf: integer, paddedToSize: paddingSize) } @@ -489,7 +483,8 @@ extension Data { // MARK: - Printing extension ArbitraryPrecisionInteger: CustomDebugStringConvertible { - public var debugDescription: String { + @usableFromInline + var debugDescription: String { guard let bio = CCryptoBoringSSL_BIO_new(CCryptoBoringSSL_BIO_s_mem()) else { return "ArbitraryPrecisionInteger: (error generating representation)" } @@ -524,3 +519,4 @@ extension ArbitraryPrecisionInteger: CustomDebugStringConvertible { return String(decoding: UnsafeBufferPointer(start: stringPointer, count: length), as: Unicode.UTF8.self) } } +#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API diff --git a/Sources/CryptoBoringWrapper/FiniteFieldArithmeticContext.swift b/Sources/Crypto/Util/BoringSSL/Shared/FiniteFieldArithmeticContext_boring.swift similarity index 73% rename from Sources/CryptoBoringWrapper/FiniteFieldArithmeticContext.swift rename to Sources/Crypto/Util/BoringSSL/Shared/FiniteFieldArithmeticContext_boring.swift index 4e219f39..165250b4 100644 --- a/Sources/CryptoBoringWrapper/FiniteFieldArithmeticContext.swift +++ b/Sources/Crypto/Util/BoringSSL/Shared/FiniteFieldArithmeticContext_boring.swift @@ -11,7 +11,14 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL +#if MODULE_IS_CRYPTO && CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +@_exported import CryptoKit +#else +@_implementationOnly import CCryptoBoringSSL +import Foundation +#if !MODULE_IS_CRYPTO +import enum Crypto.CryptoKitError +#endif /// A context for performing mathematical operations on ArbitraryPrecisionIntegers over a finite field. /// @@ -24,21 +31,21 @@ import CCryptoBoringSSL /// /// Annoyingly, because of the way we have implemented ArbitraryPrecisionInteger, we can't actually use these temporary bignums /// ourselves. -public final class FiniteFieldArithmeticContext { - /* private but @usableFromInline */ @usableFromInline var fieldSize: ArbitraryPrecisionInteger - /* private but @usableFromInline */ @usableFromInline var bnCtx: OpaquePointer +@usableFromInline +class FiniteFieldArithmeticContext { + private var fieldSize: ArbitraryPrecisionInteger + private var bnCtx: OpaquePointer - @inlinable - public init(fieldSize: ArbitraryPrecisionInteger) throws { + @usableFromInline + init(fieldSize: ArbitraryPrecisionInteger) throws { self.fieldSize = fieldSize guard let bnCtx = CCryptoBoringSSL_BN_CTX_new() else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } CCryptoBoringSSL_BN_CTX_start(bnCtx) self.bnCtx = bnCtx } - @inlinable deinit { CCryptoBoringSSL_BN_CTX_end(self.bnCtx) CCryptoBoringSSL_BN_CTX_free(self.bnCtx) @@ -48,8 +55,8 @@ public final class FiniteFieldArithmeticContext { // MARK: - Arithmetic operations extension FiniteFieldArithmeticContext { - @inlinable - public func residue(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + func residue(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var result = ArbitraryPrecisionInteger() guard x.withUnsafeBignumPointer({ xPtr in @@ -59,15 +66,14 @@ extension FiniteFieldArithmeticContext { } } }) == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return result - } - @inlinable - public func square(_ input: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + func square(_ input: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = input.withUnsafeBignumPointer { inputPointer in @@ -79,14 +85,14 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return output } - @inlinable - public func multiply(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + func multiply(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = x.withUnsafeBignumPointer { xPointer in @@ -100,14 +106,14 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return output } - @inlinable - public func add(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + func add(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = x.withUnsafeBignumPointer { xPointer in @@ -121,14 +127,14 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return output } - @inlinable - public func subtract(_ x: ArbitraryPrecisionInteger, from y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + func subtract(_ x: ArbitraryPrecisionInteger, from y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { var output = ArbitraryPrecisionInteger() let rc = x.withUnsafeBignumPointer { xPointer in @@ -143,14 +149,14 @@ extension FiniteFieldArithmeticContext { } guard rc == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return output } - @inlinable - public func positiveSquareRoot(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + func positiveSquareRoot(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { let outputPointer = x.withUnsafeBignumPointer { xPointer in self.fieldSize.withUnsafeBignumPointer { fieldSizePointer in // We can't pass a pointer in as BN_mod_sqrt may attempt to free it. @@ -159,7 +165,7 @@ extension FiniteFieldArithmeticContext { } guard let actualOutputPointer = outputPointer else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } // Ok, we own this pointer now. @@ -170,14 +176,14 @@ extension FiniteFieldArithmeticContext { return try ArbitraryPrecisionInteger(copying: actualOutputPointer) } - @inlinable - public func inverse(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger? { + @usableFromInline + func inverse(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger? { var result = ArbitraryPrecisionInteger() guard result.withUnsafeMutableBignumPointer({ resultPtr in x.withUnsafeBignumPointer { xPtr in self.fieldSize.withUnsafeBignumPointer { modPtr in - CCryptoBoringSSL_BN_mod_inverse(resultPtr, xPtr , modPtr, self.bnCtx) + CCryptoBoringSSL_BN_mod_inverse(resultPtr, xPtr, modPtr, self.bnCtx) } } }) != nil else { return nil } @@ -185,24 +191,24 @@ extension FiniteFieldArithmeticContext { return result } - @inlinable - public func pow(_ x: ArbitraryPrecisionInteger, _ p: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + @usableFromInline + func pow(_ x: ArbitraryPrecisionInteger, _ p: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { try self.pow(x, p) { r, x, p, m, ctx, _ in CCryptoBoringSSL_BN_mod_exp(r, x, p, m, ctx) } } - @inlinable - public func pow(secret x: ArbitraryPrecisionInteger, _ p: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { - guard x < self.fieldSize else { throw CryptoBoringWrapperError.incorrectParameterSize } + @usableFromInline + func pow(secret x: ArbitraryPrecisionInteger, _ p: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + guard x < self.fieldSize else { throw CryptoKitError.incorrectParameterSize } return try self.pow(x, p, using: CCryptoBoringSSL_BN_mod_exp_mont) } - @inlinable - public func pow(secret x: ArbitraryPrecisionInteger, secret p: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { - guard x < self.fieldSize else { throw CryptoBoringWrapperError.incorrectParameterSize } + @usableFromInline + func pow(secret x: ArbitraryPrecisionInteger, secret p: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger { + guard x < self.fieldSize else { throw CryptoKitError.incorrectParameterSize } return try self.pow(x, p, using: CCryptoBoringSSL_BN_mod_exp_mont_consttime) } - /* private but @usableFromInline */ @usableFromInline func pow( + fileprivate func pow( _ a: ArbitraryPrecisionInteger, _ b: ArbitraryPrecisionInteger, using method: ( @@ -227,14 +233,14 @@ extension FiniteFieldArithmeticContext { } } }) == 1 else { - throw CryptoBoringWrapperError.internalBoringSSLError() + throw CryptoKitError.internalBoringSSLError() } return result } /// Some functions require a `BN_MONT_CTX` parameter: this obtains one for the field modulus with a scoped lifetime. - /* private but @usableFromInline */ @usableFromInline func withUnsafeBN_MONT_CTX(_ body: (UnsafePointer) throws -> T) rethrows -> T { + fileprivate func withUnsafeBN_MONT_CTX(_ body: (UnsafePointer) throws -> T) rethrows -> T { try self.fieldSize.withUnsafeBignumPointer { modPtr in // We force unwrap here because this call can only fail if the allocator is broken, and if // the allocator fails we don't have long to live anyway. @@ -244,3 +250,4 @@ extension FiniteFieldArithmeticContext { } } } +#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API diff --git a/Sources/Crypto/Util/BoringSSL/Zeroization_boring.swift b/Sources/Crypto/Util/BoringSSL/Zeroization_boring.swift index c9907581..a4e1ecea 100644 --- a/Sources/Crypto/Util/BoringSSL/Zeroization_boring.swift +++ b/Sources/Crypto/Util/BoringSSL/Zeroization_boring.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// #if !canImport(Darwin) -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL typealias errno_t = CInt diff --git a/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift b/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift index 08aa3d48..4299e561 100644 --- a/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift +++ b/Sources/CryptoBoringWrapper/AEAD/BoringSSLAEAD.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL -import CCryptoBoringSSLShims +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation /// An abstraction over a BoringSSL AEAD diff --git a/Sources/CryptoBoringWrapper/CMakeLists.txt b/Sources/CryptoBoringWrapper/CMakeLists.txt index 796315f9..1de749f2 100644 --- a/Sources/CryptoBoringWrapper/CMakeLists.txt +++ b/Sources/CryptoBoringWrapper/CMakeLists.txt @@ -14,9 +14,7 @@ add_library(CryptoBoringWrapper STATIC "AEAD/BoringSSLAEAD.swift" - "ArbitraryPrecisionInteger.swift" - "CryptoKitErrors_boring.swift" - "FiniteFieldArithmeticContext.swift") + "CryptoKitErrors_boring.swift") target_include_directories(CryptoBoringWrapper PUBLIC $ diff --git a/Sources/CryptoBoringWrapper/CryptoKitErrors_boring.swift b/Sources/CryptoBoringWrapper/CryptoKitErrors_boring.swift index 6fba42ed..81f127b3 100644 --- a/Sources/CryptoBoringWrapper/CryptoKitErrors_boring.swift +++ b/Sources/CryptoBoringWrapper/CryptoKitErrors_boring.swift @@ -12,11 +12,10 @@ // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL public enum CryptoBoringWrapperError: Error { case underlyingCoreCryptoError(error: Int32) - case incorrectParameterSize } extension CryptoBoringWrapperError { diff --git a/Sources/_CryptoExtras/AES/AES_GCM_SIV.swift b/Sources/_CryptoExtras/AES/AES_GCM_SIV.swift index 43b3dd3c..527a91a3 100644 --- a/Sources/_CryptoExtras/AES/AES_GCM_SIV.swift +++ b/Sources/_CryptoExtras/AES/AES_GCM_SIV.swift @@ -13,9 +13,9 @@ //===----------------------------------------------------------------------===// import Crypto -import CCryptoBoringSSL -import CCryptoBoringSSLShims -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims +@_implementationOnly import CryptoBoringWrapper #if canImport(Darwin) || swift(>=5.9.1) import Foundation #else diff --git a/Sources/_CryptoExtras/AES/Block Function.swift b/Sources/_CryptoExtras/AES/Block Function.swift index 6e8c9e41..fc1fe23e 100644 --- a/Sources/_CryptoExtras/AES/Block Function.swift +++ b/Sources/_CryptoExtras/AES/Block Function.swift @@ -13,9 +13,9 @@ //===----------------------------------------------------------------------===// import Crypto -import CCryptoBoringSSL -import CCryptoBoringSSLShims -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims +@_implementationOnly import CryptoBoringWrapper import Foundation extension AES { diff --git a/Sources/_CryptoExtras/AES/BoringSSL/AES_CTR_boring.swift b/Sources/_CryptoExtras/AES/BoringSSL/AES_CTR_boring.swift index 084a6871..8ed5a9ef 100644 --- a/Sources/_CryptoExtras/AES/BoringSSL/AES_CTR_boring.swift +++ b/Sources/_CryptoExtras/AES/BoringSSL/AES_CTR_boring.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL import Crypto import Foundation diff --git a/Sources/_CryptoExtras/AES/BoringSSL/AES_GCM_SIV_boring.swift b/Sources/_CryptoExtras/AES/BoringSSL/AES_GCM_SIV_boring.swift index 72a403c4..c291ddd9 100644 --- a/Sources/_CryptoExtras/AES/BoringSSL/AES_GCM_SIV_boring.swift +++ b/Sources/_CryptoExtras/AES/BoringSSL/AES_GCM_SIV_boring.swift @@ -14,10 +14,10 @@ // This is a copy ChaChaPoly_boring just with a different set aes algos -import CCryptoBoringSSL -import CCryptoBoringSSLShims +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Crypto -import CryptoBoringWrapper +@_implementationOnly import CryptoBoringWrapper import Foundation extension BoringSSLAEAD { diff --git a/Sources/_CryptoExtras/CMakeLists.txt b/Sources/_CryptoExtras/CMakeLists.txt index d59ad4d5..3ea4defd 100644 --- a/Sources/_CryptoExtras/CMakeLists.txt +++ b/Sources/_CryptoExtras/CMakeLists.txt @@ -24,7 +24,9 @@ add_library(_CryptoExtras "Util/DigestType.swift" "Util/Error.swift" "Util/PEMDocument.swift" - "Util/RandomBytes.swift") + "Util/RandomBytes.swift" + "Util/Shared/ArbitraryPrecisionInteger_boring.swift" + "Util/Shared/FiniteFieldArithmeticContext_boring.swift") target_include_directories(_CryptoExtras PRIVATE $ diff --git a/Sources/_CryptoExtras/ChaCha20CTR/BoringSSL/ChaCha20CTR_boring.swift b/Sources/_CryptoExtras/ChaCha20CTR/BoringSSL/ChaCha20CTR_boring.swift index 7d6c9221..14e2e90a 100644 --- a/Sources/_CryptoExtras/ChaCha20CTR/BoringSSL/ChaCha20CTR_boring.swift +++ b/Sources/_CryptoExtras/ChaCha20CTR/BoringSSL/ChaCha20CTR_boring.swift @@ -12,10 +12,10 @@ // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL -import CCryptoBoringSSLShims +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Crypto -import CryptoBoringWrapper +@_implementationOnly import CryptoBoringWrapper import Foundation enum OpenSSLChaCha20CTRImpl { diff --git a/Sources/_CryptoExtras/ChaCha20CTR/ChaCha20CTR.swift b/Sources/_CryptoExtras/ChaCha20CTR/ChaCha20CTR.swift index f23d8ccd..975010ae 100644 --- a/Sources/_CryptoExtras/ChaCha20CTR/ChaCha20CTR.swift +++ b/Sources/_CryptoExtras/ChaCha20CTR/ChaCha20CTR.swift @@ -12,10 +12,10 @@ // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL -import CCryptoBoringSSLShims +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Crypto -import CryptoBoringWrapper +@_implementationOnly import CryptoBoringWrapper #if canImport(Darwin) || swift(>=5.9.1) import Foundation #else diff --git a/Sources/_CryptoExtras/RSA/RSA_boring.swift b/Sources/_CryptoExtras/RSA/RSA_boring.swift index 368ce5fd..e82a9baa 100644 --- a/Sources/_CryptoExtras/RSA/RSA_boring.swift +++ b/Sources/_CryptoExtras/RSA/RSA_boring.swift @@ -15,9 +15,8 @@ import Foundation import Crypto // NOTE: This file is unconditionally compiled because RSABSSA is implemented using BoringSSL on all platforms. -import CCryptoBoringSSL -import CCryptoBoringSSLShims -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims internal struct BoringSSLRSAPublicKey: Sendable { private var backing: Backing diff --git a/Sources/_CryptoExtras/Util/BoringSSLHelpers.swift b/Sources/_CryptoExtras/Util/BoringSSLHelpers.swift index 3169d348..d2ca9421 100644 --- a/Sources/_CryptoExtras/Util/BoringSSLHelpers.swift +++ b/Sources/_CryptoExtras/Util/BoringSSLHelpers.swift @@ -13,9 +13,8 @@ //===----------------------------------------------------------------------===// // NOTE: This file is unconditionally compiled because RSABSSA is implemented using BoringSSL on all platforms. -import CCryptoBoringSSL -import CCryptoBoringSSLShims -import CryptoBoringWrapper +@_implementationOnly import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSLShims import Foundation import Crypto diff --git a/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift b/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift index d083490f..55b3cb37 100644 --- a/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift +++ b/Sources/_CryptoExtras/Util/CryptoKitErrors_boring.swift @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL import Crypto extension CryptoKitError { diff --git a/Sources/_CryptoExtras/Util/DigestType.swift b/Sources/_CryptoExtras/Util/DigestType.swift index 8434007d..631d52bf 100644 --- a/Sources/_CryptoExtras/Util/DigestType.swift +++ b/Sources/_CryptoExtras/Util/DigestType.swift @@ -13,7 +13,7 @@ //===----------------------------------------------------------------------===// // NOTE: This file is unconditionally compiled because RSABSSA is implemented using BoringSSL on all platforms. -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL import Crypto struct DigestType { diff --git a/Sources/_CryptoExtras/Util/Shared b/Sources/_CryptoExtras/Util/Shared new file mode 120000 index 00000000..711a2444 --- /dev/null +++ b/Sources/_CryptoExtras/Util/Shared @@ -0,0 +1 @@ +../../Crypto/Util/BoringSSL/Shared \ No newline at end of file diff --git a/Tests/CryptoBoringWrapperTests/ArbitraryPrecisionIntegerTests.swift b/Tests/CryptoTests/BoringSSL/ArbitraryPrecisionIntegerTests.swift similarity index 95% rename from Tests/CryptoBoringWrapperTests/ArbitraryPrecisionIntegerTests.swift rename to Tests/CryptoTests/BoringSSL/ArbitraryPrecisionIntegerTests.swift index cc91a3db..4536e087 100644 --- a/Tests/CryptoBoringWrapperTests/ArbitraryPrecisionIntegerTests.swift +++ b/Tests/CryptoTests/BoringSSL/ArbitraryPrecisionIntegerTests.swift @@ -11,7 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -@testable import CryptoBoringWrapper +#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#else +@testable import Crypto import XCTest final class ArbitraryPrecisionIntegerTests: XCTestCase { @@ -41,7 +43,7 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase { func testPositiveSquareRoot() { XCTAssertNoThrow(XCTAssertEqual(try ArbitraryPrecisionInteger(144).positiveSquareRoot(), 12)) XCTAssertThrowsError(try ArbitraryPrecisionInteger(101).positiveSquareRoot()) { error in - guard case .some(.underlyingCoreCryptoError) = error as? CryptoBoringWrapperError else { + guard case .some(.underlyingCoreCryptoError) = error as? CryptoKitError else { XCTFail("Unexpected error: \(error)") return } @@ -148,8 +150,8 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase { XCTAssertEqual(try ArbitraryPrecisionInteger.random(inclusiveMin: 4, exclusiveMax: 5), 4) var previousRandom = ArbitraryPrecisionInteger() - for _ in 1...1_000 { - let exclusiveMax = try ArbitraryPrecisionInteger(bytes: Data(repeating: UInt8.max, count: 2048/8)) + for _ in 1 ... 1000 { + let exclusiveMax = try ArbitraryPrecisionInteger(bytes: Data(repeating: UInt8.max, count: 2048 / 8)) let random = try ArbitraryPrecisionInteger.random(inclusiveMin: 42, exclusiveMax: exclusiveMax) XCTAssert(random >= ArbitraryPrecisionInteger(42)) XCTAssert(random < exclusiveMax) @@ -166,3 +168,4 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase { } } } +#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API diff --git a/Tests/CryptoBoringWrapperTests/FiniteFieldArithmeticTests.swift b/Tests/CryptoTests/BoringSSL/FiniteFieldArithmeticTests.swift similarity index 91% rename from Tests/CryptoBoringWrapperTests/FiniteFieldArithmeticTests.swift rename to Tests/CryptoTests/BoringSSL/FiniteFieldArithmeticTests.swift index 5f6d1ce0..72f83ca2 100644 --- a/Tests/CryptoBoringWrapperTests/FiniteFieldArithmeticTests.swift +++ b/Tests/CryptoTests/BoringSSL/FiniteFieldArithmeticTests.swift @@ -11,10 +11,11 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -@testable import CryptoBoringWrapper +#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API +#else +@testable import Crypto import XCTest - final class FiniteFieldArithmeticTests: XCTestCase { func testResidue() throws { let ff = try FiniteFieldArithmeticContext(fieldSize: 3) @@ -85,7 +86,7 @@ final class FiniteFieldArithmeticTests: XCTestCase { XCTAssertEqual(try ff.inverse(4), 1) XCTAssertEqual(try ff.inverse(5), 2) XCTAssertEqual(try ff.inverse(6), nil) - for i: Int64 in 1...100 { + for i: Int64 in 1 ... 100 { let integer = ArbitraryPrecisionInteger(integerLiteral: i) let inverse = try ff.inverse(integer) if i % 3 == 0 { @@ -104,8 +105,8 @@ final class FiniteFieldArithmeticTests: XCTestCase { (2, 0, 1), (2, 1, 2), (2, 2, 4), (2, 3, 1), (3, 0, 1), (3, 1, 3), (3, 2, 2), (3, 3, 6), (5, 0, 1), (5, 1, 5), (5, 2, 4), (5, 3, 6), - (7, 0, 1), (7, 1, 0), (7, 2, 0), (7, 3, 0), // x = m - (8, 0, 1), (8, 1, 1), (8, 2, 1), (8, 3, 1), // x > m + (7, 0, 1), (7, 1, 0), (7, 2, 0), (7, 3, 0), // x = m + (8, 0, 1), (8, 1, 1), (8, 2, 1), (8, 3, 1), // x > m ] { let message = "\(x)^\(p) (mod \(m))" XCTAssertEqual(try ff.pow(x, p), expectedResult, message) @@ -114,8 +115,8 @@ final class FiniteFieldArithmeticTests: XCTestCase { XCTAssertEqual(try ff.pow(secret: x, secret: p), expectedResult, message) } else { XCTAssertThrowsError(try ff.pow(secret: x, p), message) { error in - switch (error as? CryptoBoringWrapperError) { - case .incorrectParameterSize: break // OK + switch error as? CryptoKitError { + case .incorrectParameterSize: break // OK default: XCTFail("Unexpected error: \(error)") } } @@ -123,3 +124,4 @@ final class FiniteFieldArithmeticTests: XCTestCase { } } } +#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API diff --git a/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift b/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift index 47cc3231..a9c6ab2c 100644 --- a/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift +++ b/Tests/CryptoTests/ECDH/BoringSSL/ASN1.swift @@ -20,7 +20,7 @@ import XCTest #if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @testable import CryptoKit #else -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL @testable import Crypto #endif diff --git a/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift b/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift index e5fbbd0d..df7751b9 100644 --- a/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift +++ b/Tests/CryptoTests/ECDH/BoringSSL/secpECDH_Runner_boring.swift @@ -20,7 +20,7 @@ import XCTest #if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API @testable import CryptoKit #else -import CCryptoBoringSSL +@_implementationOnly import CCryptoBoringSSL @testable import Crypto #endif diff --git a/scripts/update_cmakelists.sh b/scripts/update_cmakelists.sh index 48777ebc..71436216 100755 --- a/scripts/update_cmakelists.sh +++ b/scripts/update_cmakelists.sh @@ -55,7 +55,7 @@ function update_cmakelists_source() { fi # Wrap quotes around each filename since it might contain spaces - srcs=$($find "${src_root}" -type f \( "${exts_arg[@]}" \) -printf ' "%P"\n' | LC_ALL=POSIX sort) + srcs=$($find -L "${src_root}" -type f \( "${exts_arg[@]}" \) -printf ' "%P"\n' | LC_ALL=POSIX sort) echo "$srcs" # Update list of source files in CMakeLists.txt