From e34c7e362e3c63cdbab9f5bfc4eb64ffd4eaf971 Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Tue, 22 Oct 2024 16:10:48 +0200 Subject: [PATCH] Adapt custom `withUnsafeBytes` to `DataProtocol` --- Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift | 4 ++-- Sources/_CryptoExtras/Util/Optional+withUnsafeBytes.swift | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift b/Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift index a48e152f..e57bdb23 100644 --- a/Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift +++ b/Sources/_CryptoExtras/MLDSA/MLDSA_boring.swift @@ -155,7 +155,7 @@ extension MLDSA { let output = try Array(unsafeUninitializedCapacity: Signature.bytesCount) { bufferPtr, length in let bytes: ContiguousBytes = data.regions.count == 1 ? data.regions.first! : Array(data) let result = bytes.withUnsafeBytes { dataPtr in - context.map { Data($0) }.withUnsafeBytes { contextPtr in + context.withUnsafeBytes { contextPtr in CCryptoBoringSSL_MLDSA65_sign( bufferPtr.baseAddress, self.pointer, @@ -271,7 +271,7 @@ extension MLDSA { signature.withUnsafeBytes { signaturePtr in let bytes: ContiguousBytes = data.regions.count == 1 ? data.regions.first! : Array(data) let rc: CInt = bytes.withUnsafeBytes { dataPtr in - context.map { Data($0) }.withUnsafeBytes { contextPtr in + context.withUnsafeBytes { contextPtr in CCryptoBoringSSL_MLDSA65_verify( self.pointer, signaturePtr.baseAddress, diff --git a/Sources/_CryptoExtras/Util/Optional+withUnsafeBytes.swift b/Sources/_CryptoExtras/Util/Optional+withUnsafeBytes.swift index 570c4a5e..f246eb12 100644 --- a/Sources/_CryptoExtras/Util/Optional+withUnsafeBytes.swift +++ b/Sources/_CryptoExtras/Util/Optional+withUnsafeBytes.swift @@ -14,10 +14,11 @@ import Foundation -extension Optional where Wrapped == ContiguousBytes { +extension Optional where Wrapped: DataProtocol { func withUnsafeBytes(_ body: (UnsafeRawBufferPointer) throws -> ReturnValue) rethrows -> ReturnValue { if let self { - return try self.withUnsafeBytes { try body($0) } + let bytes: ContiguousBytes = self.regions.count == 1 ? self.regions.first! : Array(self) + return try bytes.withUnsafeBytes { try body($0) } } else { return try body(UnsafeRawBufferPointer(start: nil, count: 0)) }