mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
Revert "(split: XWing) Enable CheckImplementationOnly and box properties where needed"
This reverts commit 1f780b4d25c02733d1433581c3b7e4d8b657d7a5.
This commit is contained in:
1 parent
9d8037097c
commit
56f26fd233
1 file changed
+17
-34
@@ -133,41 +133,24 @@ struct OpenSSLXWingPrivateKeyImpl: Sendable {
|
||||
extension OpenSSLXWingPrivateKeyImpl {
|
||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
||||
final class Backing: @unchecked Sendable {
|
||||
// @_implementationOnly import => must use OpaquePointer for stored property.
|
||||
private let _storage: OpaquePointer
|
||||
var privateKey: UnsafeMutablePointer<XWING_private_key> {
|
||||
UnsafeMutablePointer(self._storage)
|
||||
private var privateKey: XWING_private_key
|
||||
|
||||
init(privateKey: XWING_private_key) {
|
||||
self.privateKey = privateKey
|
||||
}
|
||||
|
||||
/// This is the only designated initializer, responsible for the allocation. Deallocation happens in deinit.
|
||||
/// To reduce mistakes (e.g. double-free), all other initializers should be convenience initializers.
|
||||
private init(takingOwnershipOf pointer: UnsafeMutablePointer<XWING_private_key>) {
|
||||
self._storage = OpaquePointer(pointer)
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.privateKey.deinitialize(count: 1)
|
||||
self.privateKey.deallocate()
|
||||
}
|
||||
|
||||
convenience init(privateKey: XWING_private_key) {
|
||||
let pk = UnsafeMutablePointer<XWING_private_key>.allocate(capacity: 1)
|
||||
pk.initialize(to: privateKey)
|
||||
self.init(takingOwnershipOf: pk)
|
||||
}
|
||||
|
||||
convenience init() throws {
|
||||
self.init(privateKey: XWING_private_key())
|
||||
init() throws {
|
||||
self.privateKey = .init()
|
||||
try withUnsafeTemporaryAllocation(byteCount: Int(XWING_PUBLIC_KEY_BYTES), alignment: 1) {
|
||||
let rc = CCryptoBoringSSL_XWING_generate_key($0.baseAddress, self.privateKey)
|
||||
let rc = CCryptoBoringSSL_XWING_generate_key($0.baseAddress, &self.privateKey)
|
||||
if rc != 1 {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
convenience init<D: ContiguousBytes>(bytes: D) throws {
|
||||
self.init(privateKey: XWING_private_key())
|
||||
init<D: ContiguousBytes>(bytes: D) throws {
|
||||
self.privateKey = .init()
|
||||
|
||||
// The first bytes are the private key (in "seed representation"), the latter bytes are the public key.
|
||||
try bytes.withUnsafeBytes { ptr in
|
||||
@@ -180,7 +163,7 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
var cbs = CBS()
|
||||
CCryptoBoringSSL_CBS_init(&cbs, privateKeyBytes.baseAddress, privateKeyBytes.count)
|
||||
|
||||
let rc = CCryptoBoringSSL_XWING_parse_private_key(self.privateKey, &cbs)
|
||||
let rc = CCryptoBoringSSL_XWING_parse_private_key(&self.privateKey, &cbs)
|
||||
guard rc == 1 else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
@@ -190,8 +173,8 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
}
|
||||
}
|
||||
|
||||
convenience init<D: DataProtocol>(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws {
|
||||
self.init(privateKey: XWING_private_key())
|
||||
init<D: DataProtocol>(seedRepresentation: D, publicKeyHash: SHA3_256Digest?) throws {
|
||||
self.privateKey = .init()
|
||||
|
||||
let seedRepresentation: ContiguousBytes =
|
||||
seedRepresentation.regions.count == 1 ? seedRepresentation.regions.first! : Array(seedRepresentation)
|
||||
@@ -204,7 +187,7 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
var cbs = CBS()
|
||||
CCryptoBoringSSL_CBS_init(&cbs, privateKeyBytes.baseAddress, privateKeyBytes.count)
|
||||
|
||||
let rc = CCryptoBoringSSL_XWING_parse_private_key(self.privateKey, &cbs)
|
||||
let rc = CCryptoBoringSSL_XWING_parse_private_key(&self.privateKey, &cbs)
|
||||
guard rc == 1 else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
}
|
||||
@@ -219,7 +202,7 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
withUnsafeTemporaryAllocation(byteCount: Int(XWING_PRIVATE_KEY_BYTES), alignment: 1) {
|
||||
var cbb = CBB()
|
||||
CCryptoBoringSSL_CBB_init_fixed(&cbb, $0.baseAddress, $0.count)
|
||||
let rc = CCryptoBoringSSL_XWING_marshal_private_key(&cbb, self.privateKey)
|
||||
let rc = CCryptoBoringSSL_XWING_marshal_private_key(&cbb, &self.privateKey)
|
||||
precondition(rc == 1)
|
||||
return Data($0.prefix(CCryptoBoringSSL_CBB_len(&cbb)))
|
||||
}
|
||||
@@ -239,7 +222,7 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
|
||||
var publicKey: Data {
|
||||
withUnsafeTemporaryAllocation(byteCount: Int(XWING_PUBLIC_KEY_BYTES), alignment: 1) {
|
||||
let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, self.privateKey)
|
||||
let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, &self.privateKey)
|
||||
precondition(rc == 1)
|
||||
return Data($0)
|
||||
}
|
||||
@@ -247,7 +230,7 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
|
||||
private var publicKeyDigest: SHA3_256Digest {
|
||||
withUnsafeTemporaryAllocation(byteCount: Int(XWING_PUBLIC_KEY_BYTES), alignment: 1) {
|
||||
let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, self.privateKey)
|
||||
let rc = CCryptoBoringSSL_XWING_public_from_private($0.baseAddress, &self.privateKey)
|
||||
precondition(rc == 1)
|
||||
return SHA3_256.hash(bufferPointer: UnsafeRawBufferPointer($0))
|
||||
}
|
||||
@@ -269,7 +252,7 @@ extension OpenSSLXWingPrivateKeyImpl {
|
||||
let rc = CCryptoBoringSSL_XWING_decap(
|
||||
sharedSecretBytes.baseAddress,
|
||||
encapsulatedSecretBytes.baseAddress,
|
||||
self.privateKey
|
||||
&self.privateKey
|
||||
)
|
||||
guard rc == 1 else {
|
||||
throw CryptoKitError.internalBoringSSLError()
|
||||
|
||||
Reference in new issue
Block a user