mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a53a7e8f85 | ||
|
|
8dafe0fdce | ||
|
|
e2dd5a547b | ||
|
|
46072478ca |
No files matched your search
@@ -1,15 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
+1
-2
@@ -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
|
||||
)
|
||||
@@ -1 +0,0 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,49 +14,133 @@
|
||||
#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 {}
|
||||
|
||||
protocol BoringSSLBackedHashFunction: HashFunctionImplementationDetails {
|
||||
static var digestType: DigestContext.DigestType { get }
|
||||
associatedtype Context
|
||||
static var digestSize: Int { get }
|
||||
static func initialize() -> Context?
|
||||
static func update(_ context: inout Context, data: UnsafeRawBufferPointer) -> Bool
|
||||
static func finalize(_ context: inout Context, digest: UnsafeMutableRawBufferPointer) -> Bool
|
||||
}
|
||||
|
||||
extension Insecure.MD5: BoringSSLBackedHashFunction {
|
||||
static var digestType: DigestContext.DigestType {
|
||||
.md5
|
||||
static var digestSize: Int {
|
||||
Int(MD5_DIGEST_LENGTH)
|
||||
}
|
||||
|
||||
static func initialize() -> MD5_CTX? {
|
||||
var context = MD5_CTX()
|
||||
guard CCryptoBoringSSL_MD5_Init(&context) == 1 else {
|
||||
return nil
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
static func update(_ context: inout MD5_CTX, data: UnsafeRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_MD5_Update(&context, data.baseAddress, data.count) == 1
|
||||
}
|
||||
|
||||
static func finalize(_ context: inout MD5_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_MD5_Final(digest.baseAddress, &context) == 1
|
||||
}
|
||||
}
|
||||
|
||||
extension Insecure.SHA1: BoringSSLBackedHashFunction {
|
||||
static var digestType: DigestContext.DigestType {
|
||||
.sha1
|
||||
static var digestSize: Int {
|
||||
Int(SHA_DIGEST_LENGTH)
|
||||
}
|
||||
|
||||
static func initialize() -> SHA_CTX? {
|
||||
var context = SHA_CTX()
|
||||
guard CCryptoBoringSSL_SHA1_Init(&context) == 1 else {
|
||||
return nil
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
static func update(_ context: inout SHA_CTX, data: UnsafeRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA1_Update(&context, data.baseAddress, data.count) == 1
|
||||
}
|
||||
|
||||
static func finalize(_ context: inout SHA_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA1_Final(digest.baseAddress, &context) == 1
|
||||
}
|
||||
}
|
||||
|
||||
extension SHA256: BoringSSLBackedHashFunction {
|
||||
static var digestType: DigestContext.DigestType {
|
||||
.sha256
|
||||
static var digestSize: Int {
|
||||
Int(SHA256_DIGEST_LENGTH)
|
||||
}
|
||||
|
||||
static func initialize() -> SHA256_CTX? {
|
||||
var context = SHA256_CTX()
|
||||
guard CCryptoBoringSSL_SHA256_Init(&context) == 1 else {
|
||||
return nil
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
static func update(_ context: inout SHA256_CTX, data: UnsafeRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA256_Update(&context, data.baseAddress, data.count) == 1
|
||||
}
|
||||
|
||||
static func finalize(_ context: inout SHA256_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA256_Final(digest.baseAddress, &context) == 1
|
||||
}
|
||||
}
|
||||
|
||||
extension SHA384: BoringSSLBackedHashFunction {
|
||||
static var digestType: DigestContext.DigestType {
|
||||
.sha384
|
||||
static var digestSize: Int {
|
||||
Int(SHA384_DIGEST_LENGTH)
|
||||
}
|
||||
|
||||
static func initialize() -> SHA512_CTX? {
|
||||
var context = SHA512_CTX()
|
||||
guard CCryptoBoringSSL_SHA384_Init(&context) == 1 else {
|
||||
return nil
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
static func update(_ context: inout SHA512_CTX, data: UnsafeRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA384_Update(&context, data.baseAddress, data.count) == 1
|
||||
}
|
||||
|
||||
static func finalize(_ context: inout SHA512_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA384_Final(digest.baseAddress, &context) == 1
|
||||
}
|
||||
}
|
||||
|
||||
extension SHA512: BoringSSLBackedHashFunction {
|
||||
static var digestType: DigestContext.DigestType {
|
||||
.sha512
|
||||
static var digestSize: Int {
|
||||
Int(SHA512_DIGEST_LENGTH)
|
||||
}
|
||||
|
||||
static func initialize() -> SHA512_CTX? {
|
||||
var context = SHA512_CTX()
|
||||
guard CCryptoBoringSSL_SHA512_Init(&context) == 1 else {
|
||||
return nil
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
static func update(_ context: inout SHA512_CTX, data: UnsafeRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA512_Update(&context, data.baseAddress, data.count) == 1
|
||||
}
|
||||
|
||||
static func finalize(_ context: inout SHA512_CTX, digest: UnsafeMutableRawBufferPointer) -> Bool {
|
||||
CCryptoBoringSSL_SHA512_Final(digest.baseAddress, &context) == 1
|
||||
}
|
||||
}
|
||||
|
||||
struct OpenSSLDigestImpl<H: BoringSSLBackedHashFunction> {
|
||||
private var context: DigestContext
|
||||
private var context: DigestContext<H>
|
||||
|
||||
init() {
|
||||
self.context = DigestContext(digest: H.digestType)
|
||||
self.context = DigestContext()
|
||||
}
|
||||
|
||||
internal mutating func update(data: UnsafeRawBufferPointer) {
|
||||
@@ -67,81 +151,49 @@ struct OpenSSLDigestImpl<H: BoringSSLBackedHashFunction> {
|
||||
}
|
||||
|
||||
internal func finalize() -> H.Digest {
|
||||
// To have a non-destructive finalize operation we must allocate.
|
||||
let copyContext = DigestContext(copying: self.context)
|
||||
let digestBytes = copyContext.finalize()
|
||||
return digestBytes.withUnsafeBytes {
|
||||
// We force unwrap here because if the digest size is wrong it's an internal error.
|
||||
H.Digest(bufferPointer: $0)!
|
||||
}
|
||||
self.context.finalize()
|
||||
}
|
||||
}
|
||||
|
||||
class DigestContext {
|
||||
private var contextPointer: UnsafeMutablePointer<EVP_MD_CTX>
|
||||
fileprivate final class DigestContext<H: BoringSSLBackedHashFunction> {
|
||||
private var context: H.Context
|
||||
|
||||
init(digest: DigestType) {
|
||||
// We force unwrap because we cannot recover from allocation failure.
|
||||
self.contextPointer = CCryptoBoringSSL_EVP_MD_CTX_new()!
|
||||
guard CCryptoBoringSSL_EVP_DigestInit(self.contextPointer, digest.dispatchTable) != 0 else {
|
||||
// We can't do much but crash here.
|
||||
fatalError("Unable to initialize digest state: \(CCryptoBoringSSL_ERR_get_error())")
|
||||
init() {
|
||||
guard let contex = H.initialize() else {
|
||||
preconditionFailure("Unable to initialize digest state")
|
||||
}
|
||||
self.context = contex
|
||||
}
|
||||
|
||||
init(copying original: DigestContext) {
|
||||
// We force unwrap because we cannot recover from allocation failure.
|
||||
self.contextPointer = CCryptoBoringSSL_EVP_MD_CTX_new()!
|
||||
guard CCryptoBoringSSL_EVP_MD_CTX_copy(self.contextPointer, original.contextPointer) != 0 else {
|
||||
// We can't do much but crash here.
|
||||
fatalError("Unable to copy digest state: \(CCryptoBoringSSL_ERR_get_error())")
|
||||
}
|
||||
self.context = original.context
|
||||
}
|
||||
|
||||
func update(data: UnsafeRawBufferPointer) {
|
||||
guard let baseAddress = data.baseAddress else {
|
||||
return
|
||||
guard H.update(&self.context, data: data) else {
|
||||
preconditionFailure("Unable to update digest state")
|
||||
}
|
||||
|
||||
CCryptoBoringSSL_EVP_DigestUpdate(self.contextPointer, baseAddress, data.count)
|
||||
}
|
||||
|
||||
// This finalize function is _destructive_: do not call it if you want to reuse the object!
|
||||
func finalize() -> [UInt8] {
|
||||
let digestSize = CCryptoBoringSSL_EVP_MD_size(self.contextPointer.pointee.digest)
|
||||
var digestBytes = Array(repeating: UInt8(0), count: digestSize)
|
||||
var count = UInt32(digestSize)
|
||||
|
||||
digestBytes.withUnsafeMutableBufferPointer { digestPointer in
|
||||
assert(digestPointer.count == count)
|
||||
CCryptoBoringSSL_EVP_DigestFinal(self.contextPointer, digestPointer.baseAddress, &count)
|
||||
func finalize() -> H.Digest {
|
||||
var copyContext = self.context
|
||||
defer {
|
||||
withUnsafeMutablePointer(to: ©Context) { $0.zeroize() }
|
||||
}
|
||||
return withUnsafeTemporaryAllocation(byteCount: H.digestSize, alignment: 1) { digestPointer in
|
||||
defer {
|
||||
digestPointer.zeroize()
|
||||
}
|
||||
guard H.finalize(©Context, digest: digestPointer) else {
|
||||
preconditionFailure("Unable to finalize digest state")
|
||||
}
|
||||
// We force unwrap here because if the digest size is wrong it's an internal error.
|
||||
return H.Digest(bufferPointer: UnsafeRawBufferPointer(digestPointer))!
|
||||
}
|
||||
|
||||
return digestBytes
|
||||
}
|
||||
|
||||
deinit {
|
||||
CCryptoBoringSSL_EVP_MD_CTX_free(self.contextPointer)
|
||||
}
|
||||
}
|
||||
|
||||
extension DigestContext {
|
||||
struct DigestType {
|
||||
var dispatchTable: OpaquePointer
|
||||
|
||||
private init(_ dispatchTable: OpaquePointer) {
|
||||
self.dispatchTable = dispatchTable
|
||||
}
|
||||
|
||||
static let md5 = DigestType(CCryptoBoringSSL_EVP_md5())
|
||||
|
||||
static let sha1 = DigestType(CCryptoBoringSSL_EVP_sha1())
|
||||
|
||||
static let sha256 = DigestType(CCryptoBoringSSL_EVP_sha256())
|
||||
|
||||
static let sha384 = DigestType(CCryptoBoringSSL_EVP_sha384())
|
||||
|
||||
static let sha512 = DigestType(CCryptoBoringSSL_EVP_sha512())
|
||||
withUnsafeMutablePointer(to: &self.context) { $0.zeroize() }
|
||||
}
|
||||
}
|
||||
#endif // CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
|
||||
@@ -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 {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -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.
|
||||
|
||||
+78
-82
@@ -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
|
||||
+48
-41
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -83,6 +83,12 @@ extension _RSA.BlindSigning {
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a RSA public key with the specified parameters.
|
||||
public init(n: some ContiguousBytes, e: some ContiguousBytes, parameters: Parameters) throws {
|
||||
self.backing = try BackingPublicKey(n: n, e: e)
|
||||
self.parameters = parameters
|
||||
}
|
||||
|
||||
public var pkcs1DERRepresentation: Data {
|
||||
self.backing.pkcs1DERRepresentation
|
||||
}
|
||||
@@ -171,6 +177,12 @@ extension _RSA.BlindSigning {
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an RSA private key with the specified parameters.
|
||||
public init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes, parameters: Parameters) throws {
|
||||
self.backing = try BackingPrivateKey(n: n, e: e, d: d, p: p, q: q)
|
||||
self.parameters = parameters
|
||||
}
|
||||
|
||||
/// Randomly generate a new RSA private key of a given size.
|
||||
///
|
||||
/// This constructor will refuse to generate keys smaller than 2048 bits. Callers that want to enforce minimum
|
||||
|
||||
@@ -98,6 +98,17 @@ extension _RSA.Signing {
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an RSA public key with the specified parameters.
|
||||
///
|
||||
/// Only the BoringSSL backend provides APIs to create a key from its parameters so we first create a BoringSSL
|
||||
/// key, and then pass it to the platform-specific initializer that accepts a BoringSSL key.
|
||||
///
|
||||
/// On Darwin platforms, this will serialize it to PEM format, and then construct a platform-specific key from
|
||||
/// the PEM representation.
|
||||
public init(n: some ContiguousBytes, e: some ContiguousBytes) throws {
|
||||
self.backing = try BackingPublicKey(BoringSSLRSAPublicKey(n: n, e: e))
|
||||
}
|
||||
|
||||
public var pkcs1DERRepresentation: Data {
|
||||
self.backing.pkcs1DERRepresentation
|
||||
}
|
||||
@@ -178,6 +189,17 @@ extension _RSA.Signing {
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an RSA private key with the specified parameters.
|
||||
///
|
||||
/// Only the BoringSSL backend provides APIs to create a key from its parameters so we first create a BoringSSL
|
||||
/// key, and then pass it to the platform-specific initializer that accepts a BoringSSL key.
|
||||
///
|
||||
/// On Darwin platforms, this will serialize it to DER format, and then construct a platform-specific key from
|
||||
/// the DER representation.
|
||||
public init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes) throws {
|
||||
self.backing = try BackingPrivateKey(BoringSSLRSAPrivateKey(n: n, e: e, d: d, p: p, q: q))
|
||||
}
|
||||
|
||||
/// Randomly generate a new RSA private key of a given size.
|
||||
///
|
||||
/// This constructor will refuse to generate keys smaller than 2048 bits. Callers that want to enforce minimum
|
||||
@@ -444,6 +466,17 @@ extension _RSA.Encryption {
|
||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Construct an RSA public key with the specified parameters.
|
||||
///
|
||||
/// Only the BoringSSL backend provides APIs to create a key from its parameters so we first create a BoringSSL
|
||||
/// key, and then pass it to the platform-specific initializer that accepts a BoringSSL key.
|
||||
///
|
||||
/// On Darwin platforms, this will serialize it to DER format, and then construct a platform-specific key from
|
||||
/// the DER representation.
|
||||
public init(n: some ContiguousBytes, e: some ContiguousBytes) throws {
|
||||
self.backing = try BackingPublicKey(BoringSSLRSAPublicKey(n: n, e: e))
|
||||
}
|
||||
|
||||
public var pkcs1DERRepresentation: Data { self.backing.pkcs1DERRepresentation }
|
||||
public var pkcs1PEMRepresentation: String { self.backing.pkcs1PEMRepresentation }
|
||||
public var derRepresentation: Data { self.backing.derRepresentation }
|
||||
@@ -494,6 +527,18 @@ extension _RSA.Encryption {
|
||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
|
||||
/// Construct an RSA private key with the specified parameters.
|
||||
///
|
||||
/// Only the BoringSSL backend provides APIs to create a key from its parameters so we first create a BoringSSL
|
||||
/// key, and then pass it to the platform-specific initializer that accepts a BoringSSL key.
|
||||
///
|
||||
/// On Darwin platforms, this will serialize it to PEM format, and then construct a platform-specific key from
|
||||
/// the PEM representation.
|
||||
public init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes) throws {
|
||||
self.backing = try BackingPrivateKey(BoringSSLRSAPrivateKey(n: n, e: e, d: d, p: p, q: q))
|
||||
}
|
||||
|
||||
/// Randomly generate a new RSA private key of a given size.
|
||||
///
|
||||
/// This constructor will refuse to generate keys smaller than 2048 bits. Callers that want to enforce minimum
|
||||
|
||||
@@ -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
|
||||
@@ -30,6 +29,14 @@ internal struct BoringSSLRSAPublicKey: Sendable {
|
||||
self.backing = try Backing(derRepresentation: derRepresentation)
|
||||
}
|
||||
|
||||
init(n: some ContiguousBytes, e: some ContiguousBytes) throws {
|
||||
self.backing = try Backing(n: n, e: e)
|
||||
}
|
||||
|
||||
init(_ other: BoringSSLRSAPublicKey) throws {
|
||||
self = other
|
||||
}
|
||||
|
||||
var pkcs1DERRepresentation: Data {
|
||||
self.backing.pkcs1DERRepresentation
|
||||
}
|
||||
@@ -67,6 +74,14 @@ internal struct BoringSSLRSAPrivateKey: Sendable {
|
||||
self.backing = try Backing(derRepresentation: derRepresentation)
|
||||
}
|
||||
|
||||
init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes) throws {
|
||||
self.backing = try Backing(n: n, e: e, d: d, p: p, q: q)
|
||||
}
|
||||
|
||||
init(_ other: BoringSSLRSAPrivateKey) throws {
|
||||
self = other
|
||||
}
|
||||
|
||||
init(keySize: _RSA.Signing.KeySize) throws {
|
||||
self.backing = try Backing(keySize: keySize)
|
||||
}
|
||||
@@ -223,6 +238,20 @@ extension BoringSSLRSAPublicKey {
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate init(n: some ContiguousBytes, e: some ContiguousBytes) throws {
|
||||
self.pointer = CCryptoBoringSSL_EVP_PKEY_new()
|
||||
let n = try ArbitraryPrecisionInteger(bytes: n)
|
||||
let e = try ArbitraryPrecisionInteger(bytes: e)
|
||||
|
||||
// Create BoringSSL RSA key.
|
||||
guard let rsaPtr = n.withUnsafeBignumPointer({ n in
|
||||
e.withUnsafeBignumPointer { e in
|
||||
CCryptoBoringSSL_RSA_new_public_key(n, e)
|
||||
}
|
||||
}) else { throw CryptoKitError.internalBoringSSLError() }
|
||||
CCryptoBoringSSL_EVP_PKEY_assign_RSA(self.pointer, rsaPtr)
|
||||
}
|
||||
|
||||
fileprivate var pkcs1DERRepresentation: Data {
|
||||
return BIOHelper.withWritableMemoryBIO { bio in
|
||||
let rsaPublicKey = CCryptoBoringSSL_EVP_PKEY_get0_RSA(self.pointer)
|
||||
@@ -487,6 +516,43 @@ extension BoringSSLRSAPrivateKey {
|
||||
CCryptoBoringSSL_EVP_PKEY_assign_RSA(self.pointer, rsaPrivateKey)
|
||||
}
|
||||
|
||||
|
||||
fileprivate init(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, p: some ContiguousBytes, q: some ContiguousBytes) throws {
|
||||
self.pointer = CCryptoBoringSSL_EVP_PKEY_new()
|
||||
let n = try ArbitraryPrecisionInteger(bytes: n)
|
||||
let e = try ArbitraryPrecisionInteger(bytes: e)
|
||||
let d = try ArbitraryPrecisionInteger(bytes: d)
|
||||
let p = try ArbitraryPrecisionInteger(bytes: p)
|
||||
let q = try ArbitraryPrecisionInteger(bytes: q)
|
||||
|
||||
// Compute the CRT params.
|
||||
let dp = try FiniteFieldArithmeticContext(fieldSize: p - 1).residue(d)
|
||||
let dq = try FiniteFieldArithmeticContext(fieldSize: q - 1).residue(d)
|
||||
guard let qi = try FiniteFieldArithmeticContext(fieldSize: p).inverse(q) else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
|
||||
// Create BoringSSL RSA key.
|
||||
guard let rsaPtr = n.withUnsafeBignumPointer({ n in
|
||||
e.withUnsafeBignumPointer { e in
|
||||
d.withUnsafeBignumPointer { d in
|
||||
p.withUnsafeBignumPointer { p in
|
||||
q.withUnsafeBignumPointer { q in
|
||||
dp.withUnsafeBignumPointer { dp in
|
||||
dq.withUnsafeBignumPointer { dq in
|
||||
qi.withUnsafeBignumPointer { qi in
|
||||
CCryptoBoringSSL_RSA_new_private_key(n, e, d, p, q, dp, dq, qi)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}) else { throw CryptoKitError.internalBoringSSLError() }
|
||||
CCryptoBoringSSL_EVP_PKEY_assign_RSA(self.pointer, rsaPtr)
|
||||
}
|
||||
|
||||
private static func pkcs8DERPrivateKey<Bytes: ContiguousBytes>(_ derRepresentation: Bytes) -> OpaquePointer? {
|
||||
return derRepresentation.withUnsafeBytes { derPtr in
|
||||
return BIOHelper.withReadOnlyMemoryBIO(wrapping: derPtr) { bio in
|
||||
|
||||
@@ -43,6 +43,10 @@ internal struct SecurityRSAPublicKey: @unchecked Sendable {
|
||||
self.backing = unwrappedKey
|
||||
}
|
||||
|
||||
init(_ boringSSLKey: BoringSSLRSAPublicKey) throws {
|
||||
try self.init(derRepresentation: boringSSLKey.derRepresentation)
|
||||
}
|
||||
|
||||
var pkcs1DERRepresentation: Data {
|
||||
var error: Unmanaged<CFError>? = nil
|
||||
let representation = SecKeyCopyExternalRepresentation(self.backing, &error)
|
||||
@@ -135,6 +139,10 @@ internal struct SecurityRSAPrivateKey: @unchecked Sendable {
|
||||
self.backing = unwrappedKey
|
||||
}
|
||||
|
||||
init(_ boringSSLKey: BoringSSLRSAPrivateKey) throws {
|
||||
try self.init(derRepresentation: boringSSLKey.derRepresentation)
|
||||
}
|
||||
|
||||
var derRepresentation: Data {
|
||||
var error: Unmanaged<CFError>? = nil
|
||||
let representation = SecKeyCopyExternalRepresentation(self.backing, &error)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -93,86 +92,3 @@ extension FixedWidthInteger {
|
||||
return try block(&bn)
|
||||
}
|
||||
}
|
||||
|
||||
extension _RSA.BlindSigning.PublicKey {
|
||||
/// Construct a platform-specific RSA public key with the specified parameters.
|
||||
///
|
||||
/// This constructor is used in tests in cases where test vectors provide the key information this way.
|
||||
///
|
||||
/// Only the BoringSSL backend provides APIs to create the key from its parameters so we first create a BoringSSL
|
||||
/// key, serialize it to PEM format, and then construct a platform specific key from the PEM representation.
|
||||
internal init(nHexString: String, eHexString: String, parameters: Parameters) throws {
|
||||
let n = try ArbitraryPrecisionInteger(hexString: nHexString)
|
||||
let e = try ArbitraryPrecisionInteger(hexString: eHexString)
|
||||
|
||||
// Create BoringSSL RSA key.
|
||||
guard let rsaPtr = n.withUnsafeBignumPointer({ n in
|
||||
e.withUnsafeBignumPointer { e in
|
||||
CCryptoBoringSSL_RSA_new_public_key(n, e)
|
||||
}
|
||||
}) else { throw CryptoKitError.internalBoringSSLError() }
|
||||
defer { CCryptoBoringSSL_RSA_free(rsaPtr) }
|
||||
|
||||
// Get PEM representation for key.
|
||||
let pemRepresentation = BIOHelper.withWritableMemoryBIO { bio in
|
||||
precondition(CCryptoBoringSSL_PEM_write_bio_RSAPublicKey(bio, rsaPtr) == 1)
|
||||
return try! String(copyingUTF8MemoryBIO: bio)
|
||||
}
|
||||
|
||||
// Create a key (which might be backed by Security framework) from PEM representation.
|
||||
try self.init(pemRepresentation: pemRepresentation, parameters: parameters)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension _RSA.BlindSigning.PrivateKey {
|
||||
/// Construct a platform-specific RSA private key with the specified parameters.
|
||||
///
|
||||
/// This constructor is used in tests in cases where test vectors provide the key information this way.
|
||||
///
|
||||
/// Only the BoringSSL backend provides APIs to create the key from its parameters so we first create a BoringSSL
|
||||
/// key, serialize it to PEM format, and then construct a platform specific key from the PEM representation.
|
||||
internal init(nHexString: String, eHexString: String, dHexString: String, pHexString: String, qHexString: String, parameters: Parameters) throws {
|
||||
let n = try ArbitraryPrecisionInteger(hexString: nHexString)
|
||||
let e = try ArbitraryPrecisionInteger(hexString: eHexString)
|
||||
let d = try ArbitraryPrecisionInteger(hexString: dHexString)
|
||||
let p = try ArbitraryPrecisionInteger(hexString: pHexString)
|
||||
let q = try ArbitraryPrecisionInteger(hexString: qHexString)
|
||||
|
||||
// Compute the CRT params.
|
||||
let dp = try FiniteFieldArithmeticContext(fieldSize: p - 1).residue(d)
|
||||
let dq = try FiniteFieldArithmeticContext(fieldSize: q - 1).residue(d)
|
||||
guard let qi = try FiniteFieldArithmeticContext(fieldSize: p).inverse(q) else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
|
||||
// Create BoringSSL RSA key.
|
||||
guard let rsaPtr = n.withUnsafeBignumPointer({ n in
|
||||
e.withUnsafeBignumPointer { e in
|
||||
d.withUnsafeBignumPointer { d in
|
||||
p.withUnsafeBignumPointer { p in
|
||||
q.withUnsafeBignumPointer { q in
|
||||
dp.withUnsafeBignumPointer { dp in
|
||||
dq.withUnsafeBignumPointer { dq in
|
||||
qi.withUnsafeBignumPointer { qi in
|
||||
CCryptoBoringSSL_RSA_new_private_key(n, e, d, p, q, dp, dq, qi)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}) else { throw CryptoKitError.internalBoringSSLError() }
|
||||
defer { CCryptoBoringSSL_RSA_free(rsaPtr) }
|
||||
|
||||
// Get PEM representation for key.
|
||||
let pemRepresentation = BIOHelper.withWritableMemoryBIO { bio in
|
||||
precondition(CCryptoBoringSSL_PEM_write_bio_RSAPrivateKey(bio, rsaPtr, nil, nil, 0, nil, nil) == 1)
|
||||
return try! String(copyingUTF8MemoryBIO: bio)
|
||||
}
|
||||
|
||||
// Create a key (which might be backed by Security framework) from PEM representation.
|
||||
try self.init(pemRepresentation: pemRepresentation, parameters: parameters)
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import CCryptoBoringSSL
|
||||
@_implementationOnly import CCryptoBoringSSL
|
||||
import Crypto
|
||||
|
||||
extension CryptoKitError {
|
||||
|
||||
@@ -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
@@ -0,0 +1 @@
|
||||
../../Crypto/Util/BoringSSL/Shared
|
||||
+7
-4
@@ -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
|
||||
+9
-7
@@ -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
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import XCTest
|
||||
import Crypto
|
||||
@testable import _CryptoExtras
|
||||
|
||||
fileprivate struct TestVector: Codable {
|
||||
struct RFC9474TestVector: Codable {
|
||||
var name, p, q, n, e, d, msg, msg_prefix, prepared_msg, salt, inv, blinded_msg, blind_sig, sig: String
|
||||
|
||||
var parameters: _RSA.BlindSigning.Parameters<SHA384> {
|
||||
@@ -40,19 +40,33 @@ fileprivate struct TestVector: Codable {
|
||||
let decoder = JSONDecoder()
|
||||
return try decoder.decode([Self].self, from: json)
|
||||
}
|
||||
|
||||
static let allValues: [Self] = try! RFC9474TestVector.load(from: URL(
|
||||
fileURLWithPath: "../_CryptoExtrasVectors/rfc9474.json",
|
||||
relativeTo: URL(fileURLWithPath: #file)
|
||||
))
|
||||
}
|
||||
|
||||
final class TestRSABlindSigning: XCTestCase {
|
||||
func testAgainstRFC9474TestVectors() throws {
|
||||
let testVectors = try TestVector.load(from: URL(
|
||||
fileURLWithPath: "../_CryptoExtrasVectors/rfc9474.json",
|
||||
relativeTo: URL(fileURLWithPath: #file)
|
||||
))
|
||||
|
||||
for testVector in testVectors {
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
// Load key pair
|
||||
let privateKey = try _RSA.BlindSigning.PrivateKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
d: Data(hexString: testVector.d),
|
||||
p: Data(hexString: testVector.p),
|
||||
q: Data(hexString: testVector.q),
|
||||
parameters: testVector.parameters
|
||||
)
|
||||
let publicKey = try _RSA.BlindSigning.PublicKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
parameters: testVector.parameters
|
||||
)
|
||||
XCTAssertEqual(publicKey.derRepresentation, privateKey.publicKey.derRepresentation)
|
||||
// Prepare
|
||||
do {
|
||||
let publicKey = try _RSA.BlindSigning.PublicKey(nHexString: testVector.n, eHexString: testVector.e, parameters: testVector.parameters)
|
||||
let message = try Data(hexString: testVector.msg)
|
||||
let preparedMessage = publicKey.prepare(message)
|
||||
switch testVector.parameters.preparation {
|
||||
@@ -65,7 +79,6 @@ final class TestRSABlindSigning: XCTestCase {
|
||||
|
||||
// Blind
|
||||
do {
|
||||
let publicKey = try _RSA.BlindSigning.PublicKey(nHexString: testVector.n, eHexString: testVector.e, parameters: testVector.parameters)
|
||||
let preparedMessage = try _RSA.BlindSigning.PreparedMessage(rawRepresentation: Data(hexString: testVector.prepared_msg))
|
||||
let blindingResult = try publicKey.blind(preparedMessage)
|
||||
// NOTE: Sadly we can't validate the blinded message against the test vectors because BoringSSL doesn't
|
||||
@@ -76,14 +89,6 @@ final class TestRSABlindSigning: XCTestCase {
|
||||
|
||||
// BlindSign
|
||||
do {
|
||||
let privateKey = try _RSA.BlindSigning.PrivateKey(
|
||||
nHexString: testVector.n,
|
||||
eHexString: testVector.e,
|
||||
dHexString: testVector.d,
|
||||
pHexString: testVector.p,
|
||||
qHexString: testVector.q,
|
||||
parameters: testVector.parameters
|
||||
)
|
||||
let blindedMessage = try Data(hexString: testVector.blinded_msg)
|
||||
let blindSignature = try privateKey.blindSignature(for: blindedMessage)
|
||||
XCTAssertEqual(
|
||||
@@ -94,7 +99,6 @@ final class TestRSABlindSigning: XCTestCase {
|
||||
|
||||
// Finalize
|
||||
do {
|
||||
let publicKey = try _RSA.BlindSigning.PublicKey(nHexString: testVector.n, eHexString: testVector.e, parameters: testVector.parameters)
|
||||
let blindSignature = try _RSA.BlindSigning.BlindSignature(rawRepresentation: Data(hexString: testVector.blind_sig))
|
||||
let preparedMessage = try _RSA.BlindSigning.PreparedMessage(rawRepresentation: Data(hexString: testVector.prepared_msg))
|
||||
let blindingInverse = try _RSA.BlindSigning.BlindingInverse(rawRepresentation: Data(hexString: testVector.inv))
|
||||
@@ -107,7 +111,6 @@ final class TestRSABlindSigning: XCTestCase {
|
||||
|
||||
// Verification
|
||||
do {
|
||||
let publicKey = try _RSA.BlindSigning.PublicKey(nHexString: testVector.n, eHexString: testVector.e, parameters: testVector.parameters)
|
||||
let signature = try _RSA.Signing.RSASignature(rawRepresentation: Data(hexString: testVector.sig))
|
||||
let preparedMessage = try _RSA.BlindSigning.PreparedMessage(rawRepresentation: Data(hexString: testVector.prepared_msg))
|
||||
XCTAssert(publicKey.isValidSignature(signature, for: preparedMessage))
|
||||
@@ -164,4 +167,39 @@ final class TestRSABlindSigning: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testConstructKeyFromRSANumbers() throws {
|
||||
/// Check we can successfully construct keys from known valid values from a test vector.
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
_ = try _RSA.BlindSigning.PrivateKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
d: Data(hexString: testVector.d),
|
||||
p: Data(hexString: testVector.p),
|
||||
q: Data(hexString: testVector.q),
|
||||
parameters: testVector.parameters
|
||||
)
|
||||
_ = try _RSA.BlindSigning.PublicKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
parameters: testVector.parameters
|
||||
)
|
||||
}
|
||||
/// Also check that we can provide each argument as a different `ContiguousBytes` type.
|
||||
/// NOTE: these calls use `try?` because they are guaranteed to fail; we're just checking these calls compile.
|
||||
let bytesValues: [any ContiguousBytes] = [Data(), [UInt8]()]
|
||||
_ = try? _RSA.BlindSigning.PrivateKey(
|
||||
n: bytesValues.randomElement()!,
|
||||
e: bytesValues.randomElement()!,
|
||||
d: bytesValues.randomElement()!,
|
||||
p: bytesValues.randomElement()!,
|
||||
q: bytesValues.randomElement()!,
|
||||
parameters: .RSABSSA_SHA384_PSS_Randomized
|
||||
)
|
||||
_ = try? _RSA.BlindSigning.PublicKey(
|
||||
n: bytesValues.randomElement()!,
|
||||
e: bytesValues.randomElement()!,
|
||||
parameters: .RSABSSA_SHA384_PSS_Randomized
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -77,6 +77,37 @@ final class TestRSAEncryption: XCTestCase {
|
||||
XCTAssertEqual(valid, test.expectedValidity, "test number \(test.tcId) failed, expected \(test.result) but got \(valid)")
|
||||
}
|
||||
}
|
||||
|
||||
func testConstructKeyFromRSANumbers() throws {
|
||||
/// Check we can successfully construct keys from known valid values from a test vector.
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
_ = try _RSA.Encryption.PrivateKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
d: Data(hexString: testVector.d),
|
||||
p: Data(hexString: testVector.p),
|
||||
q: Data(hexString: testVector.q)
|
||||
)
|
||||
_ = try _RSA.Encryption.PublicKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e)
|
||||
)
|
||||
}
|
||||
/// Also check that we can provide each argument as a different `ContiguousBytes` type.
|
||||
/// NOTE: these calls use `try?` because they are guaranteed to fail; we're just checking these calls compile.
|
||||
let bytesValues: [any ContiguousBytes] = [Data(), [UInt8]()]
|
||||
_ = try? _RSA.Encryption.PrivateKey(
|
||||
n: bytesValues.randomElement()!,
|
||||
e: bytesValues.randomElement()!,
|
||||
d: bytesValues.randomElement()!,
|
||||
p: bytesValues.randomElement()!,
|
||||
q: bytesValues.randomElement()!
|
||||
)
|
||||
_ = try? _RSA.Encryption.PublicKey(
|
||||
n: bytesValues.randomElement()!,
|
||||
e: bytesValues.randomElement()!
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct RSAEncryptionOAEPTestGroup: Codable {
|
||||
|
||||
@@ -677,6 +677,37 @@ final class TestRSASigning: XCTestCase {
|
||||
XCTAssertEqual(pemKey, key.pkcs8PEMRepresentation)
|
||||
}
|
||||
|
||||
func testConstructKeyFromRSANumbers() throws {
|
||||
/// Check we can successfully construct keys from known valid values from a test vector.
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
_ = try _RSA.Signing.PrivateKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
d: Data(hexString: testVector.d),
|
||||
p: Data(hexString: testVector.p),
|
||||
q: Data(hexString: testVector.q)
|
||||
)
|
||||
_ = try _RSA.Signing.PublicKey(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e)
|
||||
)
|
||||
}
|
||||
/// Also check that we can provide each argument as a different `ContiguousBytes` type.
|
||||
/// NOTE: these calls use `try?` because they are guaranteed to fail; we're just checking these calls compile.
|
||||
let bytesValues: [any ContiguousBytes] = [Data(), [UInt8]()]
|
||||
_ = try? _RSA.Signing.PrivateKey(
|
||||
n: bytesValues.randomElement()!,
|
||||
e: bytesValues.randomElement()!,
|
||||
d: bytesValues.randomElement()!,
|
||||
p: bytesValues.randomElement()!,
|
||||
q: bytesValues.randomElement()!
|
||||
)
|
||||
_ = try? _RSA.Signing.PublicKey(
|
||||
n: bytesValues.randomElement()!,
|
||||
e: bytesValues.randomElement()!
|
||||
)
|
||||
}
|
||||
|
||||
private func testPKCS1Group(_ group: RSAPKCS1TestGroup) throws {
|
||||
let derKey: _RSA.Signing.PublicKey
|
||||
let pemKey: _RSA.Signing.PublicKey
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user