Skip to content

Commit f98e46e

Browse files
wrapper: Move EC types and random bytes into CryptoBoringWrapper (#277)
* 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
1 parent ae321d2 commit f98e46e

File tree

10 files changed

+28
-44
lines changed

10 files changed

+28
-44
lines changed

Sources/Crypto/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ add_library(Crypto
6767
"Key Wrapping/AESWrap.swift"
6868
"Key Wrapping/BoringSSL/AESWrap_boring.swift"
6969
"Keys/EC/BoringSSL/Ed25519_boring.swift"
70-
"Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift"
71-
"Keys/EC/BoringSSL/EllipticCurve_boring.swift"
7270
"Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift"
7371
"Keys/EC/BoringSSL/X25519Keys_boring.swift"
7472
"Keys/EC/Curve25519.swift"

Sources/CryptoBoringWrapper/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
add_library(CryptoBoringWrapper STATIC
1616
"AEAD/BoringSSLAEAD.swift"
1717
"CryptoKitErrors_boring.swift"
18-
"Util/ArbitraryPrecisionInteger_boring.swift"
19-
"Util/FiniteFieldArithmeticContext_boring.swift")
18+
"EC/EllipticCurve.swift"
19+
"EC/EllipticCurvePoint.swift"
20+
"Util/ArbitraryPrecisionInteger.swift"
21+
"Util/FiniteFieldArithmeticContext.swift"
22+
"Util/RandomBytes.swift")
2023

2124
target_include_directories(CryptoBoringWrapper PUBLIC
2225
$<TARGET_PROPERTY:CCryptoBoringSSL,INCLUDE_DIRECTORIES>

Sources/Crypto/Keys/EC/BoringSSL/EllipticCurve_boring.swift renamed to Sources/CryptoBoringWrapper/EC/EllipticCurve.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,18 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15-
@_exported import CryptoKit
16-
#else
1714
@_implementationOnly import CCryptoBoringSSL
18-
import CryptoBoringWrapper
1915

2016
/// A wrapper around BoringSSL's EC_GROUP object that handles reference counting and
2117
/// liveness.
2218
@usableFromInline
23-
class BoringSSLEllipticCurveGroup {
19+
package class BoringSSLEllipticCurveGroup {
2420
/* private but usableFromInline */ @usableFromInline var _group: OpaquePointer
2521

2622
@usableFromInline
27-
init(_ curve: CurveName) throws {
23+
package init(_ curve: CurveName) throws {
2824
guard let group = CCryptoBoringSSL_EC_GROUP_new_by_curve_name(curve.baseNID) else {
29-
throw CryptoKitError.internalBoringSSLError()
25+
throw CryptoBoringWrapperError.internalBoringSSLError()
3026
}
3127

3228
self._group = group
@@ -41,36 +37,36 @@ class BoringSSLEllipticCurveGroup {
4137

4238
extension BoringSSLEllipticCurveGroup {
4339
@usableFromInline
44-
var coordinateByteCount: Int {
40+
package var coordinateByteCount: Int {
4541
(Int(CCryptoBoringSSL_EC_GROUP_get_degree(self._group)) + 7) / 8
4642
}
4743

4844
@usableFromInline
49-
func makeUnsafeOwnedECKey() throws -> OpaquePointer {
45+
package func makeUnsafeOwnedECKey() throws -> OpaquePointer {
5046
guard let key = CCryptoBoringSSL_EC_KEY_new(),
5147
CCryptoBoringSSL_EC_KEY_set_group(key, self._group) == 1 else {
52-
throw CryptoKitError.internalBoringSSLError()
48+
throw CryptoBoringWrapperError.internalBoringSSLError()
5349
}
5450

5551
return key
5652
}
5753

5854
@usableFromInline
59-
func makeUnsafeOwnedECPoint() throws -> OpaquePointer {
55+
package func makeUnsafeOwnedECPoint() throws -> OpaquePointer {
6056
guard let point = CCryptoBoringSSL_EC_POINT_new(self._group) else {
61-
throw CryptoKitError.internalBoringSSLError()
57+
throw CryptoBoringWrapperError.internalBoringSSLError()
6258
}
6359

6460
return point
6561
}
6662

6763
@inlinable
68-
func withUnsafeGroupPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
64+
package func withUnsafeGroupPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
6965
try body(self._group)
7066
}
7167

7268
@usableFromInline
73-
var order: ArbitraryPrecisionInteger {
69+
package var order: ArbitraryPrecisionInteger {
7470
// Groups must have an order.
7571
let baseOrder = CCryptoBoringSSL_EC_GROUP_get0_order(self._group)!
7672
return try! ArbitraryPrecisionInteger(copying: baseOrder)
@@ -79,7 +75,7 @@ extension BoringSSLEllipticCurveGroup {
7975
/// An elliptic curve can be represented in a Weierstrass form: `y² = x³ + ax + b`. This
8076
/// property provides the values of a and b on the curve.
8177
@usableFromInline
82-
var weierstrassCoefficients: (field: ArbitraryPrecisionInteger, a: ArbitraryPrecisionInteger, b: ArbitraryPrecisionInteger) {
78+
package var weierstrassCoefficients: (field: ArbitraryPrecisionInteger, a: ArbitraryPrecisionInteger, b: ArbitraryPrecisionInteger) {
8379
var field = ArbitraryPrecisionInteger()
8480
var a = ArbitraryPrecisionInteger()
8581
var b = ArbitraryPrecisionInteger()
@@ -101,7 +97,7 @@ extension BoringSSLEllipticCurveGroup {
10197

10298
extension BoringSSLEllipticCurveGroup {
10399
@usableFromInline
104-
enum CurveName {
100+
package enum CurveName {
105101
case p256
106102
case p384
107103
case p521
@@ -121,4 +117,3 @@ extension BoringSSLEllipticCurveGroup.CurveName {
121117
}
122118
}
123119
}
124-
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Sources/Crypto/Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift renamed to Sources/CryptoBoringWrapper/EC/EllipticCurvePoint.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,35 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15-
@_exported import CryptoKit
16-
#else
1714
@_implementationOnly import CCryptoBoringSSL
18-
import CryptoBoringWrapper
1915

2016
/// A wrapper around BoringSSL's EC_POINT with some lifetime management.
2117
@usableFromInline
22-
class EllipticCurvePoint {
18+
package class EllipticCurvePoint {
2319
/* private but @usableFromInline */ @usableFromInline var _basePoint: OpaquePointer
2420

2521
@usableFromInline
26-
init(multiplying scalar: ArbitraryPrecisionInteger, on group: BoringSSLEllipticCurveGroup) throws {
22+
package init(multiplying scalar: ArbitraryPrecisionInteger, on group: BoringSSLEllipticCurveGroup) throws {
2723
self._basePoint = try group.withUnsafeGroupPointer { groupPtr in
2824
guard let basePoint = CCryptoBoringSSL_EC_POINT_new(groupPtr) else {
29-
throw CryptoKitError.internalBoringSSLError()
25+
throw CryptoBoringWrapperError.internalBoringSSLError()
3026
}
3127
return basePoint
3228
}
3329

3430
try group.withUnsafeGroupPointer { groupPtr in
3531
try scalar.withUnsafeBignumPointer { bigNumPtr in
3632
guard CCryptoBoringSSL_EC_POINT_mul(groupPtr, self._basePoint, bigNumPtr, nil, nil, nil) != 0 else {
37-
throw CryptoKitError.internalBoringSSLError()
33+
throw CryptoBoringWrapperError.internalBoringSSLError()
3834
}
3935
}
4036
}
4137
}
4238

43-
init(copying pointer: OpaquePointer, on group: BoringSSLEllipticCurveGroup) throws {
39+
package init(copying pointer: OpaquePointer, on group: BoringSSLEllipticCurveGroup) throws {
4440
self._basePoint = try group.withUnsafeGroupPointer { groupPtr in
4541
guard let basePoint = CCryptoBoringSSL_EC_POINT_dup(pointer, groupPtr) else {
46-
throw CryptoKitError.internalBoringSSLError()
42+
throw CryptoBoringWrapperError.internalBoringSSLError()
4743
}
4844
return basePoint
4945
}
@@ -58,20 +54,20 @@ class EllipticCurvePoint {
5854

5955
extension EllipticCurvePoint {
6056
@inlinable
61-
func withPointPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
57+
package func withPointPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
6258
try body(self._basePoint)
6359
}
6460

6561
@usableFromInline
66-
func affineCoordinates(group: BoringSSLEllipticCurveGroup) throws -> (x: ArbitraryPrecisionInteger, y: ArbitraryPrecisionInteger) {
62+
package func affineCoordinates(group: BoringSSLEllipticCurveGroup) throws -> (x: ArbitraryPrecisionInteger, y: ArbitraryPrecisionInteger) {
6763
var x = ArbitraryPrecisionInteger()
6864
var y = ArbitraryPrecisionInteger()
6965

7066
try x.withUnsafeMutableBignumPointer { xPtr in
7167
try y.withUnsafeMutableBignumPointer { yPtr in
7268
try group.withUnsafeGroupPointer { groupPtr in
7369
guard CCryptoBoringSSL_EC_POINT_get_affine_coordinates_GFp(groupPtr, self._basePoint, xPtr, yPtr, nil) != 0 else {
74-
throw CryptoKitError.internalBoringSSLError()
70+
throw CryptoBoringWrapperError.internalBoringSSLError()
7571
}
7672
}
7773
}
@@ -80,4 +76,3 @@ extension EllipticCurvePoint {
8076
return (x: x, y: y)
8177
}
8278
}
83-
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Sources/_CryptoExtras/Util/RandomBytes.swift renamed to Sources/CryptoBoringWrapper/Util/RandomBytes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
extension UnsafeMutableRawBufferPointer {
1616
@inlinable
17-
func initializeWithRandomBytes(count: Int) {
17+
package func initializeWithRandomBytes(count: Int) {
1818
guard count > 0 else {
1919
return
2020
}
@@ -46,7 +46,7 @@ extension UnsafeMutableRawBufferPointer {
4646

4747
extension SystemRandomNumberGenerator {
4848
@inlinable
49-
static func randomBytes(count: Int) -> [UInt8] {
49+
package static func randomBytes(count: Int) -> [UInt8] {
5050
Array(unsafeUninitializedCapacity: count) { buffer, initializedCount in
5151
UnsafeMutableRawBufferPointer(start: buffer.baseAddress, count: buffer.count).initializeWithRandomBytes(count: count)
5252
initializedCount = count

Sources/_CryptoExtras/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ add_library(_CryptoExtras
2424
"Util/DigestType.swift"
2525
"Util/Error.swift"
2626
"Util/PEMDocument.swift"
27-
"Util/RandomBytes.swift"
2827
"Util/SubjectPublicKeyInfo.swift")
2928

3029
target_include_directories(_CryptoExtras PRIVATE

Tests/CryptoBoringWrapperTests/ArbitraryPrecisionIntegerTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15-
#else
1614
@testable import CryptoBoringWrapper
1715
import XCTest
1816

@@ -168,4 +166,3 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase {
168166
}
169167
}
170168
}
171-
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

Tests/CryptoBoringWrapperTests/FiniteFieldArithmeticTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14-
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
15-
#else
1614
@testable import CryptoBoringWrapper
1715
import XCTest
1816

@@ -124,4 +122,3 @@ final class FiniteFieldArithmeticTests: XCTestCase {
124122
}
125123
}
126124
}
127-
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

0 commit comments

Comments
 (0)