mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
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
This commit is contained in:
1 parent
ae321d29be
commit
f98e46e42a
10 files changed
+28
-44
No files matched your search
@@ -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"
|
||||
|
||||
@@ -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>
|
||||
|
||||
+12
-17
@@ -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
|
||||
@@ -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)
|
||||
@@ -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()
|
||||
@@ -101,7 +97,7 @@ extension BoringSSLEllipticCurveGroup {
|
||||
|
||||
extension BoringSSLEllipticCurveGroup {
|
||||
@usableFromInline
|
||||
enum CurveName {
|
||||
package enum CurveName {
|
||||
case p256
|
||||
case p384
|
||||
case p521
|
||||
@@ -121,4 +117,3 @@ extension BoringSSLEllipticCurveGroup.CurveName {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
+9
-14
@@ -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_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
|
||||
}
|
||||
@@ -34,16 +30,16 @@ class EllipticCurvePoint {
|
||||
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
|
||||
}
|
||||
@@ -58,12 +54,12 @@ 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()
|
||||
|
||||
@@ -71,7 +67,7 @@ extension EllipticCurvePoint {
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,4 +76,3 @@ extension EllipticCurvePoint {
|
||||
return (x: x, y: y)
|
||||
}
|
||||
}
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
File renamed without changes.
File renamed without changes.
+2
-2
@@ -14,7 +14,7 @@
|
||||
|
||||
extension UnsafeMutableRawBufferPointer {
|
||||
@inlinable
|
||||
func initializeWithRandomBytes(count: Int) {
|
||||
package func initializeWithRandomBytes(count: Int) {
|
||||
guard count > 0 else {
|
||||
return
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -168,4 +166,3 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@@ -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
|
||||
|
||||
@@ -124,4 +122,3 @@ final class FiniteFieldArithmeticTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
Reference in new issue
Block a user