Files
libsignal/swift/Sources/LibSignalClient/Aes256GcmSiv.swift
Jordan Rose f3e0f61667 bridge: Remove bridge_fn_buffer
At this point, the only special behavior of bridge_fn_buffer is to
support multiple return values for the C bridge (a pointer/length
pair), and that doesn't pull its weight. Remove it in favor of a plain
bridge_fn.

This did reveal that Username_Hash was using bridge_fn_buffer and now
produces a fixed-size array, imported into Swift as a tuple, so this
commit also factors out a new helper invokeFnReturningFixedLengthArray.
2023-02-21 16:41:03 -08:00

75 lines
2.8 KiB
Swift

//
// Copyright 2020-2022 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//
import SignalFfi
import Foundation
public class Aes256GcmSiv: NativeHandleOwner {
public convenience init<Bytes: ContiguousBytes>(key bytes: Bytes) throws {
let handle: OpaquePointer? = try bytes.withUnsafeBorrowedBuffer {
var result: OpaquePointer?
try checkError(signal_aes256_gcm_siv_new(&result, $0))
return result
}
self.init(owned: handle!)
}
internal override class func destroyNativeHandle(_ handle: OpaquePointer) -> SignalFfiErrorRef? {
return signal_aes256_gcm_siv_destroy(handle)
}
public func encrypt<MessageBytes, NonceBytes, AssociatedDataBytes>(
_ message: MessageBytes,
nonce: NonceBytes,
associatedData: AssociatedDataBytes
) throws -> [UInt8]
where MessageBytes: ContiguousBytes,
NonceBytes: ContiguousBytes,
AssociatedDataBytes: ContiguousBytes {
try withNativeHandle { nativeHandle in
try message.withUnsafeBorrowedBuffer { messageBuffer in
try nonce.withUnsafeBorrowedBuffer { nonceBuffer in
try associatedData.withUnsafeBorrowedBuffer { adBuffer in
try invokeFnReturningArray {
signal_aes256_gcm_siv_encrypt($0,
nativeHandle,
messageBuffer,
nonceBuffer,
adBuffer)
}
}
}
}
}
}
public func decrypt<MessageBytes, NonceBytes, AssociatedDataBytes> (
_ message: MessageBytes,
nonce: NonceBytes,
associatedData: AssociatedDataBytes) throws -> [UInt8]
where MessageBytes: ContiguousBytes,
NonceBytes: ContiguousBytes,
AssociatedDataBytes: ContiguousBytes {
try withNativeHandle { nativeHandle in
try message.withUnsafeBorrowedBuffer { messageBuffer in
try nonce.withUnsafeBorrowedBuffer { nonceBuffer in
try associatedData.withUnsafeBorrowedBuffer { adBuffer in
try invokeFnReturningArray {
signal_aes256_gcm_siv_decrypt($0,
nativeHandle,
messageBuffer,
nonceBuffer,
adBuffer)
}
}
}
}
}
}
}