1
0
Fork 0
mirror of https://github.com/mautrix/signal.git synced 2026-05-15 05:36:53 -04:00
mautrix-signal/pkg/libsignalgo/groupcipher.go

65 lines
2.1 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:54:35 +02:00
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Sumner Evans
2025-01-17 17:49:37 +02:00
// Copyright (C) 2025 Tulir Asokan
2023-12-17 15:54:35 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package libsignalgo
/*
#include "./libsignal-ffi.h"
*/
import "C"
import (
2024-01-04 01:06:45 +02:00
"context"
2024-01-03 20:31:27 +02:00
"runtime"
"unsafe"
"github.com/google/uuid"
)
2024-01-04 01:06:45 +02:00
func GroupEncrypt(ctx context.Context, ptext []byte, sender *Address, distributionID uuid.UUID, store SenderKeyStore) (*CiphertextMessage, error) {
callbackCtx := NewCallbackContext(ctx)
defer callbackCtx.Unref()
2025-01-17 17:49:37 +02:00
var ciphertextMessage C.SignalMutPointerCiphertextMessage
signalFfiError := C.signal_group_encrypt_message(
&ciphertextMessage,
2025-01-17 17:49:37 +02:00
sender.constPtr(),
2026-01-16 15:01:48 +02:00
*(*C.SignalUuid)(unsafe.Pointer(&distributionID)),
BytesToBuffer(ptext),
2024-01-04 01:06:45 +02:00
callbackCtx.wrapSenderKeyStore(store))
2024-01-03 20:31:27 +02:00
runtime.KeepAlive(ptext)
runtime.KeepAlive(sender)
if signalFfiError != nil {
2024-01-04 01:06:45 +02:00
return nil, callbackCtx.wrapError(signalFfiError)
}
2025-01-17 17:49:37 +02:00
return wrapCiphertextMessage(ciphertextMessage.raw), nil
}
2024-01-04 01:06:45 +02:00
func GroupDecrypt(ctx context.Context, ctext []byte, sender *Address, store SenderKeyStore) ([]byte, error) {
callbackCtx := NewCallbackContext(ctx)
defer callbackCtx.Unref()
var resp C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
signalFfiError := C.signal_group_decrypt_message(
&resp,
2025-01-17 17:49:37 +02:00
sender.constPtr(),
BytesToBuffer(ctext),
2024-01-04 01:06:45 +02:00
callbackCtx.wrapSenderKeyStore(store))
2024-01-03 20:31:27 +02:00
runtime.KeepAlive(ctext)
runtime.KeepAlive(sender)
if signalFfiError != nil {
2024-01-04 01:06:45 +02:00
return nil, callbackCtx.wrapError(signalFfiError)
}
return CopySignalOwnedBufferToBytes(resp), nil
}