From 732d51e59bcf30e22b991f2daf25095c2a146941 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 6 Oct 2022 17:15:01 +0300 Subject: mir_signal: C++ helper to get rid of tons code in C --- utils/mir_signal.cpp | 40 +++++++++++++++++++++++++++++++++++++ utils/mir_signal.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 utils/mir_signal.cpp create mode 100644 utils/mir_signal.h (limited to 'utils') diff --git a/utils/mir_signal.cpp b/utils/mir_signal.cpp new file mode 100644 index 0000000000..afca4f4188 --- /dev/null +++ b/utils/mir_signal.cpp @@ -0,0 +1,40 @@ +#include + +#include + +#include "mir_signal.h" + +SignalBuffer::SignalBuffer(const MBinBuffer &buf) +{ + m_buf = signal_buffer_create(buf.data(), buf.length()); +} + +SignalBuffer::SignalBuffer(const ec_public_key *key) +{ + ec_public_key_serialize(&m_buf, key); +} + +SignalBuffer::SignalBuffer(const ec_private_key *key) +{ + ec_private_key_serialize(&m_buf, key); +} + +SignalBuffer::SignalBuffer(const session_pre_key *pre_key) +{ + session_pre_key_serialize(&m_buf, pre_key); +} + +SignalBuffer::SignalBuffer(const session_signed_pre_key *pre_key) +{ + session_signed_pre_key_serialize(&m_buf, pre_key); +} + +SignalBuffer::~SignalBuffer() +{ + signal_buffer_free(m_buf); +} + +CMStringA SignalBuffer::toBase64() const +{ + return ptrA(mir_base64_encode(m_buf->data, m_buf->len)).get(); +} \ No newline at end of file diff --git a/utils/mir_signal.h b/utils/mir_signal.h new file mode 100644 index 0000000000..ef443374ff --- /dev/null +++ b/utils/mir_signal.h @@ -0,0 +1,56 @@ +#ifndef __MIR_SIGNAL_H__ +#define __MIR_SIGNAL_H__ + +#include +#include + +#include "../libs/libsignal/src/curve.h" +#include "../libs/libsignal/src/hkdf.h" +#include "../libs/libsignal/src/key_helper.h" +#include "../libs/libsignal/src/protocol.h" +#include "../libs/libsignal/src/session_builder.h" +#include "../libs/libsignal/src/session_cipher.h" +#include "../libs/libsignal/src/signal_protocol.h" +#include "../libs/libsignal/src/signal_protocol_types.h" + +struct signal_buffer +{ + size_t len; + uint8_t data[1]; +}; + +struct signal_type_base +{ + unsigned int ref_count = 0; + void (*destroy)(signal_type_base *instance) = 0; +}; + +struct ec_public_key : public signal_type_base +{ + uint8_t data[32]; +}; + +struct ec_private_key : public signal_type_base +{ + uint8_t data[32]; +}; + +class SignalBuffer +{ + signal_buffer *m_buf; + +public: + SignalBuffer(const class MBinBuffer &buf); + SignalBuffer(const ec_public_key *key); + SignalBuffer(const ec_private_key *key); + SignalBuffer(const session_pre_key *pre_key); + SignalBuffer(const session_signed_pre_key *pre_key); + ~SignalBuffer(); + + __forceinline uint8_t* data() const { return m_buf->data; } + __forceinline unsigned len() const { return unsigned(m_buf->len); } + + CMStringA toBase64() const; +}; + +#endif // __MIR_SMILEYS_H__ -- cgit v1.2.3