Compare commits

..
1 Commits
39 changed files with 201 additions and 198 deletions

No files matched your search

+1 -2
View File
@@ -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
)
@@ -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 {
@@ -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 {
+2
View File
@@ -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"
@@ -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 {}
@@ -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 {
@@ -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 {
@@ -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.
@@ -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
@@ -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.
@@ -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
@@ -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 {
@@ -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.
@@ -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 {
@@ -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 {
@@ -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.
@@ -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<BIGNUM>) throws {
init(copying original: UnsafePointer<BIGNUM>) 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<BIGNUM>) 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: ContiguousBytes>(bytes: Bytes) throws {
@usableFromInline
init<Bytes: ContiguousBytes>(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: ContiguousBytes>(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<T>(_ body: (UnsafePointer<BIGNUM>) throws -> T) rethrows -> T {
func withUnsafeBignumPointer<T>(_ body: (UnsafePointer<BIGNUM>) throws -> T) rethrows -> T {
try self._backing.withUnsafeBignumPointer(body)
}
@inlinable
public mutating func withUnsafeMutableBignumPointer<T>(_ body: (UnsafeMutablePointer<BIGNUM>) throws -> T) rethrows -> T {
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)
@@ -161,12 +156,10 @@ 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)
}
@@ -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<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
private 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()!
@@ -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?<T: BinaryInteger>(exactly integer: T) {
init?<T: BinaryInteger>(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
@@ -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<T>(_ body: (UnsafePointer<BN_MONT_CTX>) throws -> T) rethrows -> T {
fileprivate func withUnsafeBN_MONT_CTX<T>(_ body: (UnsafePointer<BN_MONT_CTX>) 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
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
#if !canImport(Darwin)
import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSL
typealias errno_t = CInt
@@ -12,8 +12,8 @@
//
//===----------------------------------------------------------------------===//
import CCryptoBoringSSL
import CCryptoBoringSSLShims
@_implementationOnly import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSLShims
import Foundation
/// An abstraction over a BoringSSL AEAD
+1 -3
View File
@@ -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
$<TARGET_PROPERTY:CCryptoBoringSSL,INCLUDE_DIRECTORIES>
@@ -12,11 +12,10 @@
//
//===----------------------------------------------------------------------===//
import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSL
public enum CryptoBoringWrapperError: Error {
case underlyingCoreCryptoError(error: Int32)
case incorrectParameterSize
}
extension CryptoBoringWrapperError {
+3 -3
View File
@@ -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
@@ -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 {
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSL
import Crypto
import Foundation
@@ -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 {
+3 -1
View File
@@ -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
$<TARGET_PROPERTY:CCryptoBoringSSL,INCLUDE_DIRECTORIES>
@@ -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 {
@@ -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
+2 -3
View File
@@ -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
@@ -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
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import CCryptoBoringSSL
@_implementationOnly import CCryptoBoringSSL
import Crypto
extension CryptoKitError {
+1 -1
View File
@@ -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 {
+1
View File
@@ -0,0 +1 @@
../../Crypto/Util/BoringSSL/Shared
@@ -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
@@ -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
+1 -1
View File
@@ -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
@@ -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
+1 -1
View File
@@ -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