Skip to content

Commit

Permalink
wrapper: Move EC types and random bytes into CryptoBoringWrapper (#277)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
simonjbeaumont authored Oct 17, 2024
1 parent ae321d2 commit f98e46e
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 44 deletions.
2 changes: 0 additions & 2 deletions Sources/Crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ add_library(Crypto
"Key Wrapping/AESWrap.swift"
"Key Wrapping/BoringSSL/AESWrap_boring.swift"
"Keys/EC/BoringSSL/Ed25519_boring.swift"
"Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift"
"Keys/EC/BoringSSL/EllipticCurve_boring.swift"
"Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift"
"Keys/EC/BoringSSL/X25519Keys_boring.swift"
"Keys/EC/Curve25519.swift"
Expand Down
7 changes: 5 additions & 2 deletions Sources/CryptoBoringWrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
add_library(CryptoBoringWrapper STATIC
"AEAD/BoringSSLAEAD.swift"
"CryptoKitErrors_boring.swift"
"Util/ArbitraryPrecisionInteger_boring.swift"
"Util/FiniteFieldArithmeticContext_boring.swift")
"EC/EllipticCurve.swift"
"EC/EllipticCurvePoint.swift"
"Util/ArbitraryPrecisionInteger.swift"
"Util/FiniteFieldArithmeticContext.swift"
"Util/RandomBytes.swift")

target_include_directories(CryptoBoringWrapper PUBLIC
$<TARGET_PROPERTY:CCryptoBoringSSL,INCLUDE_DIRECTORIES>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CryptoBoringWrapper

/// A wrapper around BoringSSL's EC_GROUP object that handles reference counting and
/// liveness.
@usableFromInline
class BoringSSLEllipticCurveGroup {
package class BoringSSLEllipticCurveGroup {
/* private but usableFromInline */ @usableFromInline var _group: OpaquePointer

@usableFromInline
init(_ curve: CurveName) throws {
package init(_ curve: CurveName) throws {
guard let group = CCryptoBoringSSL_EC_GROUP_new_by_curve_name(curve.baseNID) else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
self._group = group
Expand All @@ -41,36 +37,36 @@ class BoringSSLEllipticCurveGroup {

extension BoringSSLEllipticCurveGroup {
@usableFromInline
var coordinateByteCount: Int {
package var coordinateByteCount: Int {
(Int(CCryptoBoringSSL_EC_GROUP_get_degree(self._group)) + 7) / 8
}

@usableFromInline
func makeUnsafeOwnedECKey() throws -> OpaquePointer {
package func makeUnsafeOwnedECKey() throws -> OpaquePointer {
guard let key = CCryptoBoringSSL_EC_KEY_new(),
CCryptoBoringSSL_EC_KEY_set_group(key, self._group) == 1 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}

return key
}

@usableFromInline
func makeUnsafeOwnedECPoint() throws -> OpaquePointer {
package func makeUnsafeOwnedECPoint() throws -> OpaquePointer {
guard let point = CCryptoBoringSSL_EC_POINT_new(self._group) else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}

return point
}

@inlinable
func withUnsafeGroupPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
package func withUnsafeGroupPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
try body(self._group)
}

@usableFromInline
var order: ArbitraryPrecisionInteger {
package var order: ArbitraryPrecisionInteger {
// Groups must have an order.
let baseOrder = CCryptoBoringSSL_EC_GROUP_get0_order(self._group)!
return try! ArbitraryPrecisionInteger(copying: baseOrder)
Expand All @@ -79,7 +75,7 @@ extension BoringSSLEllipticCurveGroup {
/// An elliptic curve can be represented in a Weierstrass form: `y² = x³ + ax + b`. This
/// property provides the values of a and b on the curve.
@usableFromInline
var weierstrassCoefficients: (field: ArbitraryPrecisionInteger, a: ArbitraryPrecisionInteger, b: ArbitraryPrecisionInteger) {
package var weierstrassCoefficients: (field: ArbitraryPrecisionInteger, a: ArbitraryPrecisionInteger, b: ArbitraryPrecisionInteger) {
var field = ArbitraryPrecisionInteger()
var a = ArbitraryPrecisionInteger()
var b = ArbitraryPrecisionInteger()
Expand All @@ -101,7 +97,7 @@ extension BoringSSLEllipticCurveGroup {

extension BoringSSLEllipticCurveGroup {
@usableFromInline
enum CurveName {
package enum CurveName {
case p256
case p384
case p521
Expand All @@ -121,4 +117,3 @@ extension BoringSSLEllipticCurveGroup.CurveName {
}
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,35 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CryptoBoringWrapper

/// A wrapper around BoringSSL's EC_POINT with some lifetime management.
@usableFromInline
class EllipticCurvePoint {
package class EllipticCurvePoint {
/* private but @usableFromInline */ @usableFromInline var _basePoint: OpaquePointer

@usableFromInline
init(multiplying scalar: ArbitraryPrecisionInteger, on group: BoringSSLEllipticCurveGroup) throws {
package init(multiplying scalar: ArbitraryPrecisionInteger, on group: BoringSSLEllipticCurveGroup) throws {
self._basePoint = try group.withUnsafeGroupPointer { groupPtr in
guard let basePoint = CCryptoBoringSSL_EC_POINT_new(groupPtr) else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return basePoint
}

try group.withUnsafeGroupPointer { groupPtr in
try scalar.withUnsafeBignumPointer { bigNumPtr in
guard CCryptoBoringSSL_EC_POINT_mul(groupPtr, self._basePoint, bigNumPtr, nil, nil, nil) != 0 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
}
}
}

init(copying pointer: OpaquePointer, on group: BoringSSLEllipticCurveGroup) throws {
package init(copying pointer: OpaquePointer, on group: BoringSSLEllipticCurveGroup) throws {
self._basePoint = try group.withUnsafeGroupPointer { groupPtr in
guard let basePoint = CCryptoBoringSSL_EC_POINT_dup(pointer, groupPtr) else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return basePoint
}
Expand All @@ -58,20 +54,20 @@ class EllipticCurvePoint {

extension EllipticCurvePoint {
@inlinable
func withPointPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
package func withPointPointer<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
try body(self._basePoint)
}

@usableFromInline
func affineCoordinates(group: BoringSSLEllipticCurveGroup) throws -> (x: ArbitraryPrecisionInteger, y: ArbitraryPrecisionInteger) {
package func affineCoordinates(group: BoringSSLEllipticCurveGroup) throws -> (x: ArbitraryPrecisionInteger, y: ArbitraryPrecisionInteger) {
var x = ArbitraryPrecisionInteger()
var y = ArbitraryPrecisionInteger()

try x.withUnsafeMutableBignumPointer { xPtr in
try y.withUnsafeMutableBignumPointer { yPtr in
try group.withUnsafeGroupPointer { groupPtr in
guard CCryptoBoringSSL_EC_POINT_get_affine_coordinates_GFp(groupPtr, self._basePoint, xPtr, yPtr, nil) != 0 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
}
}
Expand All @@ -80,4 +76,3 @@ extension EllipticCurvePoint {
return (x: x, y: y)
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

extension UnsafeMutableRawBufferPointer {
@inlinable
func initializeWithRandomBytes(count: Int) {
package func initializeWithRandomBytes(count: Int) {
guard count > 0 else {
return
}
Expand Down Expand Up @@ -46,7 +46,7 @@ extension UnsafeMutableRawBufferPointer {

extension SystemRandomNumberGenerator {
@inlinable
static func randomBytes(count: Int) -> [UInt8] {
package static func randomBytes(count: Int) -> [UInt8] {
Array(unsafeUninitializedCapacity: count) { buffer, initializedCount in
UnsafeMutableRawBufferPointer(start: buffer.baseAddress, count: buffer.count).initializeWithRandomBytes(count: count)
initializedCount = count
Expand Down
1 change: 0 additions & 1 deletion Sources/_CryptoExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ add_library(_CryptoExtras
"Util/DigestType.swift"
"Util/Error.swift"
"Util/PEMDocument.swift"
"Util/RandomBytes.swift"
"Util/SubjectPublicKeyInfo.swift")

target_include_directories(_CryptoExtras PRIVATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#else
@testable import CryptoBoringWrapper
import XCTest

Expand Down Expand Up @@ -168,4 +166,3 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase {
}
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#else
@testable import CryptoBoringWrapper
import XCTest

Expand Down Expand Up @@ -124,4 +122,3 @@ final class FiniteFieldArithmeticTests: XCTestCase {
}
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API

0 comments on commit f98e46e

Please sign in to comment.