Move ArbitraryPrecisionInteger type to CryptoBoringWrapper module (#236)

* Move ArbitraryPrecisionInteger_boring.swift to CryptoBoringWrapper

* Remove conditional compilation guards for ArbitraryPrecisionInteger

* Throw CryptoBoringWrapperError.internalBoringSSLError instead of CryptoKitError.internalBoringSSLError

* Make most operations public and add raw pointer APIs

* Move ArbitraryPrecisionIntegerTests to new CryptoBoringWrapperTests target

* rename some files

* Remove @_implementationOnly from imports and raw pointer APIs

* Also move over FiniteFieldArithmeticContext to CryptoBoringWrapper

* Add FiniteFieldArithmeticContext.inverse

* Remove conditional compilation guards for FFAC

* Update CMakeLists.txt for moved files

* Revert "Add FiniteFieldArithmeticContext.inverse"

This reverts commit e02700e07c.

* Fix CMakeLists.txt

* Make most public symbols @inlinable

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
Si BeaumontandCory Benfield authored and GitHub committed 2024-06-21 16:35:50 +01:00
1 parent 5fb3819b51
commit a3866d45d9
35 files changed
+150 -147

No files matched your search

+1
View File
@@ -168,6 +168,7 @@ let package = Package(
swiftSettings: swiftSettings
),
.testTarget(name: "_CryptoExtrasTests", dependencies: ["_CryptoExtras"]),
.testTarget(name: "CryptoBoringWrapperTests", dependencies: ["CryptoBoringWrapper"]),
],
cxxLanguageStandard: .cxx11
)
@@ -14,8 +14,8 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CryptoBoringWrapper
import CCryptoBoringSSL
import CryptoBoringWrapper
import Foundation
enum OpenSSLAESGCMImpl {
@@ -14,9 +14,9 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
@_implementationOnly import CryptoBoringWrapper
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import CryptoBoringWrapper
import Foundation
extension BoringSSLAEAD {
-2
View File
@@ -86,9 +86,7 @@ add_library(Crypto
"Signatures/ECDSA.swift"
"Signatures/Ed25519.swift"
"Signatures/Signature.swift"
"Util/BoringSSL/ArbitraryPrecisionInteger_boring.swift"
"Util/BoringSSL/CryptoKitErrors_boring.swift"
"Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift"
"Util/BoringSSL/RNG_boring.swift"
"Util/BoringSSL/SafeCompare_boring.swift"
"Util/BoringSSL/Zeroization_boring.swift"
@@ -14,7 +14,7 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
protocol HashFunctionImplementationDetails: HashFunction where Digest: DigestPrivate {}
@@ -14,7 +14,7 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
extension P256.KeyAgreement.PrivateKey {
internal func openSSLSharedSecretFromKeyAgreement(with publicKeyShare: P256.KeyAgreement.PublicKey) throws -> SharedSecret {
@@ -14,7 +14,7 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import Foundation
enum BoringSSLAESWRAPImpl {
@@ -14,8 +14,8 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Foundation
// For signing and verifying, we use BoringSSL's Ed25519, not the X25519 stuff.
@@ -14,7 +14,8 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import CryptoBoringWrapper
/// A wrapper around BoringSSL's EC_POINT with some lifetime management.
@usableFromInline
@@ -14,7 +14,8 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import CryptoBoringWrapper
/// A wrapper around BoringSSL's EC_GROUP object that handles reference counting and
/// liveness.
@@ -14,8 +14,9 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import CryptoBoringWrapper
import Foundation
@usableFromInline
@@ -14,8 +14,8 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Foundation
extension Curve25519.KeyAgreement {
@@ -14,8 +14,9 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import CryptoBoringWrapper
import Foundation
/// A wrapper around BoringSSL's ECDSA_SIG with some lifetime management.
@@ -14,7 +14,7 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import Foundation
extension Data {
@@ -14,8 +14,8 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Foundation
extension Curve25519.Signing.PublicKey {
@@ -14,7 +14,7 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
extension CryptoKitError {
/// A helper function that packs the value of `ERR_get_error` into the internal error field.
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
#if !canImport(Darwin)
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
typealias errno_t = CInt
@@ -12,8 +12,8 @@
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Foundation
/// An abstraction over a BoringSSL AEAD
@@ -11,35 +11,32 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Foundation
/// A wrapper around the OpenSSL BIGNUM object that is appropriately lifetime managed,
/// and that provides better Swift types for this object.
@usableFromInline
struct ArbitraryPrecisionInteger {
private var _backing: BackingStorage
public struct ArbitraryPrecisionInteger {
/* private but @usableFromInline */ @usableFromInline var _backing: BackingStorage
@usableFromInline
init() {
@inlinable
public init() {
self._backing = BackingStorage()
}
init(copying original: UnsafePointer<BIGNUM>) throws {
@inlinable
public init(copying original: UnsafePointer<BIGNUM>) throws {
self._backing = try BackingStorage(copying: original)
}
@usableFromInline
init(_ original: ArbitraryPrecisionInteger) throws {
@inlinable
public init(_ original: ArbitraryPrecisionInteger) throws {
self._backing = try BackingStorage(copying: original._backing)
}
@usableFromInline
init(integerLiteral value: Int64) {
@inlinable
public init(integerLiteral value: Int64) {
self._backing = BackingStorage(value)
}
}
@@ -47,31 +44,36 @@ struct ArbitraryPrecisionInteger {
// MARK: - BackingStorage
extension ArbitraryPrecisionInteger {
@usableFromInline
final class BackingStorage {
private var _backing: BIGNUM
/* private but @usableFromInline */ @usableFromInline var _backing: BIGNUM
@usableFromInline
init() {
self._backing = BIGNUM()
CCryptoBoringSSL_BN_init(&self._backing)
}
@usableFromInline
init(copying original: UnsafePointer<BIGNUM>) throws {
self._backing = BIGNUM()
guard CCryptoBoringSSL_BN_copy(&self._backing, original) != nil else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.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 CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
}
}
@usableFromInline
init(_ value: Int64) {
self._backing = BIGNUM()
let rc = CCryptoBoringSSL_BN_set_u64(&self._backing, value.magnitude)
@@ -82,6 +84,7 @@ extension ArbitraryPrecisionInteger {
}
}
@inlinable
deinit {
CCryptoBoringSSL_BN_clear_free(&self._backing)
}
@@ -91,13 +94,14 @@ extension ArbitraryPrecisionInteger {
// MARK: - Extra initializers
extension ArbitraryPrecisionInteger {
@usableFromInline
init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
@inlinable
public init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
self._backing = try BackingStorage(bytes: bytes)
}
}
extension ArbitraryPrecisionInteger.BackingStorage {
@inlinable
convenience init<Bytes: ContiguousBytes>(bytes: Bytes) throws {
self.init()
@@ -105,7 +109,7 @@ extension ArbitraryPrecisionInteger.BackingStorage {
CCryptoBoringSSLShims_BN_bin2bn(bytesPointer.baseAddress, bytesPointer.count, &self._backing)
}
guard rc != nil else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
}
}
@@ -113,11 +117,13 @@ extension ArbitraryPrecisionInteger.BackingStorage {
// MARK: - Pointer helpers
extension ArbitraryPrecisionInteger {
func withUnsafeBignumPointer<T>(_ body: (UnsafePointer<BIGNUM>) throws -> T) rethrows -> T {
@inlinable
public func withUnsafeBignumPointer<T>(_ body: (UnsafePointer<BIGNUM>) throws -> T) rethrows -> T {
try self._backing.withUnsafeBignumPointer(body)
}
mutating func withUnsafeMutableBignumPointer<T>(_ body: (UnsafeMutablePointer<BIGNUM>) throws -> T) rethrows -> T {
@inlinable
public mutating func withUnsafeMutableBignumPointer<T>(_ body: (UnsafeMutablePointer<BIGNUM>) throws -> T) rethrows -> T {
if !isKnownUniquelyReferenced(&self._backing) {
// Failing to CoW is a fatal error here.
self._backing = try! BackingStorage(copying: self._backing)
@@ -128,10 +134,12 @@ extension ArbitraryPrecisionInteger {
}
extension ArbitraryPrecisionInteger.BackingStorage {
@usableFromInline
func withUnsafeBignumPointer<T>(_ body: (UnsafePointer<BIGNUM>) throws -> T) rethrows -> T {
try body(&self._backing)
}
@usableFromInline
func withUnsafeMutableBignumPointer<T>(_ body: (UnsafeMutablePointer<BIGNUM>) throws -> T) rethrows -> T {
try body(&self._backing)
}
@@ -155,8 +163,8 @@ extension ArbitraryPrecisionInteger {
}
}
@usableFromInline
func squared() -> ArbitraryPrecisionInteger {
@inlinable
public func squared() -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
@@ -169,8 +177,8 @@ extension ArbitraryPrecisionInteger {
return result
}
@usableFromInline
func positiveSquareRoot() throws -> ArbitraryPrecisionInteger {
@inlinable
public func positiveSquareRoot() throws -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
self.withUnsafeBignumPointer { selfPtr in
@@ -181,20 +189,20 @@ extension ArbitraryPrecisionInteger {
}
guard rc == 1 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return result
}
@usableFromInline
var byteCount: Int {
@inlinable
public 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 static func withUnsafeBN_CTX<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
/* private but @usableFromInline */ @usableFromInline static func withUnsafeBN_CTX<T>(_ 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()!
@@ -210,7 +218,7 @@ extension ArbitraryPrecisionInteger {
extension ArbitraryPrecisionInteger: Equatable {
@inlinable
static func == (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
public static func == (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) == 0
}
}
@@ -219,22 +227,22 @@ extension ArbitraryPrecisionInteger: Equatable {
extension ArbitraryPrecisionInteger: Comparable {
@inlinable
static func < (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
public static func < (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) < 0
}
@inlinable
static func <= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
public static func <= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) <= 0
}
@inlinable
static func > (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
public static func > (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) > 0
}
@inlinable
static func >= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
public static func >= (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> Bool {
self._compare(lhs: lhs, rhs: rhs) >= 0
}
}
@@ -247,12 +255,12 @@ extension ArbitraryPrecisionInteger: ExpressibleByIntegerLiteral {}
extension ArbitraryPrecisionInteger: AdditiveArithmetic {
@inlinable
static var zero: ArbitraryPrecisionInteger {
public static var zero: ArbitraryPrecisionInteger {
0
}
@usableFromInline
static func + (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger {
@inlinable
public static func + (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
@@ -267,8 +275,8 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic {
return result
}
@usableFromInline
static func += (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
@inlinable
public static func += (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
CCryptoBoringSSL_BN_add(lhsPtr, lhsPtr, rhsPtr)
@@ -277,8 +285,8 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic {
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
}
@usableFromInline
static func - (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger {
@inlinable
public static func - (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
@@ -293,8 +301,8 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic {
return result
}
@usableFromInline
static func -= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
@inlinable
public static func -= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
CCryptoBoringSSL_BN_sub(lhsPtr, lhsPtr, rhsPtr)
@@ -307,11 +315,10 @@ extension ArbitraryPrecisionInteger: AdditiveArithmetic {
// MARK: - Numeric
extension ArbitraryPrecisionInteger: Numeric {
@usableFromInline
typealias Magnitude = Self
public typealias Magnitude = Self
@usableFromInline
var magnitude: Magnitude {
@inlinable
public var magnitude: Magnitude {
if self._positive {
return self
}
@@ -325,8 +332,8 @@ extension ArbitraryPrecisionInteger: Numeric {
return copy
}
@usableFromInline
static func * (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger {
@inlinable
public static func * (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger {
var result = ArbitraryPrecisionInteger()
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
@@ -343,8 +350,8 @@ extension ArbitraryPrecisionInteger: Numeric {
return result
}
@usableFromInline
static func *= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
@inlinable
public static func *= (lhs: inout ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) {
let rc = lhs.withUnsafeMutableBignumPointer { lhsPtr in
rhs.withUnsafeBignumPointer { rhsPtr in
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
@@ -356,7 +363,7 @@ extension ArbitraryPrecisionInteger: Numeric {
}
@inlinable
init?<T: BinaryInteger>(exactly integer: T) {
public init?<T: BinaryInteger>(exactly integer: T) {
fatalError("Not currently implemented")
}
}
@@ -364,8 +371,8 @@ extension ArbitraryPrecisionInteger: Numeric {
// MARK: - SignedNumeric
extension ArbitraryPrecisionInteger: SignedNumeric {
@usableFromInline
mutating func negate() {
@inlinable
public mutating func negate() {
let signBit: CInt = self._positive ? 1 : 0
self.withUnsafeMutableBignumPointer {
@@ -378,12 +385,12 @@ extension ArbitraryPrecisionInteger: SignedNumeric {
extension Data {
/// Serializes an ArbitraryPrecisionInteger padded out to a certain minimum size.
@usableFromInline
mutating func append(bytesOf integer: ArbitraryPrecisionInteger, paddedToSize paddingSize: Int) throws {
@inlinable
public mutating func append(bytesOf integer: ArbitraryPrecisionInteger, paddedToSize paddingSize: Int) throws {
let byteCount = integer.byteCount
guard paddingSize >= byteCount else {
throw CryptoKitError.incorrectParameterSize
throw CryptoBoringWrapperError.incorrectParameterSize
}
// To extend the data we need to write some zeroes into it.
@@ -406,8 +413,7 @@ extension Data {
// MARK: - Printing
extension ArbitraryPrecisionInteger: CustomDebugStringConvertible {
@usableFromInline
var debugDescription: String {
public var debugDescription: String {
guard let bio = CCryptoBoringSSL_BIO_new(CCryptoBoringSSL_BIO_s_mem()) else {
return "ArbitraryPrecisionInteger: (error generating representation)"
}
@@ -442,4 +448,3 @@ 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
+3 -1
View File
@@ -14,7 +14,9 @@
add_library(CryptoBoringWrapper STATIC
"AEAD/BoringSSLAEAD.swift"
"CryptoKitErrors_boring.swift")
"ArbitraryPrecisionInteger.swift"
"CryptoKitErrors_boring.swift"
"FiniteFieldArithmeticContext.swift")
target_include_directories(CryptoBoringWrapper PUBLIC
$<TARGET_PROPERTY:CCryptoBoringSSL,INCLUDE_DIRECTORIES>
@@ -12,10 +12,11 @@
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
public enum CryptoBoringWrapperError: Error {
case underlyingCoreCryptoError(error: Int32)
case incorrectParameterSize
}
extension CryptoBoringWrapperError {
@@ -11,11 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import Foundation
import CCryptoBoringSSL
/// A context for performing mathematical operations on ArbitraryPrecisionIntegers over a finite field.
///
@@ -28,21 +24,21 @@ import Foundation
///
/// Annoyingly, because of the way we have implemented ArbitraryPrecisionInteger, we can't actually use these temporary bignums
/// ourselves.
@usableFromInline
class FiniteFieldArithmeticContext {
private var fieldSize: ArbitraryPrecisionInteger
private var bnCtx: OpaquePointer
public final class FiniteFieldArithmeticContext {
/* private but @usableFromInline */ @usableFromInline var fieldSize: ArbitraryPrecisionInteger
/* private but @usableFromInline */ @usableFromInline var bnCtx: OpaquePointer
@usableFromInline
init(fieldSize: ArbitraryPrecisionInteger) throws {
@inlinable
public init(fieldSize: ArbitraryPrecisionInteger) throws {
self.fieldSize = fieldSize
guard let bnCtx = CCryptoBoringSSL_BN_CTX_new() else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
CCryptoBoringSSL_BN_CTX_start(bnCtx)
self.bnCtx = bnCtx
}
@inlinable
deinit {
CCryptoBoringSSL_BN_CTX_end(self.bnCtx)
CCryptoBoringSSL_BN_CTX_free(self.bnCtx)
@@ -52,8 +48,8 @@ class FiniteFieldArithmeticContext {
// MARK: - Arithmetic operations
extension FiniteFieldArithmeticContext {
@usableFromInline
func square(_ input: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
@inlinable
public func square(_ input: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
var output = ArbitraryPrecisionInteger()
let rc = input.withUnsafeBignumPointer { inputPointer in
@@ -65,14 +61,14 @@ extension FiniteFieldArithmeticContext {
}
guard rc == 1 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return output
}
@usableFromInline
func multiply(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
@inlinable
public func multiply(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
var output = ArbitraryPrecisionInteger()
let rc = x.withUnsafeBignumPointer { xPointer in
@@ -86,14 +82,14 @@ extension FiniteFieldArithmeticContext {
}
guard rc == 1 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return output
}
@usableFromInline
func add(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
@inlinable
public func add(_ x: ArbitraryPrecisionInteger, _ y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
var output = ArbitraryPrecisionInteger()
let rc = x.withUnsafeBignumPointer { xPointer in
@@ -107,14 +103,14 @@ extension FiniteFieldArithmeticContext {
}
guard rc == 1 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return output
}
@usableFromInline
func subtract(_ x: ArbitraryPrecisionInteger, from y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
@inlinable
public func subtract(_ x: ArbitraryPrecisionInteger, from y: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
var output = ArbitraryPrecisionInteger()
let rc = x.withUnsafeBignumPointer { xPointer in
@@ -129,14 +125,14 @@ extension FiniteFieldArithmeticContext {
}
guard rc == 1 else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
return output
}
@usableFromInline
func positiveSquareRoot(_ x: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
@inlinable
public 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.
@@ -145,7 +141,7 @@ extension FiniteFieldArithmeticContext {
}
guard let actualOutputPointer = outputPointer else {
throw CryptoKitError.internalBoringSSLError()
throw CryptoBoringWrapperError.internalBoringSSLError()
}
// Ok, we own this pointer now.
@@ -156,4 +152,3 @@ extension FiniteFieldArithmeticContext {
return try ArbitraryPrecisionInteger(copying: actualOutputPointer)
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
+3 -3
View File
@@ -13,9 +13,9 @@
//===----------------------------------------------------------------------===//
import Crypto
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
@_implementationOnly import CryptoBoringWrapper
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import CryptoBoringWrapper
#if canImport(Darwin) || swift(>=5.9.1)
import Foundation
#else
@@ -13,9 +13,9 @@
//===----------------------------------------------------------------------===//
import Crypto
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
@_implementationOnly import CryptoBoringWrapper
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import CryptoBoringWrapper
import Foundation
extension AES {
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import Crypto
import Foundation
@@ -14,10 +14,10 @@
// This is a copy ChaChaPoly_boring just with a different set aes algos
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Crypto
@_implementationOnly import CryptoBoringWrapper
import CryptoBoringWrapper
import Foundation
extension BoringSSLAEAD {
@@ -12,10 +12,10 @@
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Crypto
@_implementationOnly import CryptoBoringWrapper
import CryptoBoringWrapper
import Foundation
enum OpenSSLChaCha20CTRImpl {
@@ -12,10 +12,10 @@
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
import Crypto
@_implementationOnly import CryptoBoringWrapper
import CryptoBoringWrapper
#if canImport(Darwin) || swift(>=5.9.1)
import Foundation
#else
+2 -2
View File
@@ -17,8 +17,8 @@ import Crypto
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
// Nothing; this is implemented in RSA_security
#else
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import CCryptoBoringSSL
import CCryptoBoringSSLShims
internal struct BoringSSLRSAPublicKey: Sendable {
private var backing: Backing
@@ -15,7 +15,7 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
// This is only used when bulding with BoringSSL.
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import Foundation
import Crypto
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import Crypto
extension CryptoKitError {
+1 -1
View File
@@ -14,7 +14,7 @@
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
// Nothing to do in this case
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
import Crypto
struct DigestType {
@@ -11,9 +11,7 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
#else
@testable import Crypto
@testable import CryptoBoringWrapper
import XCTest
final class ArbitraryPrecisionIntegerTests: XCTestCase {
@@ -43,7 +41,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? CryptoKitError else {
guard case .some(.underlyingCoreCryptoError) = error as? CryptoBoringWrapperError else {
XCTFail("Unexpected error: \(error)")
return
}
@@ -112,4 +110,3 @@ final class ArbitraryPrecisionIntegerTests: XCTestCase {
XCTAssertTrue(two >= two)
}
}
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
+1 -1
View File
@@ -20,7 +20,7 @@ import XCTest
#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@testable import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
@testable import Crypto
#endif
@@ -20,7 +20,7 @@ import XCTest
#if !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@testable import CryptoKit
#else
@_implementationOnly import CCryptoBoringSSL
import CCryptoBoringSSL
@testable import Crypto
#endif